import { afterEach, describe, expect, it, spyOn } from "bun:test"; import * as session from "../../../core/http/hose-web-session-client.js"; import { parseArgs } from "../../parser.js"; import { createBillActionContext } from "./context.js"; import { handleList } from "./search.js"; const spies: Array<{ mockRestore(): void }> = []; afterEach(() => { for (const spy of spies.splice(0).reverse()) spy.mockRestore(); }); function json(value: unknown): Response { return new Response(JSON.stringify(value)); } function draftClient(corpId: string) { const flowsPosts: any[] = []; const client = { get: async (url: string) => { if (url.includes("staffs/actives")) { return json({ items: [{ id: "staff-1", name: "张三", code: "1001" }] }); } return json({}); }, post: async (url: string, payload: any) => { if (url.includes("datalink/entity/search")) return json({ items: [] }); if (url.endsWith("/flows/my")) { flowsPosts.push({ url, payload, corpId }); return json({ count: 1, items: [{ flow: { id: "flow-cli-draft", state: "draft", formType: "payment", createTime: 1781682826000, updateTime: 1781682870000, }, form: { code: "FK26000009", title: "付款草稿CLI", specificationId: { id: "spec-pay", name: "对公付款单" }, submitterId: { id: "staff-1", name: "张三", code: "1001" }, payMoney: { standard: "10.00", standardSymbol: "¥", standardStrCode: "CNY" }, }, }], }); } return json({}); }, }; return { client, flowsPosts }; } function context(args: string[], corpId: string, jsonMode: boolean) { const corp = { id: corpId, name: "测试企业", credentials: { corpId } }; return createBillActionContext(parseArgs(args), { jsonMode, selectedCorp: corp } as any); } describe("bill list draft mode", () => { it("routes --state=draft to flows/my with template, title, submitter and page filters", async () => { const { client, flowsPosts } = draftClient("cli-draft-corp-json"); spies.push(spyOn(session, "createSessionClient").mockResolvedValue(client as any)); const args = [ "bill", "list", "--state=draft", "--mine", `--template=spec-pay:${"a".repeat(32)}`, "--title=付款", "--staff=张三", "--page=2", "--count=10", "--json", ]; const result = await handleList(context(args, "cli-draft-corp-json", true)); expect(result.exitCode).toBe(0); const output = JSON.parse(result.stdout); expect(output.total).toBe(1); expect(output.items[0]).toMatchObject({ billId: "flow-cli-draft", billCode: "FK26000009", billName: "付款草稿CLI", state: "draft", }); expect(output.items[0].amount).toContain("10.00"); expect(flowsPosts).toHaveLength(1); const filterBy = flowsPosts[0].payload.json.filterBy as string; expect(filterBy).toContain('(state.in("draft"))'); expect(filterBy).toContain('INSTR(form.title, "付款")'); expect(filterBy).toContain('INSTR(form.specificationId, "spec-pay")'); expect(filterBy).not.toContain("a".repeat(32)); expect(filterBy).toContain('INSTR(form.submitterId, "staff-1")'); expect(flowsPosts[0].payload.json.limit).toEqual({ start: 10, count: 10 }); expect(flowsPosts[0].payload.json.orderBy).toEqual([{ value: "createTime", order: "DESC" }]); }); it("renders a draft-specific text header for --state=草稿", async () => { const { client } = draftClient("cli-draft-corp-text"); spies.push(spyOn(session, "createSessionClient").mockResolvedValue(client as any)); const result = await handleList(context(["bill", "list", "--state=草稿"], "cli-draft-corp-text", false)); expect(result.exitCode).toBe(0); expect(result.stdout).toContain("单据草稿列表"); expect(result.stdout).toContain("FK26000009"); expect(result.stdout).toContain("bill submit"); }); });