import { spawn } from "node:child_process"; import { createServer, type Server } from "node:http"; import type { AddressInfo } from "node:net"; import path from "node:path"; import { afterEach, describe, expect, it } from "vitest"; import { assertPortUnused, assertServedRelease, previewArguments, summarizeSamples, } from "../../scripts/lib/lab-performance.ts"; /** * 11-07. The lab performance gate measured once, and measured whatever server * held its port: `vite preview` without `--strictPort` moved elsewhere and the * script never checked which build answered. */ const servers: Server[] = []; afterEach(async () => { await Promise.all( servers.splice(0).map( (server) => new Promise((resolve) => server.close(() => resolve())), ), ); }); async function foreignServer(manifest: unknown): Promise { const server = createServer((_request, response) => { response .writeHead(200, { "content-type": "application/json" }) .end(JSON.stringify(manifest)); }); servers.push(server); await new Promise((resolve) => server.listen(0, "127.0.0.1", () => resolve())); return `http://127.0.0.1:${(server.address() as AddressInfo).port}`; } describe("lab performance gate (11-07)", () => { it("starts the preview server on exactly the requested port", () => { expect(previewArguments(4173)).toEqual([ "pnpm", "preview", "--host", "127.0.0.1", "--port", "4173", "--strictPort", ]); }); it("summarizes repeated runs by their median and keeps every sample", () => { expect(summarizeSamples([326, 1103, 410, 390, 505])).toEqual({ median: 410, min: 326, max: 1103, samples: [326, 1103, 410, 390, 505], }); expect(summarizeSamples([1, 4, 2, 3]).median).toBe(2.5); expect(() => summarizeSamples([])).toThrow(RangeError); }); it("refuses a port that another server already answers on", async () => { const baseUrl = await foreignServer({}); await expect(assertPortUnused(baseUrl)).rejects.toThrow(/another server/u); await expect(assertPortUnused("http://127.0.0.1:9")).resolves.toBeUndefined(); }); it("refuses a server that serves a different build", async () => { const baseUrl = await foreignServer({ buildId: "other", releaseId: "r1" }); await expect( assertServedRelease(baseUrl, { buildId: "measured", releaseId: "r1" }), ).rejects.toThrow(/serves build other\/r1, not the measured measured\/r1/u); await expect( assertServedRelease(baseUrl, { buildId: "other", releaseId: "r1" }), ).resolves.toBeUndefined(); }); it("fails the gate before measuring when its port is taken", async () => { const baseUrl = await foreignServer({ buildId: "other", releaseId: "r1" }); const child = spawn( process.execPath, [path.resolve("scripts/test-performance.ts")], { env: { ...process.env, LAB_PERFORMANCE_PORT: new URL(baseUrl).port }, stdio: ["ignore", "ignore", "pipe"], }, ); let stderr = ""; child.stderr.on("data", (chunk: Buffer) => { stderr += chunk.toString("utf8"); }); const code = await new Promise((resolve) => { const timer = setTimeout(() => child.kill("SIGKILL"), 20_000); child.on("exit", (exitCode) => { clearTimeout(timer); resolve(exitCode); }); }); expect(code).not.toBe(0); expect(stderr).toMatch(/another server holds the port/u); }, 30_000); });