Fix absent-SN record match and document --sign-key on replay

Two findings from reviewing the outbound-push extraction:

The verify poll compared `record.sourceDocumentSN === sn` with `sn` optional,
so a caller that only knows the flowId compared against `undefined` and
matched any record lacking an SN — reporting an unrelated run as the one this
push created, with its id and taskMode in the response. Guard the needle:
an absent SN matches nothing. This class did not exist on the deploy path
before (it compared against flowId only), so it was introduced by the merge.

The replay help text also never listed --sign-key even though the channel
resolves the target integration through it, and selectHoseIntegration now
errors on an ambiguous list with "请通过 --sign-key 指定" — pointing at a
flag the user could not discover from `help approval-runs`.

Adds direct tests for replay-safety.ts (the exit-14 wording, the next action,
and that --force is the only override), which previously had none.
This commit is contained in:
gitea committed 2026-10-11 19:51:29 +08:00
1 parent 9ece30255e
commit 8c3d98fd98
4 files changed
+104 -4

No files matched your search

+3 -2
View File
@@ -584,14 +584,15 @@ export const HELP_REGISTRY: AreaHelp[] = [
usage: [
"seal-use approval-runs replay <recordId> [--webhook <idOrName>] [--sn-suffix <str>] [--dry-run] [--corp <corpId>]",
"seal-use approval-runs replay --sn <documentSN> [--webhook <idOrName>] [--source-corp <corpId>] [--target-corp <corpId>]",
"seal-use approval-runs replay --sn <documentSN> --channel hose-outbound [--target-corp <corpId>] [--dry-run]",
"seal-use approval-runs replay <recordId> --channel hose-outbound [--target-corp <corpId>] [--dry-run]",
"seal-use approval-runs replay --sn <documentSN> --channel hose-outbound [--sign-key <key>] [--target-corp <corpId>] [--dry-run]",
"seal-use approval-runs replay <recordId> --channel hose-outbound [--sign-key <key>] [--target-corp <corpId>] [--dry-run]",
"seal-use approval-runs replay --limit <n> [--status <status>] [--source-corp <corpId>] [--target-corp <corpId>]"
],
options: [
{ flag: "<recordId>", description: "Approval run record ID" },
{ flag: "--sn <sn>", description: "Lookup approval record by source document SN" },
{ flag: "--channel <webhook|hose-outbound>", description: "Replay channel: webhook (default) or hose-outbound (Hosecloud official outbound)" },
{ flag: "--sign-key <key>", description: "Target Hose integration signKey for hose-outbound; required when the target has several integrations" },
{ flag: "--message-id <msgId>", description: "Explicit Hose outbound messageId override" },
{ flag: "--rejection-num <n>", description: "Target specific rejection iteration (0, 1, 2, ...)" },
{ flag: "--limit <n>", description: "Batch replay latest N records" },
+30
View File
@@ -300,6 +300,36 @@ describe("pushOutboundAndVerify", () => {
}
});
test("an absent sn never matches a record that has no SN", async () => {
// Regression: comparing `record.sourceDocumentSN === sn` with `sn`
// undefined matched any record lacking an SN, so an unrelated run was
// reported as the one this push created.
const lines: string[] = [];
const originalError = console.error;
console.error = (line: string) => lines.push(line);
try {
const { createdRecord } = await pushOutboundAndVerify({
client: fakeClient([{ id: "unrelated-old-run" }]),
signKey: "key-1",
payload: buildOutboundPayload({
flowId: "ID01abc",
messageId: "msg-1",
corporationId: "c"
}),
flowId: "ID01abc",
corpName: "目标企业",
corpId: "target-corp",
verifyWaitMs: 0
});
expect(createdRecord).toBeUndefined();
expect(lines.join("\n")).toContain("消息已入队正在异步消费");
expect(lines.join("\n")).not.toContain("unrelated-old-run");
} finally {
console.error = originalError;
}
});
test("a failing poll never turns a successful push into an error", async () => {
const originalError = console.error;
console.error = () => {};
+7 -2
View File
@@ -176,14 +176,19 @@ export async function pushOutboundAndVerify(options: {
await new Promise((resolve) => setTimeout(resolve, options.verifyWaitMs ?? OUTBOUND_VERIFY_WAIT_MS));
// `sn` is optional: callers that only know the flowId leave it undefined, and
// an undefined needle must never match a record whose SN is absent — that
// would report an unrelated run as the one this push just created.
const snNeedle = sn?.trim() || undefined;
let createdRecord: ApprovalRun | undefined;
try {
const runs = await api.listApprovalRuns(client, { limit: 5, search: flowId });
createdRecord = (runs.records || []).find(
(record) =>
record.sourceDocumentId === flowId ||
record.sourceDocumentSN === sn ||
record.sourceDocumentSN === flowId
record.sourceDocumentSN === flowId ||
(snNeedle !== undefined && record.sourceDocumentSN === snNeedle)
);
} catch {
// Non-blocking: the run may not exist yet even when the push succeeded.
+64
View File
@@ -0,0 +1,64 @@
import { describe, expect, test } from "bun:test";
import type { CorpConfig } from "../../core/config/types.js";
import { assertNonProductionReplayTarget } from "./replay-safety.js";
function corp(overrides: Partial<CorpConfig> = {}): CorpConfig {
return {
id: "corp-test",
name: "测试企业",
environment: "test",
seal: { url: "https://test.sealai.cc" },
source: { type: "seal", token: "t", sealUrl: "https://test.sealai.cc" },
auth: { refreshTtl: 300 },
...overrides
} as CorpConfig;
}
const baseOptions = {
action: "重放单据",
nextAction: "seal-use webhook replay --target-corp <testCorpId>"
};
describe("assertNonProductionReplayTarget", () => {
test("lets a test-environment target through", () => {
expect(() => assertNonProductionReplayTarget(corp(), baseOptions)).not.toThrow();
});
test("blocks a production target with an exit-14 safety error", () => {
let thrown: unknown;
try {
assertNonProductionReplayTarget(corp({ environment: "production" }), baseOptions);
} catch (error) {
thrown = error;
}
expect(thrown).toBeInstanceOf(Error);
expect((thrown as { exitCode?: number }).exitCode).toBe(14);
// The wording must stay regex-classifiable by core/errors SAFETY_GUARD so
// an error crossing a process boundary still lands on exit 14.
expect((thrown as Error).message).toContain("标记为生产正式环境");
});
test("carries the caller's next action and action verb verbatim", () => {
try {
assertNonProductionReplayTarget(corp({ environment: "production" }), {
action: "重放出站报文",
nextAction: "seal-use webhook replay --channel hose-outbound --target-corp <testCorpId>"
});
throw new Error("expected a block");
} catch (error) {
expect((error as Error).message).toContain("禁止向生产环境重放出站报文");
expect((error as { nextAction?: string }).nextAction).toContain("--channel hose-outbound");
}
});
test("--force is the only override, and it must be explicit", () => {
const production = corp({ environment: "production" });
expect(() => assertNonProductionReplayTarget(production, baseOptions)).toThrow();
expect(() =>
assertNonProductionReplayTarget(production, { ...baseOptions, force: true })
).not.toThrow();
expect(() =>
assertNonProductionReplayTarget(production, { ...baseOptions, force: false })
).toThrow();
});
});