Files
hose-use/src/cli/commands/bill/create.test.ts
T
gitea 51fb54dfe8 fix: 统一单据写前校验与传输结果契约
将模板、当前员工、草稿创建/删除、审批计划和送审共七处 HTTP 调用收口到 bill/api.ts,业务层函数签名保持兼容。创建与预演复用实际规格校验,缺失明细规格、必填字段或超长文本均先于写入拒绝。
统一检查已知业务错误及有效流程 ID,保留 flow/value/裸对象成功包装与已有单号字段;补充业务拒绝、HTTP 失败、无效 ID、计划失败不送审及 CLI 非零退出回归,同步域文档。

验证:bun test 911 通过、0 失败;bun run check、git diff --check 通过。未调用真实企业接口。
2026-10-29 18:14:52 +08:00

39 lines
2.6 KiB
TypeScript

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: "服务端拒绝创建" });
});