feat(workflow): add end-to-end workflow debug test command and condition branch explanation
This commit is contained in:
1 parent
26a6b7318c
commit
fe6183dd44
13 files changed
+1026
-3
No files matched your search
@@ -491,4 +491,178 @@ describe("CLI - workflow job 执行详情与异常精准透视", () => {
|
||||
expect(parsed.summary.statusDiffCount).toBe(1);
|
||||
expect(parsed.nodes.find((n: any) => n.taskName === "auto_approve").statusB).toBe("FAILED");
|
||||
});
|
||||
|
||||
it("handles workflow test without pipelineId", async () => {
|
||||
const res = await handleWorkflowCommand(
|
||||
{
|
||||
area: "workflow",
|
||||
action: "test",
|
||||
args: [],
|
||||
options: {},
|
||||
raw: ["workflow", "test"],
|
||||
},
|
||||
mockRuntime
|
||||
);
|
||||
expect(res.exitCode).toBe(1);
|
||||
expect(res.stderr).toContain("用法: hose-use workflow test");
|
||||
});
|
||||
|
||||
it("handles workflow test without --flow by listing candidate sources", async () => {
|
||||
const mockClient = {
|
||||
post: (url: string) => ({
|
||||
json: async () => ({
|
||||
code: 200,
|
||||
data: {
|
||||
type: "flowForm",
|
||||
total: 1,
|
||||
label: "我的单据",
|
||||
items: [
|
||||
{ id: "ZJ-20261015-001387", title: "资金调拨测试单", flowState: "审批中" },
|
||||
],
|
||||
fields: [
|
||||
{ key: "id", label: "单号" },
|
||||
{ key: "title", label: "标题" },
|
||||
],
|
||||
},
|
||||
}),
|
||||
}),
|
||||
get: (url: string) => {
|
||||
if (url.includes("platform")) {
|
||||
return { json: async () => ({ items: [] }) };
|
||||
}
|
||||
return { json: async () => ({ code: 200, data: [] }) };
|
||||
},
|
||||
};
|
||||
|
||||
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 res = await handleWorkflowCommand(
|
||||
{
|
||||
area: "workflow",
|
||||
action: "test",
|
||||
args: ["wf_test_1"],
|
||||
options: { platformId: "p1" },
|
||||
raw: ["workflow", "test", "wf_test_1", "--platform-id=p1"],
|
||||
},
|
||||
mockRuntime
|
||||
);
|
||||
|
||||
expect(res.exitCode).toBe(1);
|
||||
expect(res.stdout).toContain("未指定测试单号");
|
||||
expect(res.stdout).toContain("ZJ-20261015-001387");
|
||||
expect(res.stdout).toContain("资金调拨测试单");
|
||||
});
|
||||
|
||||
it("handles workflow test end-to-end with --flow parameter", async () => {
|
||||
const mockClient = {
|
||||
post: (url: string) => {
|
||||
if (url.includes("debugSource")) {
|
||||
return {
|
||||
json: async () => ({
|
||||
code: 200,
|
||||
data: {
|
||||
type: "flowForm",
|
||||
total: 1,
|
||||
items: [{ id: "ZJ-20261015-001387", title: "资金调拨", flowState: "审批中" }],
|
||||
},
|
||||
}),
|
||||
};
|
||||
}
|
||||
if (url.includes("api/easo/jobs")) {
|
||||
return {
|
||||
json: async () => ({
|
||||
code: 200,
|
||||
data: "job_debug_888",
|
||||
}),
|
||||
};
|
||||
}
|
||||
return { json: async () => ({ code: 200, data: null }) };
|
||||
},
|
||||
get: (url: string) => {
|
||||
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: "control_1",
|
||||
label: "条件节点",
|
||||
type: "condition",
|
||||
status: "COMPLETED",
|
||||
executionTime: 5,
|
||||
tasks: [{ name: "action_auto", label: "自动审批", type: "http" }],
|
||||
},
|
||||
{
|
||||
id: "e3",
|
||||
name: "action_stop",
|
||||
label: "终止节点",
|
||||
type: "stop",
|
||||
status: "COMPLETED",
|
||||
executionTime: 2,
|
||||
},
|
||||
],
|
||||
}),
|
||||
};
|
||||
}
|
||||
return {
|
||||
json: async () => ({
|
||||
code: 200,
|
||||
data: {
|
||||
id: "job_debug_888",
|
||||
pipelineId: "wf_test_1",
|
||||
status: "COMPLETED",
|
||||
runType: "DEBUG",
|
||||
durationTime: 17,
|
||||
},
|
||||
}),
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
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 res = await handleWorkflowCommand(
|
||||
{
|
||||
area: "workflow",
|
||||
action: "test",
|
||||
args: ["wf_test_1"],
|
||||
options: { flow: "ZJ-20261015-001387", platformId: "p1" },
|
||||
raw: ["workflow", "test", "wf_test_1", "--flow=ZJ-20261015-001387", "--platform-id=p1"],
|
||||
},
|
||||
mockRuntime
|
||||
);
|
||||
|
||||
expect(res.exitCode).toBe(0);
|
||||
expect(res.stdout).toContain("工作流自动化调测报告 [wf_test_1]");
|
||||
expect(res.stdout).toContain("job_debug_888");
|
||||
expect(res.stdout).toContain("ZJ-20261015-001387");
|
||||
expect(res.stdout).toContain("control_1");
|
||||
});
|
||||
});
|
||||
@@ -7,6 +7,7 @@ import { handleJob } from "./job.js";
|
||||
import { handleCheck } from "./check.js";
|
||||
import { handleAnalyze } from "./analyze.js";
|
||||
import { handleWorkflowDiff, handleWorkflowDiffJob } from "./diff.js";
|
||||
import { handleWorkflowTest } from "./test.js";
|
||||
|
||||
export async function handleWorkflowCommand(
|
||||
parsed: CliParsedArgs,
|
||||
@@ -69,10 +70,14 @@ export async function handleWorkflowCommand(
|
||||
case "diff-jobs":
|
||||
case "compare-job":
|
||||
return handleWorkflowDiffJob(parsed, runtime);
|
||||
case "test":
|
||||
case "debug":
|
||||
case "run-test":
|
||||
return handleWorkflowTest(parsed, runtime);
|
||||
default:
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `未知 workflow 子命令: ${action}。支持: list, get, find, analyze, toggle, publish, patch, save, history, logs, job, diff, diff-job, check\n`,
|
||||
stderr: `未知 workflow 子命令: ${action}。支持: list, get, find, analyze, toggle, publish, patch, save, history, logs, job, diff, diff-job, check, test, debug\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
getJobFailedExecutions,
|
||||
isTaskExecutionFailed,
|
||||
listPlatforms,
|
||||
explainConditionBranches,
|
||||
} from "../../../domains/platform/services.js";
|
||||
import type { WorkflowTaskExecution } from "../../../domains/platform/types.js";
|
||||
import { stringOption, numberOption, booleanOption } from "../../parser.js";
|
||||
@@ -402,6 +403,8 @@ export async function handleJob(parsed: CliParsedArgs, runtime: CliRuntime): Pro
|
||||
]);
|
||||
const failedExecutions = await enrichFailedExecutions(client, jobId, failedPage.items);
|
||||
|
||||
const conditionDiagnoses = explainConditionBranches(tree);
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson(
|
||||
@@ -410,6 +413,7 @@ export async function handleJob(parsed: CliParsedArgs, runtime: CliRuntime): Pro
|
||||
job,
|
||||
executionTree: tree,
|
||||
failedExecutions,
|
||||
conditionDiagnoses,
|
||||
},
|
||||
runtime.outputOptions
|
||||
),
|
||||
@@ -473,6 +477,25 @@ export async function handleJob(parsed: CliParsedArgs, runtime: CliRuntime): Pro
|
||||
);
|
||||
}
|
||||
|
||||
if (conditionDiagnoses.length > 0) {
|
||||
outLines.push("");
|
||||
outLines.push("── 💡 条件分支跳过诊断 (为什么没有走内部子任务?) ──");
|
||||
for (const diag of conditionDiagnoses) {
|
||||
outLines.push(` ⚠️ [${diag.conditionNodeName}] ${diag.conditionNodeLabel} (Execution ID: ${diag.conditionNodeId})`);
|
||||
outLines.push(` • 判定结论: ${diag.summary}`);
|
||||
outLines.push(` • 未执行子任务: ${diag.skippedTaskNames.join(", ")}`);
|
||||
if (diag.rules.length > 0) {
|
||||
outLines.push(" • 规则比对明细:");
|
||||
for (const r of diag.rules) {
|
||||
const statusIcon = r.matched ? "✅" : "❌";
|
||||
const missTag = r.isMissing ? " [⚠️ 属性缺失]" : "";
|
||||
outLines.push(` ${statusIcon} ${r.leftLabel} ${r.operatorLabel} ${JSON.stringify(r.expectedValue)}${missTag}`);
|
||||
outLines.push(` ↳ ${r.detail}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
stdout: `${outLines.join("\n")}\n`,
|
||||
stderr: "",
|
||||
|
||||
@@ -17,8 +17,15 @@ export async function createWorkflowDetailClient(
|
||||
const explicit = stringOption(parsed.options, PLATFORM_ID_OPTION_KEYS);
|
||||
let platformId = explicit;
|
||||
if (!platformId) {
|
||||
const all = await listAllWorkflows(clients, { page: 1, pageSize: 100 });
|
||||
platformId = all.workflows.find((workflow) => workflow.id === pipelineId)?.platformId;
|
||||
try {
|
||||
const corpId = runtime.selectedCorp!.corpId || runtime.selectedCorp!.id;
|
||||
const { listPlatforms } = await import("../../../domains/platform/services.js");
|
||||
const platforms = await listPlatforms(clients.ekbClient, corpId);
|
||||
const firstActive = platforms.find((p) => p.active) ?? platforms[0];
|
||||
platformId = firstActive?.id;
|
||||
} catch {
|
||||
// Fall back to easoClient
|
||||
}
|
||||
}
|
||||
return {
|
||||
client: platformId ? await createPiperClientForCorp(runtime.selectedCorp!, platformId) : clients.easoClient,
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
import {
|
||||
getWorkflowDebugSource,
|
||||
runWorkflowDebugTest,
|
||||
} from "../../../domains/platform/services.js";
|
||||
import { stringOption, booleanOption, numberOption } from "../../parser.js";
|
||||
import { printJson, printTable, printKeyValue } from "../../printer.js";
|
||||
import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js";
|
||||
import { createWorkflowDetailClient } from "./shared.js";
|
||||
|
||||
export async function handleWorkflowTest(
|
||||
parsed: CliParsedArgs,
|
||||
runtime: CliRuntime
|
||||
): Promise<CliResult> {
|
||||
const pipelineId = parsed.args[0] || stringOption(parsed.options, ["id", "pipeline-id", "pipelineId"]);
|
||||
if (!pipelineId) {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: "用法: hose-use workflow test <pipelineId> --flow=<单号|单据ID> [--auto-publish] [--timeout=<ms>]\n别名: hose-use workflow debug <pipelineId> --flow=<单号>\n",
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
const { client } = await createWorkflowDetailClient(parsed, runtime, pipelineId);
|
||||
const flowCodeOrId = stringOption(parsed.options, ["flow", "code", "flow-code", "flowCode", "bill", "entity", "id"]);
|
||||
const autoPublish = booleanOption(parsed.options, ["auto-publish", "autoPublish", "publish"]);
|
||||
const timeoutMs = numberOption(parsed.options, ["timeout", "timeout-ms", "timeoutMs"]) || 15000;
|
||||
|
||||
// 如果没有传入 --flow,先列出当前可用的调试数据源供用户参考
|
||||
if (!flowCodeOrId) {
|
||||
try {
|
||||
const source = await getWorkflowDebugSource(client, pipelineId, { page: 1, pageSize: 5 });
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson(source, runtime.outputOptions),
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
const rows = source.items.map((it, idx) => [
|
||||
idx + 1,
|
||||
it.id,
|
||||
it.title || "-",
|
||||
it.flowState || "-",
|
||||
]);
|
||||
const out = [
|
||||
`⚠️ 未指定测试单号,请通过 --flow=<单号> 传入测试单据。`,
|
||||
"",
|
||||
`── 工作流 [${pipelineId}] 最近可用测试数据源 (${source.label || "候选单据"}) ──`,
|
||||
rows.length ? printTable(["#", "单据编号 (ID)", "单据名称 (Title)", "状态 (Status)"], rows) : "暂无可用测试单据",
|
||||
"",
|
||||
`示例: hose-use workflow test ${pipelineId} --flow=${source.items[0]?.id || "ZJ-xxxx"}`,
|
||||
].join("\n");
|
||||
return { stdout: `${out}\n`, stderr: "", exitCode: 1 };
|
||||
} catch (err: any) {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `未指定测试单号 (--flow=<单号>),且拉取候选单据失败: ${err.message || String(err)}\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 执行端到端调试测试
|
||||
const report = await runWorkflowDebugTest(client, pipelineId, {
|
||||
flowCodeOrId,
|
||||
autoPublish,
|
||||
timeoutMs,
|
||||
});
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson(report, runtime.outputOptions),
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
const treeRows = report.executionTree.map((node, idx) => [
|
||||
idx + 1,
|
||||
node.id,
|
||||
node.name || "-",
|
||||
node.label || (node as any).displayName || "-",
|
||||
node.type || "-",
|
||||
node.status || "-",
|
||||
String((node as any).durationTime ?? node.executionTime ?? "-"),
|
||||
node.error?.message || "-",
|
||||
]);
|
||||
|
||||
const outLines = [
|
||||
`====== 工作流自动化调测报告 [${pipelineId}] ======`,
|
||||
printKeyValue({
|
||||
"工作流 ID (pipelineId)": pipelineId,
|
||||
"生成 Job ID": report.jobId,
|
||||
"测试目标单据": report.targetEntityId,
|
||||
"执行结果状态": report.status,
|
||||
"运行模式": report.runType,
|
||||
"总耗时(ms)": String(report.durationTime ?? "-"),
|
||||
"节点执行总数": report.executionTree.length,
|
||||
"异常/跳过节点数": report.failedExecutions.length,
|
||||
}),
|
||||
"",
|
||||
"── 顶层节点执行树 ──",
|
||||
treeRows.length
|
||||
? printTable(
|
||||
["#", "Execution ID", "节点标识 (name)", "节点名称", "类型 (type)", "状态 (status)", "耗时(ms)", "错误信息"],
|
||||
treeRows
|
||||
)
|
||||
: "暂无顶层节点执行记录",
|
||||
];
|
||||
|
||||
if (report.conditionDiagnoses.length > 0) {
|
||||
outLines.push("");
|
||||
outLines.push("── 💡 条件分支跳过诊断 (为什么没有走内部子任务?) ──");
|
||||
for (const diag of report.conditionDiagnoses) {
|
||||
outLines.push(` ⚠️ [${diag.conditionNodeName}] ${diag.conditionNodeLabel} (Execution ID: ${diag.conditionNodeId})`);
|
||||
outLines.push(` • 判定结论: ${diag.summary}`);
|
||||
outLines.push(` • 未执行子任务: ${diag.skippedTaskNames.join(", ")}`);
|
||||
if (diag.rules.length > 0) {
|
||||
outLines.push(" • 规则比对明细:");
|
||||
for (const r of diag.rules) {
|
||||
const statusIcon = r.matched ? "✅" : "❌";
|
||||
const missTag = r.isMissing ? " [⚠️ 属性缺失]" : "";
|
||||
outLines.push(` ${statusIcon} ${r.leftLabel} ${r.operatorLabel} ${JSON.stringify(r.expectedValue)}${missTag}`);
|
||||
outLines.push(` ↳ ${r.detail}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
stdout: `${outLines.join("\n")}\n`,
|
||||
stderr: "",
|
||||
exitCode: report.status === "FAILED" ? 1 : 0,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user