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"); });