Files
hose-use/src/domains/approval/gateway.test.ts
T

286 lines
11 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import {
analyzeFindings,
validateGatewayAction,
evaluateGatewayPolicy,
ApprovalGatewayPolicy,
GatewayPolicyConfig,
} from "./gateway.js";
import { ApprovalFindingRecord, ApprovalGatewayBlockedError } from "./types.js";
function makeFinding(partial: Partial<ApprovalFindingRecord>): ApprovalFindingRecord {
return {
id: crypto.randomUUID(),
runId: "test-run",
clauseText: partial.clauseText ?? "测试条款",
strictness: partial.strictness ?? "硬性",
checkStatus: partial.checkStatus ?? "PASS",
evidence: partial.evidence ?? null,
reasoning: partial.reasoning ?? null,
createdAt: Date.now(),
};
}
describe("Approval Gateway Policy - 可配置审批网关规则测试", () => {
// ── 1. 硬性违规场景 ────────────────────────────────────────────────────────
test("硬性违规:必须驳回,绝对禁止同意放行", () => {
const findings: ApprovalFindingRecord[] = [
makeFinding({ clauseText: "必须上传真实发票", strictness: "硬性", checkStatus: "FAIL", evidence: "无发票" }),
makeFinding({ clauseText: "建议填写出差事由", strictness: "建议", checkStatus: "PASS" }),
];
// 1.1 尝试 approve 必须被网关强力阻断
expect(() => {
validateGatewayAction({
documentSn: "DOC-001",
findings,
requestedAction: "approve",
opinion: "尝试放行",
});
}).toThrow(ApprovalGatewayBlockedError);
// 1.2 尝试 reject 可以正常放行
expect(() => {
validateGatewayAction({
documentSn: "DOC-001",
findings,
requestedAction: "reject",
opinion: "缺少发票予以驳回",
});
}).not.toThrow();
// 1.3 默认仲裁决断结果必须为 REJECT
const result = evaluateGatewayPolicy({
documentSn: "DOC-001",
findings,
});
expect(result.decision).toBe("REJECT");
expect(result.hardFailedCount).toBe(1);
expect(result.decisionSummary).toContain("触犯硬性刚性条款,决策锁定为【驳回】");
});
// ── 2. 零违规(全通过)场景 ────────────────────────────────────────────────
test("零违规:没有硬性或弹性违规不能驳回,必须同意放行", () => {
const findings: ApprovalFindingRecord[] = [
makeFinding({ clauseText: "必须上传真实发票", strictness: "硬性", checkStatus: "PASS" }),
makeFinding({ clauseText: "尽量上传付款水单", strictness: "弹性", checkStatus: "PASS" }),
makeFinding({ clauseText: "建议补充事由", strictness: "建议", checkStatus: "SKIP" }),
];
// 2.1 尝试 reject 必须被网关阻断(禁止无因驳回)
expect(() => {
validateGatewayAction({
documentSn: "DOC-002",
findings,
requestedAction: "reject",
opinion: "恶意驳回",
});
}).toThrow(ApprovalGatewayBlockedError);
try {
validateGatewayAction({
documentSn: "DOC-002",
findings,
requestedAction: "reject",
opinion: "恶意驳回",
});
} catch (err: any) {
expect(err.message).toContain("不存在任何【硬性】或【弹性】违规条款");
}
// 2.2 尝试 approve 正常放行
expect(() => {
validateGatewayAction({
documentSn: "DOC-002",
findings,
requestedAction: "approve",
opinion: "合规通过",
});
}).not.toThrow();
// 2.3 决策结果为 APPROVE
const result = evaluateGatewayPolicy({
documentSn: "DOC-002",
findings,
});
expect(result.decision).toBe("APPROVE");
expect(result.decisionSummary).toContain("决策锁定为【同意通过】");
});
// ── 3. 仅建议项违规场景 ────────────────────────────────────────────────────
test("仅建议项违规:不构成驳回理由,禁止驳回,决断为备注通过", () => {
const findings: ApprovalFindingRecord[] = [
makeFinding({ clauseText: "必须上传真实发票", strictness: "硬性", checkStatus: "PASS" }),
makeFinding({ clauseText: "建议补充说明消费事由", strictness: "建议", checkStatus: "FAIL", evidence: "事由偏简略" }),
];
// 3.1 尝试驳回:建议项不能作为驳回理由,必须阻断
expect(() => {
validateGatewayAction({
documentSn: "DOC-003",
findings,
requestedAction: "reject",
opinion: "因事由简略驳回",
});
}).toThrow("建议项不构成驳回理由");
// 3.2 尝试同意:放行
expect(() => {
validateGatewayAction({
documentSn: "DOC-003",
findings,
requestedAction: "approve",
opinion: "备注事由后放行",
});
}).not.toThrow();
// 3.3 决断结果为 PASS_WITH_NOTE
const result = evaluateGatewayPolicy({
documentSn: "DOC-003",
findings,
});
expect(result.decision).toBe("PASS_WITH_NOTE");
expect(result.decisionSummary).toContain("决策锁定为【备注通过】");
});
// ── 4. 弹性违规场景 ────────────────────────────────────────────────────────
test("弹性违规:允许驳回;数量 <= 2 时为备注通过,>= 3 时建议人工复核", () => {
// 4.1 存在 1 项弹性违规
const findings1: ApprovalFindingRecord[] = [
makeFinding({ clauseText: "必须上传发票", strictness: "硬性", checkStatus: "PASS" }),
makeFinding({ clauseText: "尽量上传水单", strictness: "弹性", checkStatus: "FAIL", evidence: "未提供水单" }),
];
// 因为有弹性违规,允许驳回
expect(() => {
validateGatewayAction({
documentSn: "DOC-004-1",
findings: findings1,
requestedAction: "reject",
opinion: "未提供水单予以驳回",
});
}).not.toThrow();
// 自动决断为 PASS_WITH_NOTE(弹性超标未达阈值)
const result1 = evaluateGatewayPolicy({
documentSn: "DOC-004-1",
findings: findings1,
});
expect(result1.decision).toBe("PASS_WITH_NOTE");
// 4.2 存在 3 项弹性违规(达到阈值)
const findings3: ApprovalFindingRecord[] = [
makeFinding({ clauseText: "必须上传发票", strictness: "硬性", checkStatus: "PASS" }),
makeFinding({ clauseText: "尽量上传水单1", strictness: "弹性", checkStatus: "FAIL" }),
makeFinding({ clauseText: "尽量上传水单2", strictness: "弹性", checkStatus: "FAIL" }),
makeFinding({ clauseText: "尽量上传水单3", strictness: "弹性", checkStatus: "FAIL" }),
];
const result3 = evaluateGatewayPolicy({
documentSn: "DOC-004-3",
findings: findings3,
});
expect(result3.decision).toBe("REVIEW");
expect(result3.decisionSummary).toContain("建议人工复核");
});
// ── 5. 特殊项违规场景 ──────────────────────────────────────────────────────
test("特殊项命中:转人工复核,禁止直接驳回", () => {
const findings: ApprovalFindingRecord[] = [
makeFinding({ clauseText: "必须上传发票", strictness: "硬性", checkStatus: "PASS" }),
makeFinding({ clauseText: "大额争议须法务审核", strictness: "特殊", checkStatus: "FAIL", evidence: "单据超10万" }),
];
expect(() => {
validateGatewayAction({
documentSn: "DOC-005",
findings,
requestedAction: "reject",
opinion: "法务未审直接驳回",
});
}).toThrow("特殊");
const result = evaluateGatewayPolicy({
documentSn: "DOC-005",
findings,
});
expect(result.decision).toBe("REVIEW");
});
// ── 6. 待核验(PENDING)漏检拦截 ───────────────────────────────────────────
test("待核验漏检保护:存在 PENDING 要点时,同意和驳回均被阻断", () => {
const findings: ApprovalFindingRecord[] = [
makeFinding({ clauseText: "发票检查", strictness: "硬性", checkStatus: "PASS" }),
makeFinding({ clauseText: "住宿限额检查", strictness: "硬性", checkStatus: "PENDING" }),
];
// 同意被阻断
expect(() => {
validateGatewayAction({
documentSn: "DOC-006",
findings,
requestedAction: "approve",
opinion: "盲目同意",
});
}).toThrow("PENDING 待核验状态");
// 驳回也被阻断(必须先完成核验)
expect(() => {
validateGatewayAction({
documentSn: "DOC-006",
findings,
requestedAction: "reject",
opinion: "盲目驳回",
});
}).toThrow("PENDING 待核验状态");
});
// ── 7. 审批意见长度边角限制 ───────────────────────────────────────────────
test("审批意见长度控制:超过 150 字符时抛错阻断", () => {
const findings: ApprovalFindingRecord[] = [
makeFinding({ clauseText: "发票检查", strictness: "硬性", checkStatus: "FAIL" }),
];
const overLength = "超长意见内容".repeat(35); // > 150 字
expect(() => {
validateGatewayAction({
documentSn: "DOC-007",
findings,
requestedAction: "reject",
opinion: overLength,
});
}).toThrow("不可超过 150 字符");
});
// ── 8. 自定义逻辑扩展钩子 (customHook) ────────────────────────────────────
test("支持配置自定义网关钩子覆盖或扩展逻辑", () => {
const findings: ApprovalFindingRecord[] = [
makeFinding({ clauseText: "普通合规检查", strictness: "建议", checkStatus: "PASS" }),
];
const policy = new ApprovalGatewayPolicy({
customHook: (context, analysis, config) => {
if (context.documentSn.startsWith("VIP-")) {
return {
decision: "APPROVE",
decisionSummary: "VIP 单据走免审专属绿色通道",
analysis,
hardFailedCount: 0,
elasticFailedCount: 0,
suggestFailedCount: 0,
specialFailedCount: 0,
};
}
},
});
const result = policy.evaluate({
documentSn: "VIP-88888",
findings,
});
expect(result.decision).toBe("APPROVE");
expect(result.decisionSummary).toBe("VIP 单据走免审专属绿色通道");
});
});