Skip to content

Commit 8e90a92

Browse files
authored
chore: split withBrowser into withBrowser and withMcpContext (#620)
I split this PR off from my "create one DevTools universe per page" PR in preparation. This allows tests to re-use browser instances without creating an `McpContext`. Drive-by: Move mocked browser/page into utils.ts.
1 parent a67528a commit 8e90a92

13 files changed

+212
-208
lines changed

tests/McpContext.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import sinon from 'sinon';
1111

1212
import type {TraceResult} from '../src/trace-processing/parse.js';
1313

14-
import {html, withBrowser} from './utils.js';
14+
import {html, withMcpContext} from './utils.js';
1515

1616
describe('McpContext', () => {
1717
it('list pages', async () => {
18-
await withBrowser(async (_response, context) => {
18+
await withMcpContext(async (_response, context) => {
1919
const page = context.getSelectedPage();
2020
await page.setContent(
2121
html`<button>Click me</button
@@ -40,7 +40,7 @@ describe('McpContext', () => {
4040
});
4141

4242
it('can store and retrieve performance traces', async () => {
43-
await withBrowser(async (_response, context) => {
43+
await withMcpContext(async (_response, context) => {
4444
const fakeTrace1 = {} as unknown as TraceResult;
4545
const fakeTrace2 = {} as unknown as TraceResult;
4646
context.storeTraceRecording(fakeTrace1);
@@ -50,7 +50,7 @@ describe('McpContext', () => {
5050
});
5151

5252
it('should update default timeout when cpu throttling changes', async () => {
53-
await withBrowser(async (_response, context) => {
53+
await withMcpContext(async (_response, context) => {
5454
const page = await context.newPage();
5555
const timeoutBefore = page.getDefaultTimeout();
5656
context.setCpuThrottlingRate(2);
@@ -60,7 +60,7 @@ describe('McpContext', () => {
6060
});
6161

6262
it('should update default timeout when network conditions changes', async () => {
63-
await withBrowser(async (_response, context) => {
63+
await withMcpContext(async (_response, context) => {
6464
const page = await context.newPage();
6565
const timeoutBefore = page.getDefaultNavigationTimeout();
6666
context.setNetworkConditions('Slow 3G');
@@ -70,7 +70,7 @@ describe('McpContext', () => {
7070
});
7171

7272
it('should call waitForEventsAfterAction with correct multipliers', async () => {
73-
await withBrowser(async (_response, context) => {
73+
await withMcpContext(async (_response, context) => {
7474
const page = await context.newPage();
7575

7676
context.setCpuThrottlingRate(2);
@@ -86,7 +86,7 @@ describe('McpContext', () => {
8686
});
8787

8888
it('should should detect open DevTools pages', async () => {
89-
await withBrowser(
89+
await withMcpContext(
9090
async (_response, context) => {
9191
const page = await context.newPage();
9292
// TODO: we do not know when the CLI flag to auto open DevTools will run

tests/McpResponse.test.ts

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import {
1616
getMockResponse,
1717
html,
1818
stabilizeResponseOutput,
19-
withBrowser,
19+
withMcpContext,
2020
} from './utils.js';
2121

2222
describe('McpResponse', () => {
2323
it('list pages', async t => {
24-
await withBrowser(async (response, context) => {
24+
await withMcpContext(async (response, context) => {
2525
response.setIncludePages(true);
2626
const result = await response.handle('test', context);
2727
assert.equal(result[0].type, 'text');
@@ -30,7 +30,7 @@ describe('McpResponse', () => {
3030
});
3131

3232
it('allows response text lines to be added', async t => {
33-
await withBrowser(async (response, context) => {
33+
await withMcpContext(async (response, context) => {
3434
response.appendResponseLine('Testing 1');
3535
response.appendResponseLine('Testing 2');
3636
const result = await response.handle('test', context);
@@ -40,7 +40,7 @@ describe('McpResponse', () => {
4040
});
4141

4242
it('does not include anything in response if snapshot is null', async () => {
43-
await withBrowser(async (response, context) => {
43+
await withMcpContext(async (response, context) => {
4444
const page = context.getSelectedPage();
4545
page.accessibility.snapshot = async () => null;
4646
const result = await response.handle('test', context);
@@ -50,7 +50,7 @@ describe('McpResponse', () => {
5050
});
5151

5252
it('returns correctly formatted snapshot for a simple tree', async t => {
53-
await withBrowser(async (response, context) => {
53+
await withMcpContext(async (response, context) => {
5454
const page = context.getSelectedPage();
5555
await page.setContent(
5656
html`<button>Click me</button
@@ -68,7 +68,7 @@ describe('McpResponse', () => {
6868
});
6969

7070
it('returns values for textboxes', async t => {
71-
await withBrowser(async (response, context) => {
71+
await withMcpContext(async (response, context) => {
7272
const page = context.getSelectedPage();
7373
await page.setContent(
7474
html`<label
@@ -86,7 +86,7 @@ describe('McpResponse', () => {
8686
});
8787

8888
it('returns verbose snapshot', async t => {
89-
await withBrowser(async (response, context) => {
89+
await withMcpContext(async (response, context) => {
9090
const page = context.getSelectedPage();
9191
await page.setContent(html`<aside>test</aside>`);
9292
response.includeSnapshot({
@@ -101,7 +101,7 @@ describe('McpResponse', () => {
101101
it('saves snapshot to file', async t => {
102102
const filePath = join(tmpdir(), 'test-screenshot.png');
103103
try {
104-
await withBrowser(async (response, context) => {
104+
await withMcpContext(async (response, context) => {
105105
const page = context.getSelectedPage();
106106
await page.setContent(html`<aside>test</aside>`);
107107
response.includeSnapshot({
@@ -120,7 +120,7 @@ describe('McpResponse', () => {
120120
});
121121

122122
it('adds throttling setting when it is not null', async t => {
123-
await withBrowser(async (response, context) => {
123+
await withMcpContext(async (response, context) => {
124124
context.setNetworkConditions('Slow 3G');
125125
const result = await response.handle('test', context);
126126
assert.equal(result[0].type, 'text');
@@ -129,15 +129,15 @@ describe('McpResponse', () => {
129129
});
130130

131131
it('does not include throttling setting when it is null', async () => {
132-
await withBrowser(async (response, context) => {
132+
await withMcpContext(async (response, context) => {
133133
const result = await response.handle('test', context);
134134
context.setNetworkConditions(null);
135135
assert.equal(result[0].type, 'text');
136136
assert.strictEqual(result[0].text, `# test response`);
137137
});
138138
});
139139
it('adds image when image is attached', async () => {
140-
await withBrowser(async (response, context) => {
140+
await withMcpContext(async (response, context) => {
141141
response.attachImage({data: 'imageBase64', mimeType: 'image/png'});
142142
const result = await response.handle('test', context);
143143
assert.strictEqual(result[0].text, `# test response`);
@@ -148,23 +148,23 @@ describe('McpResponse', () => {
148148
});
149149

150150
it('adds cpu throttling setting when it is over 1', async t => {
151-
await withBrowser(async (response, context) => {
151+
await withMcpContext(async (response, context) => {
152152
context.setCpuThrottlingRate(4);
153153
const result = await response.handle('test', context);
154154
t.assert.snapshot?.(result[0].text);
155155
});
156156
});
157157

158158
it('does not include cpu throttling setting when it is 1', async () => {
159-
await withBrowser(async (response, context) => {
159+
await withMcpContext(async (response, context) => {
160160
context.setCpuThrottlingRate(1);
161161
const result = await response.handle('test', context);
162162
assert.strictEqual(result[0].text, `# test response`);
163163
});
164164
});
165165

166166
it('adds a prompt dialog', async t => {
167-
await withBrowser(async (response, context) => {
167+
await withMcpContext(async (response, context) => {
168168
const page = context.getSelectedPage();
169169
const dialogPromise = new Promise<void>(resolve => {
170170
page.on('dialog', () => {
@@ -182,7 +182,7 @@ describe('McpResponse', () => {
182182
});
183183

184184
it('adds an alert dialog', async t => {
185-
await withBrowser(async (response, context) => {
185+
await withMcpContext(async (response, context) => {
186186
const page = context.getSelectedPage();
187187
const dialogPromise = new Promise<void>(resolve => {
188188
page.on('dialog', () => {
@@ -200,7 +200,7 @@ describe('McpResponse', () => {
200200
});
201201

202202
it('add network requests when setting is true', async t => {
203-
await withBrowser(async (response, context) => {
203+
await withMcpContext(async (response, context) => {
204204
response.setIncludeNetworkRequests(true);
205205
context.getNetworkRequests = () => {
206206
return [getMockRequest({stableId: 1}), getMockRequest({stableId: 2})];
@@ -211,7 +211,7 @@ describe('McpResponse', () => {
211211
});
212212

213213
it('does not include network requests when setting is false', async () => {
214-
await withBrowser(async (response, context) => {
214+
await withMcpContext(async (response, context) => {
215215
response.setIncludeNetworkRequests(false);
216216
context.getNetworkRequests = () => {
217217
return [getMockRequest()];
@@ -222,7 +222,7 @@ describe('McpResponse', () => {
222222
});
223223

224224
it('add network request when attached with POST data', async t => {
225-
await withBrowser(async (response, context) => {
225+
await withMcpContext(async (response, context) => {
226226
response.setIncludeNetworkRequests(true);
227227
const httpResponse = getMockResponse();
228228
httpResponse.buffer = () => {
@@ -254,7 +254,7 @@ describe('McpResponse', () => {
254254
});
255255

256256
it('add network request when attached', async t => {
257-
await withBrowser(async (response, context) => {
257+
await withMcpContext(async (response, context) => {
258258
response.setIncludeNetworkRequests(true);
259259
const request = getMockRequest();
260260
context.getNetworkRequests = () => {
@@ -270,7 +270,7 @@ describe('McpResponse', () => {
270270
});
271271

272272
it('adds console messages when the setting is true', async t => {
273-
await withBrowser(async (response, context) => {
273+
await withMcpContext(async (response, context) => {
274274
response.setIncludeConsoleData(true);
275275
const page = context.getSelectedPage();
276276
const consoleMessagePromise = new Promise<void>(resolve => {
@@ -289,7 +289,7 @@ describe('McpResponse', () => {
289289
});
290290

291291
it('adds a message when no console messages exist', async t => {
292-
await withBrowser(async (response, context) => {
292+
await withMcpContext(async (response, context) => {
293293
response.setIncludeConsoleData(true);
294294
const result = await response.handle('test', context);
295295
assert.ok(result[0].text);
@@ -298,7 +298,7 @@ describe('McpResponse', () => {
298298
});
299299

300300
it("doesn't list the issue message if mapping returns null", async () => {
301-
await withBrowser(async (response, context) => {
301+
await withMcpContext(async (response, context) => {
302302
const mockAggregatedIssue = getMockAggregatedIssue();
303303
const mockDescription = {
304304
file: 'not-existing-description-file.md',
@@ -317,7 +317,7 @@ describe('McpResponse', () => {
317317
});
318318

319319
it('throws error if mapping returns null on get issue details', async () => {
320-
await withBrowser(async (response, context) => {
320+
await withMcpContext(async (response, context) => {
321321
const mockAggregatedIssue = getMockAggregatedIssue();
322322
const mockDescription = {
323323
file: 'not-existing-description-file.md',
@@ -340,7 +340,7 @@ describe('McpResponse', () => {
340340

341341
describe('McpResponse network request filtering', () => {
342342
it('filters network requests by resource type', async t => {
343-
await withBrowser(async (response, context) => {
343+
await withMcpContext(async (response, context) => {
344344
response.setIncludeNetworkRequests(true, {
345345
resourceTypes: ['script', 'stylesheet'],
346346
});
@@ -358,7 +358,7 @@ describe('McpResponse network request filtering', () => {
358358
});
359359

360360
it('filters network requests by single resource type', async t => {
361-
await withBrowser(async (response, context) => {
361+
await withMcpContext(async (response, context) => {
362362
response.setIncludeNetworkRequests(true, {
363363
resourceTypes: ['image'],
364364
});
@@ -375,7 +375,7 @@ describe('McpResponse network request filtering', () => {
375375
});
376376

377377
it('shows no requests when filter matches nothing', async t => {
378-
await withBrowser(async (response, context) => {
378+
await withMcpContext(async (response, context) => {
379379
response.setIncludeNetworkRequests(true, {
380380
resourceTypes: ['font'],
381381
});
@@ -392,7 +392,7 @@ describe('McpResponse network request filtering', () => {
392392
});
393393

394394
it('shows all requests when no filters are provided', async t => {
395-
await withBrowser(async (response, context) => {
395+
await withMcpContext(async (response, context) => {
396396
response.setIncludeNetworkRequests(true);
397397
context.getNetworkRequests = () => {
398398
return [
@@ -410,7 +410,7 @@ describe('McpResponse network request filtering', () => {
410410
});
411411

412412
it('shows all requests when empty resourceTypes array is provided', async t => {
413-
await withBrowser(async (response, context) => {
413+
await withMcpContext(async (response, context) => {
414414
response.setIncludeNetworkRequests(true, {
415415
resourceTypes: [],
416416
});
@@ -431,7 +431,7 @@ describe('McpResponse network request filtering', () => {
431431

432432
describe('McpResponse network pagination', () => {
433433
it('returns all requests when pagination is not provided', async () => {
434-
await withBrowser(async (response, context) => {
434+
await withMcpContext(async (response, context) => {
435435
const requests = Array.from({length: 5}, () => getMockRequest());
436436
context.getNetworkRequests = () => requests;
437437
response.setIncludeNetworkRequests(true);
@@ -444,7 +444,7 @@ describe('McpResponse network pagination', () => {
444444
});
445445

446446
it('returns first page by default', async () => {
447-
await withBrowser(async (response, context) => {
447+
await withMcpContext(async (response, context) => {
448448
const requests = Array.from({length: 30}, (_, idx) =>
449449
getMockRequest({method: `GET-${idx}`}),
450450
);
@@ -461,7 +461,7 @@ describe('McpResponse network pagination', () => {
461461
});
462462

463463
it('returns subsequent page when pageIdx provided', async () => {
464-
await withBrowser(async (response, context) => {
464+
await withMcpContext(async (response, context) => {
465465
const requests = Array.from({length: 25}, (_, idx) =>
466466
getMockRequest({method: `GET-${idx}`}),
467467
);
@@ -479,7 +479,7 @@ describe('McpResponse network pagination', () => {
479479
});
480480

481481
it('handles invalid page number by showing first page', async () => {
482-
await withBrowser(async (response, context) => {
482+
await withMcpContext(async (response, context) => {
483483
const requests = Array.from({length: 5}, () => getMockRequest());
484484
context.getNetworkRequests = () => requests;
485485
response.setIncludeNetworkRequests(true, {

0 commit comments

Comments
 (0)