feat(workflow): support webhook sample records, params preview and expand trigger catalog

This commit is contained in:
gitea committed 2026-10-15 15:01:20 +08:00
1 parent 8531530ccd
commit c56460716f
6 files changed
+175 -15

No files matched your search

+40 -4
View File
@@ -880,7 +880,26 @@ describe("CLI - workflow job 执行详情与异常精准透视", () => {
}),
};
},
post: () => ({ json: async () => ({ code: 200, data: "job_webhook_202" }) }),
post: (url: string) => {
if (url && url.includes("sampleData")) {
return {
json: async () => ({
code: 200,
data: {
records: [
{
id: "sample_001",
pipelineId: "wf_webhook_1",
dataJson: JSON.stringify({ code: "SAMPLE-PAYLOAD-001" }),
createTime: 1792047100000,
},
],
},
}),
};
}
return { json: async () => ({ code: 200, data: "job_webhook_202" }) };
},
};
spies.push(
@@ -894,7 +913,7 @@ describe("CLI - workflow job 执行详情与异常精准透视", () => {
spyOn(helpers, "createPiperClientForCorp").mockResolvedValue(mockClient as any)
);
// 1. 未传 body 时提示参数说明
// 1. 未传 body 时提示参数说明与服务器历史样本
const noBodyRes = await handleWorkflowCommand(
{
area: "workflow",
@@ -907,9 +926,26 @@ describe("CLI - workflow job 执行详情与异常精准透视", () => {
);
expect(noBodyRes.exitCode).toBe(1);
expect(noBodyRes.stdout).toContain("外部事件触发器");
expect(noBodyRes.stdout).toContain("--body");
expect(noBodyRes.stdout).toContain("服务器最近接收的数据样本");
expect(noBodyRes.stdout).toContain("sample_001");
expect(noBodyRes.stdout).toContain("--sample");
// 2. 传入 --body 成功派发
// 2. 传入 --sample=1 成功提取样本发起测试
const withSampleRes = await handleWorkflowCommand(
{
area: "workflow",
action: "test",
args: ["wf_webhook_1"],
options: { sample: "1", platformId: "p1", yes: true },
raw: ["workflow", "test", "wf_webhook_1", "--sample=1", "--platform-id=p1", "--yes"],
},
mockRuntime
);
expect(withSampleRes.exitCode).toBe(0);
expect(withSampleRes.stdout).toContain("[WEBHOOK]");
expect(withSampleRes.stdout).toContain("Webhook Payload: code");
// 3. 传入 --body 成功派发
const withBodyRes = await handleWorkflowCommand(
{
area: "workflow",
+78 -4
View File
@@ -3,6 +3,7 @@ import {
classifyWorkflowTrigger,
getWorkflowDebugSource,
getWorkflowDetail,
getWorkflowSampleData,
runWorkflowDebugTest,
type WorkflowTriggerCategory,
} from "../../../domains/platform/services.js";
@@ -47,6 +48,7 @@ export async function handleWorkflowTest(
const rawBody = stringOption(parsed.options, ["body", "data", "payload"]);
const filePath = stringOption(parsed.options, ["file", "path"]);
const isEmpty = booleanOption(parsed.options, ["empty", "none"]);
const sampleOption = stringOption(parsed.options, ["sample", "sample-id", "sampleId"]);
const warnings = ["调试会在真实环境执行工作流节点,可能改变业务数据、发送通知并生成快照;auto-publish 还可能发布流程。"];
const confirmExecution = () => requireMutationConfirmation(parsed, runtime, "执行工作流调试", `hose-use workflow test ${pipelineId} --yes`);
@@ -79,7 +81,8 @@ export async function handleWorkflowTest(
}
case "WEBHOOK": {
// 分支 B: Webhook / 外部事件触发器 ── 接收模拟请求体 (Payload)
// 分支 B: Webhook / 外部事件触发器 ── 支持 sample / body / file / empty
targetId = "";
if (rawBody) {
try {
customPayload = JSON.parse(rawBody);
@@ -107,21 +110,92 @@ export async function handleWorkflowTest(
exitCode: 1,
};
}
} else if (sampleOption) {
// 从服务器获取历史接收样本发起测试
try {
const sampleResult = await getWorkflowSampleData(client, pipelineId);
const records = sampleResult.records || [];
let matched = records.find((r) => r.id === sampleOption);
if (!matched) {
const num = parseInt(sampleOption, 10);
if (!isNaN(num) && num >= 1 && num <= records.length) {
matched = records[num - 1];
}
}
if (!matched) {
return {
stdout: "",
stderr: `❌ 未找到指定的数据样本 [${sampleOption}]。当前服务器保存了 ${records.length} 条样本。\n`,
exitCode: 1,
};
}
customPayload = JSON.parse(matched.dataJson);
} catch (e: any) {
return {
stdout: "",
stderr: `❌ 获取或解析数据样本失败: ${e.message}\n`,
exitCode: 1,
};
}
} else if (isEmpty) {
customPayload = {};
} else {
// 未提供 Payload 时给出交互式指引
// 未提供 Payload 时,展示参数定义与服务器最近接收的数据样本
let sampleLines: string[] = [];
try {
const sampleResult = await getWorkflowSampleData(client, pipelineId);
const records = sampleResult.records || [];
if (records.length > 0) {
const rows = records.map((r, idx) => {
const timeStr = r.createTime
? new Date(r.createTime).toLocaleString("zh-CN", { hour12: false })
: "-";
let preview = r.dataJson || "";
if (preview.length > 55) preview = preview.slice(0, 52) + "...";
return [idx + 1, r.id, timeStr, preview];
});
sampleLines = [
"",
`── 服务器最近接收的数据样本 (最近 ${records.length} 条) ──`,
printTable(["#", "样本 ID", "接收时间", "数据摘要 (Payload Preview)"], rows),
];
}
} catch {}
// 解析触发器定义的请求参数与中文昵称映射
const triggerParams = Array.isArray((triggerNode as any)?.params)
? ((triggerNode as any).params as Array<{ key: string; value: string }>)
: [];
let paramLines: string[] = [];
if (triggerParams.length > 0) {
paramLines = [
"",
"── 接收的请求参数定义 (Params) ──",
printTable(
["参数名 (Key)", "中文昵称 (Label)"],
triggerParams.map((p) => [p.key, p.value])
),
];
}
return {
stdout: [
`📡 [外部事件触发器: ${triggerLabel} (${triggerType})]`,
`该工作流由 Webhook / 外部系统事件驱动,需提供模拟请求体 (Payload) 进行测试。`,
...paramLines,
...sampleLines,
"",
"调试传参选项:",
` --body='{"key": "value"}' 直接传入 JSON 请求体`,
...(sampleLines.length > 0
? [` --sample=<1|2|3> 直接选用上述服务器最近接收的数据样本发起测试`]
: []),
` --body='{"key": "value"}' 直接传入自定义 JSON 请求体`,
` --file=<payload.json> 从本地 JSON 文件读取请求体`,
` --empty 使用空对象 {} 直接发起测试`,
"",
`示例: hose-use workflow test ${pipelineId} --body='{"code": "TEST-001"}'`,
`示例: hose-use workflow test ${pipelineId} ${
sampleLines.length > 0 ? "--sample=1" : `--body='{"code": "TEST-001"}'`
}`,
].join("\n") + "\n",
stderr: "",
exitCode: 1,