import { expect, test } from "bun:test"; import { handleCreate } from "./create.js"; import { parseArgs } from "../../parser.js"; import { cliErrorResult } from "../../runner.js"; test("create dry-run resolves the same available fee as execution and never posts", async () => { const writes: any[] = []; const client = { get: (url: string) => ({ json: async () => url.includes("specifications/") ? { items: [{ id: "spec", name: "模板", components: [{ field: "title" }] }] } : url.includes("staffs/me") ? { value: { staff: { id: "staff", defaultDepartment: "dept" } } } : { items: [{ feeType: { id: "travel", name: "差旅", expenseSpecificationId: "detail" }, children: [] }] } }), post: (_: string, payload: any) => { writes.push(payload.json); return { json: async () => ({ flow: { id: "new" } }) }; }, }; const base = ["bill", "create", "--template", `spec:${"a".repeat(32)}`, "--json"]; const context = (args: string[]) => ({ parsed: parseArgs(args), runtime: { jsonMode: true }, corpId: "corp", getClient: async () => client, requireWriteConfirmation: async () => {} }) as any; const preview = await handleCreate(context([...base, "--dry-run"])); expect(JSON.parse(preview.stdout)).toMatchObject({ dryRun: true, feeType: "差旅" }); expect(writes).toEqual([]); await handleCreate(context([...base, "--yes"])); expect(writes[0].form.details[0].feeTypeId).toBe("travel"); await expect(handleCreate(context([...base, "--fee-type", "不存在", "--dry-run"]))).rejects.toThrow("费用类型"); expect(writes).toHaveLength(1); }); test("创建收到业务错误时由 CLI 错误边界返回非零,不输出创建成功", async () => { const client = { get: (url: string) => ({ json: async () => url.includes("specifications/") ? { items: [{ id: "spec", name: "模板", components: [{ field: "title" }] }] } : url.includes("staffs/me") ? { value: { staff: { id: "staff", defaultDepartment: "dept" } } } : { items: [{ id: "fee", name: "费用", expenseSpecificationId: "detail" }] } }), post: () => ({ json: async () => ({ errorCode: "REJECTED", errorMessage: "服务端拒绝创建" }) }), }; const context = { parsed: parseArgs(["bill", "create", "--template", `spec:${"a".repeat(32)}`, "--yes", "--json"]), runtime: { jsonMode: true }, corpId: "corp", getClient: async () => client, requireWriteConfirmation: async () => {} } as any; const result = await handleCreate(context).catch(error => cliErrorResult(error, true)); expect(result.exitCode).not.toBe(0); expect(JSON.parse(result.stdout)).toMatchObject({ error: true, message: "服务端拒绝创建" }); });