fix(workflow): return script JSON reports and honor output options

This commit is contained in:
gitea committed 2026-10-20 18:02:31 +08:00
1 parent fac81b08e8
commit 4c4cd42edd
4 files changed
+51 -16

No files matched your search

@@ -0,0 +1,37 @@
import { expect, spyOn, test } from "bun:test";
import { mkdtempSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { parseArgs } from "../../parser.js";
import { handleWorkflowLintScript, handleWorkflowEvalScript } from "./script.js";
import * as shared from "./shared.js";
import * as services from "../../../domains/platform/services.js";
test("all local and remote script JSON branches return reports and honor output files", async () => {
const client = spyOn(shared, "createWorkflowDetailClient").mockResolvedValue({ client: {} } as any);
const detail = spyOn(services, "getWorkflowDetail").mockResolvedValue({ id: "wf", tasks: [{ name: "code", type: "code-js2", code: "return 7;" }] } as any);
const root = mkdtempSync(join(tmpdir(), "hose-script-json-"));
try {
for (const action of ["lint-script", "eval-script"]) {
for (const remote of [false, true]) {
const handler = action === "lint-script" ? handleWorkflowLintScript : handleWorkflowEvalScript;
const args = remote ? ["wf", "code", "--input", '{"x":1}'] : ["--code", "return 7;"];
const parsed = parseArgs(["workflow", action, ...args, "--json"]);
const report = await handler(parsed, { jsonMode: true } as any);
expect(report.exitCode).toBe(0);
const data = JSON.parse(report.stdout);
if (action === "eval-script") expect(data.returnValue).toBe(7);
else expect(data.valid ?? data.allValid).toBe(true);
const outputFile = join(root, `${action}-${remote}.json`);
const output = await handler(parsed, { jsonMode: true, outputOptions: { outputFile } } as any);
expect(JSON.parse(output.stdout).outputFile).toBe(outputFile);
expect(JSON.parse(readFileSync(outputFile, "utf8"))).toEqual(expect.objectContaining(action === "eval-script" ? { returnValue: 7 } : {}));
}
}
} finally { client.mockRestore(); detail.mockRestore(); }
});
test("script failures retain JSON output and failure exit code", async () => {
const result = await handleWorkflowEvalScript(parseArgs(["workflow", "eval-script", "--code", "throw new Error('failure');", "-j"]), { jsonMode: true } as any);
expect(result.exitCode).toBe(1);
expect(JSON.parse(result.stdout).error.message).toBe("failure");
});
+6 -10
View File
@@ -20,7 +20,7 @@ export async function handleWorkflowLintScript(
parsed: CliParsedArgs,
runtime: CliRuntime
): Promise<CliResult> {
const isJson = booleanOption(parsed.options, ["json"]);
const isJson = runtime.jsonMode || booleanOption(parsed.options, ["json", "j"]);
const filePath = stringOption(parsed.options, ["file", "path"]);
const codeInline = stringOption(parsed.options, ["code"]);
const explicitType = (stringOption(parsed.options, ["type"]) || "code-js2").toLowerCase();
@@ -68,8 +68,7 @@ export async function handleWorkflowLintScript(
};
if (isJson) {
printJson(report);
return { stdout: "", stderr: "", exitCode: hasErrors ? 1 : 0 };
return { stdout: printJson(report, runtime.outputOptions), stderr: "", exitCode: hasErrors ? 1 : 0 };
}
const lines: string[] = [];
@@ -132,8 +131,7 @@ export async function handleWorkflowLintScript(
const report = lintWorkflowScripts(workflow, targetNode);
if (isJson) {
printJson(report);
return { stdout: "", stderr: "", exitCode: report.allValid ? 0 : 1 };
return { stdout: printJson(report, runtime.outputOptions), stderr: "", exitCode: report.allValid ? 0 : 1 };
}
const lines: string[] = [];
@@ -193,7 +191,7 @@ export async function handleWorkflowEvalScript(
parsed: CliParsedArgs,
runtime: CliRuntime
): Promise<CliResult> {
const isJson = booleanOption(parsed.options, ["json"]);
const isJson = runtime.jsonMode || booleanOption(parsed.options, ["json", "j"]);
const filePath = stringOption(parsed.options, ["file", "path"]);
const codeInline = stringOption(parsed.options, ["code"]);
const timeoutStr = stringOption(parsed.options, ["timeout"]);
@@ -250,8 +248,7 @@ export async function handleWorkflowEvalScript(
const evalResult = await executeJavaScriptSandbox(code, inputs, { timeoutMs });
if (isJson) {
printJson(evalResult);
return { stdout: "", stderr: "", exitCode: evalResult.success ? 0 : 1 };
return { stdout: printJson(evalResult, runtime.outputOptions), stderr: "", exitCode: evalResult.success ? 0 : 1 };
}
return formatEvalCliResult(evalResult, filePath ? `file: ${filePath}` : "inline-code");
@@ -358,8 +355,7 @@ export async function handleWorkflowEvalScript(
const evalResult = await executeJavaScriptSandbox(code, inputs, { timeoutMs });
if (isJson) {
printJson(evalResult);
return { stdout: "", stderr: "", exitCode: evalResult.success ? 0 : 1 };
return { stdout: printJson(evalResult, runtime.outputOptions), stderr: "", exitCode: evalResult.success ? 0 : 1 };
}
const targetDesc = `节点 [${task.name}] ${task.label || task.displayName || ""} (${pipelineId})`;
@@ -128,3 +128,5 @@ hose-use workflow eval-script --code="return a + b;" --input='{"a":1,"b":2}'
- 借助 Kimi 网桥(`kimi-webbridge`)建立真实微前端会话,自动探测并解析不同应用类型下的非费用类表单元数据;
- 提取出动态列、关联实体 ID 与系统字段字典,在 CLI 层面提供对非费用类业务对象的只读探针与结构化校验能力。
脚本静态校验和离线仿真的本地、远端节点路径均支持 `--json/-j` 与 `--output-file`:stdout 返回 JSON 报告或导出摘要,失败时保留结构化错误并返回非零退出码。