Skip to content

Commit bb80837

Browse files
cliffhallclaude
andcommitted
ci: address review on the headless-browser boot smoke
- Don't fail on benign console.error (CDN font loads, React warnings) — the #1612 class arrives as an uncaught pageerror. console errors are now diagnostics, hard-failing only when they carry the externalization signature. - Broaden the externalization regex to non-prefixed built-ins ("fs" as well as "node:fs") and label the signature on both the pageerror and console channels. - Rename the port override to SMOKE_WEB_BROWSER_PORT so it doesn't collide with smoke:web's SMOKE_WEB_PORT. - Add a settle window (networkidle + 500ms) so a late-evaluating lazy chunk that throws is caught, and race the render against launcher death so a mid-load crash reports the real cause instead of a render timeout. - Print an actionable hint when Playwright/chromium can't launch (missing --with-deps system libs). - Extract the shared spawn/readiness boilerplate into scripts/lib/prod-web-server.mjs so smoke-web and smoke-web-browser can't drift. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01XNdjEPKLG637X8YmhDiEk5
1 parent acd3f98 commit bb80837

3 files changed

Lines changed: 235 additions & 137 deletions

File tree

‎scripts/lib/prod-web-server.mjs‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* Shared boot/readiness helper for the prod web smokes.
3+
*
4+
* Both `scripts/smoke-web.mjs` (serves-the-HTML check) and
5+
* `scripts/smoke-web-browser.mjs` (runs-the-bundle check, #1615) boot the *same*
6+
* prod `mcp-inspector --web` server, so the spawn + readiness-poll boilerplate
7+
* lives here once instead of being copy-pasted (and drifting) in each script.
8+
*
9+
* Repo-root paths are derived from import.meta.url, so a caller's cwd (e.g.
10+
* `smoke:web:browser` does `cd clients/web` first so `import("playwright")`
11+
* resolves) doesn't affect which launcher/build tree is exercised.
12+
*/
13+
14+
import { spawn } from "node:child_process";
15+
import { setTimeout as delay } from "node:timers/promises";
16+
import { fileURLToPath } from "node:url";
17+
import { dirname, resolve } from "node:path";
18+
19+
const libDir = dirname(fileURLToPath(import.meta.url));
20+
const repoRoot = resolve(libDir, "..", "..");
21+
const launcherEntry = resolve(repoRoot, "clients/launcher/build/index.js");
22+
23+
/**
24+
* Spawn `mcp-inspector --web` (prod, no `--dev`) against the built
25+
* `clients/web/dist` and return handles for readiness + teardown.
26+
*
27+
* @param {object} opts
28+
* @param {string} opts.host
29+
* @param {string} opts.port
30+
* @param {string} opts.token value injected as MCP_INSPECTOR_API_TOKEN
31+
*/
32+
export function startProdWebServer({ host, port, token }) {
33+
const baseUrl = `http://${host}:${port}`;
34+
35+
const child = spawn(process.execPath, [launcherEntry, "--web"], {
36+
env: {
37+
...process.env,
38+
CLIENT_PORT: port,
39+
HOST: host,
40+
MCP_INSPECTOR_API_TOKEN: token,
41+
// Don't pop a browser in CI.
42+
MCP_AUTO_OPEN_ENABLED: "false",
43+
},
44+
stdio: ["ignore", "inherit", "inherit"],
45+
});
46+
47+
let exited = false;
48+
let exitCode = null;
49+
child.on("exit", (code) => {
50+
exited = true;
51+
exitCode = code;
52+
});
53+
54+
/**
55+
* Poll `GET /` until the server answers with an ok status. Rejects early if
56+
* the launcher exits before serving (so a boot failure surfaces immediately
57+
* instead of after the full timeout). Resolves with the first ok Response.
58+
*/
59+
async function waitForReady({ attempts = 120, intervalMs = 500 } = {}) {
60+
for (let attempt = 0; attempt < attempts; attempt++) {
61+
if (exited) {
62+
throw new Error(
63+
`launcher exited (code ${exitCode}) before serving — see output above`,
64+
);
65+
}
66+
try {
67+
const res = await fetch(`${baseUrl}/`);
68+
if (res.ok) return res;
69+
} catch {
70+
// not listening yet
71+
}
72+
await delay(intervalMs);
73+
}
74+
throw new Error(
75+
`server did not start within ${(attempts * intervalMs) / 1000}s`,
76+
);
77+
}
78+
79+
/**
80+
* A promise that rejects when the launcher process exits — race it against
81+
* page-load work so a mid-flight server death is reported as the real cause
82+
* instead of a downstream render timeout. Never resolves; if the server stays
83+
* up it simply stays pending until the process exits.
84+
*/
85+
function whenChildExits() {
86+
return new Promise((_resolve, reject) => {
87+
const onExit = (code) =>
88+
reject(
89+
new Error(
90+
`launcher exited (code ${code ?? exitCode}) mid-run — see output above`,
91+
),
92+
);
93+
if (exited) onExit(exitCode);
94+
else child.once("exit", onExit);
95+
});
96+
}
97+
98+
return {
99+
baseUrl,
100+
child,
101+
isExited: () => exited,
102+
exitCode: () => exitCode,
103+
waitForReady,
104+
whenChildExits,
105+
stop: () => {
106+
if (!exited) child.kill("SIGTERM");
107+
},
108+
};
109+
}

