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" ;
3239import { 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
4042const 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" ;
4246const 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 = / M o d u l e " [ ^ " ] + " h a s b e e n e x t e r n a l i z e d / ;
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 } ) ;
6460let browser = null ;
6561
6662async 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
7874async 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 ( / M o d u l e " n o d e : .* " h a s b e e n e x t e r n a l i z e d / . 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
11398try {
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 : / A d d S e r v e r s / } )
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 : / A d d S e r v e r s / } )
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