feat(workflow): add end-to-end workflow debug test command and condition branch explanation

This commit is contained in:
gitea committed 2026-10-15 13:24:14 +08:00
1 parent 26a6b7318c
commit fe6183dd44
13 files changed
+1026 -3

No files matched your search

+135
View File
@@ -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,
};
}