Files
mtusi2026lab7/student-tests/command-runner.test.ts

64 lines
2.0 KiB
TypeScript

import { test, expect } from "bun:test";
import { createCommandRunner } from "../scripts/command-runner";
test("secondary diagnostic failures cannot replace command exit and both receipt writers are attempted", async () => {
for (const failed of ["unique", "last", "both"]) {
const calls: string[] = [];
const runner = createCommandRunner({
execute: async () => ({
out: "fixture-output",
err: "fixture-error",
code: 17,
}),
unique: async () => {
calls.push("unique");
if (failed !== "last") throw new Error("FIXTURE_EVIDENCE_IO_FAILED");
},
last: async () => {
calls.push("last");
if (failed !== "unique") throw new Error("FIXTURE_EVIDENCE_IO_FAILED");
},
});
await expect(runner(["fixture-command"])).rejects.toThrow(
"COMMAND_FAILED_fixture_command_17",
);
expect(calls.sort()).toEqual(["last", "unique"]);
}
});
test("successful and explicitly allowed command outcomes are returned without diagnostic writes", async () => {
for (const code of [0, 17]) {
let writes = 0;
const result = { out: "fixture-output", err: "fixture-error", code };
const runner = createCommandRunner({
execute: async () => result,
unique: async () => {
writes++;
},
last: async () => {
writes++;
},
});
expect(await runner(["fixture-command"], { allowFailure: true })).toEqual(
result,
);
expect(writes).toBe(0);
}
});
test("runner requires explicit fake ports and preserves primary execution error", async () => {
expect(() => createCommandRunner({} as never)).toThrow(
"INVALID_COMMAND_RUNNER_PORTS",
);
const primary = new Error("FIXTURE_EXECUTION_FAILED");
const runner = createCommandRunner({
execute: async () => {
throw primary;
},
unique: async () => {
throw new Error("UNEXPECTED_WRITE");
},
last: async () => {
throw new Error("UNEXPECTED_WRITE");
},
});
await expect(runner(["fixture-command"])).rejects.toBe(primary);
});