import { createServer, type Server } from "node:net"; import { afterEach, describe, expect, it, vi } from "vitest"; import { findFreePort, moduleSpecifier, reservePort, runOutputPath, runOutputRoot, testRunPath, testRunRoot, } from "../../scripts/lib/run-isolation.ts"; const servers: Server[] = []; afterEach(async () => { await Promise.all( servers.splice(0).map((server) => new Promise((resolve) => server.close(() => resolve()))), ); }); async function listen(port: number): Promise { const server = createServer(); servers.push(server); await new Promise((resolve, reject) => { server.once("error", reject); server.listen(port, "127.0.0.1", () => resolve()); }); const address = server.address(); return typeof address === "object" && address !== null ? address.port : -1; } /** * 1105/12-04. Builds and Playwright runs wrote to fixed paths (`dist`, * `artifacts/tests/*`), so a second run in one checkout emptied the first * one's dist and deleted its results. These are the path rules every config * and build step uses. */ describe("test run paths (TEST_RUN_ID)", () => { it("are the checkout's own paths without a run ID", () => { for (const environment of [{}, { TEST_RUN_ID: "" }]) { expect(testRunRoot(environment)).toBe("."); expect(testRunPath("artifacts/tests/e2e/results", environment)).toBe( "artifacts/tests/e2e/results", ); expect(testRunPath("dist", environment)).toBe("dist"); } }); it("give each run ID its own folder under .tmp/runs", () => { const a = { TEST_RUN_ID: "gate-008.a" }; const b = { TEST_RUN_ID: "gate-008.b" }; expect(testRunRoot(a)).toBe(".tmp/runs/gate-008.a"); expect(testRunPath("artifacts/tests/e2e/results", a)).toBe( ".tmp/runs/gate-008.a/artifacts/tests/e2e/results", ); expect(testRunPath("dist", b)).toBe(".tmp/runs/gate-008.b/dist"); expect(testRunPath("dist", a)).not.toBe(testRunPath("dist", b)); }); it("refuse a run ID that is not one folder name", () => { for (const runId of ["../dist", "a/b", ".hidden", "-a", "a b", "x".repeat(65), "a\\b"]) { expect(() => testRunRoot({ TEST_RUN_ID: runId }), runId).toThrow(/TEST_RUN_ID/u); } }); }); describe("build output root (RUN_OUTPUT_ROOT)", () => { it("is the checkout without a root", () => { for (const environment of [{}, { RUN_OUTPUT_ROOT: "" }, { RUN_OUTPUT_ROOT: "." }]) { expect(runOutputRoot(environment)).toBe("."); expect(runOutputPath("dist", environment)).toBe("dist"); expect(runOutputPath(".generated/frontend-runtime", environment)).toBe( ".generated/frontend-runtime", ); } }); it("puts every build output under a root in .tmp", () => { const environment = { RUN_OUTPUT_ROOT: "./.tmp/runs/a/" }; expect(runOutputRoot(environment)).toBe(".tmp/runs/a"); expect(runOutputPath("dist/config.json", environment)).toBe(".tmp/runs/a/dist/config.json"); expect(runOutputPath("artifacts/release/build-manifest.json", environment)).toBe( ".tmp/runs/a/artifacts/release/build-manifest.json", ); }); it("refuses a root outside .tmp, which git, Tailwind and the watchers do not ignore", () => { for (const root of ["dist", "out/a", "/tmp/a", ".tmp", ".tmp/../dist", "../other", ".tmp\\a"]) { expect(() => runOutputRoot({ RUN_OUTPUT_ROOT: root }), root).toThrow(/RUN_OUTPUT_ROOT/u); } }); }); describe("server ports", () => { it("finds a port a server can listen on right away", async () => { const port = await findFreePort(); expect(port).toBeGreaterThan(0); expect(await listen(port)).toBe(port); }); it("never offers a port another run's server holds", async () => { const taken = await listen(0); for (let attempt = 0; attempt < 20; attempt += 1) { expect(await findFreePort()).not.toBe(taken); } }); it("keeps the port it picked in the variable, so the next evaluation reuses it", async () => { const environment: Record = {}; const find = vi.fn(async () => 41_234); expect(await reservePort("TEST_E2E_PORT", environment, find)).toBe(41_234); expect(environment["TEST_E2E_PORT"]).toBe("41234"); // A Playwright worker: same config, the runner's environment. expect(await reservePort("TEST_E2E_PORT", environment, find)).toBe(41_234); expect(find).toHaveBeenCalledTimes(1); }); it("uses a port the caller pinned and refuses one that is not a port", async () => { const find = vi.fn(async () => 41_234); expect(await reservePort("TEST_DEV_PORT", { TEST_DEV_PORT: "5173" }, find)).toBe(5173); expect(find).not.toHaveBeenCalled(); for (const value of ["0", "65536", "-1", "4173x", "abc", "41.5"]) { await expect(reservePort("TEST_DEV_PORT", { TEST_DEV_PORT: value }, find), value) .rejects.toThrow(/TEST_DEV_PORT/u); } }); }); describe("generated module specifiers", () => { it("keep today's specifier in .generated and reach src from a deeper root", () => { expect( moduleSpecifier(".generated/frontend-runtime/contract-set.ts", "src/contracts/contract-set.ts"), ).toBe("../../src/contracts/contract-set.ts"); expect( moduleSpecifier( ".tmp/runs/a/.generated/frontend-runtime/contract-set.ts", "src/contracts/contract-set.ts", ), ).toBe("../../../../../src/contracts/contract-set.ts"); }); });