feat(workflow): implement declarative node editing commands

This commit is contained in:
gitea committed 2026-10-15 16:24:25 +08:00
1 parent 61123ad550
commit 3261b6ebdb
9 files changed
+1415 -6

No files matched your search

+223
View File
@@ -1460,4 +1460,227 @@ describe("CLI - workflow job 执行详情与异常精准透视", () => {
await (await import("fs/promises")).unlink(tmpFile).catch(() => {});
});
it("handles workflow node list, get, add, edit, and rm commands with dry-run and safety checks", async () => {
// 1. Missing pipelineId -> usage
const noArgRes = await handleWorkflowCommand(
{
area: "workflow",
action: "node",
args: [],
options: {},
raw: ["workflow", "node"],
},
mockRuntime
);
expect(noArgRes.exitCode).toBe(1);
expect(noArgRes.stderr).toContain("用法: hose-use workflow node");
let savedWorkflowPayload: any = null;
const testTasks = [
{
name: "trigger_webhook_1",
type: "webhook",
label: "发票数据接入",
groupName: "trigger",
orderIndex: 0,
levelIndex: [0],
status: "active",
urlPath: "test-uuid-1",
params: [],
},
{
name: "action_js_1",
type: "code-js2",
label: "数据清洗",
groupName: "action",
orderIndex: 1,
levelIndex: [1],
status: "active",
code: "return items.filter(x => x.amount > 0);",
},
];
const mockDetail = {
id: "pipe_node_test",
label: "节点编排测试流程",
state: 0,
tasks: testTasks,
};
const mockClient = {
get: (url: string) => {
if (url.includes("api/easo/pipelines/pipe_node_test")) {
return {
json: async () => ({
code: 200,
data: mockDetail,
}),
};
}
return { json: async () => ({ code: 200, data: null }) };
},
put: (url: string, opts: any) => {
if (url.includes("api/easo/pipelines/pipe_node_test")) {
savedWorkflowPayload = opts.json;
return {
json: async () => ({
code: 200,
data: {
...mockDetail,
...opts.json,
},
}),
};
}
return { json: async () => ({ code: 200, data: null }) };
},
};
spies.push(
spyOn(helpers, "createClientsForCorp").mockResolvedValue({
ekbClient: mockClient as any,
easoClient: mockClient as any,
corp: mockCorp,
} as any)
);
spies.push(
spyOn(helpers, "createPiperClientForCorp").mockResolvedValue(mockClient as any)
);
// 2. node list
const listRes = await handleWorkflowCommand(
{
area: "workflow",
action: "node",
args: ["list", "pipe_node_test"],
options: {},
raw: ["workflow", "node", "list", "pipe_node_test"],
},
mockRuntime
);
expect(listRes.exitCode).toBe(0);
expect(listRes.stdout).toContain("任务节点树 (共 2 个节点)");
expect(listRes.stdout).toContain("trigger_webhook_1");
expect(listRes.stdout).toContain("action_js_1");
// 2.1 node list in json mode
const listJsonRes = await handleWorkflowCommand(
{
area: "workflow",
action: "node",
args: ["ls", "pipe_node_test"],
options: {},
raw: ["workflow", "node", "ls", "pipe_node_test"],
},
{ ...mockRuntime, jsonMode: true }
);
expect(listJsonRes.exitCode).toBe(0);
const parsedList = JSON.parse(listJsonRes.stdout);
expect(parsedList.totalNodes).toBe(2);
// 3. node get
const getRes = await handleWorkflowCommand(
{
area: "workflow",
action: "node",
args: ["get", "pipe_node_test", "action_js_1"],
options: {},
raw: ["workflow", "node", "get", "pipe_node_test", "action_js_1"],
},
mockRuntime
);
expect(getRes.exitCode).toBe(0);
expect(getRes.stdout).toContain("节点详情 [action_js_1]");
expect(getRes.stdout).toContain("return items.filter");
// 4. node add (requires confirmation)
await expect(
handleWorkflowCommand(
{
area: "workflow",
action: "node",
args: ["add", "pipe_node_test"],
options: { type: "stop", label: "终止超额" },
raw: ["workflow", "node", "add", "pipe_node_test", "--type=stop"],
},
mockRuntime
)
).rejects.toThrow("请先使用 --dry-run");
// 4.1 node add dry-run
const addDryRunRes = await handleWorkflowCommand(
{
area: "workflow",
action: "node",
args: ["add", "pipe_node_test"],
options: { type: "stop", label: "终止超额", "dry-run": true },
raw: ["workflow", "node", "add", "pipe_node_test", "--type=stop", "--label=终止超额", "--dry-run"],
},
mockRuntime
);
expect(addDryRunRes.exitCode).toBe(0);
expect(addDryRunRes.stdout).toContain("[DRY-RUN]");
expect(addDryRunRes.stdout).toContain("终止超额");
expect(savedWorkflowPayload).toBeNull();
// 4.2 node add confirmed execution
const addRes = await handleWorkflowCommand(
{
area: "workflow",
action: "node",
args: ["add", "pipe_node_test"],
options: { type: "stop", label: "终止超额", yes: true },
raw: ["workflow", "node", "add", "pipe_node_test", "--type=stop", "--label=终止超额", "--yes"],
},
mockRuntime
);
expect(addRes.exitCode).toBe(0);
expect(addRes.stdout).toContain("已成功添加至工作流");
expect(addRes.stdout).toContain("终止超额");
expect(savedWorkflowPayload).not.toBeNull();
expect(savedWorkflowPayload.tasks).toHaveLength(3);
// 5. node edit
const editRes = await handleWorkflowCommand(
{
area: "workflow",
action: "node",
args: ["edit", "pipe_node_test", "action_js_1"],
options: { label: "数据清洗升级版", code: "return true;", yes: true },
raw: ["workflow", "node", "edit", "pipe_node_test", "action_js_1", "--label=数据清洗升级版", "--code=return true;", "--yes"],
},
mockRuntime
);
expect(editRes.exitCode).toBe(0);
expect(editRes.stdout).toContain("修改已保存至工作流");
// 6. node rm
const rmRes = await handleWorkflowCommand(
{
area: "workflow",
action: "node",
args: ["rm", "pipe_node_test", "action_js_1"],
options: { yes: true },
raw: ["workflow", "node", "rm", "pipe_node_test", "action_js_1", "--yes"],
},
mockRuntime
);
expect(rmRes.exitCode).toBe(0);
expect(rmRes.stdout).toContain("已从工作流 [pipe_node_test] 中安全移除");
// 6.1 node rm trigger guard
await expect(
handleWorkflowCommand(
{
area: "workflow",
action: "node",
args: ["rm", "pipe_node_test", "trigger_webhook_1"],
options: { yes: true },
raw: ["workflow", "node", "rm", "pipe_node_test", "trigger_webhook_1", "--yes"],
},
mockRuntime
)
).rejects.toThrow("触发源节点");
});
});