feat(workflow): implement workflow copy and delete lifecycle commands

This commit is contained in:
gitea committed 2026-10-15 15:18:49 +08:00
1 parent fec25bff8a
commit b043cb614e
9 files changed
+521 -2

No files matched your search

+262
View File
@@ -1035,4 +1035,266 @@ describe("CLI - workflow job 执行详情与异常精准透视", () => {
expect(res.stdout).toContain("张三");
expect(res.stdout).toContain("--record=");
});
it("handles workflow copy: requires pipelineId, confirmation, dry-run, execution, and json mode", async () => {
// 1. Missing pipelineId
const noArgRes = await handleWorkflowCommand(
{
area: "workflow",
action: "copy",
args: [],
options: {},
raw: ["workflow", "copy"],
},
mockRuntime
);
expect(noArgRes.exitCode).toBe(1);
expect(noArgRes.stderr).toContain("用法: hose-use workflow copy");
// 2. Missing mutation confirmation
await expect(
handleWorkflowCommand(
{
area: "workflow",
action: "copy",
args: ["pipe_orig"],
options: {},
raw: ["workflow", "copy", "pipe_orig"],
},
mockRuntime
)
).rejects.toThrow("请先使用 --dry-run");
// 3. Dry-run preview
let copiedPayload: any = null;
const mockClient = {
get: (url: string) => {
if (url.includes("api/easo/pipelines/pipe_orig")) {
return {
json: async () => ({
code: 200,
data: {
id: "pipe_orig",
label: "风险发票实时监控",
platformId: "app_orig",
state: 1,
tasks: [
{
id: "t1",
name: "webhook_1",
label: "发票数据接入",
type: "webhook",
urlPath: "old-path-uuid",
sampleId: "old-sample-id",
},
],
},
}),
};
}
return { json: async () => ({ code: 200, data: null }) };
},
put: (url: string, opts: any) => {
if (url.includes("api/easo/pipelines/pipe_orig/copy")) {
copiedPayload = opts.json;
return {
json: async () => ({
code: 200,
data: {
id: "pipe_cloned_888",
label: opts.json.label,
platformId: opts.json.platformId || "app_orig",
state: 0,
version: "26.10.1",
tasks: opts.json.tasks,
},
}),
};
}
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)
);
const dryRunRes = await handleWorkflowCommand(
{
area: "workflow",
action: "copy",
args: ["pipe_orig"],
options: { "dry-run": true },
raw: ["workflow", "copy", "pipe_orig", "--dry-run"],
},
mockRuntime
);
expect(dryRunRes.exitCode).toBe(0);
expect(dryRunRes.stdout).toContain("[DRY-RUN]");
expect(dryRunRes.stdout).toContain("pipe_orig");
expect(dryRunRes.stdout).toContain("风险发票实时监控 副本");
expect(copiedPayload).toBeNull();
// 4. Confirmed execution with custom name and platform
const copyRes = await handleWorkflowCommand(
{
area: "workflow",
action: "copy",
args: ["pipe_orig"],
options: { name: "发票监控克隆版", "platform-id": "app_custom", yes: true },
raw: ["workflow", "copy", "pipe_orig", "--name=发票监控克隆版", "--platform-id=app_custom", "--yes"],
},
mockRuntime
);
expect(copyRes.exitCode).toBe(0);
expect(copyRes.stdout).toContain("工作流复制成功");
expect(copyRes.stdout).toContain("pipe_cloned_888");
expect(copyRes.stdout).toContain("发票监控克隆版");
expect(copyRes.stdout).toContain("草稿未发布");
// Verify webhook payload transformation
expect(copiedPayload).not.toBeNull();
expect(copiedPayload.label).toBe("发票监控克隆版");
expect(copiedPayload.platformId).toBe("app_custom");
expect(copiedPayload.tasks[0].urlPath).not.toBe("old-path-uuid");
expect(copiedPayload.tasks[0].sampleId).toBeUndefined();
// 5. JSON mode via alias "cp"
const jsonRes = await handleWorkflowCommand(
{
area: "workflow",
action: "cp",
args: ["pipe_orig"],
options: { yes: true },
raw: ["workflow", "cp", "pipe_orig", "--yes"],
},
{ ...mockRuntime, jsonMode: true }
);
expect(jsonRes.exitCode).toBe(0);
const parsedJson = JSON.parse(jsonRes.stdout);
expect(parsedJson.id).toBe("pipe_cloned_888");
expect(parsedJson.state).toBe(0);
});
it("handles workflow delete: requires pipelineId, confirmation, dry-run, execution, and json mode", async () => {
// 1. Missing pipelineId
const noArgRes = await handleWorkflowCommand(
{
area: "workflow",
action: "delete",
args: [],
options: {},
raw: ["workflow", "delete"],
},
mockRuntime
);
expect(noArgRes.exitCode).toBe(1);
expect(noArgRes.stderr).toContain("用法: hose-use workflow delete");
// 2. Missing mutation confirmation
await expect(
handleWorkflowCommand(
{
area: "workflow",
action: "delete",
args: ["pipe_del_999"],
options: {},
raw: ["workflow", "delete", "pipe_del_999"],
},
mockRuntime
)
).rejects.toThrow("请先使用 --dry-run");
let deletedUrl: string | null = null;
const mockClient = {
get: (url: string) => {
if (url.includes("api/easo/pipelines/pipe_del_999")) {
return {
json: async () => ({
code: 200,
data: {
id: "pipe_del_999",
label: "待删除工作流",
state: 0,
},
}),
};
}
return { json: async () => ({ code: 200, data: null }) };
},
delete: (url: string) => {
deletedUrl = url;
return {
text: async () => "",
};
},
};
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)
);
// 3. Dry-run preview
const dryRunRes = await handleWorkflowCommand(
{
area: "workflow",
action: "delete",
args: ["pipe_del_999"],
options: { "dry-run": true },
raw: ["workflow", "delete", "pipe_del_999", "--dry-run"],
},
mockRuntime
);
expect(dryRunRes.exitCode).toBe(0);
expect(dryRunRes.stdout).toContain("[DRY-RUN]");
expect(dryRunRes.stdout).toContain("pipe_del_999");
expect(dryRunRes.stdout).toContain("待删除工作流");
expect(deletedUrl).toBeNull();
// 4. Confirmed execution via alias "rm"
const delRes = await handleWorkflowCommand(
{
area: "workflow",
action: "rm",
args: ["pipe_del_999"],
options: { yes: true },
raw: ["workflow", "rm", "pipe_del_999", "--yes"],
},
mockRuntime
);
expect(delRes.exitCode).toBe(0);
expect(delRes.stdout).toContain("pipe_del_999");
expect(delRes.stdout).toContain("已成功删除");
expect(deletedUrl as string | null).toBe("api/easo/pipelines/pipe_del_999");
// 5. JSON mode via alias "remove"
const jsonRes = await handleWorkflowCommand(
{
area: "workflow",
action: "remove",
args: ["pipe_del_999"],
options: { yes: true },
raw: ["workflow", "remove", "pipe_del_999", "--yes"],
},
{ ...mockRuntime, jsonMode: true }
);
expect(jsonRes.exitCode).toBe(0);
const parsedJson = JSON.parse(jsonRes.stdout);
expect(parsedJson.success).toBe(true);
expect(parsedJson.pipelineId).toBe("pipe_del_999");
});
});