feat(workflow): implement script linting and offline sandbox evaluation

This commit is contained in:
gitea committed 2026-10-15 16:50:24 +08:00
1 parent 97eaed1066
commit d293bb47a4
10 files changed
+1255 -3

No files matched your search

+170
View File
@@ -1819,5 +1819,175 @@ describe("CLI - workflow job 执行详情与异常精准透视", () => {
expect(watchJobRes.exitCode).toBe(0);
expect(watchJobRes.stdout).toContain("Job [job_tail_101]");
});
it("handles workflow lint-script and eval-script commands end-to-end", async () => {
// 1. Missing args usage
const missingRes = await handleWorkflowCommand(
{
area: "workflow",
action: "lint-script",
args: [],
options: {},
raw: ["workflow", "lint-script"],
},
mockRuntime
);
expect(missingRes.exitCode).toBe(1);
expect(missingRes.stderr).toContain("用法: hose-use workflow lint-script");
// 2. Inline JavaScript linting - valid
const validJsRes = await handleWorkflowCommand(
{
area: "workflow",
action: "lint-script",
args: [],
options: { code: "const x = 10; return x * 2;" },
raw: ["workflow", "lint-script", '--code="const x = 10; return x * 2;"'],
},
mockRuntime
);
expect(validJsRes.exitCode).toBe(0);
expect(validJsRes.stdout).toContain("✅ 语法完全合规");
// 3. Inline JavaScript linting - syntax error
const syntaxErrRes = await handleWorkflowCommand(
{
area: "workflow",
action: "lint-script",
args: [],
options: { code: "const a = ;" },
raw: ["workflow", "lint-script", '--code="const a = ;"'],
},
mockRuntime
);
expect(syntaxErrRes.exitCode).toBe(1);
expect(syntaxErrRes.stdout).toContain("JavaScript 语法错误");
// 4. Inline MVEL linting - unclosed bracket
const mvelErrRes = await handleWorkflowCommand(
{
area: "workflow",
action: "lint",
args: [],
options: { code: "var list = [1, 2; return list;", type: "mvel" },
raw: ["workflow", "lint", '--code="var list = [1, 2; return list;"', "--type=mvel"],
},
mockRuntime
);
expect(mvelErrRes.exitCode).toBe(1);
expect(mvelErrRes.stdout).toContain("未闭合的括号");
// 5. Offline sandbox eval - inline code with inputs
const evalSuccessRes = await handleWorkflowCommand(
{
area: "workflow",
action: "eval-script",
args: [],
options: {
code: 'console.log("计算中:", a, b); return { result: a + b };',
input: JSON.stringify({ a: 15, b: 25 }),
},
raw: ["workflow", "eval-script", '--code="..."', '--input="..."'],
},
mockRuntime
);
expect(evalSuccessRes.exitCode).toBe(0);
expect(evalSuccessRes.stdout).toContain("执行成功 ✅");
expect(evalSuccessRes.stdout).toContain("计算中: 15 25");
expect(evalSuccessRes.stdout).toContain('"result": 40');
// 6. Offline sandbox eval - runtime error
const evalFailRes = await handleWorkflowCommand(
{
area: "workflow",
action: "eval-script",
args: [],
options: {
code: 'return obj.nonExistent.subField;',
input: JSON.stringify({ obj: {} }),
},
raw: ["workflow", "eval-script", '--code="..."', '--input="..."'],
},
mockRuntime
);
expect(evalFailRes.exitCode).toBe(1);
expect(evalFailRes.stdout).toContain("执行失败 ❌");
expect(evalFailRes.stdout).toContain("TypeError");
// 7. Workflow level lint-script with mocked pipeline
const mockDetail = {
id: "pipe_script_test",
label: "脚本测试流水线",
tasks: [
{
name: "trigger_webhook",
type: "webhook",
label: "Webhook 触发器",
},
{
name: "action_js_clean",
type: "code-js2",
label: "数据清洗 JS",
code: "console.log(info); return { total: 100 };",
mapping: [{ key: "info", value: "@{choose(trigger_webhook.data)}" }],
},
{
name: "action_mvel_calc",
type: "code",
label: "MVEL 计算",
code: "var val = trigger_webhook.data;\nreturn val;",
},
],
};
const mockClient = {
get: () => ({
json: async () => ({
code: 200,
data: mockDetail,
}),
}),
};
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 wfLintRes = await handleWorkflowCommand(
{
area: "workflow",
action: "lint-script",
args: ["pipe_script_test"],
options: {},
raw: ["workflow", "lint-script", "pipe_script_test"],
},
mockRuntime
);
expect(wfLintRes.exitCode).toBe(0);
expect(wfLintRes.stdout).toContain("共检测 2 个代码节点");
expect(wfLintRes.stdout).toContain("action_js_clean");
expect(wfLintRes.stdout).toContain("action_mvel_calc");
expect(wfLintRes.stdout).toContain("总体验证结果: 全部代码节点均通过语法与引用校验 ✅");
// 8. Workflow level eval-script targeting node
const wfEvalRes = await handleWorkflowCommand(
{
area: "workflow",
action: "eval-script",
args: ["pipe_script_test", "action_js_clean"],
options: { input: JSON.stringify({ info: "sample_input" }) },
raw: ["workflow", "eval-script", "pipe_script_test", "action_js_clean"],
},
mockRuntime
);
expect(wfEvalRes.exitCode).toBe(0);
expect(wfEvalRes.stdout).toContain("节点 [action_js_clean]");
expect(wfEvalRes.stdout).toContain("执行成功 ✅");
expect(wfEvalRes.stdout).toContain('"total": 100');
});
});