‎scripts/smoke-web-browser.mjs‎

Lines changed: 111 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -16,51 +16,47 @@
1616
* time: it launches `mcp-inspector --web` (prod, no `--dev`) against the built
1717
* `clients/web/dist`, opens the served page in headless Chromium (Playwright,
1818
* already a clients/web devDependency for the Storybook tests), and asserts the
19-
* app renders its first meaningful frame (the "Add Servers" control) with **no
20-
* uncaught page errors** — in particular no `node:*` externalization error.
19+
* app renders its first meaningful frame (the "Add Servers" control).
20+
*
21+
* Failure signal: an **uncaught `pageerror`** — that's how the #1612 class
22+
* arrives (Vite's `__vite-browser-external` proxy *throws* on property access).
23+
* A `console.error` is NOT treated as a hard failure by itself, because the
24+
* console is also where Chromium reports benign things a boot smoke shouldn't
25+
* fail on: a failed subresource load (e.g. the Google-Fonts `<link>` in
26+
* index.html on a network-restricted box) or a React key/prop warning. Console
27+
* errors are collected and printed as diagnostics, and only fail the run when
28+
* they carry the externalized-module signature.
2129
*
2230
* Run from the clients/web directory (`npm run smoke:web:browser` does
2331
* `cd clients/web` first) so `import("playwright")` resolves against that
24-
* client's node_modules. Repo-root paths below are derived from import.meta.url,
25-
* so the cwd change doesn't affect which launcher/build tree is exercised.
32+
* client's node_modules. Repo-root paths are derived inside the shared helper
33+
* from import.meta.url, so the cwd change doesn't affect which build tree runs.
2634
*
2735
* Expects `clients/web/dist` and `clients/launcher/build` to be built first —
2836
* the validate / CI ordering guarantees this.
2937
*/
3038

31-
import { spawn } from "node:child_process";
3239
import { setTimeout as delay } from "node:timers/promises";
33-
import { fileURLToPath } from "node:url";
34-
import { dirname, resolve } from "node:path";
35-
36-
const scriptDir = dirname(fileURLToPath(import.meta.url));
37-
const repoRoot = resolve(scriptDir, "..");
38-
const launcherEntry = resolve(repoRoot, "clients/launcher/build/index.js");
40+
import { startProdWebServer } from "./lib/prod-web-server.mjs";
3941

4042
const HOST = "127.0.0.1";
41-
const PORT = process.env.SMOKE_WEB_PORT ?? "6298";
43+
// Distinct from smoke:web's SMOKE_WEB_PORT so overriding one doesn't make both
44+
// back-to-back smokes bind the same port (→ EADDRINUSE on the second).
45+
const PORT = process.env.SMOKE_WEB_BROWSER_PORT ?? "6298";
4246
const TOKEN = "smoke-web-browser-token";
43-
const BASE_URL = `http://${HOST}:${PORT}`;
4447

45-
const child = spawn(process.execPath, [launcherEntry, "--web"], {
46-
env: {
47-
...process.env,
48-
CLIENT_PORT: PORT,
49-
HOST,
50-
MCP_INSPECTOR_API_TOKEN: TOKEN,
51-
// Don't pop a browser in CI.
52-
MCP_AUTO_OPEN_ENABLED: "false",
53-
},
54-
stdio: ["ignore", "inherit", "inherit"],
55-
});
48+
// Vite emits `Module "${id}" has been externalized for browser compatibility…`
49+
// where `id` is whatever was imported — `node:fs` OR a bare `fs` (both common
50+
// in transitive deps). Match either so the diagnostic label always attaches.
51+
const EXTERNALIZED = /Module "[^"]+" has been externalized/;
5652

