feat(workflow): implement workflow create command from scratch with draft state defense
This commit is contained in:
1 parent
aa58dd7f6f
commit
02b197c119
7 files changed
+390
-7
No files matched your search
@@ -1297,4 +1297,167 @@ describe("CLI - workflow job 执行详情与异常精准透视", () => {
|
||||
expect(parsedJson.success).toBe(true);
|
||||
expect(parsedJson.pipelineId).toBe("pipe_del_999");
|
||||
});
|
||||
|
||||
it("handles workflow create: requires confirmation, dry-run, execution, triggers, and json mode", async () => {
|
||||
// 1. Missing mutation confirmation
|
||||
await expect(
|
||||
handleWorkflowCommand(
|
||||
{
|
||||
area: "workflow",
|
||||
action: "create",
|
||||
args: ["新风险拦截流程"],
|
||||
options: {},
|
||||
raw: ["workflow", "create", "新风险拦截流程"],
|
||||
},
|
||||
mockRuntime
|
||||
)
|
||||
).rejects.toThrow("请先使用 --dry-run");
|
||||
|
||||
// 2. Dry-run preview
|
||||
const dryRunRes = await handleWorkflowCommand(
|
||||
{
|
||||
area: "workflow",
|
||||
action: "create",
|
||||
args: ["新风险拦截流程"],
|
||||
options: { trigger: "webhook", "dry-run": true },
|
||||
raw: ["workflow", "create", "新风险拦截流程", "--trigger=webhook", "--dry-run"],
|
||||
},
|
||||
mockRuntime
|
||||
);
|
||||
expect(dryRunRes.exitCode).toBe(0);
|
||||
expect(dryRunRes.stdout).toContain("[DRY-RUN]");
|
||||
expect(dryRunRes.stdout).toContain("新风险拦截流程");
|
||||
expect(dryRunRes.stdout).toContain("webhook");
|
||||
|
||||
let createdPayload: any = null;
|
||||
const mockClient = {
|
||||
post: (url: string, opts: any) => {
|
||||
if (url.includes("api/easo/pipelines")) {
|
||||
createdPayload = opts.json;
|
||||
return {
|
||||
json: async () => ({
|
||||
code: 200,
|
||||
data: {
|
||||
id: "pipe_new_777",
|
||||
label: opts.json.label,
|
||||
platformId: opts.json.platformId || "app_default",
|
||||
state: opts.json.state ?? 0,
|
||||
version: "26.10.1",
|
||||
tasks: opts.json.tasks || [],
|
||||
},
|
||||
}),
|
||||
};
|
||||
}
|
||||
return { json: async () => ({ code: 200, data: null }) };
|
||||
},
|
||||
get: () => ({ 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)
|
||||
);
|
||||
|
||||
// 3. Confirmed execution with webhook trigger
|
||||
const createRes = await handleWorkflowCommand(
|
||||
{
|
||||
area: "workflow",
|
||||
action: "create",
|
||||
args: ["外部发票接入"],
|
||||
options: { trigger: "webhook", yes: true },
|
||||
raw: ["workflow", "create", "外部发票接入", "--trigger=webhook", "--yes"],
|
||||
},
|
||||
mockRuntime
|
||||
);
|
||||
expect(createRes.exitCode).toBe(0);
|
||||
expect(createRes.stdout).toContain("工作流创建成功");
|
||||
expect(createRes.stdout).toContain("pipe_new_777");
|
||||
expect(createRes.stdout).toContain("外部发票接入");
|
||||
expect(createRes.stdout).toContain("https://piper.ekuaibao.com/api/webhook/");
|
||||
expect(createRes.stdout).toContain("草稿未发布");
|
||||
|
||||
expect(createdPayload).not.toBeNull();
|
||||
expect(createdPayload.label).toBe("外部发票接入");
|
||||
expect(createdPayload.state).toBe(0);
|
||||
expect(createdPayload.tasks).toHaveLength(1);
|
||||
expect(createdPayload.tasks[0].type).toBe("webhook");
|
||||
expect(createdPayload.tasks[0].urlPath).toBeDefined();
|
||||
|
||||
// 4. Create with clock trigger via alias "new"
|
||||
const clockRes = await handleWorkflowCommand(
|
||||
{
|
||||
area: "workflow",
|
||||
action: "new",
|
||||
args: ["每日定时同步"],
|
||||
options: { trigger: "clock", yes: true },
|
||||
raw: ["workflow", "new", "每日定时同步", "--trigger=clock", "--yes"],
|
||||
},
|
||||
mockRuntime
|
||||
);
|
||||
expect(clockRes.exitCode).toBe(0);
|
||||
expect(createdPayload.tasks[0].type).toBe("clock");
|
||||
expect(createdPayload.tasks[0].cron).toBe("0 0 * * *");
|
||||
|
||||
// 5. JSON mode via alias "add"
|
||||
const jsonRes = await handleWorkflowCommand(
|
||||
{
|
||||
area: "workflow",
|
||||
action: "add",
|
||||
args: ["API创建流程"],
|
||||
options: { yes: true },
|
||||
raw: ["workflow", "add", "API创建流程", "--yes"],
|
||||
},
|
||||
{ ...mockRuntime, jsonMode: true }
|
||||
);
|
||||
expect(jsonRes.exitCode).toBe(0);
|
||||
const parsedJson = JSON.parse(jsonRes.stdout);
|
||||
expect(parsedJson.id).toBe("pipe_new_777");
|
||||
expect(parsedJson.state).toBe(0);
|
||||
|
||||
// 6. Create from template file
|
||||
const tmpFile = `/tmp/test_wf_${Date.now()}.json`;
|
||||
await (await import("fs/promises")).writeFile(
|
||||
tmpFile,
|
||||
JSON.stringify({
|
||||
label: "文件模板流程",
|
||||
state: 1,
|
||||
desc: "来自模板文件",
|
||||
tasks: [
|
||||
{
|
||||
name: "trigger_webhook",
|
||||
type: "webhook",
|
||||
label: "Webhook触发",
|
||||
groupName: "trigger",
|
||||
orderIndex: 0,
|
||||
levelIndex: [0],
|
||||
status: "active",
|
||||
},
|
||||
],
|
||||
})
|
||||
);
|
||||
|
||||
const fileRes = await handleWorkflowCommand(
|
||||
{
|
||||
area: "workflow",
|
||||
action: "create",
|
||||
args: [],
|
||||
options: { file: tmpFile, yes: true },
|
||||
raw: ["workflow", "create", `--file=${tmpFile}`, "--yes"],
|
||||
},
|
||||
mockRuntime
|
||||
);
|
||||
expect(fileRes.exitCode).toBe(0);
|
||||
expect(createdPayload.label).toBe("文件模板流程");
|
||||
expect(createdPayload.state).toBe(0);
|
||||
expect(createdPayload.desc).toBe("来自模板文件");
|
||||
expect(createdPayload.tasks[0].urlPath).toBeDefined();
|
||||
|
||||
await (await import("fs/promises")).unlink(tmpFile).catch(() => {});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user