import { describe, it, expect, beforeEach, afterEach, spyOn } from "bun:test"; import { handleWorkflowCommand } from "./workflow/index.js"; import * as helpers from "./workflow/shared.js"; import { CorpContext } from "../../core/config/manager.js"; import type { CorpConfig } from "../../core/config/types.js"; import type { CliRuntime } from "../types.js"; const mockCorp: CorpConfig = { id: "ID01ofWoOC1wiX", name: "沈玥妍测试企业", baseUrl: "https://app.ekuaibao.com", cluster: "app", source: "link", credentials: { accessToken: "test-token", corpId: "ID01ofWoOC1wiX", staffId: "ID01ofWoOC1wiX:ID01KsDgHaH5nN", }, env: "test", isTest: true, }; describe("CLI - workflow node inspect 与 sample-save (只读探针与样本一键固化)", () => { let spies: any[] = []; let mockRuntime: CliRuntime; beforeEach(() => { spies = []; const corpContext = new CorpContext([mockCorp], { stateful: true, defaultCorpId: mockCorp.id }); mockRuntime = { corpContext, selectedCorp: mockCorp, jsonMode: false, outputOptions: {}, }; }); afterEach(() => { for (const spy of spies) { spy.mockRestore(); } spies = []; }); it("workflow node inspect: 成功透视节点配置、已存样本与历史调试出参", async () => { const mockTasks = [ { name: "trigger_1", type: "ekuaibao-ebot-trigger", label: "EBOT触发", orderIndex: 0, levelIndex: [0], status: "active", }, { name: "action_calc_1", type: "code", label: "代码节点", displayName: "时间转换节点", orderIndex: 1, levelIndex: [1], status: "active", code: "result = ['minute': '2026-11-19 20:59'];\nreturn result;", sampleData: { minute: "2026-11-19 20:59", }, }, ]; const mockDetail = { id: "wf_inspect_test", label: "测试工作流", state: 0, tasks: mockTasks, }; const mockExecutionTree = [ { id: "exec_1", name: "action_calc_1", status: "COMPLETED", executionTime: 8, output: { minute: "2026-11-19 20:59", timestampMs: 1795093140000, }, }, ]; const mockClient = { get: (url: string) => { if (url.includes("api/easo/pipelines/wf_inspect_test/log")) { return { text: async () => JSON.stringify({ code: 200 }), json: async () => ({ code: 200, data: { totalCount: 1, items: [{ id: "job_test_100", status: "COMPLETED" }], }, }), }; } if (url.includes("api/easo/pipelines/wf_inspect_test")) { return { text: async () => JSON.stringify({ code: 200 }), json: async () => ({ code: 200, data: mockDetail }), }; } if (url.includes("api/easo/jobs/job_test_100/execution")) { return { text: async () => JSON.stringify({ code: 200 }), json: async () => ({ code: 200, data: mockExecutionTree }), }; } return { text: async () => JSON.stringify({ code: 200 }), json: async () => ({ code: 200, data: null }), }; }, }; spies.push( spyOn(helpers, "createWorkflowDetailClient").mockResolvedValue({ client: mockClient as any, clients: {} as any, platformId: undefined, }) ); // 1. 终端文本透视 const resText = await handleWorkflowCommand( { area: "workflow", action: "node", args: ["inspect", "wf_inspect_test", "action_calc_1"], options: {}, raw: ["workflow", "node", "inspect", "wf_inspect_test", "action_calc_1"], }, mockRuntime ); expect(resText.exitCode).toBe(0); expect(resText.stdout).toContain("工作流节点只读探针"); expect(resText.stdout).toContain("action_calc_1"); expect(resText.stdout).toContain("时间转换节点"); expect(resText.stdout).toContain("已固化样本"); expect(resText.stdout).toContain("最近一次调试执行出参"); expect(resText.stdout).toContain("job_test_100"); expect(resText.stdout).toContain("1795093140000"); // 2. JSON 模式结构化输出 mockRuntime.jsonMode = true; const resJson = await handleWorkflowCommand( { area: "workflow", action: "node", args: ["inspect", "wf_inspect_test", "action_calc_1"], options: {}, raw: ["workflow", "node", "inspect", "wf_inspect_test", "action_calc_1", "--json"], }, mockRuntime ); expect(resJson.exitCode).toBe(0); const parsedJson = JSON.parse(resJson.stdout); expect(parsedJson.sampleData.minute).toBe("2026-11-19 20:59"); expect(parsedJson.executionProbe.jobId).toBe("job_test_100"); expect(parsedJson.executionProbe.output.timestampMs).toBe(1795093140000); }); it("workflow node sample-save: 支持 dry-run 预览并一键固化出参至节点 sampleData", async () => { let savedTasks: any = null; let choosesRefreshed = false; const mockTasks = [ { name: "action_target_node", type: "code", label: "代码节点", orderIndex: 0, levelIndex: [0], status: "active", }, ]; const mockDetail = { id: "wf_save_sample_test", label: "样本固化测试流", version: "1.0.0", tasksVersion: 1, state: 0, tasks: mockTasks, }; const mockExecutionTree = [ { id: "exec_node_target", name: "action_target_node", status: "COMPLETED", output: { formattedDate: "2026-11-24 11:30", rangeSec: "2026-11-24 11:30:00 ~ 2026-11-24 12:00:00", }, }, ]; const mockClient = { get: (url: string) => { if (url.includes("api/easo/pipelines/wf_save_sample_test")) { return { json: async () => ({ code: 200, data: mockDetail }) }; } if (url.includes("api/easo/jobs/job_custom_999/execution")) { return { json: async () => ({ code: 200, data: mockExecutionTree }) }; } return { json: async () => ({ code: 200, data: null }) }; }, put: (url: string, opts: any) => { if (url.endsWith("/chooses")) { choosesRefreshed = true; return { text: async () => JSON.stringify({ code: 200, data: [] }), json: async () => ({ code: 200, data: [] }), }; } if (url.includes("api/easo/pipelines/wf_save_sample_test")) { savedTasks = opts.json.tasks; return { text: async () => JSON.stringify({ code: 200, data: { ...mockDetail, ...opts.json, tasksVersion: 2, }, }), json: async () => ({ code: 200, data: { ...mockDetail, ...opts.json, tasksVersion: 2, }, }), }; } return { text: async () => JSON.stringify({ code: 200, data: null }), json: async () => ({ code: 200, data: null }), }; }, }; spies.push( spyOn(helpers, "createWorkflowDetailClient").mockResolvedValue({ client: mockClient as any, clients: {} as any, platformId: undefined, }) ); // 1. Dry-run 预览 const dryRes = await handleWorkflowCommand( { area: "workflow", action: "node", args: ["sample-save", "wf_save_sample_test", "action_target_node"], options: { dryRun: true, job: "job_custom_999" }, raw: ["workflow", "node", "sample-save", "wf_save_sample_test", "action_target_node", "--job=job_custom_999", "--dry-run"], }, mockRuntime ); expect(dryRes.exitCode).toBe(0); expect(dryRes.stdout).toContain("[DRY-RUN]"); expect(dryRes.stdout).toContain("固化节点样本数据 (sampleData)"); expect(savedTasks).toBeNull(); // 2. 确认执行 const execRes = await handleWorkflowCommand( { area: "workflow", action: "node", args: ["sample-save", "wf_save_sample_test", "action_target_node"], options: { yes: true, job: "job_custom_999" }, raw: ["workflow", "node", "sample-save", "wf_save_sample_test", "action_target_node", "--job=job_custom_999", "--yes"], }, mockRuntime ); expect(execRes.exitCode).toBe(0); expect(execRes.stdout).toContain("样本数据 (sampleData) 固化成功"); expect(execRes.stdout).toContain("固化字段数 : 2 个属性"); expect(savedTasks).not.toBeNull(); expect(savedTasks[0].sampleData.formattedDate).toBe("2026-11-24 11:30"); expect(choosesRefreshed).toBe(true); }); it("workflow test --save-sample: 调试通过后自动固化执行出参为节点 sampleData", async () => { let savedTasks: any = null; let choosesRefreshed = false; const mockTasks = [ { name: "trigger_1", type: "ekuaibao-flowform-trigger", label: "单据触发", orderIndex: 0, levelIndex: [0], status: "active", }, { name: "action_code_date", type: "code", label: "代码节点", orderIndex: 1, levelIndex: [1], status: "active", code: "return ['minute': '2026-11-24 11:35'];", }, ]; const mockDetail = { id: "wf_test_save_sample", label: "自动保存样本流程", version: "1.0.0", tasksVersion: 1, state: 0, tasks: mockTasks, }; const mockClient = { post: (url: string) => { if (url.includes("debugSource")) { return { json: async () => ({ code: 200, data: { type: "flowForm", total: 1, items: [{ id: "S26000001", title: "测试单据", flowState: "审批中" }], }, }), }; } if (url.includes("api/easo/jobs")) { return { json: async () => ({ code: 200, data: "job_auto_sample_123", }), }; } return { json: async () => ({ code: 200, data: null }) }; }, get: (url: string) => { if (url.includes("/level/failed")) return { json: async () => ({ code: 200, data: [] }) }; if (url.includes("execution")) { return { json: async () => ({ code: 200, data: [ { id: "e1", name: "trigger_1", label: "单据触发", type: "ekuaibao-flowform-trigger", status: "COMPLETED", executionTime: 10, }, { id: "e2", name: "action_code_date", label: "代码节点", type: "code", status: "COMPLETED", executionTime: 5, output: { minute: "2026-11-24 11:35", timestampMs: 1795093140000, }, }, ], }), }; } if (url.includes("api/easo/jobs/job_auto_sample_123")) { return { json: async () => ({ code: 200, data: { id: "job_auto_sample_123", pipelineId: "wf_test_save_sample", status: "COMPLETED", startTime: 1795490000000, endTime: 1795490001000, executionTime: 1000, }, }), }; } if (url.includes("api/easo/pipelines/wf_test_save_sample")) { return { json: async () => ({ code: 200, data: mockDetail, }), }; } return { json: async () => ({ code: 200, data: null }) }; }, put: (url: string, opts: any) => { if (url.endsWith("/chooses")) { choosesRefreshed = true; return { text: async () => JSON.stringify({ code: 200, data: [] }), json: async () => ({ code: 200, data: [] }), }; } if (url.includes("api/easo/pipelines/wf_test_save_sample")) { savedTasks = opts.json.tasks; return { text: async () => JSON.stringify({ code: 200, data: { ...mockDetail, ...opts.json, tasksVersion: 2, }, }), json: async () => ({ code: 200, data: { ...mockDetail, ...opts.json, tasksVersion: 2, }, }), }; } return { text: async () => JSON.stringify({ code: 200, data: null }), json: async () => ({ code: 200, data: null }), }; }, }; spies.push( spyOn(helpers, "createWorkflowDetailClient").mockResolvedValue({ client: mockClient as any, clients: {} as any, platformId: undefined, }) ); const testRes = await handleWorkflowCommand( { area: "workflow", action: "test", args: ["wf_test_save_sample"], options: { flow: "S26000001", saveSample: true, yes: true }, raw: ["workflow", "test", "wf_test_save_sample", "--flow=S26000001", "--save-sample", "--yes"], }, mockRuntime ); expect(testRes.exitCode).toBe(0); expect(testRes.stdout).toContain("工作流自动化调测报告"); expect(testRes.stdout).toContain("样本数据自动固化"); expect(testRes.stdout).toContain("action_code_date"); expect(savedTasks).not.toBeNull(); expect(savedTasks[1].sampleData.minute).toBe("2026-11-24 11:35"); expect(savedTasks[1].sampleData.timestampMs).toBe(1795093140000); expect(choosesRefreshed).toBe(true); }); });