forked from ChromeDevTools/chrome-devtools-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.test.ts
More file actions
457 lines (415 loc) · 19.3 KB
/
console.test.ts
File metadata and controls
457 lines (415 loc) · 19.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import assert from 'node:assert';
import {before, describe, it} from 'node:test';
import {loadIssueDescriptions} from '../../src/issue-descriptions.js';
import {McpResponse} from '../../src/McpResponse.js';
import {DevTools} from '../../src/third_party/index.js';
import {
getConsoleMessage,
listConsoleMessages,
} from '../../src/tools/console.js';
import {serverHooks} from '../server.js';
import {getTextContent, withMcpContext} from '../utils.js';
describe('console', () => {
before(async () => {
await loadIssueDescriptions();
});
describe('list_console_messages', () => {
it('list messages', async () => {
await withMcpContext(async (response, context) => {
await listConsoleMessages.handler({params: {}}, response, context);
assert.ok(response.includeConsoleData);
});
});
it('lists error messages', async () => {
await withMcpContext(async (response, context) => {
const page = await context.newPage();
await page.setContent(
'<script>console.error("This is an error")</script>',
);
await listConsoleMessages.handler({params: {}}, response, context);
const formattedResponse = await response.handle('test', context);
const textContent = getTextContent(formattedResponse.content[0]);
assert.ok(textContent.includes('msgid=1 [error] This is an error'));
});
});
it('groups identical messages', async () => {
await withMcpContext(async (response, context) => {
const page = await context.newPage();
await page.setContent(`
<script>
for (let i = 0; i < 5; i++) {
console.log('spam');
}
</script>
`);
await listConsoleMessages.handler({params: {}}, response, context);
const formattedResponse = await response.handle('test', context);
const textContent = getTextContent(formattedResponse.content[0]);
assert.ok(
textContent.includes('msgid=1 [log] spam (1 args) [5 times]'),
textContent,
);
assert.ok(
!textContent.includes('msgid=2 [log] spam'),
'Should collapse subsequent identical messages',
);
});
});
it('lists error objects', async t => {
await withMcpContext(async (response, context) => {
const page = await context.newPage();
await page.setContent(
'<script>console.error(new Error("This is an error"))</script>',
);
await listConsoleMessages.handler({params: {}}, response, context);
const formattedResponse = await response.handle('test', context);
const textContent = getTextContent(formattedResponse.content[0]);
t.assert.snapshot?.(textContent);
});
});
it('work with primitive unhandled errors', async () => {
await withMcpContext(async (response, context) => {
const page = await context.newPage();
await page.setContent('<script>throw undefined;</script>');
await listConsoleMessages.handler({params: {}}, response, context);
const formattedResponse = await response.handle('test', context);
const textContent = getTextContent(formattedResponse.content[0]);
assert.ok(textContent.includes('msgid=1 [error] Uncaught (0 args)'));
});
});
describe('issues', () => {
it('lists issues', async () => {
await withMcpContext(async (response, context) => {
const page = await context.newPage();
const issuePromise = new Promise<void>(resolve => {
page.once('issue', () => {
resolve();
});
});
await page.setContent('<input type="text" name="username" />');
await issuePromise;
await listConsoleMessages.handler({params: {}}, response, context);
const formattedResponse = await response.handle('test', context);
const textContent = getTextContent(formattedResponse.content[0]);
assert.ok(
textContent.includes(
`msgid=1 [issue] An element doesn't have an autocomplete attribute (count: 1)`,
),
);
});
});
it('lists issues after a page reload', async () => {
await withMcpContext(async (response, context) => {
const page = await context.newPage();
const issuePromise = new Promise<void>(resolve => {
page.once('issue', () => {
resolve();
});
});
await page.setContent('<input type="text" name="username" />');
await issuePromise;
await listConsoleMessages.handler({params: {}}, response, context);
{
const formattedResponse = await response.handle('test', context);
const textContent = getTextContent(formattedResponse.content[0]);
assert.ok(
textContent.includes(
`msgid=1 [issue] An element doesn't have an autocomplete attribute (count: 1)`,
),
);
}
const anotherIssuePromise = new Promise<void>(resolve => {
page.once('issue', () => {
resolve();
});
});
await page.reload();
await page.setContent('<input type="text" name="username" />');
await anotherIssuePromise;
{
const formattedResponse = await response.handle('test', context);
const textContent = getTextContent(formattedResponse.content[0]);
assert.ok(
textContent.includes(
`msgid=2 [issue] An element doesn't have an autocomplete attribute (count: 1)`,
),
);
}
});
});
});
});
describe('get_console_message', () => {
const server = serverHooks();
it('gets a specific console message', async () => {
await withMcpContext(async (response, context) => {
const page = await context.newPage();
await page.setContent(
'<script>console.error("This is an error")</script>',
);
// The list is needed to populate the console messages in the context.
await listConsoleMessages.handler({params: {}}, response, context);
await getConsoleMessage.handler(
{params: {msgid: 1}},
response,
context,
);
const formattedResponse = await response.handle('test', context);
const textContent = getTextContent(formattedResponse.content[0]);
assert.ok(
textContent.includes('msgid=1 [error] This is an error'),
'Should contain console message body',
);
});
});
describe('issues type', () => {
it('gets issue details with node id parsing', async t => {
await withMcpContext(async (response, context) => {
const page = await context.newPage();
const issuePromise = new Promise<void>(resolve => {
page.once('issue', () => {
resolve();
});
});
await page.setContent('<input type="text" name="username" />');
await context.createTextSnapshot();
await issuePromise;
await listConsoleMessages.handler({params: {}}, response, context);
const response2 = new McpResponse();
await getConsoleMessage.handler(
{params: {msgid: 1}},
response2,
context,
);
const formattedResponse = await response2.handle('test', context);
t.assert.snapshot?.(getTextContent(formattedResponse.content[0]));
});
});
it('gets issue details with request id parsing', async t => {
server.addRoute('/data.json', (_req, res) => {
res.setHeader('Content-Type', 'application/json');
res.statusCode = 200;
res.end(JSON.stringify({data: 'test data'}));
});
await withMcpContext(async (response, context) => {
const page = await context.newPage();
const issuePromise = new Promise<void>(resolve => {
page.once('issue', () => {
resolve();
});
});
const url = server.getRoute('/data.json');
await page.setContent(`
<script>
fetch('${url}', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-Custom-Header': 'MyValue'
}
});
</script>
`);
await context.createTextSnapshot();
await issuePromise;
const messages = context.getConsoleData();
let issueMsg;
for (const message of messages) {
if (message instanceof DevTools.AggregatedIssue) {
issueMsg = message;
break;
}
}
assert.ok(issueMsg);
const id = context.getConsoleMessageStableId(issueMsg);
assert.ok(id);
await listConsoleMessages.handler(
{params: {types: ['issue']}},
response,
context,
);
const response2 = new McpResponse();
await getConsoleMessage.handler(
{params: {msgid: id}},
response2,
context,
);
const formattedResponse = await response2.handle('test', context);
const rawText = getTextContent(formattedResponse.content[0]);
const sanitizedText = rawText
.replaceAll(/ID: \d+/g, 'ID: <ID>')
.replaceAll(/reqid=\d+/g, 'reqid=<reqid>')
.replaceAll(/localhost:\d+/g, 'hostname:port');
t.assert.snapshot?.(sanitizedText);
});
});
});
it('applies source maps to stack traces of console messages', async t => {
server.addRoute('/main.min.js', (_req, res) => {
res.setHeader('Content-Type', 'text/javascript');
res.statusCode = 200;
res.end(`function n(){console.warn("hello world")}function o(){n()}(function n(){o()})();
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJiYXIiLCJjb25zb2xlIiwid2FybiIsImZvbyIsIklpZmUiXSwic291cmNlcyI6WyIuL21haW4uanMiXSwic291cmNlc0NvbnRlbnQiOlsiXG5mdW5jdGlvbiBiYXIoKSB7XG4gIGNvbnNvbGUud2FybignaGVsbG8gd29ybGQnKTtcbn1cblxuZnVuY3Rpb24gZm9vKCkge1xuICBiYXIoKTtcbn1cblxuKGZ1bmN0aW9uIElpZmUoKSB7XG4gIGZvbygpO1xufSkoKTtcblxuIl0sIm1hcHBpbmdzIjoiQUFDQSxTQUFTQSxJQUNQQyxRQUFRQyxLQUFLLGNBQ2YsQ0FFQSxTQUFTQyxJQUNQSCxHQUNGLEVBRUEsU0FBVUksSUFDUkQsR0FDRCxFQUZEIiwiaWdub3JlTGlzdCI6W119
`);
});
server.addHtmlRoute(
'/index.html',
`<script src="${server.getRoute('/main.min.js')}"></script>`,
);
await withMcpContext(async (response, context) => {
const page = await context.newPage();
await page.goto(server.getRoute('/index.html'));
await getConsoleMessage.handler(
{params: {msgid: 1}},
response,
context,
);
const formattedResponse = await response.handle('test', context);
const rawText = getTextContent(formattedResponse.content[0]);
t.assert.snapshot?.(rawText);
});
});
it('applies source maps to stack traces of uncaught exceptions', async t => {
server.addRoute('/main.min.js', (_req, res) => {
res.setHeader('Content-Type', 'text/javascript');
res.statusCode = 200;
res.end(`function n(){throw new Error("b00m!")}function o(){n()}(function n(){o()})();
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJiYXIiLCJFcnJvciIsImZvbyIsIklpZmUiXSwic291cmNlcyI6WyIuL21haW4uanMiXSwic291cmNlc0NvbnRlbnQiOlsiXG5mdW5jdGlvbiBiYXIoKSB7XG4gIHRocm93IG5ldyBFcnJvcignYjAwbSEnKTtcbn1cblxuZnVuY3Rpb24gZm9vKCkge1xuICBiYXIoKTtcbn1cblxuKGZ1bmN0aW9uIElpZmUoKSB7XG4gIGZvbygpO1xufSkoKTtcblxuIl0sIm1hcHBpbmdzIjoiQUFDQSxTQUFTQSxJQUNQLE1BQU0sSUFBSUMsTUFBTSxRQUNsQixDQUVBLFNBQVNDLElBQ1BGLEdBQ0YsRUFFQSxTQUFVRyxJQUNSRCxHQUNELEVBRkQiLCJpZ25vcmVMaXN0IjpbXX0=
`);
});
server.addHtmlRoute(
'/index.html',
`<script src="${server.getRoute('/main.min.js')}"></script>`,
);
await withMcpContext(async (response, context) => {
const page = await context.newPage();
await page.goto(server.getRoute('/index.html'));
await getConsoleMessage.handler(
{params: {msgid: 1}},
response,
context,
);
const formattedResponse = await response.handle('test', context);
const rawText = getTextContent(formattedResponse.content[0]);
t.assert.snapshot?.(rawText);
});
});
it('applies source maps to stack traces of Error object console.log arguments', async t => {
server.addRoute('/main.min.js', (_req, res) => {
res.setHeader('Content-Type', 'text/javascript');
res.statusCode = 200;
res.end(`function n(){throw new Error("b00m!")}function o(){n()}(function n(){try{o()}catch(n){console.log("An error happened:",n)}})();
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJiYXIiLCJFcnJvciIsImZvbyIsIklpZmUiLCJlIiwiY29uc29sZSIsImxvZyJdLCJzb3VyY2VzIjpbIi4vbWFpbi5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyJcbmZ1bmN0aW9uIGJhcigpIHtcbiAgdGhyb3cgbmV3IEVycm9yKCdiMDBtIScpO1xufVxuXG5mdW5jdGlvbiBmb28oKSB7XG4gIGJhcigpO1xufVxuXG4oZnVuY3Rpb24gSWlmZSgpIHtcbiAgdHJ5IHtcbiAgICBmb28oKTtcbiAgfSBjYXRjaCAoZSkge1xuICAgIGNvbnNvbGUubG9nKCdBbiBlcnJvciBoYXBwZW5lZDonLCBlKTtcbiAgfVxufSkoKTtcblxuIl0sIm1hcHBpbmdzIjoiQUFDQSxTQUFTQSxJQUNQLE1BQU0sSUFBSUMsTUFBTSxRQUNsQixDQUVBLFNBQVNDLElBQ1BGLEdBQ0YsRUFFQSxTQUFVRyxJQUNSLElBQ0VELEdBQ0YsQ0FBRSxNQUFPRSxHQUNQQyxRQUFRQyxJQUFJLHFCQUFzQkYsRUFDcEMsQ0FDRCxFQU5EIiwiaWdub3JlTGlzdCI6W119
`);
});
server.addHtmlRoute(
'/index.html',
`<script src="${server.getRoute('/main.min.js')}"></script>`,
);
await withMcpContext(async (response, context) => {
const page = await context.newPage();
await page.goto(server.getRoute('/index.html'));
await getConsoleMessage.handler(
{params: {msgid: 1}},
response,
context,
);
const formattedResponse = await response.handle('test', context);
const rawText = getTextContent(formattedResponse.content[0]);
t.assert.snapshot?.(rawText);
});
});
it('applies source maps to stack traces of uncaught exceptions with cause', async t => {
server.addRoute('/main.min.js', (_req, res) => {
res.setHeader('Content-Type', 'text/javascript');
res.statusCode = 200;
res.end(`function r(){throw new Error("b00m!")}function o(){try{r()}catch(r){throw new Error("bar failed",{cause:r})}}(function r(){try{o()}catch(r){throw new Error("foo failed",{cause:r})}})();
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJiYXIiLCJFcnJvciIsImZvbyIsImUiLCJjYXVzZSIsIklpZmUiXSwic291cmNlcyI6WyIuL21haW4uanMiXSwic291cmNlc0NvbnRlbnQiOlsiXG5mdW5jdGlvbiBiYXIoKSB7XG4gIHRocm93IG5ldyBFcnJvcignYjAwbSEnKTtcbn1cblxuZnVuY3Rpb24gZm9vKCkge1xuICB0cnkge1xuICAgIGJhcigpO1xuICB9IGNhdGNoIChlKSB7XG4gICAgdGhyb3cgbmV3IEVycm9yKCdiYXIgZmFpbGVkJywgeyBjYXVzZTogZSB9KTtcbiAgfVxufVxuXG4oZnVuY3Rpb24gSWlmZSgpIHtcbiAgdHJ5IHtcbiAgICBmb28oKTtcbiAgfSBjYXRjaCAoZSkge1xuICAgIHRocm93IG5ldyBFcnJvcignZm9vIGZhaWxlZCcsIHsgY2F1c2U6IGUgfSk7XG4gIH1cbn0pKCk7XG5cbiJdLCJtYXBwaW5ncyI6IkFBQ0EsU0FBU0EsSUFDUCxNQUFNLElBQUlDLE1BQU0sUUFDbEIsQ0FFQSxTQUFTQyxJQUNQLElBQ0VGLEdBQ0YsQ0FBRSxNQUFPRyxHQUNQLE1BQU0sSUFBSUYsTUFBTSxhQUFjLENBQUVHLE1BQU9ELEdBQ3pDLENBQ0YsRUFFQSxTQUFVRSxJQUNSLElBQ0VILEdBQ0YsQ0FBRSxNQUFPQyxHQUNQLE1BQU0sSUFBSUYsTUFBTSxhQUFjLENBQUVHLE1BQU9ELEdBQ3pDLENBQ0QsRUFORCIsImlnbm9yZUxpc3QiOltdfQ==
`);
});
server.addHtmlRoute(
'/index.html',
`<script src="${server.getRoute('/main.min.js')}"></script>`,
);
await withMcpContext(async (response, context) => {
const page = await context.newPage();
await page.goto(server.getRoute('/index.html'));
await getConsoleMessage.handler(
{params: {msgid: 1}},
response,
context,
);
const formattedResponse = await response.handle('test', context);
const rawText = getTextContent(formattedResponse.content[0]);
t.assert.snapshot?.(rawText);
});
});
it('applies source maps to stack traces of Error object (with cause) console.log arguments', async t => {
server.addRoute('/main.min.js', (_req, res) => {
res.setHeader('Content-Type', 'text/javascript');
res.statusCode = 200;
res.end(`function o(){throw new Error("b00m!")}function r(){try{o()}catch(o){throw new Error("bar failed",{cause:o})}}(function o(){try{r()}catch(o){console.log("foo failed",o)}})();
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJiYXIiLCJFcnJvciIsImZvbyIsImUiLCJjYXVzZSIsIklpZmUiLCJjb25zb2xlIiwibG9nIl0sInNvdXJjZXMiOlsiLi9tYWluLmpzIl0sInNvdXJjZXNDb250ZW50IjpbIlxuZnVuY3Rpb24gYmFyKCkge1xuICB0aHJvdyBuZXcgRXJyb3IoJ2IwMG0hJyk7XG59XG5cbmZ1bmN0aW9uIGZvbygpIHtcbiAgdHJ5IHtcbiAgICBiYXIoKTtcbiAgfSBjYXRjaCAoZSkge1xuICAgIHRocm93IG5ldyBFcnJvcignYmFyIGZhaWxlZCcsIHsgY2F1c2U6IGUgfSk7XG4gIH1cbn1cblxuKGZ1bmN0aW9uIElpZmUoKSB7XG4gIHRyeSB7XG4gICAgZm9vKCk7XG4gIH0gY2F0Y2ggKGUpIHtcbiAgICBjb25zb2xlLmxvZygnZm9vIGZhaWxlZCcsIGUpO1xuICB9XG59KSgpO1xuXG4iXSwibWFwcGluZ3MiOiJBQUNBLFNBQVNBLElBQ1AsTUFBTSxJQUFJQyxNQUFNLFFBQ2xCLENBRUEsU0FBU0MsSUFDUCxJQUNFRixHQUNGLENBQUUsTUFBT0csR0FDUCxNQUFNLElBQUlGLE1BQU0sYUFBYyxDQUFFRyxNQUFPRCxHQUN6QyxDQUNGLEVBRUEsU0FBVUUsSUFDUixJQUNFSCxHQUNGLENBQUUsTUFBT0MsR0FDUEcsUUFBUUMsSUFBSSxhQUFjSixFQUM1QixDQUNELEVBTkQiLCJpZ25vcmVMaXN0IjpbXX0=
`);
});
server.addHtmlRoute(
'/index.html',
`<script src="${server.getRoute('/main.min.js')}"></script>`,
);
await withMcpContext(async (response, context) => {
const page = await context.newPage();
await page.goto(server.getRoute('/index.html'));
await getConsoleMessage.handler(
{params: {msgid: 1}},
response,
context,
);
const formattedResponse = await response.handle('test', context);
const rawText = getTextContent(formattedResponse.content[0]);
t.assert.snapshot?.(rawText);
});
});
it('ignores frames from ignore listed URLs', async t => {
server.addHtmlRoute(
'/index.html',
`<!DOCTYPE html>
<script>
function ignoredFn1(cb) {
ignoredFn2(cb);
}
function ignoredFn2(cb) {
cb();
}
//# sourceURL=./node_modules/foo.js
</script>
<script>
function callback() {
console.log('hello from callback');
}
(function callIt() {
ignoredFn1(callback);
})();
//# sourceURL='main.js'
</script>
`,
);
await withMcpContext(async (response, context) => {
const page = await context.newPage();
await page.goto(server.getRoute('/index.html'));
await getConsoleMessage.handler(
{params: {msgid: 1}},
response,
context,
);
const formattedResponse = await response.handle('test', context);
const rawText = getTextContent(formattedResponse.content[0]);
t.assert.snapshot?.(rawText);
});
});
});
});