// Unit tests for the dev proxy's port resolution (#2650). // // The backend honours `PORT` while Vite's proxy targeted a literal `localhost:3001`, // so `PORT=3100 yarn dev` moved only the server — and with a first instance still on // 3001, the second browser silently rendered the FIRST instance's data. // // The rule under test is therefore an EQUIVALENCE, not a validator: for any value, // the proxy must land on the port the backend binds. So the table below asserts // against `env.port`'s own coercion (`asInt` + `PORT_RANGE`) rather than against a // second opinion written here — a stricter parser was the finding Codex raised on // iter-1, and this is what makes a future divergence fail loudly. import { describe, it } from "node:test"; import assert from "node:assert/strict"; import { assertProxyablePort, DEFAULT_SERVER_PORT, describeRejection, parseServerPort, resolveProxyTarget, resolveServerPort, serverOrigins, type PortResolution, } from "../../scripts/lib/devServerPort.js"; // The backend's own coercion and range, and the launcher's `.env` parser — the two // authorities this module borrows from instead of reimplementing. import { asInt, PORT_RANGE } from "../../server/utils/envCoerce.js"; import { parseEnvFile } from "../../server/utils/launch-env.mjs"; const serverWouldBind = (raw: string | undefined) => asInt(raw, DEFAULT_SERVER_PORT, PORT_RANGE); describe("parseServerPort agrees with the port the backend would bind", () => { // Every form `Number()` accepts, which is what the backend uses. A parser built on // `/^\d+$/` rejects the last four and silently proxies to the default instead. for (const raw of ["3100", " 3100 ", "+3100", "3100.0", "0x1f", "1e3", "65535", "1"]) { it(`resolves ${JSON.stringify(raw)} to the same port as the server`, () => { const { port } = parseServerPort(raw); assert.equal(port, serverWouldBind(raw), `proxy and backend must agree on ${raw}`); }); } // Values the backend itself ignores: it falls back to 3001, and so must the proxy. for (const raw of ["", "abc", "3100.5", "-1", "65536", "3100abc", undefined, null]) { it(`treats ${JSON.stringify(raw)} as unusable, exactly as the server does`, () => { const { port, reason } = parseServerPort(raw); assert.equal(port, null); assert.equal(reason, "ignored-by-server"); assert.equal(serverWouldBind(raw ?? undefined), DEFAULT_SERVER_PORT); }); } // `Number(" ")` is 0, so a whitespace-only PORT makes the BACKEND take an // ephemeral port. Surprising, and worth pinning: the proxy must refuse it for the // ephemeral reason, not report it as junk the server ignored. it("treats a whitespace-only PORT the way the server does — as 0, i.e. ephemeral", () => { const { port, reason } = parseServerPort(" "); assert.equal(port, null); assert.equal(reason, "ephemeral"); assert.equal(serverWouldBind(" "), 0); }); // The one place the two CANNOT agree, so it is refused by name rather than // silently mapped to 3001: the backend accepts 0 and lets the OS pick, and no // value computed at Vite-config time can know which port that turned out to be. it("refuses PORT=0 as ephemeral rather than pretending it is the default", () => { const { port, reason } = parseServerPort("0"); assert.equal(port, null); assert.equal(reason, "ephemeral"); assert.equal(serverWouldBind("0"), 0, "the backend does accept 0 — that is why this is a named refusal"); assert.match(describeRejection("ephemeral"), /ephemeral/); }); }); // `.env` is parsed by the launcher's `parseEnvFile` (dotenv), not by this module. // These pin the pipeline end to end, because a hand-rolled parser here was the // divergence risk: dotenv strips inline comments, honours `export `, and unquotes — // get any of those wrong and the server reads 3100 while the proxy reads 3001, which // is this very bug one level down (observed during review, not flagged by a bot). const portFromEnvText = (text: string): number => { const { parsed } = parseEnvFile("(memory)", { readFileSync: () => text }); return resolveServerPort({ processEnv: {}, envFileValues: parsed }).port; }; describe(".env values, parsed the way the server parses them", () => { it("reads a plain assignment", () => { assert.equal(portFromEnvText("PORT=3100"), 3100); }); it("survives an inline comment — the case a hand-rolled parser gets wrong", () => { assert.equal(portFromEnvText("PORT=3100 # scratch instance"), 3100); }); it("honours an `export ` prefix", () => { assert.equal(portFromEnvText("export PORT=3100"), 3100); }); it("unquotes", () => { assert.equal(portFromEnvText('PORT="3100"'), 3100); assert.equal(portFromEnvText("PORT='3100'"), 3100); }); it("ignores a commented-out assignment", () => { assert.equal(portFromEnvText("# PORT=3100"), DEFAULT_SERVER_PORT); }); it("does not match a key that merely ends in PORT", () => { assert.equal(portFromEnvText("MY_PORT=3200"), DEFAULT_SERVER_PORT); }); it("takes the last assignment", () => { assert.equal(portFromEnvText("PORT=3100\nPORT=3200"), 3200); }); it("finds PORT among other keys", () => { assert.equal(portFromEnvText("GEMINI_API_KEY=abc\nPORT=3100\nMULMOCLAUDE_WORKSPACE_PATH=/tmp/ws"), 3100); }); it("falls back when the file has no PORT, or none that is usable", () => { assert.equal(portFromEnvText("OTHER=1"), DEFAULT_SERVER_PORT); assert.equal(portFromEnvText("PORT=nonsense"), DEFAULT_SERVER_PORT); }); it("treats an unreadable file as no file", () => { const { parsed } = parseEnvFile("/definitely/not/here/.env"); assert.equal(resolveServerPort({ processEnv: {}, envFileValues: parsed }).port, DEFAULT_SERVER_PORT); }); }); describe("resolveServerPort — the server's own precedence", () => { const portOf = (sources: Parameters[0]) => resolveServerPort(sources).port; it("defaults to the backend's own default when nothing is set", () => { assert.equal(resolveServerPort().port, DEFAULT_SERVER_PORT); assert.deepEqual(resolveServerPort({ processEnv: {}, envFileValues: {} }), { port: DEFAULT_SERVER_PORT, problems: [] }); }); it("takes PORT from the environment", () => { assert.equal(portOf({ processEnv: { PORT: "3100" } }), 3100); }); it("uses .env only when the shell has no PORT key — the split this bug was made of", () => { assert.equal(portOf({ processEnv: {}, envFileValues: { PORT: "3100" } }), 3100); }); it("lets the shell win over .env", () => { assert.equal(portOf({ processEnv: { PORT: "3100" }, envFileValues: { PORT: "3200" } }), 3100); }); // dotenv's no-override rule keys off PRESENCE, not usefulness: `mergeLaunchEnv` // skips `.env` whenever the shell has the key at all. So an unusable shell value // sends the BACKEND to 3001 — and a resolver that "kept looking" would have aimed // the proxy at `.env`'s port instead, which is the same split one branch over // (Codex, #2653). it("does NOT fall through to .env when the shell PORT is present but unusable", () => { const resolution = resolveServerPort({ processEnv: { PORT: "nonsense" }, envFileValues: { PORT: "3200" } }); assert.equal(resolution.port, DEFAULT_SERVER_PORT, "the backend lands on the default, so the proxy must too"); assert.deepEqual(resolution.problems, [{ source: "PORT", raw: "nonsense", reason: "ignored-by-server" }]); }); it("does NOT fall through to .env for an EMPTY shell PORT either", () => { const resolution = resolveServerPort({ processEnv: { PORT: "" }, envFileValues: { PORT: "3200" } }); assert.equal(resolution.port, DEFAULT_SERVER_PORT); assert.deepEqual(resolution.problems, [], "an empty value is the server's own idea of unset — nothing to report"); }); // `undefined` is absence, not presence: `mergeLaunchEnv` requires the value to be // defined before it shadows, so `.env` applies here. it("treats PORT=undefined as absent, so .env applies", () => { assert.equal(portOf({ processEnv: { PORT: undefined }, envFileValues: { PORT: "3200" } }), 3200); }); // Was silent before: `raw.trim()` gated the report, so a whitespace-only value — // which the backend coerces to 0 — was refused with nothing said (Codex / CodeRabbit). it("reports a whitespace-only PORT rather than refusing it in silence", () => { const resolution = resolveServerPort({ processEnv: { PORT: " " } }); assert.deepEqual(resolution.problems, [{ source: "PORT", raw: " ", reason: "ephemeral" }]); }); it("labels the source as .env when that is where the value came from", () => { const resolution = resolveServerPort({ processEnv: {}, envFileValues: { PORT: "70000" } }); assert.deepEqual(resolution.problems, [{ source: ".env PORT", raw: "70000", reason: "ignored-by-server" }]); assert.equal(resolution.port, DEFAULT_SERVER_PORT); }); }); // A warning was not enough for `PORT=0`: the proxy fell through to another source // (or `:3001`), the page loaded, and the client talked to whatever else was // listening there — the silent mis-wiring this module exists to prevent. The dev // server has to refuse to start (Codex, #2653). describe("assertProxyablePort", () => { it("passes when every source was usable", () => { assert.doesNotThrow(() => assertProxyablePort(resolveServerPort({ processEnv: { PORT: "3100" } }))); assert.doesNotThrow(() => assertProxyablePort(resolveServerPort({ processEnv: {} }))); }); it("passes when the only problem is a value the server itself ignores", () => { assert.doesNotThrow(() => assertProxyablePort(resolveServerPort({ processEnv: { PORT: "nonsense" } }))); }); it("throws for PORT=0, naming the port and what to do", () => { assert.throws(() => assertProxyablePort(resolveServerPort({ processEnv: { PORT: "0" } })), /OS-assigned port.*PORT=3100/s); }); it("throws for a whitespace-only PORT, which coerces to 0", () => { assert.throws(() => assertProxyablePort(resolveServerPort({ processEnv: { PORT: " " } })), /OS-assigned port/); }); // `.env` cannot rescue it — the shell key shadows the file, so the BACKEND is on // an ephemeral port no matter what the file says. it("throws for a shell PORT=0 even when .env carries a usable port", () => { const resolution = resolveServerPort({ processEnv: { PORT: "0" }, envFileValues: { PORT: "3200" } }); assert.equal(resolution.port, DEFAULT_SERVER_PORT); assert.throws(() => assertProxyablePort(resolution), /OS-assigned port/); }); it("throws for an ephemeral value that came from .env", () => { const resolution = resolveServerPort({ processEnv: {}, envFileValues: { PORT: "0" } }); assert.throws(() => assertProxyablePort(resolution), /\.env PORT="0"/); }); }); describe("serverOrigins", () => { it("builds both origins from one port, so the proxy entries cannot drift", () => { assert.deepEqual(serverOrigins(3100), { http: "http://127.0.0.1:3100", ws: "ws://127.0.0.1:3100" }); }); it("keeps the default shape unchanged", () => { assert.deepEqual(serverOrigins(DEFAULT_SERVER_PORT), { http: "http://127.0.0.1:3001", ws: "ws://127.0.0.1:3001" }); }); // Not `localhost`. The backend binds the IPv4 loopback explicitly, while // `localhost` resolves to `::1` first on a dual-stack host — usually harmless // (refused, falls back to IPv4) but silently fatal when something else IS on // `::1:`, because the proxy connects there and never falls back. // Observed while verifying #2981: an unrelated process on `*:3002` answered // 404 through `localhost:3002` while this backend answered 200 through // `127.0.0.1:3002`. it("names the address the backend actually binds, not a name that may resolve elsewhere", () => { Object.values(serverOrigins(3001)).forEach((origin) => { assert.match(origin, /\/\/127\.0\.0\.1:/); assert.doesNotMatch(origin, /localhost/); }); }); }); // #2981 — the proxy FOLLOWS the port the backend actually bound. // // `PORT` says what the backend was asked for; `.server-port` says what it got. // Those differ exactly when the request could not be honoured, which is #2650: // an implicit default that was busy, walked forward by `server/index.ts` while // the client kept addressing the port nobody was on. describe("resolveProxyTarget", () => { const envOnly = (port: number): PortResolution => ({ port, problems: [] }); it("a published port wins over what PORT asked for — that is the whole fix", () => { assert.deepEqual(resolveProxyTarget("3002\n", envOnly(3001)), { port: 3002, source: "published" }); }); it("agreement resolves the same way, just attributed to the publish", () => { assert.deepEqual(resolveProxyTarget("3001\n", envOnly(3001)), { port: 3001, source: "published" }); }); it("nothing published falls back to PORT — a client with no backend behind it", () => { assert.deepEqual(resolveProxyTarget(null, envOnly(3001)), { port: 3001, source: "env" }); }); it("an unusable file falls back rather than resolving to NaN or 0", () => { // An empty file, a half-finished write, a build too old to publish at all. ["", " ", "\n", "not-a-port", "0", "-1", "99999999"].forEach((raw) => { assert.deepEqual(resolveProxyTarget(raw, envOnly(3001)), { port: 3001, source: "env" }, `expected "${raw}" to fall back`); }); }); it("borrows the server's own coercion, so odd-but-valid spellings resolve alike", () => { // `asInt` + PORT_RANGE is what the backend itself uses, so `0x1f`-style // values must not be treated as ports here while the backend accepts them // there (or vice versa). const raw = "3100.0"; assert.deepEqual(resolveProxyTarget(raw, envOnly(3001)), { port: parseServerPort(raw).port ?? 3001, source: "published" }); }); }); describe("assertProxyablePort with a published target", () => { const ephemeral = resolveServerPort({ processEnv: { PORT: "0" } }); it("still refuses PORT=0 when nothing published — the port is unknowable", () => { assert.throws(() => assertProxyablePort(ephemeral, { port: 3001, source: "env" }), /OS-assigned port/); }); it("allows PORT=0 once the backend has published — it is knowable now (#2981)", () => { assert.doesNotThrow(() => assertProxyablePort(ephemeral, { port: 51234, source: "published" })); }); it("keeps refusing when called without a target at all", () => { assert.throws(() => assertProxyablePort(ephemeral), /OS-assigned port/); }); });