- Schema 扩展: CorpConfigSchema 增加 env ('prod' | 'test' | 'demo') 与 isTest 字段,未知企业保守兜底为 prod
- 智能初判与归一化: inferCorpEnvFromName 依据名称初判环境,normalizeCorpConfig 确保配置持久化时环境对齐
- CLI 视图升级: corps list 与 search 增加「环境」列 (🏭 正式 / 🧪 测试 / 🎪 演示),支持 --test/--prod/--env 筛选
- 环境打标与迁移: 新增 hose-use corps tag 命令,支持单个企业标记与 --migrate-all 批量显式升级持久化
- 生产安全阻断: workflow test 等敏感调试在正式生产环境 (prod) 阻断执行,需显式附加 --force 确认生产风险
- 上下文与探针增强: context、probe、corps current 全面输出企业环境属性
- 测试与文档补全: 覆盖 489 项全量测试并通过 tsc 类型检查
117 lines
6.3 KiB
TypeScript
117 lines
6.3 KiB
TypeScript
import { afterEach, describe, expect, it, spyOn } from "bun:test";
|
|
import { handleWorkflowTest } from "./workflow/test.js";
|
|
import * as shared from "./workflow/shared.js";
|
|
import * as services from "../../domains/platform/services.js";
|
|
import type { CliParsedArgs, CliRuntime } from "../types.js";
|
|
import type { WorkflowTestReport } from "../../domains/platform/types.js";
|
|
|
|
const runtime = {
|
|
selectedCorp: { id: "mock-corp", name: "Mock测试企业", env: "test", isTest: true } as any,
|
|
jsonMode: true,
|
|
} as CliRuntime;
|
|
const args = (options: Record<string, string | boolean> = {}): CliParsedArgs => ({ area: "workflow", action: "test", args: ["pipe"], options: { flow: "ID01-record", ...options }, raw: ["workflow", "test", "pipe", "--flow=ID01-record"] });
|
|
const completed: WorkflowTestReport = { jobId: "job", pipelineId: "pipe", status: "COMPLETED", outcome: "completed", timedOut: false, diagnosticsComplete: true, diagnosticsErrors: [], runType: "DEBUG", targetEntityId: "record", executionTree: [], failedExecutions: [], conditionDiagnoses: [] };
|
|
|
|
describe("workflow debug mutation and result contract", () => {
|
|
const spies: Array<{ mockRestore(): void }> = [];
|
|
afterEach(() => { while (spies.length) spies.pop()!.mockRestore(); });
|
|
function setup(report = completed) {
|
|
const client = spyOn(shared, "createWorkflowDetailClient").mockResolvedValue({ client: {} } as any);
|
|
const detail = spyOn(services, "getWorkflowDetail").mockResolvedValue({ state: 1 } as any);
|
|
const run = spyOn(services, "runWorkflowDebugTest").mockResolvedValue(report);
|
|
spies.push(client, detail, run);
|
|
return { client, detail, run };
|
|
}
|
|
it("dry-run with auto-publish does not create clients or start a job even if --yes is also set", async () => {
|
|
const { client, run } = setup();
|
|
const result = await handleWorkflowTest(args({ "dry-run": true, "auto-publish": true, yes: true }), runtime);
|
|
const preview = JSON.parse(result.stdout);
|
|
expect(preview.dryRun).toBe(true);
|
|
expect(preview.changes.autoPublish).toBe(true);
|
|
expect(preview.changes.warnings).not.toHaveLength(0);
|
|
expect(client).not.toHaveBeenCalled();
|
|
expect(run).not.toHaveBeenCalled();
|
|
});
|
|
it("requires explicit mutation confirmation before creating clients", async () => {
|
|
const { client, run } = setup();
|
|
await expect(handleWorkflowTest(args(), runtime)).rejects.toThrow("请先使用 --dry-run");
|
|
expect(client).not.toHaveBeenCalled();
|
|
expect(run).not.toHaveBeenCalled();
|
|
});
|
|
for (const [triggerType, options] of [
|
|
["clock", {}],
|
|
["webhook", { body: '{"event":"test"}' }],
|
|
["webhook", { empty: true }],
|
|
["webhook", { file: "/unused-in-dry-run.json" }],
|
|
["ekuaibao-datalink-create-trigger", { record: "ID01-record" }],
|
|
] as Array<[string, Record<string, string | boolean>]>) {
|
|
it(`previews ${triggerType} without creating clients (${Object.keys(options).join(",")})`, async () => {
|
|
const { client, run } = setup();
|
|
const result = await handleWorkflowTest({ ...args(), options: { ...options, "dry-run": true, yes: true } }, runtime);
|
|
expect(JSON.parse(result.stdout).dryRun).toBe(true);
|
|
expect(client).not.toHaveBeenCalled();
|
|
expect(run).not.toHaveBeenCalled();
|
|
});
|
|
}
|
|
it("requires confirmation for a schedule trigger without --flow", async () => {
|
|
const { detail, run } = setup();
|
|
detail.mockResolvedValue({ state: 1, tasks: [{ type: "clock" }] } as any);
|
|
await expect(handleWorkflowTest({ ...args(), options: {} }, runtime)).rejects.toThrow("请先使用 --dry-run");
|
|
expect(run).not.toHaveBeenCalled();
|
|
});
|
|
it("keeps confirmed schedule and webhook trigger routing", async () => {
|
|
const { detail, run } = setup();
|
|
detail.mockResolvedValue({ state: 1, tasks: [{ type: "clock" }] } as any);
|
|
const scheduled = await handleWorkflowTest({ ...args(), options: { yes: true } }, runtime);
|
|
expect(scheduled.exitCode).toBe(0);
|
|
expect(run.mock.calls[0]?.[2]?.triggerCategory).toBe("SCHEDULE");
|
|
expect(run.mock.calls[0]?.[2]?.flowCodeOrId).toBe("");
|
|
detail.mockResolvedValue({ state: 1, tasks: [{ type: "webhook" }] } as any);
|
|
const webhook = await handleWorkflowTest({ ...args(), options: { body: '{"event":"test"}', yes: true } }, runtime);
|
|
expect(webhook.exitCode).toBe(0);
|
|
expect(run.mock.calls[1]?.[2]?.data).toEqual({ event: "test" });
|
|
expect(run.mock.calls[1]?.[2]?.triggerCategory).toBe("WEBHOOK");
|
|
});
|
|
it("allows confirmed enabled-workflow debug with machine-readable risk information", async () => {
|
|
const { run } = setup();
|
|
const result = await handleWorkflowTest(args({ yes: true }), runtime);
|
|
expect(result.exitCode).toBe(0);
|
|
const report = JSON.parse(result.stdout);
|
|
expect(report.productionEnabled).toBe(true);
|
|
expect(report.warnings).not.toHaveLength(0);
|
|
expect(run).toHaveBeenCalledTimes(1);
|
|
});
|
|
it("retains an unknown production state in the result when the status check fails", async () => {
|
|
const { detail } = setup();
|
|
detail.mockRejectedValue(new Error("status unavailable"));
|
|
const result = await handleWorkflowTest(args({ yes: true }), runtime);
|
|
const report = JSON.parse(result.stdout);
|
|
expect(report.productionEnabled).toBeNull();
|
|
expect(report.warnings.join(" ")).toContain("启用状态未知");
|
|
});
|
|
for (const jsonMode of [true, false]) {
|
|
it(`returns a failing exit code for failed and incomplete runs (json=${jsonMode})`, async () => {
|
|
setup({ ...completed, status: "FAILED", outcome: "failed" });
|
|
const result = await handleWorkflowTest(args({ yes: true }), { ...runtime, jsonMode });
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.stdout).toContain("FAILED");
|
|
});
|
|
}
|
|
|
|
it("blocks workflow test without --force when selected corp is production environment", async () => {
|
|
const { run } = setup();
|
|
const prodRuntime = {
|
|
selectedCorp: { id: "prod-corp", name: "生产正式企业", env: "prod", isTest: false },
|
|
jsonMode: false,
|
|
} as any;
|
|
const blocked = await handleWorkflowTest(args({ yes: true }), prodRuntime);
|
|
expect(blocked.exitCode).toBe(1);
|
|
expect(blocked.stderr).toContain("生产环境安全阻断");
|
|
expect(run).not.toHaveBeenCalled();
|
|
|
|
const allowed = await handleWorkflowTest(args({ yes: true, force: true }), prodRuntime);
|
|
expect(allowed.exitCode).toBe(0);
|
|
expect(run).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|