248 lines
8.2 KiB
TypeScript
248 lines
8.2 KiB
TypeScript
import { afterEach, describe, expect, test } from "bun:test";
|
|
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
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;
|
|
}
|
|
}
|
|
|
|
function setupMockEnv() {
|
|
const root = mkdtempSync(join(tmpdir(), "seal-use-replay-cli-"));
|
|
const enterprises = join(root, "enterprises");
|
|
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");
|
|
|
|
// Create enterprises dir
|
|
const fs = require("node:fs");
|
|
fs.mkdirSync(enterprises, { recursive: true });
|
|
|
|
// Write test enterprise config
|
|
const corpConfig = {
|
|
id: "test-corp-replay",
|
|
name: "测试企业重放",
|
|
sourceType: "seal",
|
|
seal: {
|
|
url: "https://test-replay.sealai.cc",
|
|
tenantSlug: "test-replay",
|
|
endpoints: {
|
|
approvalStylePreferences: "api/v1/system/preferences/approval-style",
|
|
sourceStatus: "api/v1/source/status",
|
|
token: "api/v1/auth/token"
|
|
}
|
|
},
|
|
source: {
|
|
type: "seal",
|
|
token: "mock-token-xyz",
|
|
sealUrl: "https://test-replay.sealai.cc"
|
|
}
|
|
};
|
|
writeFileSync(join(enterprises, "test-corp-replay.json"), JSON.stringify(corpConfig, null, 2));
|
|
|
|
// Write global config
|
|
writeFileSync(
|
|
join(root, "config.json"),
|
|
JSON.stringify({ defaultCorpId: "test-corp-replay", stateful: true }, null, 2)
|
|
);
|
|
|
|
return enterprises;
|
|
}
|
|
|
|
const mockApprovalRun = {
|
|
id: "run-001",
|
|
status: "completed",
|
|
sourceSystem: "hosecloud",
|
|
sourceDocumentSN: "BX9990001",
|
|
sourceDocumentId: "DOC-999",
|
|
createdAt: 1789115000000,
|
|
document: {
|
|
id: "DOC-999",
|
|
sn: "BX9990001",
|
|
fields: [
|
|
{ key: "cost_type", label: "费用类型", type: "TEXT", value: "打车" },
|
|
{ key: "amount", label: "金额", type: "AMOUNT", value: { amount: 120 } },
|
|
{ key: "ref_doc", label: "关联申请", type: "REFERENCE", value: "SQ001" }
|
|
]
|
|
}
|
|
};
|
|
|
|
const mockWebhookIntegrations = [
|
|
{
|
|
type: "webhook",
|
|
id: "wh-integration-1",
|
|
name: "默认重放通道",
|
|
mode: "assisted",
|
|
incomingDocumentWebhook: {
|
|
id: "wh_endpoint_1",
|
|
url: "https://test-replay.sealai.cc/api/v1/integrations/webhook/wh_endpoint_1/document"
|
|
},
|
|
outgoingResultWebhook: "https://example.com/result",
|
|
incomingManualResultWebhook: {
|
|
id: "wh_endpoint_1",
|
|
url: "https://test-replay.sealai.cc/api/v1/integrations/webhook/wh_endpoint_1/manual-result"
|
|
},
|
|
secret: "mock-secret-456"
|
|
}
|
|
];
|
|
|
|
describe("seal-use webhook replay CLI", () => {
|
|
test("fails with usage error if neither recordId nor sn is provided", async () => {
|
|
setupMockEnv();
|
|
const result = await runCli(["webhook", "replay"]);
|
|
expect(result.isError).toBe(true);
|
|
expect(result.stderr).toContain("Usage: seal-use webhook replay");
|
|
});
|
|
|
|
test("executes dry-run preview and strips unallowed types like REFERENCE", 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/run-001")) {
|
|
return new Response(JSON.stringify({ data: 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",
|
|
"run-001",
|
|
"--dry-run",
|
|
"--sn-suffix",
|
|
"_replay"
|
|
]);
|
|
|
|
expect(result.isError).toBe(false);
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stderr).toContain("预览模式");
|
|
|
|
const parsed = JSON.parse(result.stdout);
|
|
expect(parsed.dryRun).toBe(true);
|
|
expect(parsed.originalRecord.sourceDocumentSN).toBe("BX9990001");
|
|
expect(parsed.document.documentSN).toBe("BX9990001_replay");
|
|
expect(parsed.document.documentId).toBe("DOC-999_replay");
|
|
// REFERENCE must be normalized to TEXT
|
|
const refField = parsed.document.fields.find((f: any) => f.key === "ref_doc");
|
|
expect(refField.type).toBe("TEXT");
|
|
});
|
|
|
|
test("executes live webhook replay with successful response", async () => {
|
|
setupMockEnv();
|
|
let pushedBody: any = null;
|
|
|
|
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
const requestUrl =
|
|
typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
|
|
|
|
if (requestUrl.includes("/api/v1/approvals/run-001")) {
|
|
return new Response(JSON.stringify({ data: 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" } }
|
|
);
|
|
}
|
|
if (requestUrl.includes("/integrations/webhook/wh_endpoint_1/document")) {
|
|
let rawBody = init?.body;
|
|
if (typeof rawBody !== "string" && typeof input === "object" && "text" in input) {
|
|
rawBody = await (input as Request).clone().text();
|
|
}
|
|
pushedBody = JSON.parse(String(rawBody));
|
|
return new Response(
|
|
JSON.stringify({ data: { success: true, documentId: "spawned-run-doc-123" } }),
|
|
{ status: 200, headers: { "Content-Type": "application/json" } }
|
|
);
|
|
}
|
|
return new Response("Not found", { status: 404 });
|
|
}) as any;
|
|
|
|
const result = await runCli(["webhook", "replay", "run-001"]);
|
|
|
|
expect(result.isError).toBe(false);
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stderr).toContain("成功重放单据");
|
|
|
|
const parsed = JSON.parse(result.stdout);
|
|
expect(parsed.success).toBe(true);
|
|
expect(parsed.pushResult.data.success).toBe(true);
|
|
expect(pushedBody.documentSN).toBe("BX9990001");
|
|
});
|
|
|
|
test("approval-runs replay routes to webhook replay", 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/run-001")) {
|
|
return new Response(JSON.stringify({ data: 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(["approval-runs", "replay", "run-001", "--dry-run"]);
|
|
expect(result.isError).toBe(false);
|
|
expect(result.exitCode).toBe(0);
|
|
const parsed = JSON.parse(result.stdout);
|
|
expect(parsed.dryRun).toBe(true);
|
|
});
|
|
});
|