127 lines
4.4 KiB
TypeScript
127 lines
4.4 KiB
TypeScript
import { describe, it, expect } from "bun:test";
|
|
import { formatSingleBill, getBillReferences, getBillRelations } 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");
|
|
});
|
|
});
|