353 lines
12 KiB
TypeScript
353 lines
12 KiB
TypeScript
import { describe, test, expect, beforeEach, afterEach } from "bun:test";
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import os from "node:os";
|
|
import {
|
|
extractApprovalRunAttachments,
|
|
downloadApprovalRunAttachments,
|
|
sanitizeUrlForLog,
|
|
sanitizeFileName,
|
|
type FetchLike
|
|
} from "./attachments.js";
|
|
import type { ApprovalRun } from "./types.js";
|
|
|
|
describe("attachments domain", () => {
|
|
let tmpDir: string;
|
|
|
|
beforeEach(async () => {
|
|
tmpDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "seal-att-test-"));
|
|
});
|
|
|
|
afterEach(async () => {
|
|
try {
|
|
await fs.promises.rm(tmpDir, { recursive: true, force: true });
|
|
} catch {}
|
|
});
|
|
|
|
test("sanitizeUrlForLog masks query string signatures", () => {
|
|
const rawUrl = "https://oss-bucket.aliyuncs.com/tenant/doc1/pic.jpg?X-Amz-Signature=secret123&OSSAccessKeyId=key456";
|
|
const masked = sanitizeUrlForLog(rawUrl);
|
|
expect(masked).toBe("https://oss-bucket.aliyuncs.com/tenant/doc1/pic.jpg?[MASKED]");
|
|
expect(masked).not.toContain("secret123");
|
|
expect(masked).not.toContain("key456");
|
|
|
|
const plainUrl = "https://example.com/files/receipt.pdf";
|
|
expect(sanitizeUrlForLog(plainUrl)).toBe("https://example.com/files/receipt.pdf");
|
|
});
|
|
|
|
test("sanitizeFileName strips dangerous characters", () => {
|
|
expect(sanitizeFileName("foo/bar:baz?.png")).toBe("foo_bar_baz_.png");
|
|
expect(sanitizeFileName("../../evil.exe")).toBe(".._.._evil.exe");
|
|
expect(sanitizeFileName("")).toBe("attachment");
|
|
});
|
|
|
|
test("extractApprovalRunAttachments extracts attachments from Issue #35 field structure", () => {
|
|
const fixture: ApprovalRun = {
|
|
id: "run-hose-1",
|
|
status: "completed",
|
|
sourceDocumentSN: "BX2026080001",
|
|
sourceDocumentId: "doc-hose-1",
|
|
document: {
|
|
id: "doc-1",
|
|
sn: "BX2026080001",
|
|
fields: [
|
|
{ key: "expense_type", label: "费用类型", value: "差旅" },
|
|
{
|
|
key: "attachments",
|
|
type: "ATTACHMENT",
|
|
label: "附件",
|
|
value: [
|
|
{
|
|
url: "https://oss-bucket.aliyuncs.com/tenant/attachments/doc1/receipt1.jpg",
|
|
ossSignedUrl: "https://oss-bucket.aliyuncs.com/tenant/attachments/doc1/receipt1.jpg?X-Amz-Signature=sig1",
|
|
name: "发票1.jpg",
|
|
token: JSON.stringify({ source: "hosecloud", fileId: "file-101", key: "obj-key-101" }),
|
|
fileId: "file-101",
|
|
ossPath: "tenant/attachments/doc1/obj-key-101",
|
|
category: "file",
|
|
mimeType: "image/jpeg",
|
|
ossFileSize: 1024
|
|
},
|
|
{
|
|
url: "https://oss-bucket.aliyuncs.com/tenant/attachments/doc1/receipt2.jpg",
|
|
ossSignedUrl: "https://oss-bucket.aliyuncs.com/tenant/attachments/doc1/receipt2.jpg?X-Amz-Signature=sig2",
|
|
name: "水单2.jpg",
|
|
token: JSON.stringify({ source: "hosecloud", fileId: "file-102", key: "obj-key-102" }),
|
|
fileId: "file-102",
|
|
ossPath: "tenant/attachments/doc1/obj-key-102",
|
|
category: "file",
|
|
mimeType: "image/jpeg",
|
|
ossFileSize: 2048
|
|
},
|
|
{
|
|
url: "https://oss-bucket.aliyuncs.com/tenant/attachments/doc1/doc3.pdf",
|
|
ossSignedUrl: "https://oss-bucket.aliyuncs.com/tenant/attachments/doc1/doc3.pdf?X-Amz-Signature=sig3",
|
|
name: "行程单.pdf",
|
|
token: JSON.stringify({ source: "hosecloud", fileId: "file-103", key: "obj-key-103" }),
|
|
fileId: "file-103",
|
|
category: "file",
|
|
mimeType: "application/pdf",
|
|
ossFileSize: 4096
|
|
},
|
|
{
|
|
url: "https://oss-bucket.aliyuncs.com/tenant/attachments/doc1/photo4.png",
|
|
ossSignedUrl: "https://oss-bucket.aliyuncs.com/tenant/attachments/doc1/photo4.png?X-Amz-Signature=sig4",
|
|
name: "现场打卡.png",
|
|
token: JSON.stringify({ source: "hosecloud", fileId: "file-104", key: "obj-key-104" }),
|
|
fileId: "file-104",
|
|
category: "file",
|
|
mimeType: "image/png",
|
|
ossFileSize: 8192
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
};
|
|
|
|
const fullResult = extractApprovalRunAttachments(fixture);
|
|
expect(fullResult.count).toBe(4);
|
|
expect(fullResult.recordId).toBe("run-hose-1");
|
|
expect(fullResult.sourceDocumentSN).toBe("BX2026080001");
|
|
|
|
const first = fullResult.attachments[0] as any;
|
|
expect(first.fileName).toBe("发票1.jpg");
|
|
expect(first.fileId).toBe("file-101");
|
|
expect(first.tokenKey).toBe("obj-key-101");
|
|
expect(first.ossFileSize).toBe(1024);
|
|
expect(first.ossSignedUrl).toContain("X-Amz-Signature=sig1");
|
|
expect(first.sourceField).toBe("附件");
|
|
expect(first.kind).toBe("invoice"); // "发票" in name triggers invoice kind
|
|
|
|
const summaryResult = extractApprovalRunAttachments(fixture, { summary: true });
|
|
expect(summaryResult.count).toBe(4);
|
|
expect((summaryResult.attachments[0] as any).hasSignedUrl).toBe(true);
|
|
expect((summaryResult.attachments[0] as any).ossFileSize).toBe(1024);
|
|
expect((summaryResult.attachments[0] as any).ossSignedUrl).toBeUndefined(); // summary masks raw url
|
|
});
|
|
|
|
test("extractApprovalRunAttachments extracts attachments from legacy layout", () => {
|
|
const fixture: ApprovalRun = {
|
|
id: "run-legacy-1",
|
|
status: "completed",
|
|
document: {
|
|
fields: [
|
|
{
|
|
label: "附件",
|
|
attachments: [
|
|
{
|
|
fileName: "legacy.pdf",
|
|
fileId: "file-legacy-1",
|
|
mimeType: "application/pdf",
|
|
ossPath: "oss://bucket/legacy.pdf"
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
};
|
|
|
|
const result = extractApprovalRunAttachments(fixture);
|
|
expect(result.count).toBe(1);
|
|
expect((result.attachments[0] as any).fileName).toBe("legacy.pdf");
|
|
expect((result.attachments[0] as any).fileId).toBe("file-legacy-1");
|
|
});
|
|
|
|
test("downloadApprovalRunAttachments downloads all files with --all and matches size", async () => {
|
|
const content1 = Buffer.from("Invoice mock data 12345");
|
|
const content2 = Buffer.from("Receipt mock data 67890");
|
|
|
|
const fakeFetch: FetchLike = async (url: any) => {
|
|
const urlStr = String(url);
|
|
if (urlStr.includes("att1.jpg")) {
|
|
return new Response(content1, { status: 200 });
|
|
}
|
|
if (urlStr.includes("att2.jpg")) {
|
|
return new Response(content2, { status: 200 });
|
|
}
|
|
return new Response("Not found", { status: 404 });
|
|
};
|
|
|
|
const record: ApprovalRun = {
|
|
id: "run-dl-1",
|
|
document: {
|
|
fields: [
|
|
{
|
|
key: "attachments",
|
|
type: "ATTACHMENT",
|
|
label: "附件",
|
|
value: [
|
|
{
|
|
name: "发票.jpg",
|
|
ossSignedUrl: "https://oss.example.com/att1.jpg?sign=abc",
|
|
ossFileSize: content1.length
|
|
},
|
|
{
|
|
name: "水单.jpg",
|
|
ossSignedUrl: "https://oss.example.com/att2.jpg?sign=def",
|
|
ossFileSize: content2.length
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
};
|
|
|
|
const result = await downloadApprovalRunAttachments({
|
|
record,
|
|
all: true,
|
|
outDir: tmpDir,
|
|
fetchFn: fakeFetch
|
|
});
|
|
|
|
expect(result.total).toBe(2);
|
|
expect(result.downloaded).toBe(2);
|
|
expect(result.failed).toBe(0);
|
|
|
|
const file1Path = path.join(tmpDir, "发票.jpg");
|
|
const file2Path = path.join(tmpDir, "水单.jpg");
|
|
expect(fs.existsSync(file1Path)).toBe(true);
|
|
expect(fs.existsSync(file2Path)).toBe(true);
|
|
expect(fs.readFileSync(file1Path)).toEqual(content1);
|
|
expect(fs.readFileSync(file2Path)).toEqual(content2);
|
|
});
|
|
|
|
test("downloadApprovalRunAttachments filters by --name", async () => {
|
|
const content = Buffer.from("Target attachment data");
|
|
const fakeFetch: FetchLike = async () => new Response(content, { status: 200 });
|
|
|
|
const record: ApprovalRun = {
|
|
id: "run-dl-2",
|
|
document: {
|
|
fields: [
|
|
{
|
|
key: "attachments",
|
|
type: "ATTACHMENT",
|
|
value: [
|
|
{ name: "test_a.pdf", ossSignedUrl: "https://oss.example.com/a.pdf" },
|
|
{ name: "target_b.pdf", ossSignedUrl: "https://oss.example.com/b.pdf" }
|
|
]
|
|
}
|
|
]
|
|
}
|
|
};
|
|
|
|
const result = await downloadApprovalRunAttachments({
|
|
record,
|
|
name: "target_b",
|
|
outDir: tmpDir,
|
|
fetchFn: fakeFetch
|
|
});
|
|
|
|
expect(result.total).toBe(1);
|
|
expect(result.downloaded).toBe(1);
|
|
expect(result.files[0]!.name).toBe("target_b.pdf");
|
|
expect(fs.existsSync(path.join(tmpDir, "target_b.pdf"))).toBe(true);
|
|
expect(fs.existsSync(path.join(tmpDir, "test_a.pdf"))).toBe(false);
|
|
});
|
|
|
|
test("downloadApprovalRunAttachments requires --all when multiple attachments exist without filter", async () => {
|
|
const record: ApprovalRun = {
|
|
id: "run-dl-3",
|
|
document: {
|
|
fields: [
|
|
{
|
|
key: "attachments",
|
|
type: "ATTACHMENT",
|
|
value: [
|
|
{ name: "f1.jpg", ossSignedUrl: "https://oss.example.com/1.jpg" },
|
|
{ name: "f2.jpg", ossSignedUrl: "https://oss.example.com/2.jpg" }
|
|
]
|
|
}
|
|
]
|
|
}
|
|
};
|
|
|
|
expect(
|
|
downloadApprovalRunAttachments({
|
|
record,
|
|
outDir: tmpDir
|
|
})
|
|
).rejects.toThrow("请使用 --all 下载全部附件");
|
|
});
|
|
|
|
test("downloadApprovalRunAttachments handles expired URL (HTTP 403) gracefully without aborting others", async () => {
|
|
const validContent = Buffer.from("Valid attachment data");
|
|
|
|
const fakeFetch: FetchLike = async (url: any) => {
|
|
const urlStr = String(url);
|
|
if (urlStr.includes("expired.jpg")) {
|
|
return new Response("Access Denied / Expired", { status: 403, statusText: "Forbidden" });
|
|
}
|
|
return new Response(validContent, { status: 200 });
|
|
};
|
|
|
|
const record: ApprovalRun = {
|
|
id: "run-dl-4",
|
|
document: {
|
|
fields: [
|
|
{
|
|
key: "attachments",
|
|
type: "ATTACHMENT",
|
|
value: [
|
|
{ name: "expired.jpg", ossSignedUrl: "https://oss.example.com/expired.jpg?sign=old" },
|
|
{ name: "valid.jpg", ossSignedUrl: "https://oss.example.com/valid.jpg?sign=fresh" }
|
|
]
|
|
}
|
|
]
|
|
}
|
|
};
|
|
|
|
const result = await downloadApprovalRunAttachments({
|
|
record,
|
|
all: true,
|
|
outDir: tmpDir,
|
|
fetchFn: fakeFetch
|
|
});
|
|
|
|
expect(result.total).toBe(2);
|
|
expect(result.downloaded).toBe(1);
|
|
expect(result.failed).toBe(1);
|
|
|
|
const failedItem = result.files.find((f) => f.name === "expired.jpg");
|
|
expect(failedItem?.status).toBe("failed");
|
|
expect(failedItem?.error).toContain("403");
|
|
|
|
const successItem = result.files.find((f) => f.name === "valid.jpg");
|
|
expect(successItem?.status).toBe("success");
|
|
expect(fs.existsSync(path.join(tmpDir, "valid.jpg"))).toBe(true);
|
|
});
|
|
|
|
test("downloadApprovalRunAttachments disambiguates duplicate filenames", async () => {
|
|
const fakeFetch: FetchLike = async () => new Response("data", { status: 200 });
|
|
|
|
const record: ApprovalRun = {
|
|
id: "run-dl-5",
|
|
document: {
|
|
fields: [
|
|
{
|
|
key: "attachments",
|
|
type: "ATTACHMENT",
|
|
value: [
|
|
{ name: "same.jpg", ossSignedUrl: "https://oss.example.com/1.jpg" },
|
|
{ name: "same.jpg", ossSignedUrl: "https://oss.example.com/2.jpg" }
|
|
]
|
|
}
|
|
]
|
|
}
|
|
};
|
|
|
|
const result = await downloadApprovalRunAttachments({
|
|
record,
|
|
all: true,
|
|
outDir: tmpDir,
|
|
fetchFn: fakeFetch
|
|
});
|
|
|
|
expect(result.downloaded).toBe(2);
|
|
expect(result.files.map((f) => f.name)).toEqual(["same.jpg", "same_1.jpg"]);
|
|
expect(fs.existsSync(path.join(tmpDir, "same.jpg"))).toBe(true);
|
|
expect(fs.existsSync(path.join(tmpDir, "same_1.jpg"))).toBe(true);
|
|
});
|
|
});
|