57-
let exited = false;
58-
let exitCode = null;
59-
child.on("exit", (code) => {
60-
exited = true;
61-
exitCode = code;
62-
});
53+
function labelExternalized(message) {
54+
return EXTERNALIZED.test(message)
55+
? `Node built-in reached the browser bundle: ${message}`
56+
: message;
57+
}
6358

59+
const server = startProdWebServer({ host: HOST, port: PORT, token: TOKEN });
6460
let browser = null;
6561

6662
async function shutdown() {
@@ -72,7 +68,7 @@ async function shutdown() {
7268
}
7369
browser = null;
7470
}
75-
if (!exited) child.kill("SIGTERM");
71+
server.stop();
7672
}
7773

7874
async function fail(message) {
@@ -81,78 +77,109 @@ async function fail(message) {
8177
process.exit(1);
8278
}
8379

84-
async function waitForServer() {
85-
for (let attempt = 0; attempt < 120; attempt++) {
86-
if (exited) {
87-
throw new Error(
88-
`launcher exited (code ${exitCode}) before serving — see output above`,
89-
);
90-
}
91-
try {
92-
const res = await fetch(`${BASE_URL}/`);
93-
if (res.ok) return;
94-
} catch {
95-
// not listening yet
96-
}
97-
await delay(500);
80+
async function loadChromium() {
81+
let playwright;
82+
try {
83+
playwright = await import("playwright");
84+
} catch (err) {
85+
throw new Error(
86+
`could not load Playwright — run \`npx playwright install --with-deps chromium\` (${err instanceof Error ? err.message : String(err)})`,
87+
);
9888
}
99-
throw new Error("server did not start within 60s");
100-
}
101-
102-
// A page error whose message mentions a node: built-in being externalized is
103-
// the #1612 signature; treat *any* uncaught page error as a failure, but call
104-
// this class out explicitly since it's the regression this smoke exists for.
105-
function describeError(err) {
106-
const message = err instanceof Error ? err.message : String(err);
107-
if (/Module "node:.*" has been externalized/.test(message)) {
108-
return `Node built-in reached the browser bundle: ${message}`;
89+
try {
90+
return await playwright.chromium.launch({ headless: true });
91+
} catch (err) {
92+
throw new Error(
93+
`chromium failed to launch — on a bare Linux box run \`npx playwright install --with-deps chromium\` for the system libraries (${err instanceof Error ? err.message : String(err)})`,
94+
);
10995
}
110-
return message;
11196
}
11297

11398
try {
114-
const { chromium } = await import("playwright");
115-
await waitForServer();
116-
117-
browser = await chromium.launch({ headless: true });
99+
await server.waitForReady();
100+
browser = await loadChromium();
118101
const page = await browser.newPage();
119102

103+
// Uncaught page errors are the hard failure (the #1612 signature lands here).
120104
const pageErrors = [];
121-
page.on("pageerror", (err) => pageErrors.push(describeError(err)));
105+
// Console errors are diagnostic unless they carry the externalization
106+
// signature (see the header comment for why they're not a blanket failure).
107+
const consoleErrors = [];
108+
page.on("pageerror", (err) =>
109+
pageErrors.push(err instanceof Error ? err.message : String(err)),
110+
);
122111
page.on("console", (msg) => {
123-
if (msg.type() === "error") pageErrors.push(`console.error: ${msg.text()}`);
112+
if (msg.type() === "error") consoleErrors.push(msg.text());
124113
});
125114

126-
// Token is injected into index.html by the prod server, so a bare `/` load
127-
// authenticates without a query param.
128-
const response = await page.goto(BASE_URL, {
129-
waitUntil: "domcontentloaded",
130-
timeout: 30_000,
131-
});
132-
if (!response || !response.ok()) {
115+
const render = async () => {
116+
// Token is injected into index.html by the prod server, so a bare `/` load
117+
// authenticates without a query param.
118+
const response = await page.goto(server.baseUrl, {
119+
waitUntil: "domcontentloaded",
120+
timeout: 30_000,
121+
});
122+
if (!response || !response.ok()) {
123+
throw new Error(
124+
`GET / returned HTTP ${response ? response.status() : "no response"}`,
125+
);
126+
}
127+
// First meaningful frame: the always-present "Add Servers" control.
128+
await page
129+
.getByRole("button", { name: /Add Servers/ })
130+
.waitFor({ state: "visible", timeout: 30_000 });
131+
// Settle window: let lazily-evaluated chunks that throw a tick after first
132+
// paint surface before we assert a clean boot. networkidle is best-effort
133+
// (the Google-Fonts request may never idle on a restricted network).
134+
await page
135+
.waitForLoadState("networkidle", { timeout: 5_000 })
136+
.catch(() => {});
137+
await delay(500);
138+
};
139+
140+
// Race the render against launcher death so a mid-load server crash is
141+
// reported as the real cause instead of a 30s render timeout.
142+
try {
143+
await Promise.race([server.whenChildExits(), render()]);
144+
} catch (err) {
145+
const diagnostics = [
146+
...pageErrors.map(labelExternalized),
147+
...consoleErrors.map((m) => `console: ${labelExternalized(m)}`),
148+
];
133149
await fail(
134-
`GET / returned HTTP ${response ? response.status() : "no response"}`,
150+
`${err instanceof Error ? err.message : String(err)}${
151+
diagnostics.length
152+
? ` — page diagnostics: ${diagnostics.join("; ")}`
153+
: ""
154+
}`,
135155
);
136156
}
137157

138-
// First meaningful frame: the always-present "Add Servers" control.
139-
await page
140-
.getByRole("button", { name: /Add Servers/ })
141-
.waitFor({ state: "visible", timeout: 30_000 })
142-
.catch(async () => {
143-
await fail(
144-
`app did not render the "Add Servers" control within 30s${
145-
pageErrors.length ? ` — page errors: ${pageErrors.join("; ")}` : ""
146-
}`,
147-
);
148-
});
158+
// Hard failures: any uncaught page error, plus console errors that carry the
159+
// externalization signature.
160+
const hardErrors = [
161+
...pageErrors.map(labelExternalized),
162+
...consoleErrors
163+
.filter((m) => EXTERNALIZED.test(m))
164+
.map((m) => `console: ${labelExternalized(m)}`),
165+
];
166+
if (hardErrors.length > 0) {
167+
await fail(
168+
`app logged uncaught / externalization errors: ${hardErrors.join("; ")}`,
169+
);
170+
}
149171

150-
if (pageErrors.length > 0) {
151-
await fail(`app rendered but logged page errors: ${pageErrors.join("; ")}`);
172+
// Non-fatal console errors: surface them so a real problem isn't invisible,
173+
// without failing the smoke on benign subresource/warning noise.
174+
const benignConsole = consoleErrors.filter((m) => !EXTERNALIZED.test(m));
175+
if (benignConsole.length > 0) {
176+
console.log(
177+
`smoke:web:browser note — ${benignConsole.length} non-fatal console error(s): ${benignConsole.join("; ")}`,
178+
);
152179
}
153180

154181
console.log(
155-
`smoke:web:browser OK — app booted at ${BASE_URL}, rendered "Add Servers" with no page errors`,
182+
`smoke:web:browser OK — app booted at ${server.baseUrl}, rendered "Add Servers" with no uncaught page errors`,
156183
);
157184
await shutdown();
158185
process.exit(0);

0 commit comments

Comments
 (0)