// Unit tests for the per-worker server sets (e2e/servers.ts), the data roots // (e2e/testPaths.ts) and the @serial list (e2e/serial.ts). Named `.unit.ts` so // Playwright's own testMatch never collects it; run by `pnpm --filter editor test`. import assert from "node:assert/strict"; import { existsSync } from "node:fs"; import { join } from "node:path"; import test from "node:test"; import { SERIAL_SPECS, SERIAL_SPEC_PATTERN } from "./serial"; import { isFocusedRun, resolveWorkers, slotPorts, slotServers } from "./servers"; import { EDITOR_ROOT, slotRoot, testPaths } from "./testPaths"; test("resolveWorkers: 1 by default, a number capped at 8, auto from memory capped at 3", () => { assert.equal(resolveWorkers({}), 1); assert.equal(resolveWorkers({ E2E_WORKERS: "2" }), 2); assert.equal(resolveWorkers({ E2E_WORKERS: "99" }), 8); assert.throws(() => resolveWorkers({ E2E_WORKERS: "two" }), /a number of workers, or auto/); const auto = (mb: number | null, env: Record = {}) => resolveWorkers({ E2E_WORKERS: "auto", ...env }, () => mb); assert.equal(auto(12_000), 3); // (12000 − 6000) / 1500 = 4, capped assert.equal(auto(9_100), 2); assert.equal(auto(7_400), 1); assert.equal(auto(3_000), 1); assert.equal(auto(null), 1); assert.equal(auto(9_100, { HEAVY_MIN_FREE_MB: "3000" }), 3); }); test("slotPorts: slot 0 is the suite's own; slot n takes three from the top of the worktree's block", () => { assert.deepEqual(slotPorts(0, {}), { editor: 3011, export: 3010, ollama: 11435 }); assert.deepEqual(slotPorts(1, {}), { editor: 3060, export: 3061, ollama: 3062 }); assert.deepEqual(slotPorts(2, {}), { editor: 3063, export: 3064, ollama: 3065 }); // A worktree at offset 2000 (PORT=5011, the injector's): its own block. const wt = { PORT: "5011", EXPORT_PORT: "5010", OLLAMA_STUB_PORT: "13435" }; assert.deepEqual(slotPorts(0, wt), { editor: 5011, export: 5010, ollama: 13435 }); // 5060 and 5061 are bad ports (SIP): `next start` refuses them. assert.deepEqual(slotPorts(1, wt), { editor: 5062, export: 5063, ollama: 5064 }); assert.deepEqual(slotPorts(3, wt), { editor: 5068, export: 5069, ollama: 5070 }); // Eight slots stay inside the block of a hundred, past every bad port. assert.ok(slotPorts(8, {}).ollama < 3100); const wt66 = { PORT: "9611" }; // worktree #66: 6665–6669 are bad for (let s = 1; s <= 8; s++) { const p = slotPorts(s, { PORT: "6611" }); assert.ok(![p.editor, p.export, p.ollama].some((n) => n >= 6665 && n <= 6669), `slot ${s}`); assert.ok(p.ollama < 6700, `slot ${s} left the block`); } assert.ok(slotPorts(1, wt66).editor === 9660); }); test("slotServers: slot 0 is the webServer of before; slot n is its own data root, ports and dist dirs", () => { const [editor0, export0, ollama0] = slotServers(0, "start", {}); assert.equal(editor0.command, "pnpm start:test"); assert.equal(editor0.url, "http://localhost:3011"); assert.equal(editor0.env.E2E_NEXT_DIST_DIR, ".next/e2e"); assert.equal(editor0.env.E2E_DATA_ROOT, undefined); assert.equal(editor0.env.PORT, undefined); assert.equal(export0.env.SETTINGS_FILE, join(EDITOR_ROOT, "test-settings.json")); assert.equal(export0.env.E2E_NEXT_DIST_DIR, ".next/e2e-editor"); assert.equal(ollama0.url, "http://127.0.0.1:11435/api/tags"); const [editor2, export2, ollama2] = slotServers(2, "dev", {}); assert.equal(editor2.command, "pnpm dev:test"); assert.equal(editor2.env.E2E_DATA_ROOT, join(EDITOR_ROOT, ".e2e-workers", "w2")); assert.equal(editor2.env.PORT, "3063"); assert.equal(editor2.env.OLLAMA_STUB_PORT, "3065"); assert.equal(editor2.env.E2E_NEXT_DIST_DIR, ".next/e2e-dev-w2"); assert.equal(export2.command, "pnpm --filter export run dev --port 3064"); assert.equal(export2.env.SETTINGS_FILE, join(EDITOR_ROOT, ".e2e-workers", "w2", "test-settings.json")); assert.equal(export2.env.E2E_NEXT_DIST_DIR, ".next/e2e-editor-w2"); assert.equal(ollama2.env.OLLAMA_STUB_PORT, "3065"); // A start-mode slot serves the one stamped build. assert.equal(slotServers(2, "start", {})[0].env.E2E_NEXT_DIST_DIR, ".next/e2e"); }); test("testPaths: the editor directory for slot 0, the slot's own root for a worker, E2E_DATA_ROOT over both", () => { const saved = { idx: process.env.TEST_PARALLEL_INDEX, root: process.env.E2E_DATA_ROOT }; try { delete process.env.E2E_DATA_ROOT; delete process.env.TEST_PARALLEL_INDEX; assert.equal(testPaths().transcriptsDir, join(EDITOR_ROOT, "test-transcripts")); process.env.TEST_PARALLEL_INDEX = "0"; assert.equal(testPaths().settingsFile, join(EDITOR_ROOT, "test-settings.json")); process.env.TEST_PARALLEL_INDEX = "2"; assert.equal(testPaths().root, slotRoot(2)); assert.equal(testPaths().transcriptsDir, join(EDITOR_ROOT, ".e2e-workers", "w2", "test-transcripts")); process.env.E2E_DATA_ROOT = "/somewhere/else"; assert.equal(testPaths().settingsFile, "/somewhere/else/test-settings.json"); } finally { if (saved.idx == null) delete process.env.TEST_PARALLEL_INDEX; else process.env.TEST_PARALLEL_INDEX = saved.idx; if (saved.root == null) delete process.env.E2E_DATA_ROOT; else process.env.E2E_DATA_ROOT = saved.root; } }); test("the @serial list names spec files that exist, and matches whole names only", () => { for (const f of SERIAL_SPECS) assert.ok(existsSync(join(EDITOR_ROOT, "e2e", f)), f); assert.ok(SERIAL_SPEC_PATTERN.test("/x/editor/e2e/pacing.spec.ts")); assert.ok(!SERIAL_SPEC_PATTERN.test("/x/editor/e2e/my-pacing.spec.ts")); assert.ok(!SERIAL_SPEC_PATTERN.test("/x/editor/e2e/build.spec.tsx")); }); test("isFocusedRun: a spec file or a filter narrows the run; options with values do not", () => { const pw = (...args: string[]) => ["node", "/x/playwright/cli.js", "test", ...args]; assert.equal(isFocusedRun(pw()), false); assert.equal(isFocusedRun(pw("--workers", "2", "--reporter", "list", "--retries=1")), false); assert.equal(isFocusedRun(pw("-u", "all", "--debug", "--headed")), false); assert.equal(isFocusedRun(pw("pacing.spec.ts")), true); assert.equal(isFocusedRun(pw("--workers", "2", "pacing.spec.ts", "whisper.spec.ts")), true); assert.equal(isFocusedRun(pw("-g", "lane")), true); assert.equal(isFocusedRun(pw("--grep=lane")), true); assert.equal(isFocusedRun(pw("--last-failed")), true); assert.equal(isFocusedRun(pw("--project", "serial")), true); assert.equal(isFocusedRun(["node", "something-else"]), true); // unsure: focused });