豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/McpContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export class McpContext implements Context {
#consoleCollector: PageCollector<ConsoleMessage | Error>;

#isRunningTrace = false;
#networkConditions: string | null = null;
#cpuThrottlingRate = 1;
#networkConditionsMap = new WeakMap<Page, string>();
#cpuThrottlingRateMap = new WeakMap<Page, number>();
#dialog?: Dialog;

#nextSnapshotId = 1;
Expand Down Expand Up @@ -130,19 +130,27 @@ export class McpContext implements Context {
}

setNetworkConditions(conditions: string | null): void {
this.#networkConditions = conditions;
const page = this.getSelectedPage();
if (conditions === null) {
this.#networkConditionsMap.delete(page);
} else {
this.#networkConditionsMap.set(page, conditions);
}
}

getNetworkConditions(): string | null {
return this.#networkConditions;
const page = this.getSelectedPage();
return this.#networkConditionsMap.get(page) ?? null;
}

setCpuThrottlingRate(rate: number): void {
this.#cpuThrottlingRate = rate;
const page = this.getSelectedPage();
this.#cpuThrottlingRateMap.set(page, rate);
}

getCpuThrottlingRate(): number {
return this.#cpuThrottlingRate;
const page = this.getSelectedPage();
return this.#cpuThrottlingRateMap.get(page) ?? 1;
}

setIsRunningPerformanceTrace(x: boolean): void {
Expand Down
42 changes: 42 additions & 0 deletions tests/tools/emulation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ describe('emulation', () => {
assert.strictEqual(context.getNetworkConditions(), null);
});
});

it('report correctly for the currently selected page', async () => {
await withBrowser(async (response, context) => {
await context.newPage();
await emulateNetwork.handler(
{
params: {
throttlingOption: 'Slow 3G',
},
},
response,
context,
);

assert.strictEqual(context.getNetworkConditions(), 'Slow 3G');

context.setSelectedPageIdx(0);

assert.strictEqual(context.getNetworkConditions(), null);
});
});
});

describe('cpu', () => {
Expand Down Expand Up @@ -92,5 +113,26 @@ describe('emulation', () => {
assert.strictEqual(context.getCpuThrottlingRate(), 1);
});
});

it('report correctly for the currently selected page', async () => {
await withBrowser(async (response, context) => {
await context.newPage();
await emulateCpu.handler(
{
params: {
throttlingRate: 4,
},
},
response,
context,
);

assert.strictEqual(context.getCpuThrottlingRate(), 4);

context.setSelectedPageIdx(0);

assert.strictEqual(context.getCpuThrottlingRate(), 1);
});
});
});
});
Loading