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).
361 lines
11 KiB
TypeScript
361 lines
11 KiB
TypeScript
import { afterEach, describe, expect, test } from "bun:test";
|
|
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import type { CorpConfig } from "../../core/config/types.js";
|
|
import { runCli } from "../runner.js";
|
|
|
|
const ORIGINAL_ENV = {
|
|
enterprises: process.env.SEAL_USE_ENTERPRISES_DIR,
|
|
config: process.env.SEAL_USE_CONFIG_FILE,
|
|
tokenStore: process.env.SEAL_USE_TOKEN_STORE
|
|
};
|
|
const ORIGINAL_FETCH = globalThis.fetch;
|
|
const tempDirs: string[] = [];
|
|
|
|
afterEach(() => {
|
|
restoreEnv("SEAL_USE_ENTERPRISES_DIR", ORIGINAL_ENV.enterprises);
|
|
restoreEnv("SEAL_USE_CONFIG_FILE", ORIGINAL_ENV.config);
|
|
restoreEnv("SEAL_USE_TOKEN_STORE", ORIGINAL_ENV.tokenStore);
|
|
globalThis.fetch = ORIGINAL_FETCH;
|
|
|
|
for (const dir of tempDirs.splice(0)) {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
function restoreEnv(name: string, value: string | undefined) {
|
|
if (value === undefined) delete process.env[name];
|
|
else process.env[name] = value;
|
|
}
|
|
|
|
const mockApprovalRuns = [
|
|
{
|
|
id: "run-001",
|
|
status: "completed",
|
|
sourceSystem: "hosecloud",
|
|
sourceDocumentSN: "BX9990001",
|
|
sourceDocumentId: "DOC-999",
|
|
createdAt: 1789115000000,
|
|
result: { decision: "APPROVE", summary: "合规通过" },
|
|
document: {
|
|
id: "DOC-999",
|
|
sn: "BX9990001",
|
|
fields: [
|
|
{ key: "cost_type", label: "费用类型", type: "TEXT", value: "打车" },
|
|
{ key: "amount", label: "金额", type: "AMOUNT", value: { amount: 120 } }
|
|
]
|
|
}
|
|
},
|
|
{
|
|
id: "run-002",
|
|
status: "completed",
|
|
sourceSystem: "hosecloud",
|
|
sourceDocumentSN: "BX9990002",
|
|
sourceDocumentId: "DOC-998",
|
|
createdAt: 1789115100000,
|
|
result: { decision: "REJECT", summary: "超标" },
|
|
document: {
|
|
id: "DOC-998",
|
|
sn: "BX9990002",
|
|
fields: [
|
|
{ key: "cost_type", label: "费用类型", type: "TEXT", value: "住宿" },
|
|
{ key: "amount", label: "金额", type: "AMOUNT", value: { amount: 800 } }
|
|
]
|
|
}
|
|
}
|
|
];
|
|
|
|
function setupTestEnv() {
|
|
const root = mkdtempSync(join(tmpdir(), "seal-use-test-cli-"));
|
|
const enterprises = join(root, "enterprises");
|
|
mkdirSync(enterprises, { recursive: true });
|
|
tempDirs.push(root);
|
|
|
|
process.env.SEAL_USE_ENTERPRISES_DIR = enterprises;
|
|
process.env.SEAL_USE_CONFIG_FILE = join(root, "config.json");
|
|
process.env.SEAL_USE_TOKEN_STORE = join(root, "tokens.json");
|
|
|
|
const prodCorp: CorpConfig = {
|
|
id: "corp-prod",
|
|
name: "生产企业",
|
|
environment: "production",
|
|
testCorpId: "corp-test",
|
|
seal: {
|
|
url: "https://acme-prod.sealai.cc",
|
|
endpoints: { approvalStylePreferences: "api/v1/agent/ai-approval/config" }
|
|
},
|
|
source: {
|
|
type: "seal",
|
|
token: "prod-token",
|
|
sealUrl: "https://acme-prod.sealai.cc",
|
|
expiresIn: 3600,
|
|
staffId: "seal"
|
|
},
|
|
auth: { refreshTtl: 300 }
|
|
};
|
|
|
|
const testCorp: CorpConfig = {
|
|
id: "corp-test",
|
|
name: "测试靶场企业",
|
|
environment: "test",
|
|
prodCorpId: "corp-prod",
|
|
seal: {
|
|
url: "https://acme-test.sealai.cc",
|
|
endpoints: { approvalStylePreferences: "api/v1/agent/ai-approval/config" }
|
|
},
|
|
source: {
|
|
type: "seal",
|
|
token: "test-token",
|
|
sealUrl: "https://acme-test.sealai.cc",
|
|
expiresIn: 3600,
|
|
staffId: "seal"
|
|
},
|
|
auth: { refreshTtl: 300 }
|
|
};
|
|
|
|
const unpairedCorp: CorpConfig = {
|
|
id: "corp-unpaired",
|
|
name: "未配对企业",
|
|
environment: "production",
|
|
seal: {
|
|
url: "https://acme-unpaired.sealai.cc",
|
|
endpoints: { approvalStylePreferences: "api/v1/agent/ai-approval/config" }
|
|
},
|
|
source: {
|
|
type: "seal",
|
|
token: "unpaired-token",
|
|
sealUrl: "https://acme-unpaired.sealai.cc",
|
|
expiresIn: 3600,
|
|
staffId: "seal"
|
|
},
|
|
auth: { refreshTtl: 300 }
|
|
};
|
|
|
|
writeFileSync(join(enterprises, "corp-prod.json"), JSON.stringify(prodCorp, null, 2));
|
|
writeFileSync(join(enterprises, "corp-test.json"), JSON.stringify(testCorp, null, 2));
|
|
writeFileSync(join(enterprises, "corp-unpaired.json"), JSON.stringify(unpairedCorp, null, 2));
|
|
|
|
writeFileSync(
|
|
join(root, "config.json"),
|
|
JSON.stringify({ defaultCorpId: "corp-prod", stateful: true }, null, 2)
|
|
);
|
|
|
|
return { root, enterprises, prodCorp, testCorp, unpairedCorp };
|
|
}
|
|
|
|
function mockApiFetch(options: { integrations?: unknown[] } = {}) {
|
|
globalThis.fetch = (async (input: RequestInfo | URL) => {
|
|
const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
|
|
|
|
if (url.includes("/api/v1/billing/status")) {
|
|
return new Response(
|
|
JSON.stringify({
|
|
data: {
|
|
features: [
|
|
{
|
|
code: "ai-approval",
|
|
balance: 5000,
|
|
deductionAmount: 100
|
|
}
|
|
]
|
|
}
|
|
}),
|
|
{ status: 200, headers: { "Content-Type": "application/json" } }
|
|
);
|
|
}
|
|
|
|
if (url.includes("/api/v1/approvals")) {
|
|
return new Response(
|
|
JSON.stringify({
|
|
data: {
|
|
records: mockApprovalRuns,
|
|
total: mockApprovalRuns.length
|
|
}
|
|
}),
|
|
{ status: 200, headers: { "Content-Type": "application/json" } }
|
|
);
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
describe("seal-use test regression CLI", () => {
|
|
test("fails with exit code 2 when unknown subaction is requested", async () => {
|
|
setupTestEnv();
|
|
const result = await runCli(["test", "invalid-action"]);
|
|
expect(result.exitCode).toBe(2);
|
|
expect(result.stderr).toContain("未知的 test 子命令");
|
|
});
|
|
|
|
test("rejects when production enterprise has no bound test enterprise", async () => {
|
|
setupTestEnv();
|
|
const result = await runCli(["test", "regression", "--corp", "corp-unpaired"]);
|
|
expect(result.exitCode).toBe(2);
|
|
expect(result.stderr).toContain("未绑定测试靶场企业");
|
|
expect(result.stderr).toContain("硬性规定:回归测试严禁在生产环境直接运行");
|
|
});
|
|
|
|
test("executes dry-run regression preview on paired test enterprise", async () => {
|
|
setupTestEnv();
|
|
mockApiFetch();
|
|
|
|
const result = await runCli(["test", "regression", "--corp", "corp-test", "--samples", "2", "--dry-run"]);
|
|
expect(result.exitCode).toBe(0);
|
|
|
|
const payload = JSON.parse(result.stdout);
|
|
expect(payload.ok).toBe(true);
|
|
expect(payload.verdict).toBe("DRY_RUN");
|
|
expect(payload.safeToPublish).toBe(true);
|
|
expect(payload.metrics.sampleCount).toBe(2);
|
|
expect(payload.sourceCorp.id).toBe("corp-prod");
|
|
expect(payload.targetCorp.id).toBe("corp-test");
|
|
expect(payload.records.length).toBe(2);
|
|
|
|
expect(result.stderr).toContain("规则回归测试矩阵");
|
|
expect(result.stderr).toContain("DRY-RUN");
|
|
});
|
|
|
|
test("corps regression alias routes to test regression command", async () => {
|
|
setupTestEnv();
|
|
mockApiFetch();
|
|
|
|
const result = await runCli(["corps", "regression", "--corp", "corp-test", "--samples", "2", "--dry-run"]);
|
|
expect(result.exitCode).toBe(0);
|
|
|
|
const payload = JSON.parse(result.stdout);
|
|
expect(payload.verdict).toBe("DRY_RUN");
|
|
expect(payload.targetCorp.id).toBe("corp-test");
|
|
});
|
|
|
|
test("test regression accepts --webhook option and succeeds in dry-run", async () => {
|
|
setupTestEnv();
|
|
mockApiFetch();
|
|
|
|
const result = await runCli([
|
|
"test",
|
|
"regression",
|
|
"--corp",
|
|
"corp-test",
|
|
"--samples",
|
|
"2",
|
|
"--dry-run",
|
|
"--webhook",
|
|
"自定义通道"
|
|
]);
|
|
expect(result.exitCode).toBe(0);
|
|
|
|
const payload = JSON.parse(result.stdout);
|
|
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);
|
|
});
|
|
});
|