import { expect, test } from "bun:test"; import { createRuntimePreparation, type RuntimeMode, } from "../scripts/runtime-preparation"; test("canonical runtime invokes its build exactly once", async () => { const calls: string[] = []; await createRuntimePreparation({ build: async () => { calls.push("build"); }, verifyCache: async () => { calls.push("cache"); }, })("canonical"); expect(calls).toEqual(["build"]); }); test("checked cache verifies existing runtime without an implicit build", async () => { const calls: string[] = []; await createRuntimePreparation({ build: async () => { calls.push("build"); }, verifyCache: async () => { calls.push("cache"); }, })("checked-cache"); expect(calls).toEqual(["cache"]); }); test("failed cache verification preserves its error and never falls back to a build", async () => { const error = new Error("FIXTURE_WRONG_BINARY"), calls: string[] = []; let observed: unknown; try { await createRuntimePreparation({ build: async () => { calls.push("build"); }, verifyCache: async () => { calls.push("cache"); throw error; }, })("checked-cache"); } catch (e) { observed = e; } expect(observed).toBe(error); expect(calls).toEqual(["cache"]); }); test("unknown runtime mode fails before either operational port", async () => { const calls: string[] = []; await expect( createRuntimePreparation({ build: async () => { calls.push("build"); }, verifyCache: async () => { calls.push("cache"); }, })("foreign" as RuntimeMode), ).rejects.toThrow("INVALID_RUNTIME_MODE"); expect(calls).toEqual([]); }); test("constructor requires both explicit ports", () => { expect(() => createRuntimePreparation({ build: async () => {} } as never), ).toThrow("RUNTIME_PREPARATION_PORTS_REQUIRED"); });