219 lines
7.1 KiB
TypeScript
219 lines
7.1 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { existsSync, unlinkSync } from "node:fs";
|
|
import { homedir } from "node:os";
|
|
import { join } from "node:path";
|
|
import ExcelJS from "exceljs";
|
|
import type { KyInstance } from "ky";
|
|
import { type CorpConfig, CorpConfigSchema } from "../../core/config/types.js";
|
|
import { exportInsightReportToExcel, resolveInsightOutputFilePath } from "./insight-export.js";
|
|
|
|
describe("insight-export domain", () => {
|
|
test("resolveInsightOutputFilePath respects custom outputFile", () => {
|
|
const custom = "/tmp/test-insight-report.xlsx";
|
|
const res = resolveInsightOutputFilePath({
|
|
outputFile: custom,
|
|
corpName: "TestCorp"
|
|
});
|
|
expect(res).toBe(custom);
|
|
});
|
|
|
|
test("resolveInsightOutputFilePath respects toDownloads flag", () => {
|
|
const res = resolveInsightOutputFilePath({
|
|
toDownloads: true,
|
|
corpName: "渠道AI演示环境",
|
|
dateTag: "2026-09-12"
|
|
});
|
|
expect(res.startsWith(join(homedir(), "Downloads"))).toBe(true);
|
|
expect(res.endsWith("渠道AI演示环境_智能洞察报告_2026-09-12.xlsx")).toBe(true);
|
|
});
|
|
|
|
test("exportInsightReportToExcel generates valid multi-sheet Excel file", async () => {
|
|
const mockClient = {
|
|
get: (url: string) => {
|
|
if (url.includes("insight/stats")) {
|
|
return {
|
|
json: async () => ({
|
|
data: {
|
|
tagStats: [
|
|
{
|
|
tagId: "B.1.6",
|
|
tagName: "缺少验收/签收证明",
|
|
tagPath: "凭证/附件问题 > 凭证缺失 > 缺少验收/签收证明",
|
|
count: 7,
|
|
avgConfidence: 0.95,
|
|
errorCount: 5,
|
|
warningCount: 2
|
|
}
|
|
],
|
|
totalRisks: 7,
|
|
analyzedRisks: 7,
|
|
pendingRisks: 0
|
|
}
|
|
})
|
|
};
|
|
}
|
|
if (url.includes("insight/performance")) {
|
|
return {
|
|
json: async () => ({
|
|
data: {
|
|
metrics: {
|
|
approvalCount: { value: 24, trend: 0 },
|
|
accuracy: { value: 100, trend: 0 },
|
|
automationRate: { value: 75, trend: 0 },
|
|
riskCount: { value: 7, trend: 0 },
|
|
avgDuration: { value: 1041.4, trend: 0 }
|
|
},
|
|
sankeyData: {
|
|
nodes: [{ id: "辅助模式" }, { id: "添加评论" }],
|
|
links: [{ source: "辅助模式", target: "添加评论", value: 24 }]
|
|
}
|
|
}
|
|
})
|
|
};
|
|
}
|
|
if (url.includes("insight/risks-by-tag")) {
|
|
return {
|
|
json: async () => ({
|
|
data: {
|
|
tagId: "B.1.6",
|
|
tagName: "缺少验收/签收证明",
|
|
tagPath: "凭证/附件问题 > 凭证缺失 > 缺少验收/签收证明",
|
|
tagDescription: "维修费等未提供验收单",
|
|
risks: [
|
|
{
|
|
riskId: "risk-1",
|
|
recordId: "rec-1",
|
|
riskDetails: "【冰箱缺少验收单】无资产验收单",
|
|
riskLevel: "error",
|
|
documentType: "费用报销",
|
|
documentSN: "B26000009",
|
|
sourceSystem: "custom",
|
|
confidence: 0.95,
|
|
reasoning: "缺少零星采购验收单",
|
|
createdAt: "2026-09-11T06:31:01.616Z"
|
|
}
|
|
],
|
|
total: 1
|
|
}
|
|
})
|
|
};
|
|
}
|
|
throw new Error(`Unexpected url: ${url}`);
|
|
}
|
|
} as unknown as KyInstance;
|
|
|
|
const mockCorp: CorpConfig = CorpConfigSchema.parse({
|
|
id: "test-corp",
|
|
name: "测试演示企业",
|
|
seal: {
|
|
url: "https://seal.example.com",
|
|
tenantSlug: "test"
|
|
},
|
|
source: {
|
|
type: "direct",
|
|
token: "fake-token",
|
|
sealUrl: "https://seal.example.com"
|
|
}
|
|
});
|
|
|
|
const outPath = `/tmp/test-insight-export-${Date.now()}.xlsx`;
|
|
|
|
try {
|
|
const result = await exportInsightReportToExcel(mockClient, mockCorp, {
|
|
outputFile: outPath,
|
|
startDate: "2026-09-01T00:00:00Z",
|
|
endDate: "2026-09-30T23:59:59Z"
|
|
});
|
|
|
|
expect(result.filePath).toBe(outPath);
|
|
expect(result.corpId).toBe("test-corp");
|
|
expect(result.totalRisks).toBe(7);
|
|
expect(result.tagCount).toBe(1);
|
|
expect(result.riskDetailCount).toBe(1);
|
|
expect(result.fileSizeBytes).toBeGreaterThan(0);
|
|
expect(existsSync(outPath)).toBe(true);
|
|
|
|
// Verify Excel Workbook content
|
|
const wb = new ExcelJS.Workbook();
|
|
await wb.xlsx.readFile(outPath);
|
|
|
|
expect(wb.worksheets.length).toBe(3);
|
|
const sheetNames = wb.worksheets.map((w) => w.name);
|
|
expect(sheetNames).toContain("审核效能与决策流向");
|
|
expect(sheetNames).toContain("Top 风险标签分析");
|
|
expect(sheetNames).toContain("风险单据穿透明细");
|
|
|
|
const overview = wb.getWorksheet("审核效能与决策流向");
|
|
expect(overview).toBeDefined();
|
|
|
|
const tags = wb.getWorksheet("Top 风险标签分析");
|
|
expect(tags?.rowCount).toBeGreaterThanOrEqual(2);
|
|
|
|
const details = wb.getWorksheet("风险单据穿透明细");
|
|
expect(details?.rowCount).toBeGreaterThanOrEqual(2);
|
|
} finally {
|
|
if (existsSync(outPath)) {
|
|
unlinkSync(outPath);
|
|
}
|
|
}
|
|
});
|
|
|
|
test("exportInsightReportToExcel rejects when time range exceeds 180 days without force", async () => {
|
|
const mockClient = {} as any;
|
|
const testCorp: CorpConfig = {
|
|
id: "test-corp",
|
|
name: "测试企业",
|
|
environment: "production",
|
|
seal: {
|
|
endpoints: {
|
|
approvalStylePreferences: "api/v1/agent/ai-approval/config"
|
|
}
|
|
},
|
|
source: {
|
|
type: "direct",
|
|
token: "mock-token",
|
|
sealUrl: "https://mock.sealai.cc",
|
|
expiresIn: 3600,
|
|
staffId: "direct"
|
|
},
|
|
auth: { refreshTtl: 300 }
|
|
};
|
|
|
|
await expect(
|
|
exportInsightReportToExcel(mockClient, testCorp, {
|
|
startDate: "2026-01-01",
|
|
endDate: "2026-09-13"
|
|
})
|
|
).rejects.toThrow("时间跨度超过 180 天");
|
|
});
|
|
|
|
test("exportInsightReportToExcel rejects when startDate is after endDate", async () => {
|
|
const mockClient = {} as any;
|
|
const testCorp: CorpConfig = {
|
|
id: "test-corp",
|
|
name: "测试企业",
|
|
environment: "production",
|
|
seal: {
|
|
endpoints: {
|
|
approvalStylePreferences: "api/v1/agent/ai-approval/config"
|
|
}
|
|
},
|
|
source: {
|
|
type: "direct",
|
|
token: "mock-token",
|
|
sealUrl: "https://mock.sealai.cc",
|
|
expiresIn: 3600,
|
|
staffId: "direct"
|
|
},
|
|
auth: { refreshTtl: 300 }
|
|
};
|
|
|
|
await expect(
|
|
exportInsightReportToExcel(mockClient, testCorp, {
|
|
startDate: "2026-09-15",
|
|
endDate: "2026-09-13"
|
|
})
|
|
).rejects.toThrow("起始时间 (2026-09-15) 不能晚于结束时间 (2026-09-13)");
|
|
});
|
|
});
|