Files
hose-use/src/domains/bill/write-response.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

44 lines
2.0 KiB
TypeScript

import { expect, test } from "bun:test";
import ky from "ky";
import { createBillFlow, deleteBillFlow, submitBillFlow } from "./api.js";
import { AuthenticationError, EkbApiError, PermissionError } from "../../core/errors.js";
function fixture(response: unknown, status = 200) {
let requests = 0;
const client = ky.create({ prefix: "https://tenant.invalid", retry: 0, fetch: async () => {
requests++;
return Response.json(response, { status });
} });
return { client, count: () => requests };
}
test("创建兼容 flow/value/裸对象包装,保留正常业务单号", async () => {
const flow = { id: "created", code: "B26000001", form: { code: "B26000001" } };
for (const response of [{ flow }, { value: flow }, flow]) {
expect(await createBillFlow(fixture(response).client, "corp", { title: "草稿" })).toEqual(flow);
}
});
test("所有单据写动作检查 HTTP 200 中的业务拒绝与鉴权错误", async () => {
for (const action of [
(client: ReturnType<typeof ky.create>) => createBillFlow(client, "corp", {}),
(client: ReturnType<typeof ky.create>) => submitBillFlow(client, "corp", "f", {}, "expense", []),
(client: ReturnType<typeof ky.create>) => deleteBillFlow(client, "corp", "f"),
]) {
for (const [errorCode, ErrorClass] of [["REJECTED", EkbApiError], [401, AuthenticationError], [403, PermissionError]] as const) {
const { client, count } = fixture({ errorCode, errorMessage: "拒绝操作" });
await expect(action(client)).rejects.toBeInstanceOf(ErrorClass);
expect(count()).toBe(1);
}
}
});
test("HTTP 失败不被吞掉;无效或空流程 ID 不被强转为成功", async () => {
const { client, count } = fixture({ error: "unavailable" }, 503);
await expect(createBillFlow(client, "corp", {})).rejects.toThrow();
expect(count()).toBe(1);
for (const response of [{ flow: {} }, { flow: { id: 1 } }, { value: { id: " " } }, { id: "f", form: [] }]) {
await expect(createBillFlow(fixture(response).client, "corp", {})).rejects.toBeInstanceOf(EkbApiError);
}
});