Add P0 regression tests for replay --sn and regression --webhook

Both P0s from the September bug report were fixed but had no regression
test guarding them:

- replay --sn ignored the argument and replayed the newest record. The new
  test feeds a two-record search and asserts the requested (older) record is
  replayed — and that a missing SN fails with a lookup error rather than
  falling back to the newest.
- regression demanded --webhook but honouring it did nothing. The new test
  runs the live path with two target channels: without a selector it exits 13
  with guidance, with --webhook the selector error disappears (mockApiFetch
  learns to serve the approval-style channel catalog).
This commit is contained in:
gitea committed 2026-09-29 22:05:49 +08:00
1 parent 7ceb200aac
commit 8a822aa99a
3 files changed
+259 -1

No files matched your search

+108 -1
View File
@@ -144,7 +144,7 @@ function setupTestEnv() {
return { root, enterprises, prodCorp, testCorp, unpairedCorp };
}
function mockApiFetch() {
function mockApiFetch(options: { integrations?: unknown[] } = {}) {
globalThis.fetch = (async (input: RequestInfo | URL) => {
const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
@@ -177,6 +177,34 @@ function mockApiFetch() {
);
}
// Approval-style preferences: the webhook channel catalog regression reads
// through ensureTestWebhookIntegration. Default to one channel so the
// existing dry-run tests keep resolving without a selector.
if (url.includes("ai-approval/config")) {
const integrations = options.integrations ?? [
{
type: "webhook",
id: "wh-default",
name: "默认通道",
mode: "assisted",
incomingDocumentWebhook: {
id: "in-default",
url: "https://test.sealai.cc/api/v1/integrations/webhook/wh-default/document"
},
outgoingResultWebhook: "https://test.sealai.cc/out",
incomingManualResultWebhook: {
id: "in-default",
url: "https://test.sealai.cc/man"
},
secret: "default-secret"
}
];
return new Response(JSON.stringify({ data: { integrations } }), {
status: 200,
headers: { "Content-Type": "application/json" }
});
}
return new Response("Not found", { status: 404 });
}) as any;
}
@@ -250,4 +278,83 @@ describe("seal-use test regression CLI", () => {
expect(payload.verdict).toBe("DRY_RUN");
expect(payload.targetCorp.id).toBe("corp-test");
});
test("regression without --webhook and with several target channels fails with guidance instead of picking one (P0 regression)", async () => {
setupTestEnv();
// Target tenant exposes TWO channels and neither is replay-named, so the
// selector must refuse rather than silently choose. The original bug
// repeated this same error even when --webhook WAS supplied, so pair this
// with the passing `--webhook` case above: guidance now matches behavior.
mockApiFetch({
integrations: [
{
type: "webhook",
id: "wh-1",
name: "通道甲",
mode: "assisted",
incomingDocumentWebhook: {
id: "in_1",
url: "https://test.sealai.cc/api/v1/integrations/webhook/wh-1/document"
},
outgoingResultWebhook: "https://test.sealai.cc/out",
incomingManualResultWebhook: {
id: "in_1",
url: "https://test.sealai.cc/man"
},
secret: "s1"
},
{
type: "webhook",
id: "wh-2",
name: "通道乙",
mode: "assisted",
incomingDocumentWebhook: {
id: "in_2",
url: "https://test.sealai.cc/api/v1/integrations/webhook/wh-2/document"
},
outgoingResultWebhook: "https://test.sealai.cc/out",
incomingManualResultWebhook: {
id: "in_2",
url: "https://test.sealai.cc/man"
},
secret: "s2"
}
]
});
// Live run (no --dry-run): the selector is consulted on the live path,
// which is where the P0 bug reproduced — guidance demanded --webhook but
// supplying it did not help.
const result = await runCli([
"test",
"regression",
"--corp",
"corp-test",
"--samples",
"1"
]);
// 13: local config/input validation — the caller must name a channel.
expect(result.exitCode).toBe(13);
expect(result.stderr).toContain("请通过 --webhook <idOrName> 指定目标通道");
// Supplying the selector resolves THAT channel instead of repeating the
// error (the exact P0 scenario). The run then proceeds past channel
// resolution; a later upstream failure in the minimal mock is fine as long
// as the selector error is gone.
const withSelector = await runCli([
"test",
"regression",
"--corp",
"corp-test",
"--samples",
"1",
"--webhook",
"通道乙"
]);
expect(withSelector.stderr).not.toContain("请通过 --webhook <idOrName> 指定目标通道");
expect(withSelector.stderr).not.toContain("未找到匹配 \"通道乙\"");
// It must have resolved channel 乙: any failure after that point is
// mock-surface, not selector logic.
expect(withSelector.exitCode).not.toBe(13);
});
});
+85
View File
@@ -167,6 +167,91 @@ describe("seal-use webhook replay CLI", () => {
expect(refField.type).toBe("TEXT");
});
test("--sn replays exactly the requested record, not the newest one (regression for --sn being ignored)", async () => {
setupMockEnv();
// Two historical records; the requested one is the OLDER of the pair.
// The original P0 bug ignored --sn and replayed the newest record.
const oldRecord = { ...mockApprovalRun, id: "run-old", sourceDocumentSN: "BX9990001", createdAt: 1789000000000 };
const newRecord = {
...mockApprovalRun,
id: "run-new",
sourceDocumentSN: "BX9990002",
sourceDocumentId: "DOC-NEW",
createdAt: 1789999999000,
document: { ...mockApprovalRun.document, id: "DOC-NEW", sn: "BX9990002" }
};
const requestedSn = "BX9990001";
let approvalsSearchUrl = "";
globalThis.fetch = (async (input: RequestInfo | URL) => {
const requestUrl =
typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
if (requestUrl.includes("api/v1/approvals") && !requestUrl.includes("api/v1/approvals/")) {
approvalsSearchUrl = requestUrl;
// Newest first, as the API returns them: the bug picked records[0].
return new Response(JSON.stringify({ data: { records: [newRecord, oldRecord] } }), {
status: 200,
headers: { "Content-Type": "application/json" }
});
}
if (requestUrl.includes("api/v1/system/preferences/approval-style")) {
return new Response(JSON.stringify({ data: { integrations: mockWebhookIntegrations } }), {
status: 200,
headers: { "Content-Type": "application/json" }
});
}
return new Response("Not found", { status: 404 });
}) as any;
const result = await runCli(["webhook", "replay", "--sn", requestedSn, "--dry-run"]);
expect(result.isError).toBe(false);
expect(result.exitCode).toBe(0);
// The search itself must target the requested SN.
expect(approvalsSearchUrl).toContain(`sourceDocumentSN=${requestedSn}`);
const parsed = JSON.parse(result.stdout);
expect(parsed.dryRun).toBe(true);
expect(parsed.totalCount).toBe(1);
expect(parsed.originalRecord.id).toBe("run-old");
expect(parsed.originalRecord.sourceDocumentSN).toBe(requestedSn);
expect(parsed.document.documentSN).toBe(requestedSn);
// The newer record must NOT be the one replayed.
expect(JSON.stringify(parsed)).not.toContain("run-new");
expect(JSON.stringify(parsed)).not.toContain("BX9990002");
});
test("--sn with a document that does not exist fails with a lookup error instead of falling back to the newest", async () => {
setupMockEnv();
globalThis.fetch = (async (input: RequestInfo | URL) => {
const requestUrl =
typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
if (requestUrl.includes("api/v1/approvals")) {
return new Response(JSON.stringify({ data: { records: [mockApprovalRun] } }), {
status: 200,
headers: { "Content-Type": "application/json" }
});
}
if (requestUrl.includes("api/v1/system/preferences/approval-style")) {
return new Response(JSON.stringify({ data: { integrations: mockWebhookIntegrations } }), {
status: 200,
headers: { "Content-Type": "application/json" }
});
}
return new Response("Not found", { status: 404 });
}) as any;
const result = await runCli(["webhook", "replay", "--sn", "DOES-NOT-EXIST", "--dry-run"]);
expect(result.isError).toBe(true);
expect(result.stderr).toContain("未找到单据编号为「DOES-NOT-EXIST」的审批记录");
});
test("executes live webhook replay with successful response", async () => {
setupMockEnv();
let pushedBody: any = null;