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

+6 -1
View File
@@ -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,
};
}
+23
View File
@@ -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: "",
+9 -2
View File
@@ -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,
+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,
};
}