merge: integrate review-02-approval-corp-boundary

This commit is contained in:
gitea committed 2026-10-20 23:01:34 +08:00
commit 51e4b39807
4 files changed
+170 -133

No files matched your search

@@ -0,0 +1,30 @@
import { afterEach, expect, mock, spyOn, test } from "bun:test";
import { getDatabase, closeDatabase } from "../../../core/db/index.js";
import { ApprovalRepository } from "../../../domains/approval/repository.js";
import * as bills from "../../../domains/bill/tools.js";
import { handleAiProDecide } from "./session.js";
afterEach(() => { closeDatabase(); });
for (const action of ["approve", "reject"] as const) {
test(`blocks cross-corp ${action} before creating a client or changing the run`, async () => {
const repo = new ApprovalRepository(getDatabase(":memory:"));
const run = repo.createApprovalRun({ documentSn: "SAME-SN", corpId: "corp-A" });
repo.injectFindings(run.id, [{ clauseText: "invoice", strictness: "硬性", checkStatus: action === "approve" ? "PASS" : "FAIL" }]);
const getClient = mock(async () => ({ client: {} as any, corpId: "corp-B" }));
await expect(handleAiProDecide({ repo, corpId: "corp-B", command: action, positional: [action, run.shortId!], parsed: { options: { apply: true, yes: true, opinion: "checked" } }, getClient } as any)).rejects.toThrow("不一致");
expect(getClient).not.toHaveBeenCalled();
expect(repo.getRunById(run.id)?.decision).toBe("PENDING");
});
}
test("uses the checked corp client for same-corp approval", async () => {
const repo = new ApprovalRepository(getDatabase(":memory:"));
const run = repo.createApprovalRun({ documentSn: "SAME-SN", corpId: "corp-A" });
repo.injectFindings(run.id, [{ clauseText: "invoice", strictness: "硬性", checkStatus: "PASS" }]);
const approve = spyOn(bills, "approveBill").mockResolvedValue({} as any);
const client = {} as any;
try {
const result = await handleAiProDecide({ repo, corpId: "corp-A", command: "approve", positional: ["approve", run.shortId!], parsed: { options: { apply: true, yes: true, opinion: "checked" } }, getClient: async () => ({ client, corpId: "corp-A" }), rawArgs: [], operator: "staff" } as any);
expect(result.exitCode).toBe(0);
expect(approve).toHaveBeenCalledWith(client, "corp-A", "SAME-SN", { comment: "checked" });
} finally { approve.mockRestore(); }
});
+5
View File
@@ -1,3 +1,4 @@
import { PermissionError } from "../../../core/errors.js";
import {
type ApprovalRunRecord,
type CreateFindingInput,
@@ -440,6 +441,9 @@ export async function handleAiProDecide(ctx: AiProExecutionContext): Promise<AiP
let executionStatus = "模拟完成(未带 --apply,未真实修改线上单据)";
if (apply) {
if (!runRecord.corpId || ctx.corpId !== runRecord.corpId) {
throw new PermissionError(`审批会话所属企业 [${runRecord.corpId || "未知"}] 与当前企业 [${ctx.corpId || "未选择"}] 不一致,请显式选择会话所属企业。`);
}
const confirmed = booleanOption(parsed.options, ["yes", "y", "confirm"]);
if (!confirmed) {
if (!process.stdin.isTTY || !process.stdout.isTTY) {
@@ -453,6 +457,7 @@ export async function handleAiProDecide(ctx: AiProExecutionContext): Promise<AiP
}
const context = await getClient();
if (context.corpId !== runRecord.corpId) throw new PermissionError("审批客户端企业与会话所属企业不一致");
if (action === "APPROVE") {
await approveBill(context.client, context.corpId, runRecord.documentSn, {
comment: opinion,
+2
View File
@@ -15,3 +15,5 @@
- 决策必须经 gateway 仲裁:`ApprovalDecision` 为封闭枚举(`APPROVE` / `REJECT` / `PASS_WITH_NOTE` / `REVIEW` / `PENDING` / `ERROR`),写动作由 `validateGatewayAction` 卡点。
- 规则严格度分四级:`硬性` / `弹性` / `建议` / `特殊`。
AI PRO 执行真实审批前必须校验会话企业与当前企业、客户端企业一致;企业信息缺失或不一致时拒绝下发,不更新该会话的决策。