import { describe, expect, it } from "vitest"; import { executeTestRunPlan } from "./execute-test-run-plan.ts"; import type { CommandExecutor } from "../ports/command-executor.ts"; import type { GoTestPlan } from "./plan-go-test-from-slash-args.ts"; function goTestPlan(overrides: Partial = {}): GoTestPlan { return { language: "go", cwd: "/workspace/service", command: "go", args: ["test", "-race", "./..."], displayCommand: "go test -race ./...", ...overrides, }; } describe("executeTestRunPlan", () => { it("executes the command array in the plan cwd", async () => { const calls: Array<{ command: string; args: string[]; cwd: string }> = []; const executor: CommandExecutor = { async exec(command, args, options) { calls.push({ command, args, cwd: options.cwd }); return { stdout: "ok", stderr: "", code: 0, killed: false }; }, }; await executeTestRunPlan(goTestPlan(), { executor }); expect(calls).toEqual([ { command: "go", args: ["test", "-race", "./..."], cwd: "/workspace/service", }, ]); }); it("returns a successful normalized result for exit code zero", async () => { const plan = goTestPlan({ displayCommand: "custom test command" }); const result = await executeTestRunPlan(plan, { executor: executorReturning({ stdout: "PASS", stderr: "", code: 0, killed: false, }), }); expect(result).toEqual({ ok: true, displayCommand: "custom test command", stdout: "PASS", stderr: "", exitCode: 0, killed: false, }); }); it("returns a failed normalized result for non-zero exit codes", async () => { const result = await executeTestRunPlan(goTestPlan(), { executor: executorReturning({ stdout: "", stderr: "FAIL", code: 1, killed: false, }), }); expect(result).toMatchObject({ ok: false, displayCommand: "go test -race ./...", stdout: "", stderr: "FAIL", exitCode: 1, killed: false, }); }); it("returns a failed normalized result when execution is killed", async () => { const result = await executeTestRunPlan(goTestPlan(), { executor: executorReturning({ stdout: "", stderr: "timeout", code: 0, killed: true, }), }); expect(result).toMatchObject({ ok: false, exitCode: 0, killed: true, }); }); it("passes timeout and abort signal to the executor", async () => { const signal = new AbortController().signal; const observedOptions: Array<{ cwd: string; timeoutMs?: number; signal?: AbortSignal; }> = []; const executor: CommandExecutor = { async exec(_command, _args, options) { observedOptions.push(options); return { stdout: "", stderr: "", code: 0, killed: false }; }, }; await executeTestRunPlan(goTestPlan(), { executor, timeoutMs: 30_000, signal, }); expect(observedOptions).toEqual([ { cwd: "/workspace/service", timeoutMs: 30_000, signal }, ]); }); }); function executorReturning(result: { stdout: string; stderr: string; code: number; killed: boolean; }): CommandExecutor { return { async exec() { return result; }, }; }