327 lines
14 KiB
TypeScript
327 lines
14 KiB
TypeScript
import { describe, it, expect } from "bun:test";
|
|
import { formatSingleBill, getBillReferences, getBillRelations, getBillAttachments, getBillRisk, getBillFlow, approveBill, rejectBill, transferBill, addNodeBill, commentBill, resolveBillStaffIds } from "./tools.js";
|
|
import { RawBillItemSchema, EntitySchema } from "./types.js";
|
|
|
|
describe("Bill Domain", () => {
|
|
it("validates EntitySchema", () => {
|
|
const rawEntity = {
|
|
id: "ae1037940b1c304017c0",
|
|
code: "FEE_DETAIL",
|
|
name: "费用明细",
|
|
type: "EXPENSE",
|
|
active: true,
|
|
fields: [
|
|
{
|
|
name: "E_ae1037940b1c304017c0_payMoney",
|
|
type: "money",
|
|
label: "支付金额",
|
|
optional: false,
|
|
formula: false,
|
|
},
|
|
],
|
|
};
|
|
|
|
const parsed = EntitySchema.parse(rawEntity);
|
|
expect(parsed.id).toBe("ae1037940b1c304017c0");
|
|
expect(parsed.fields[0].label).toBe("支付金额");
|
|
});
|
|
|
|
it("validates RawBillItemSchema and formats fields with dictionary", () => {
|
|
const rawBill = {
|
|
id: "ID01TRzg7HMLcb",
|
|
code: "n0GMIM0dkQmKbS",
|
|
name: "项目&100",
|
|
corporationId: "ID01ofWoOC1wiX",
|
|
ownerId: "ID01ofWoOC1wiX:ID01ojZ2n2e0PR",
|
|
createTime: 1781682826000,
|
|
updateTime: 1781682870000,
|
|
form: {
|
|
E_test_code: "n0GMIM0dkQmKbS",
|
|
E_test_title: "测试单据标题",
|
|
E_test_stage: "expense",
|
|
E_test_state: "paid",
|
|
E_test_amount: {
|
|
standard: "100.00",
|
|
standardUnit: "元",
|
|
standardSymbol: "¥",
|
|
standardStrCode: "CNY",
|
|
},
|
|
E_test_submitterId: "staff-123",
|
|
E_test_customField: "VIP客户",
|
|
},
|
|
};
|
|
|
|
const parsed = RawBillItemSchema.parse(rawBill);
|
|
expect(parsed.id).toBe("ID01TRzg7HMLcb");
|
|
|
|
const dict = new Map<string, { label: string; type: string }>();
|
|
dict.set("E_test_customField", { label: "客户等级", type: "text" });
|
|
|
|
const formatted = formatSingleBill(parsed, dict);
|
|
expect(formatted.billId).toBe("ID01TRzg7HMLcb");
|
|
expect(formatted.billCode).toBe("n0GMIM0dkQmKbS");
|
|
expect(formatted.title).toBe("测试单据标题");
|
|
expect(formatted.stage).toBe("expense");
|
|
expect(formatted.state).toBe("paid");
|
|
expect(formatted.amount).toBe("¥100.00 CNY");
|
|
expect(formatted.details["客户等级"]).toBe("VIP客户");
|
|
});
|
|
|
|
it("resolves approval references from staff, department and fee type catalogs", async () => {
|
|
const responses = [
|
|
{
|
|
id: "backlog-1",
|
|
state: "APPROVING",
|
|
flowId: {
|
|
id: "flow-1",
|
|
form: {
|
|
code: "S26000001",
|
|
title: "团建申请",
|
|
submitterId: "corp:staff-1",
|
|
expenseDepartment: "corp:dept-2",
|
|
details: { feeTypeId: "fee-1" },
|
|
},
|
|
},
|
|
},
|
|
];
|
|
const client = {
|
|
post: async () => new Response(JSON.stringify({ items: responses })),
|
|
get: async (path: string) => {
|
|
if (path.includes("staffs/actives")) {
|
|
return new Response(JSON.stringify({ items: [{ id: "corp:staff-1", userId: "staff-1", name: "张三", code: "1001" }] }));
|
|
}
|
|
if (path.includes("departments/tree")) {
|
|
return new Response(JSON.stringify({ items: [{ id: "corp", name: "总公司", children: [{ id: "corp:dept-2", name: "研发部", children: [] }] }] }));
|
|
}
|
|
return new Response(JSON.stringify({ items: [{ id: "fee-1", name: "差旅费", code: "COST01", children: [] }] }));
|
|
},
|
|
} as any;
|
|
|
|
const result = await getBillReferences(client, "corp", "S26000001");
|
|
expect(result?.submitter?.name).toBe("张三");
|
|
expect(result?.department?.path).toBe("总公司/研发部");
|
|
expect(result?.feeTypes[0]?.name).toBe("差旅费");
|
|
expect(result?.unresolved).toEqual([]);
|
|
});
|
|
|
|
it("extracts linked loan and requisition nodes as a relation tree", async () => {
|
|
const client = {
|
|
post: async () => new Response(JSON.stringify({ value: {
|
|
id: "backlog-2",
|
|
flowId: {
|
|
id: "flow-2",
|
|
form: {
|
|
code: "E26000001",
|
|
title: "费用报销",
|
|
expenseLink: { id: "loan-1", code: "J26000001", title: "员工借款", loanMoney: { standard: "500" } },
|
|
linkRequisitionInfo: { id: "req-1", code: "Q26000001", title: "出差申请" },
|
|
},
|
|
},
|
|
} })),
|
|
} as any;
|
|
const result = await getBillRelations(client, "corp-relations", "E26000001");
|
|
expect(result?.relations.map((item) => item.id)).toEqual(["loan-1", "req-1"]);
|
|
expect(result?.relations[0]?.amount).toContain("500");
|
|
});
|
|
|
|
it("normalizes nested bill attachments and removes duplicates", async () => {
|
|
const client = {
|
|
post: async () => new Response(JSON.stringify({ value: {
|
|
id: "backlog-3",
|
|
flowId: {
|
|
form: {
|
|
code: "E26000002",
|
|
title: "带附件报销",
|
|
attachments: [
|
|
{ fileId: "file-1", key: "receipt.pdf", fileName: "receipt.pdf" },
|
|
{ fileId: "file-1", key: "receipt.pdf", fileName: "receipt.pdf" },
|
|
],
|
|
},
|
|
},
|
|
} })),
|
|
} as any;
|
|
const result = await getBillAttachments(client, "corp-files", "E26000002");
|
|
expect(result?.attachments).toHaveLength(1);
|
|
expect(result?.attachments[0]?.fileId).toBe("file-1");
|
|
expect(result?.attachments[0]?.fileName).toBe("receipt.pdf");
|
|
});
|
|
|
|
it("enriches attachment metadata with file-service download URLs", async () => {
|
|
const client = {
|
|
post: async (path: string) => {
|
|
if (path.includes("attachment/attachments/ids")) {
|
|
return new Response(JSON.stringify({
|
|
items: [{ id: "file-2", key: "receipt-key", name: "receipt.pdf", url: "https://files.example/receipt.pdf", thumbUrl: "https://files.example/thumb" }],
|
|
}));
|
|
}
|
|
if (path.includes("backlogs/search")) {
|
|
return new Response(JSON.stringify({ items: [{ id: "backlog-4", flowId: { form: { code: "E26000003", title: "附件元数据", attachments: [{ fileId: "file-2" }] } } }] }));
|
|
}
|
|
return new Response(JSON.stringify({ value: { id: "backlog-4", flowId: { form: { code: "E26000003", title: "附件元数据", attachments: [{ fileId: "file-2" }] } } } }));
|
|
},
|
|
} as any;
|
|
const result = await getBillAttachments(client, "corp-files", "E26000003");
|
|
expect(result?.attachments[0]).toMatchObject({
|
|
fileId: "file-2",
|
|
fileName: "receipt.pdf",
|
|
key: "receipt-key",
|
|
url: "https://files.example/receipt.pdf",
|
|
thumbUrl: "https://files.example/thumb",
|
|
});
|
|
});
|
|
|
|
it("normalizes risk warnings, budget state and invoice warnings", async () => {
|
|
const client = {
|
|
post: async (path: string) => {
|
|
if (path.includes("backlogs/search")) {
|
|
return new Response(JSON.stringify({ items: [{ id: "backlog-risk", flowId: { id: "flow-risk", form: { code: "S26000004", submitterId: { id: "staff-1" } } } }] }));
|
|
}
|
|
return new Response(JSON.stringify({ value: { id: "backlog-risk", flowId: { id: "flow-risk", form: { code: "S26000004", submitterId: { id: "staff-1" } } } } }));
|
|
},
|
|
get: async (path: string) => {
|
|
if (path.includes("coalition")) return new Response(JSON.stringify({ riskWarning: [{ riskLevel: "WARNING", controlName: "重复报销", messages: ["命中测试规则"] }] }));
|
|
if (path.includes("budget/report")) return new Response(JSON.stringify({ value: true, status: "OVER" }));
|
|
if (path.includes("invoice/riskWarning")) return new Response(JSON.stringify({ items: [{ type: "invoice", message: "发票抬头不一致" }] }));
|
|
return new Response(JSON.stringify({}));
|
|
},
|
|
} as any;
|
|
const result = await getBillRisk(client, "corp-risk", "S26000004");
|
|
expect(result?.flowId).toBe("flow-risk");
|
|
expect(result?.budgetExceeded).toBe(true);
|
|
expect(result?.warnings[0]?.controlName).toBe("重复报销");
|
|
expect(result?.invoiceWarnings).toHaveLength(1);
|
|
});
|
|
|
|
it("calculates approval-node dwell time and automatic skip markers", async () => {
|
|
const client = {
|
|
post: async (path: string) => {
|
|
if (path.includes("backlogs/search")) {
|
|
return new Response(JSON.stringify({ items: [{ id: "backlog-flow", flowId: { id: "flow-flow", form: { code: "S26000005", title: "流程时长" } } }] }));
|
|
}
|
|
return new Response(JSON.stringify({ value: { id: "backlog-flow", state: "APPROVING", nodeName: "财务审批", flowId: { id: "flow-flow", form: { code: "S26000005", title: "流程时长" }, logs: [
|
|
{ id: "log-1", time: 1000, operatorId: "staff-1", attributes: { nodeName: "提交" } },
|
|
{ id: "log-2", time: 2500, operatorId: null, state: "SKIPPED", attributes: { nodeName: "自动校验", isAuto: true, isSkip: true } },
|
|
], plan: { nodes: [] } } } }));
|
|
},
|
|
} as any;
|
|
const result = await getBillFlow(client, "corp-flow", "S26000005");
|
|
expect(result?.history[0]?.durationMs).toBe(1500);
|
|
expect(result?.history[1]?.automatic).toBe(true);
|
|
expect(result?.history[1]?.skipped).toBe(true);
|
|
});
|
|
|
|
it("builds the captured approve, reject, transfer, add-node and comment payloads", async () => {
|
|
const calls: Array<{ path: string; options: any }> = [];
|
|
const client = {
|
|
post: async (path: string, options: any) => {
|
|
calls.push({ path, options });
|
|
if (path.includes("backlogs/search")) {
|
|
return new Response(JSON.stringify({ items: [{ id: "backlog-action", flowId: { id: "flow-action", form: { code: "E26000007" } } }] }));
|
|
}
|
|
if (path.includes("backlogs/id")) {
|
|
return new Response(JSON.stringify({ value: {
|
|
id: "backlog-action",
|
|
nodeId: "FLOW:node-action",
|
|
nodeName: "直接领导审批",
|
|
state: "APPROVING",
|
|
flowId: { id: "flow-action", form: { code: "E26000007" }, plan: { nodes: [] } },
|
|
} }));
|
|
}
|
|
return new Response(null, { status: 204 });
|
|
},
|
|
} as any;
|
|
|
|
await approveBill(client, "corp-action", "E26000007", { comment: "同意" });
|
|
const approveCall = calls.at(-1);
|
|
expect(approveCall?.path).toBe("api/flow/v2/backlogs/$backlog-action");
|
|
expect(approveCall?.options.json).toMatchObject({
|
|
node: { flowId: "flow-action", id: "FLOW:node-action", name: "freeflow.agree" },
|
|
comment: "同意",
|
|
name: "freeflow.agree",
|
|
});
|
|
|
|
await rejectBill(client, "corp-action", "E26000007", { comment: "金额 < 不符" });
|
|
expect(calls.at(-1)?.path).toBe("api/flow/v2/backlogs/approveListWithNode");
|
|
expect(calls.at(-1)?.options.json.data[0]).toMatchObject({
|
|
id: "backlog-action",
|
|
comment: "<p>金额 < 不符</p>",
|
|
name: "freeflow.reject",
|
|
});
|
|
|
|
await transferBill(client, "corp-action", "E26000007", { approverIds: ["staff-2"] });
|
|
expect(calls.at(-1)?.options.json).toMatchObject({
|
|
approverId: ["staff-2"],
|
|
type: "SHIFT_NODE",
|
|
name: "freeflow.addnode",
|
|
});
|
|
|
|
await addNodeBill(client, "corp-action", "E26000007", { approverIds: ["staff-3"], comment: "请协助", addNodeType: "AFT_ADD_NODE" });
|
|
expect(calls.at(-1)?.options.json).toMatchObject({
|
|
approverId: ["staff-3"],
|
|
type: "AFT_ADD_NODE",
|
|
comment: "<p>请协助</p>",
|
|
name: "freeflow.addnode",
|
|
});
|
|
|
|
await commentBill(client, "corp-action", "E26000007", { comment: "补充说明" });
|
|
expect(calls.at(-1)?.path).toBe("api/flow/v1/flows/comment/$flow-action");
|
|
expect(calls.at(-1)?.options.json).toMatchObject({
|
|
id: "flow-action",
|
|
params: { comment: "<p>补充说明</p>", participants: [], attachments: [], keepItSecret: false },
|
|
name: "freeflow.comment",
|
|
});
|
|
});
|
|
|
|
it("resolves transfer and add-node people by full ID, employee code or unique name", async () => {
|
|
const client = {
|
|
get: async () => new Response(JSON.stringify({ items: [
|
|
{ id: "corp:staff-1", userId: "user-1", code: "101", name: "谢子豪" },
|
|
{ id: "corp:staff-2", userId: "user-2", code: "111", name: "沈玥妍" },
|
|
] })),
|
|
} as any;
|
|
await expect(resolveBillStaffIds(client, "corp-action", ["111", "谢子豪"])).resolves.toEqual(["corp:staff-2", "corp:staff-1"]);
|
|
});
|
|
|
|
it("translates raw bill flow into standard markdown representation", () => {
|
|
const { translateBillFlowToMarkdown } = require("./translator.js");
|
|
const rawFlow = {
|
|
flowId: {
|
|
state: "APPROVING",
|
|
form: {
|
|
code: "S26000001",
|
|
title: "我🩷吃醋",
|
|
specificationId: "ID01MCcOgJl1td:PRESET_REQUISITION_TRIP",
|
|
submitterId: { name: "谢子豪" },
|
|
requisitionDate: 1769961600000,
|
|
submitDate: 1770284988706,
|
|
requisitionMoney: { standard: "1000.00", standardStrCode: "CNY" },
|
|
details: [
|
|
{
|
|
feeTypeId: "ID01MCcOgJl1td:hotel",
|
|
feeTypeForm: {
|
|
amount: { standard: "1000", standardStrCode: "CNY" },
|
|
feeDate: 1770220800000,
|
|
city: '[{"key":"513","label":"辽宁省/大连市/大连市区"}]',
|
|
consumptionReasons: "今日在大连见客户",
|
|
detailId: "ID01QkCjK2m47Z",
|
|
detailNo: 1,
|
|
},
|
|
},
|
|
],
|
|
},
|
|
},
|
|
};
|
|
|
|
const markdown = translateBillFlowToMarkdown(rawFlow);
|
|
expect(markdown).toContain("# 单据:S26000001");
|
|
expect(markdown).toContain("- **状态**: 审批中 (approving)");
|
|
expect(markdown).toContain("- **单据模板**: `差旅申请单`");
|
|
expect(markdown).toContain("- **标题**: `我🩷吃醋`");
|
|
expect(markdown).toContain("- **提交人**: 谢子豪");
|
|
expect(markdown).toContain("- **申请金额**: CNY 1000.00");
|
|
expect(markdown).toContain("### 费用明细(共 1 条)");
|
|
expect(markdown).toContain("- **城市**: 辽宁省/大连市/大连市区");
|
|
expect(markdown).toContain("- **消费事由**: `今日在大连见客户`");
|
|
});
|
|
});
|