Skip to content

Commit c09351b

Browse files
committed
rebase
1 parent b508d63 commit c09351b

3 files changed

Lines changed: 31 additions & 10 deletions

File tree

src/DevtoolsUtils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {Mutex} from './Mutex.js';
99
import {DevTools} from './third_party/index.js';
1010
import type {
1111
Browser,
12+
CDPSession,
1213
ConsoleMessage,
1314
Page,
1415
Protocol,
@@ -49,6 +50,8 @@ export interface TargetUniverse {
4950
/** The DevTools target corresponding to the puppeteer Page */
5051
target: DevTools.Target;
5152
universe: DevTools.Foundation.Universe.Universe;
53+
/** The secondary session created for this page */
54+
session: CDPSession;
5255
}
5356
export type TargetUniverseFactoryFn = (page: Page) => Promise<TargetUniverse>;
5457

@@ -158,7 +161,7 @@ const DEFAULT_FACTORY: TargetUniverseFactoryFn = async (page: Page) => {
158161
undefined,
159162
connection,
160163
);
161-
return {target, universe};
164+
return {target, universe, session};
162165
};
163166

164167
// We don't want to pause any DevTools universe session ever on the MCP side.

src/McpContext.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,22 @@ export class McpContext implements Context {
329329
newSettings.networkConditions = options.networkConditions;
330330
}
331331

332+
const secondarySession = this.getDevToolsUniverse(mcpPage)?.session;
332333
if (!options.cpuThrottlingRate) {
333334
await page.emulateCPUThrottling(1);
335+
if (secondarySession) {
336+
await secondarySession.send('Emulation.setCPUThrottlingRate', {
337+
rate: 1,
338+
});
339+
}
334340
delete newSettings.cpuThrottlingRate;
335341
} else {
336342
await page.emulateCPUThrottling(options.cpuThrottlingRate);
343+
if (secondarySession) {
344+
await secondarySession.send('Emulation.setCPUThrottlingRate', {
345+
rate: options.cpuThrottlingRate,
346+
});
347+
}
337348
newSettings.cpuThrottlingRate = options.cpuThrottlingRate;
338349
}
339350

tests/tools/emulation.test.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import assert from 'node:assert';
88
import type {IncomingHttpHeaders} from 'node:http';
9-
import {beforeEach, describe, it} from 'node:test';
9+
import {beforeEach, describe, it, mock} from 'node:test';
1010

1111
import {emulate} from '../../src/tools/emulation.js';
1212
import {
@@ -209,26 +209,33 @@ describe('emulation', () => {
209209
});
210210
});
211211

212-
it('scales the default navigation timeout with cpu throttling rate', async () => {
212+
it('applies cpu throttling to secondary session', async () => {
213213
await withMcpContext(async (response, context) => {
214-
const page = context.getSelectedMcpPage();
215-
let lastNavTimeout: number | undefined;
216-
page.pptrPage.setDefaultNavigationTimeout = (timeout: number) => {
217-
lastNavTimeout = timeout;
218-
};
214+
const mcpPage = context.getSelectedMcpPage();
215+
const universe = context.getDevToolsUniverse(mcpPage);
216+
assert.ok(universe);
217+
218+
const sendSpy = mock.method(universe.session, 'send');
219219

220220
await emulate.handler(
221221
{
222222
params: {
223223
cpuThrottlingRate: 4,
224224
},
225-
page,
225+
page: mcpPage,
226226
},
227227
response,
228228
context,
229229
);
230230

231-
assert.strictEqual(lastNavTimeout, 40000);
231+
assert.ok(sendSpy.mock.calls.length > 0);
232+
const cpuCall = sendSpy.mock.calls.find(
233+
call => call.arguments[0] === 'Emulation.setCPUThrottlingRate',
234+
);
235+
assert.ok(cpuCall);
236+
assert.deepStrictEqual(cpuCall.arguments[1], {rate: 4});
237+
238+
sendSpy.mock.restore();
232239
});
233240
});
234241

0 commit comments

Comments
 (0)