chromedevtools--chrome-devtools-mcp
b4546ef86b
release-please / release-please (push) Has been cancelled
Compile and run tests / Tests on macos-latest with node 22 (push) Has been cancelled
Compile and run tests / Tests on ubuntu-latest with node 22 (push) Has been cancelled
Compile and run tests / Tests on windows-latest with node 22 (push) Has been cancelled
Compile and run tests / Tests on macos-latest with node 24 (push) Has been cancelled
Compile and run tests / Tests on ubuntu-latest with node 24 (push) Has been cancelled
Compile and run tests / Tests on windows-latest with node 24 (push) Has been cancelled
Compile and run tests / Tests on macos-latest with node 26 (push) Has been cancelled
Compile and run tests / Tests on ubuntu-latest with node 26 (push) Has been cancelled
Compile and run tests / Tests on windows-latest with node 26 (push) Has been cancelled
Check code before submitting / [Required] Check correct format (push) Has been cancelled
Check code before submitting / [Required] Check docs updated (push) Has been cancelled
Compile and run tests / [Required] Tests passed (push) Has been cancelled
Assortment of various flakiness conditions found running tests in a loop locally: This PR introduces a comprehensive set of hermetic retry layers and aggressive timeout handlers across the test suite to insulate it from random Chromium startup hangs, CDP deadlocks, and Puppeteer lifecycle flakes. It guarantees that temporary browser infrastructure failures are automatically retried without failing the CI, while actual code assertion failures still fail fast. ### Test Harness & Retry Improvements • tests/utils.ts: Rewrote withBrowser to include a 30-second internal timeout and a 3-attempt retry loop. If Chromium locks up or disconnects (Target closed / socket hang up), the browser is forcibly evicted (via SIGKILL if browser.close() hangs) and the test setup is cleanly retried. • tests/index.test.ts: Wrapped withClient (used by E2E tests) in a 3-attempt retry loop to handle the daemon/Chromium hanging during launch and triggering the 60-second MCP client timeout. • tests/browser.test.ts: Added a safeClose helper that imposes a 2-second timeout before SIGKILLing browsers, and wrapped raw Puppeteer tests in runWithRetry to handle startup hangs. • tests/shutdown.test.ts: Added a setupServerWithRetry helper to prevent random 60s RPC timeouts when the server's Chrome instance hangs during boot. ### Flaky Operations & Navigation Fixes • src/tools/performance.ts & tests/tools/performance.test.ts: Replaced the notoriously flaky waitUntil: ['networkidle0'] with 'load' when navigating to about:blank in performance_start_trace. This prevents random 10-second Navigation timeout exceeded errors. Also stubbed goto in the associated unit tests for better hermeticity. • src/McpContext.ts: Wrapped browser.installExtension() with a 15-second timeout to prevent deadlocks when an extension fails to load. • tests/tools/extensions.test.ts: Removed flaky headless UI navigations to chrome://extensions in favor of using the context.listExtensions() API. • tests/tools/pages.test.js.snapshot: Synced test snapshots to reflect updated environment baselines.
146 行
3.3 KiB
TypeScript
146 行
3.3 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2025 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
import http, {
|
|
type IncomingMessage,
|
|
type Server,
|
|
type ServerResponse,
|
|
} from 'node:http';
|
|
import {before, after, afterEach} from 'node:test';
|
|
|
|
import {html} from './utils.js';
|
|
|
|
export class TestServer {
|
|
#port: number;
|
|
#server: Server;
|
|
|
|
static randomPort() {
|
|
/**
|
|
* Some ports are restricted by Chromium and will fail to connect
|
|
* to prevent we start after the
|
|
*
|
|
* https://source.chromium.org/chromium/chromium/src/+/main:net/base/port_util.cc;l=107?q=kRestrictedPorts&ss=chromium
|
|
*/
|
|
const min = 10101;
|
|
const max = 20202;
|
|
return Math.floor(Math.random() * (max - min + 1) + min);
|
|
}
|
|
|
|
#routes: Record<string, (req: IncomingMessage, res: ServerResponse) => void> =
|
|
{};
|
|
|
|
constructor(port: number) {
|
|
this.#port = port;
|
|
this.#server = http.createServer((req, res) => this.#handle(req, res));
|
|
}
|
|
|
|
get baseUrl(): string {
|
|
return `http://localhost:${this.#port}`;
|
|
}
|
|
|
|
getRoute(path: string) {
|
|
if (!this.#routes[path]) {
|
|
throw new Error(`Route ${path} was not setup.`);
|
|
}
|
|
return `${this.baseUrl}${path}`;
|
|
}
|
|
|
|
addHtmlRoute(path: string, htmlContent: string) {
|
|
if (this.#routes[path]) {
|
|
throw new Error(`Route ${path} was already setup.`);
|
|
}
|
|
this.#routes[path] = (_req: IncomingMessage, res: ServerResponse) => {
|
|
res.setHeader('Content-Type', 'text/html; charset=utf-8');
|
|
res.statusCode = 200;
|
|
res.end(htmlContent);
|
|
};
|
|
}
|
|
|
|
addRoute(
|
|
path: string,
|
|
handler: (req: IncomingMessage, res: ServerResponse) => void,
|
|
) {
|
|
if (this.#routes[path]) {
|
|
throw new Error(`Route ${path} was already setup.`);
|
|
}
|
|
this.#routes[path] = handler;
|
|
}
|
|
|
|
#handle(req: IncomingMessage, res: ServerResponse) {
|
|
const url = req.url ?? '';
|
|
const routeHandler = this.#routes[url];
|
|
|
|
if (routeHandler) {
|
|
routeHandler(req, res);
|
|
} else {
|
|
res.writeHead(404, {'Content-Type': 'text/html'});
|
|
res.end(
|
|
html`<h1>404 - Not Found</h1><p>The requested page does not exist.</p>`,
|
|
);
|
|
}
|
|
}
|
|
|
|
restore() {
|
|
this.#routes = {};
|
|
}
|
|
|
|
async start(): Promise<void> {
|
|
let retries = 5;
|
|
while (retries > 0) {
|
|
try {
|
|
await new Promise<void>((res, rej) => {
|
|
this.#server.once('error', rej);
|
|
this.#server.listen(this.#port, () => {
|
|
this.#server.off('error', rej);
|
|
res();
|
|
});
|
|
});
|
|
return;
|
|
} catch (err) {
|
|
if (
|
|
err instanceof Error &&
|
|
'code' in err &&
|
|
err.code === 'EADDRINUSE'
|
|
) {
|
|
retries--;
|
|
this.#port = TestServer.randomPort();
|
|
} else {
|
|
throw err;
|
|
}
|
|
}
|
|
}
|
|
throw new Error('Failed to bind to a port after 5 retries');
|
|
}
|
|
|
|
stop(): Promise<void> {
|
|
return new Promise((res, rej) => {
|
|
this.#server.closeAllConnections();
|
|
this.#server.close(err => {
|
|
if (err) {
|
|
rej(err);
|
|
} else {
|
|
res();
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
export function serverHooks() {
|
|
const server = new TestServer(TestServer.randomPort());
|
|
before(async () => {
|
|
await server.start();
|
|
});
|
|
after(async () => {
|
|
await server.stop();
|
|
});
|
|
afterEach(() => {
|
|
server.restore();
|
|
});
|
|
|
|
return server;
|
|
}
|