|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import puppeteer, {Locator} from 'puppeteer'; |
| 8 | +import logger from 'debug'; |
| 9 | +import {McpContext} from './McpContext.js'; |
| 10 | +import {McpResponse} from './McpResponse.js'; |
| 11 | +import {startTrace} from './tools/performance.js'; |
| 12 | + |
| 13 | +async function run() { |
| 14 | + console.log('Launching browser...'); |
| 15 | + const browser = await puppeteer.launch({ |
| 16 | + headless: false, // Visible for manual observation if needed |
| 17 | + defaultViewport: null, |
| 18 | + handleDevToolsAsPage: true, |
| 19 | + args: ['--no-sandbox', '--disable-setuid-sandbox'], // Useful for some envs |
| 20 | + }); |
| 21 | + |
| 22 | + try { |
| 23 | + const page = await browser.newPage(); |
| 24 | + // Close other pages (like about:blank opened by default if any) |
| 25 | + const pages = await browser.pages(); |
| 26 | + for (const p of pages) { |
| 27 | + if (p !== page) await p.close(); |
| 28 | + } |
| 29 | + |
| 30 | + console.log('Setting up McpContext...'); |
| 31 | + const context = await McpContext.from( |
| 32 | + browser, |
| 33 | + logger('test'), |
| 34 | + { |
| 35 | + experimentalDevToolsDebugging: false, |
| 36 | + }, |
| 37 | + Locator, |
| 38 | + ); |
| 39 | + |
| 40 | + // Ensure we have a page selected (McpContext selects one by default but good to be sure) |
| 41 | + console.log('Context initialized.'); |
| 42 | + |
| 43 | + // Pre-navigate to something so we can reload it |
| 44 | + const targetUrl = 'https://example.com'; |
| 45 | + console.log(`Navigating to ${targetUrl}...`); |
| 46 | + await context.getSelectedPage().goto(targetUrl); |
| 47 | + |
| 48 | + console.log('Starting trace with autoStop: true, reload: true...'); |
| 49 | + const response = new McpResponse(); |
| 50 | + const request = { |
| 51 | + params: { |
| 52 | + reload: true, |
| 53 | + autoStop: true, |
| 54 | + }, |
| 55 | + }; |
| 56 | + |
| 57 | + // startTrace.handler is async. It waits for the trace to finish if autoStop is true. |
| 58 | + await startTrace.handler(request, response, context); |
| 59 | + |
| 60 | + console.log('Trace handler returned.'); |
| 61 | + |
| 62 | + // Assertions |
| 63 | + const lines = response.responseLines; |
| 64 | + |
| 65 | + // Check for stop message |
| 66 | + const stoppedMsg = lines.find(l => |
| 67 | + l.includes('The performance trace has been stopped'), |
| 68 | + ); |
| 69 | + if (!stoppedMsg) { |
| 70 | + console.error('Response lines:', lines); |
| 71 | + throw new Error('FAILED: Did not find stop message'); |
| 72 | + } else { |
| 73 | + console.log('Verified: Stop message found.'); |
| 74 | + } |
| 75 | + |
| 76 | + // Check if context thinks it is running |
| 77 | + const isRunning = context.isRunningPerformanceTrace(); |
| 78 | + if (isRunning) { |
| 79 | + throw new Error('FAILED: Trace is still marked as running in context'); |
| 80 | + } else { |
| 81 | + console.log('Verified: Context marks trace as stopped.'); |
| 82 | + } |
| 83 | + |
| 84 | + // Check if trace is recorded |
| 85 | + const traces = context.recordedTraces(); |
| 86 | + if (traces.length !== 1) { |
| 87 | + throw new Error( |
| 88 | + `FAILED: Expected 1 recorded trace, got ${traces.length}`, |
| 89 | + ); |
| 90 | + } else { |
| 91 | + console.log('Verified: 1 trace recorded.'); |
| 92 | + } |
| 93 | + |
| 94 | + // Check trace content (basic check) |
| 95 | + const traceResult = traces[0]; |
| 96 | + // We assume traceResult is valid if we got here and storeTraceRecording was called. |
| 97 | + // startTrace.handler calls storeTraceRecording only on success. |
| 98 | + console.log( |
| 99 | + 'Trace result summary:', |
| 100 | + lines.find(l => l.includes('Trace duration')), |
| 101 | + ); |
| 102 | + |
| 103 | + console.log('SUCCESS: Trace recorded and stopped automatically.'); |
| 104 | + } catch (err) { |
| 105 | + console.error('Test FAILED with error:', err); |
| 106 | + process.exit(1); |
| 107 | + } finally { |
| 108 | + console.log('Closing browser...'); |
| 109 | + await browser.close(); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +run(); |
0 commit comments