项目文件夹

文件
Alex Rudenko 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
test: fix potential flakiness in tests (#2396)
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.
2026-07-21 13:47:47 +00:00

284 行
8.1 KiB
TypeScript

/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import assert from 'node:assert/strict';
import {describe, it} from 'node:test';
import {emulate} from '../src/tools/emulation.js';
import {lighthouseAudit} from '../src/tools/lighthouse.js';
import {navigatePage} from '../src/tools/pages.js';
import {evaluateScript} from '../src/tools/script.js';
import {serverHooks} from './server.js';
import {withMcpContext} from './utils.js';
describe('Network Blocking Integration', () => {
const server = serverHooks();
it('blocks URLs in blocklist', async () => {
server.addHtmlRoute('/allowed.html', '<html><body>Allowed</body></html>');
server.addHtmlRoute('/blocked.html', '<html><body>Blocked</body></html>');
const blockedUrlPattern = [server.getRoute('/blocked.html')];
await withMcpContext(
async (response, context) => {
const allowedUrl = server.getRoute('/allowed.html');
await navigatePage().handler(
{
params: {url: allowedUrl},
page: context.getSelectedMcpPage(),
},
response,
context,
);
assert.strictEqual(
response.responseLines[0],
`Successfully navigated to ${allowedUrl}.`,
);
response.resetResponseLineForTesting();
await evaluateScript().handler(
{
params: {function: String(() => document.body.textContent)},
},
response,
context,
);
assert.strictEqual(
JSON.parse(response.responseLines.at(2)!),
'Allowed',
);
const blockedUrl = server.getRoute('/blocked.html');
response.resetResponseLineForTesting();
await evaluateScript().handler(
{
params: {
function: `async () => {
try {
await fetch("${blockedUrl}", { signal: AbortSignal.timeout(5000) });
return 'SUCCESS';
} catch (err) {
return err instanceof Error ? err.message : String(err);
}
}`,
},
},
response,
context,
);
assert.strictEqual(
JSON.parse(response.responseLines.at(2)!),
'Failed to fetch',
);
},
{
blockedUrlPattern,
},
);
});
it('blocks URLs not in allowlist', async () => {
server.addHtmlRoute('/allowed.html', '<html><body>Allowed</body></html>');
server.addHtmlRoute('/blocked.html', '<html><body>Blocked</body></html>');
const allowedUrlPattern = [server.getRoute('/allowed.html')];
await withMcpContext(
async (response, context) => {
const allowedUrl = server.getRoute('/allowed.html');
await navigatePage().handler(
{
params: {url: allowedUrl},
page: context.getSelectedMcpPage(),
},
response,
context,
);
assert.strictEqual(
response.responseLines[0],
`Successfully navigated to ${allowedUrl}.`,
);
response.resetResponseLineForTesting();
await evaluateScript().handler(
{
params: {function: String(() => document.body.textContent)},
},
response,
context,
);
assert.strictEqual(
JSON.parse(response.responseLines.at(2)!),
'Allowed',
);
const blockedUrl = server.getRoute('/blocked.html');
response.resetResponseLineForTesting();
await evaluateScript().handler(
{
params: {
function: `async () => {
try {
await fetch("${blockedUrl}", { signal: AbortSignal.timeout(5000) });
return 'SUCCESS';
} catch (err) {
return err instanceof Error ? err.message : String(err);
}
}`,
},
},
response,
context,
);
assert.strictEqual(
JSON.parse(response.responseLines.at(2)!),
'Failed to fetch',
);
},
{
allowedUrlPattern,
},
);
});
it('respects blocklist after Lighthouse audits', async () => {
server.addHtmlRoute('/allowed.html', '<html><body>Allowed</body></html>');
server.addHtmlRoute('/blocked.html', '<html><body>Blocked</body></html>');
const blockedUrlPattern = [server.getRoute('/blocked.html')];
await withMcpContext(
async (response, context) => {
const allowedUrl = server.getRoute('/allowed.html');
await navigatePage().handler(
{
params: {url: allowedUrl},
page: context.getSelectedMcpPage(),
},
response,
context,
);
assert.strictEqual(
response.responseLines[0],
`Successfully navigated to ${allowedUrl}.`,
);
const blockedUrl = server.getRoute('/blocked.html');
// Verifies fetch is blocked before Lighthouse audit
response.resetResponseLineForTesting();
await evaluateScript().handler(
{
params: {
function: `async () => {
try {
await fetch("${blockedUrl}", { signal: AbortSignal.timeout(5000) });
return 'SUCCESS';
} catch (err) {
return err instanceof Error ? err.message : String(err);
}
}`,
},
},
response,
context,
);
assert.strictEqual(
JSON.parse(response.responseLines.at(2)!),
'Failed to fetch',
'Fetch should be blocked before audit',
);
await lighthouseAudit.handler(
{
params: {
mode: 'navigation',
device: 'desktop',
},
page: context.getSelectedMcpPage(),
},
response,
context,
);
assert.equal(
response.attachedLighthouseResult?.summary.mode,
'navigation',
);
// 2. Verify fetch remains blocked AFTER Lighthouse audit
response.resetResponseLineForTesting();
await evaluateScript().handler(
{
params: {
function: `async () => {
try {
await fetch("${blockedUrl}", { signal: AbortSignal.timeout(5000) });
return 'SUCCESS';
} catch (err) {
return err instanceof Error ? err.message : String(err);
}
}`,
},
},
response,
context,
);
assert.strictEqual(
JSON.parse(response.responseLines.at(2)!),
'Failed to fetch',
'Fetch should still be blocked after audit',
);
},
{
blockedUrlPattern,
},
);
});
it('throws error when trying to emulate network conditions while blocklist is configured', async () => {
const blockedUrlPattern = ['*://*/*'];
await withMcpContext(
async (response, context) => {
// Attempting to emulate network conditions should throw an error.
await assert.rejects(async () => {
await emulate.handler(
{
params: {
networkConditions: 'Offline',
},
page: context.getSelectedMcpPage(),
},
response,
context,
);
}, /Network throttling is not supported when network blocking \(allowlist\/blocklist\) is configured\./);
// Attempting to emulate CPU rate or other things should succeed without errors.
await emulate.handler(
{
params: {
cpuThrottlingRate: 2,
},
page: context.getSelectedMcpPage(),
},
response,
context,
);
assert.strictEqual(
response.responseLines[0],
'Emulation configured successfully',
);
},
{
blockedUrlPattern,
},
);
});
});