import { describe, expect, it, spyOn, afterEach } from "bun:test"; import { handleWorkflowCommand } from "./workflow/index.js"; import * as services from "../../domains/platform/workflow/services.js"; import * as api from "../../domains/platform/workflow/api.js"; import * as helpers from "../helpers.js"; import type { CliRuntime } from "../types.js"; import type { CorpConfig } from "../../core/config/types.js"; const mockCorp: CorpConfig = { id: "ID01rJEL708iTI", name: "浙江麦瑞克科技有限公司", baseUrl: "https://app.ekuaibao.com", source: "openapi", env: "test", credentials: { accessToken: "mock-access-token", corpId: "ID01rJEL708iTI", staffId: "ID01rJEL708iTI:RkQ8G8L_eMhI00", appKey: "test-key", appSecurity: "test-security", }, }; const mockRuntime: CliRuntime = { corpContext: { listCorps: () => [mockCorp], getCurrent: () => mockCorp, switchCorp: () => true, resolve: () => mockCorp, } as any, selectedCorp: mockCorp, jsonMode: false, outputOptions: {}, }; describe("CLI - workflow nodes 字典探针与灰度开通测试", () => { let spies: ReturnType[] = []; afterEach(() => { for (const spy of spies) { spy.mockRestore(); } spies = []; delete process.env.HOSE_ADMIN_PASSWORD; }); it("workflow nodes: 成功探查当前企业已开通节点字典", async () => { spies.push( spyOn(helpers, "createPiperClientForCorp").mockResolvedValue({} as any), spyOn(services, "getWorkflowAvailableNodes").mockResolvedValue({ allTypes: new Set(["http", "code", "condition", "clock"]), actionNodes: [ { name: "http节点", type: "http" }, { name: "代码节点", type: "code" }, ], controlNodes: [{ name: "条件节点", type: "condition" }], triggerNodes: [{ name: "定时触发", type: "clock" }], }) ); const res = await handleWorkflowCommand( { area: "workflow", action: "nodes", args: [], options: {}, raw: ["workflow", "nodes"], }, mockRuntime ); expect(res.exitCode).toBe(0); expect(res.stdout).toContain("当前企业已开通工作流节点字典 [浙江麦瑞克科技有限公司]"); expect(res.stdout).toContain("代码节点 (code)"); expect(res.stdout).toContain("已开通节点数"); }); it("workflow nodes --all: 成功输出与 108 个灰度节点对照表", async () => { spies.push( spyOn(helpers, "createPiperClientForCorp").mockResolvedValue({} as any), spyOn(services, "getWorkflowAvailableNodes").mockResolvedValue({ allTypes: new Set(["code", "http"]), actionNodes: [{ name: "代码节点", type: "code" }], controlNodes: [], triggerNodes: [], }) ); const res = await handleWorkflowCommand( { area: "workflow", action: "nodes", args: [], options: { all: true }, raw: ["workflow", "nodes", "--all"], }, mockRuntime ); expect(res.exitCode).toBe(0); expect(res.stdout).toContain("工作流节点字典与灰度开通对照 [浙江麦瑞克科技有限公司]"); expect(res.stdout).toContain("code-js2"); expect(res.stdout).toContain("🔒 未开通"); expect(res.stdout).toContain("✅ 已开通"); }); it("workflow nodes --json: 输出机器可读的结构化 JSON", async () => { spies.push( spyOn(helpers, "createPiperClientForCorp").mockResolvedValue({} as any), spyOn(services, "getWorkflowAvailableNodes").mockResolvedValue({ allTypes: new Set(["code"]), actionNodes: [{ name: "代码节点", type: "code" }], controlNodes: [], triggerNodes: [], }) ); const jsonRuntime: CliRuntime = { ...mockRuntime, jsonMode: true }; const res = await handleWorkflowCommand( { area: "workflow", action: "nodes", args: [], options: {}, raw: ["workflow", "nodes", "--json"], }, jsonRuntime ); expect(res.exitCode).toBe(0); const parsedJson = JSON.parse(res.stdout); expect(parsedJson.corpId).toBe("ID01rJEL708iTI"); expect(parsedJson.totalGranted).toBe(1); expect(parsedJson.grantedTypes).toContain("code"); }); it("workflow nodes grant: 免密模式下成功发起灰度节点开通", async () => { const grantSpy = spyOn(api, "grantWorkflowNodesApi").mockResolvedValue({ code: 200, msg: "新开通的节点:code-js2", }); spies.push(grantSpy); const res = await handleWorkflowCommand( { area: "workflow", action: "nodes", args: ["grant", "code-js2"], options: { yes: true }, raw: ["workflow", "nodes", "grant", "code-js2"], }, mockRuntime ); expect(res.exitCode).toBe(0); expect(res.stdout).toContain("灰度节点开通结果 [浙江麦瑞克科技有限公司]"); expect(res.stdout).toContain("新开通成功"); expect(res.stdout).toContain("code-js2"); expect(grantSpy).toHaveBeenCalledWith( expect.objectContaining({ payload: expect.objectContaining({ dslTypes: ["code-js2"] }), token: "mock-access-token", corpId: "ID01rJEL708iTI", }) ); }); it("workflow nodes grant: 节点名称自动中文归一化 (如 'JS节点(新)' 映射为 'code-js2')", async () => { const grantSpy = spyOn(api, "grantWorkflowNodesApi").mockResolvedValue({ code: 200, msg: "新开通的节点:code-js2", }); spies.push(grantSpy); const res = await handleWorkflowCommand( { area: "workflow", action: "nodes", args: ["grant", "JS节点(新)"], options: { yes: true }, raw: ["workflow", "nodes", "grant", "JS节点(新)"], }, mockRuntime ); expect(res.exitCode).toBe(0); expect(grantSpy).toHaveBeenCalledWith( expect.objectContaining({ payload: expect.objectContaining({ dslTypes: ["code-js2"] }), }) ); }); it("workflow nodes grant: 可选管理员密码保护生效 (未提供密码或密码错误被拒绝)", async () => { process.env.HOSE_ADMIN_PASSWORD = "secret_admin_pass"; // 1. 无密码调用被拒绝 expect( services.grantWorkflowNodes(mockCorp, { nodes: ["code-js2"], }) ).rejects.toThrow("管理员保护"); // 2. 错误密码调用被拒绝 expect( services.grantWorkflowNodes(mockCorp, { nodes: ["code-js2"], password: "wrong_password", }) ).rejects.toThrow("管理员保护"); // 3. 正确密码调用放行 const grantSpy = spyOn(api, "grantWorkflowNodesApi").mockResolvedValue({ code: 200, msg: "开通成功", }); spies.push(grantSpy); const result = await services.grantWorkflowNodes(mockCorp, { nodes: ["code-js2"], password: "secret_admin_pass", }); expect(result.success).toBe(true); expect(grantSpy).toHaveBeenCalled(); }); it("workflow analyze: 显式输出硬门禁与软建议边界,且支持 --fail 退出码门禁", async () => { spies.push( spyOn(helpers, "createClientsForCorp").mockResolvedValue({ ekbClient: {}, easoClient: {}, } as any), spyOn(helpers, "createPiperClientForCorp").mockResolvedValue({} as any), spyOn(services, "getWorkflowDetail").mockResolvedValue({ id: "wf-gate-test", label: "门禁测试流程", state: 0, version: "1", tasks: [ { name: "bad_node", type: "code-js2", label: "未开通JS2节点", code: "return 1;", }, ], } as any), spyOn(services, "getWorkflowAvailableNodes").mockResolvedValue({ allTypes: new Set(["http", "code"]), catalog: { action: [], control: [], trigger: [] }, } as any) ); // 1. 常规运行(无 --fail):展示门禁裁决,exitCode 为 0 const normalRes = await handleWorkflowCommand( { action: "analyze", args: ["wf-gate-test"], options: {}, raw: ["workflow", "analyze", "wf-gate-test"], }, mockRuntime ); expect(normalRes.exitCode).toBe(0); expect(normalRes.stdout).toContain("门禁裁决"); expect(normalRes.stdout).toContain("硬门禁数"); expect(normalRes.stdout).toContain("软建议数"); expect(normalRes.stdout).toContain("⛔ 硬门禁"); expect(normalRes.stdout).toContain("💡 软建议"); expect(normalRes.stdout).toContain("PERMISSION_MISSING_NODE"); // 2. 带 --fail 运行:检测到硬门禁,exitCode 为 2 const failRes = await handleWorkflowCommand( { action: "analyze", args: ["wf-gate-test"], options: { fail: true }, raw: ["workflow", "analyze", "wf-gate-test", "--fail"], }, mockRuntime ); expect(failRes.exitCode).toBe(2); }); });