Skip to content

Commit c06f452

Browse files
authored
refactor: merge emulate tools into one (#494)
Merges emulateCpu and emulateNetwork into a single tool to reduce token consumption. Changes descriptions to be more concise.
1 parent 60fd66e commit c06f452

File tree

4 files changed

+69
-88
lines changed

4 files changed

+69
-88
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,8 @@ If you run into any issues, checkout our [troubleshooting guide](./docs/troubles
260260
- [`new_page`](docs/tool-reference.md#new_page)
261261
- [`select_page`](docs/tool-reference.md#select_page)
262262
- [`wait_for`](docs/tool-reference.md#wait_for)
263-
- **Emulation** (3 tools)
264-
- [`emulate_cpu`](docs/tool-reference.md#emulate_cpu)
265-
- [`emulate_network`](docs/tool-reference.md#emulate_network)
263+
- **Emulation** (2 tools)
264+
- [`emulate`](docs/tool-reference.md#emulate)
266265
- [`resize_page`](docs/tool-reference.md#resize_page)
267266
- **Performance** (3 tools)
268267
- [`performance_analyze_insight`](docs/tool-reference.md#performance_analyze_insight)

docs/tool-reference.md

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
- [`new_page`](#new_page)
1919
- [`select_page`](#select_page)
2020
- [`wait_for`](#wait_for)
21-
- **[Emulation](#emulation)** (3 tools)
22-
- [`emulate_cpu`](#emulate_cpu)
23-
- [`emulate_network`](#emulate_network)
21+
- **[Emulation](#emulation)** (2 tools)
22+
- [`emulate`](#emulate)
2423
- [`resize_page`](#resize_page)
2524
- **[Performance](#performance)** (3 tools)
2625
- [`performance_analyze_insight`](#performance_analyze_insight)
@@ -190,23 +189,14 @@
190189

191190
## Emulation
192191

193-
### `emulate_cpu`
192+
### `emulate`
194193

195-
**Description:** Emulates CPU throttling by slowing down the selected page's execution.
194+
**Description:** Emulates various features on the selected page.
196195

197196
**Parameters:**
198197

199-
- **throttlingRate** (number) **(required)**: The CPU throttling rate representing the slowdown factor 1-20x. Set the rate to 1 to disable throttling
200-
201-
---
202-
203-
### `emulate_network`
204-
205-
**Description:** Emulates network conditions such as throttling or offline mode on the selected page.
206-
207-
**Parameters:**
208-
209-
- **throttlingOption** (enum: "No emulation", "Offline", "Slow 3G", "Fast 3G", "Slow 4G", "Fast 4G") **(required)**: The network throttling option to emulate. Available throttling options are: No emulation, Offline, Slow 3G, Fast 3G, Slow 4G, Fast 4G. Set to "No emulation" to disable. Set to "Offline" to simulate offline network conditions.
198+
- **cpuThrottlingRate** (number) _(optional)_: Represents the CPU slowdown factor. Set the rate to 1 to disable throttling. If omitted, throttling remains unchanged.
199+
- **networkConditions** (enum: "No emulation", "Offline", "Slow 3G", "Fast 3G", "Slow 4G", "Fast 4G") _(optional)_: Throttle network. Set to "No emulation" to disable. If omitted, conditions remain unchanged.
210200

211201
---
212202

src/tools/emulation.ts

Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,73 +15,65 @@ const throttlingOptions: [string, ...string[]] = [
1515
...Object.keys(PredefinedNetworkConditions),
1616
];
1717

18-
export const emulateNetwork = defineTool({
19-
name: 'emulate_network',
20-
description: `Emulates network conditions such as throttling or offline mode on the selected page.`,
18+
export const emulate = defineTool({
19+
name: 'emulate',
20+
description: `Emulates various features on the selected page.`,
2121
annotations: {
2222
category: ToolCategory.EMULATION,
2323
readOnlyHint: false,
2424
},
2525
schema: {
26-
throttlingOption: zod
26+
networkConditions: zod
2727
.enum(throttlingOptions)
28+
.optional()
2829
.describe(
29-
`The network throttling option to emulate. Available throttling options are: ${throttlingOptions.join(', ')}. Set to "No emulation" to disable. Set to "Offline" to simulate offline network conditions.`,
30+
`Throttle network. Set to "No emulation" to disable. If omitted, conditions remain unchanged.`,
3031
),
31-
},
32-
handler: async (request, _response, context) => {
33-
const page = context.getSelectedPage();
34-
const conditions = request.params.throttlingOption;
35-
36-
if (conditions === 'No emulation') {
37-
await page.emulateNetworkConditions(null);
38-
context.setNetworkConditions(null);
39-
return;
40-
}
41-
42-
if (conditions === 'Offline') {
43-
await page.emulateNetworkConditions({
44-
offline: true,
45-
download: 0,
46-
upload: 0,
47-
latency: 0,
48-
});
49-
context.setNetworkConditions('Offline');
50-
return;
51-
}
52-
53-
if (conditions in PredefinedNetworkConditions) {
54-
const networkCondition =
55-
PredefinedNetworkConditions[
56-
conditions as keyof typeof PredefinedNetworkConditions
57-
];
58-
await page.emulateNetworkConditions(networkCondition);
59-
context.setNetworkConditions(conditions);
60-
}
61-
},
62-
});
63-
64-
export const emulateCpu = defineTool({
65-
name: 'emulate_cpu',
66-
description: `Emulates CPU throttling by slowing down the selected page's execution.`,
67-
annotations: {
68-
category: ToolCategory.EMULATION,
69-
readOnlyHint: false,
70-
},
71-
schema: {
72-
throttlingRate: zod
32+
cpuThrottlingRate: zod
7333
.number()
7434
.min(1)
7535
.max(20)
36+
.optional()
7637
.describe(
77-
'The CPU throttling rate representing the slowdown factor 1-20x. Set the rate to 1 to disable throttling',
38+
'Represents the CPU slowdown factor. Set the rate to 1 to disable throttling. If omitted, throttling remains unchanged.',
7839
),
7940
},
8041
handler: async (request, _response, context) => {
8142
const page = context.getSelectedPage();
82-
const {throttlingRate} = request.params;
43+
const networkConditions = request.params.networkConditions;
44+
const cpuThrottlingRate = request.params.cpuThrottlingRate;
8345

84-
await page.emulateCPUThrottling(throttlingRate);
85-
context.setCpuThrottlingRate(throttlingRate);
46+
if (networkConditions) {
47+
if (networkConditions === 'No emulation') {
48+
await page.emulateNetworkConditions(null);
49+
context.setNetworkConditions(null);
50+
return;
51+
}
52+
53+
if (networkConditions === 'Offline') {
54+
await page.emulateNetworkConditions({
55+
offline: true,
56+
download: 0,
57+
upload: 0,
58+
latency: 0,
59+
});
60+
context.setNetworkConditions('Offline');
61+
return;
62+
}
63+
64+
if (networkConditions in PredefinedNetworkConditions) {
65+
const networkCondition =
66+
PredefinedNetworkConditions[
67+
networkConditions as keyof typeof PredefinedNetworkConditions
68+
];
69+
await page.emulateNetworkConditions(networkCondition);
70+
context.setNetworkConditions(networkConditions);
71+
}
72+
}
73+
74+
if (cpuThrottlingRate) {
75+
await page.emulateCPUThrottling(cpuThrottlingRate);
76+
context.setCpuThrottlingRate(cpuThrottlingRate);
77+
}
8678
},
8779
});

tests/tools/emulation.test.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
import assert from 'node:assert';
77
import {describe, it} from 'node:test';
88

9-
import {emulateCpu, emulateNetwork} from '../../src/tools/emulation.js';
9+
import {emulate} from '../../src/tools/emulation.js';
1010
import {withBrowser} from '../utils.js';
1111

1212
describe('emulation', () => {
1313
describe('network', () => {
1414
it('emulates offline network conditions', async () => {
1515
await withBrowser(async (response, context) => {
16-
await emulateNetwork.handler(
16+
await emulate.handler(
1717
{
1818
params: {
19-
throttlingOption: 'Offline',
19+
networkConditions: 'Offline',
2020
},
2121
},
2222
response,
@@ -26,12 +26,12 @@ describe('emulation', () => {
2626
assert.strictEqual(context.getNetworkConditions(), 'Offline');
2727
});
2828
});
29-
it('emulates network throttling when the throttling option is valid ', async () => {
29+
it('emulates network throttling when the throttling option is valid', async () => {
3030
await withBrowser(async (response, context) => {
31-
await emulateNetwork.handler(
31+
await emulate.handler(
3232
{
3333
params: {
34-
throttlingOption: 'Slow 3G',
34+
networkConditions: 'Slow 3G',
3535
},
3636
},
3737
response,
@@ -44,10 +44,10 @@ describe('emulation', () => {
4444

4545
it('disables network emulation', async () => {
4646
await withBrowser(async (response, context) => {
47-
await emulateNetwork.handler(
47+
await emulate.handler(
4848
{
4949
params: {
50-
throttlingOption: 'No emulation',
50+
networkConditions: 'No emulation',
5151
},
5252
},
5353
response,
@@ -60,10 +60,10 @@ describe('emulation', () => {
6060

6161
it('does not set throttling when the network throttling is not one of the predefined options', async () => {
6262
await withBrowser(async (response, context) => {
63-
await emulateNetwork.handler(
63+
await emulate.handler(
6464
{
6565
params: {
66-
throttlingOption: 'Slow 11G',
66+
networkConditions: 'Slow 11G',
6767
},
6868
},
6969
response,
@@ -77,10 +77,10 @@ describe('emulation', () => {
7777
it('report correctly for the currently selected page', async () => {
7878
await withBrowser(async (response, context) => {
7979
await context.newPage();
80-
await emulateNetwork.handler(
80+
await emulate.handler(
8181
{
8282
params: {
83-
throttlingOption: 'Slow 3G',
83+
networkConditions: 'Slow 3G',
8484
},
8585
},
8686
response,
@@ -99,10 +99,10 @@ describe('emulation', () => {
9999
describe('cpu', () => {
100100
it('emulates cpu throttling when the rate is valid (1-20x)', async () => {
101101
await withBrowser(async (response, context) => {
102-
await emulateCpu.handler(
102+
await emulate.handler(
103103
{
104104
params: {
105-
throttlingRate: 4,
105+
cpuThrottlingRate: 4,
106106
},
107107
},
108108
response,
@@ -116,10 +116,10 @@ describe('emulation', () => {
116116
it('disables cpu throttling', async () => {
117117
await withBrowser(async (response, context) => {
118118
context.setCpuThrottlingRate(4); // Set it to something first.
119-
await emulateCpu.handler(
119+
await emulate.handler(
120120
{
121121
params: {
122-
throttlingRate: 1,
122+
cpuThrottlingRate: 1,
123123
},
124124
},
125125
response,
@@ -133,10 +133,10 @@ describe('emulation', () => {
133133
it('report correctly for the currently selected page', async () => {
134134
await withBrowser(async (response, context) => {
135135
await context.newPage();
136-
await emulateCpu.handler(
136+
await emulate.handler(
137137
{
138138
params: {
139-
throttlingRate: 4,
139+
cpuThrottlingRate: 4,
140140
},
141141
},
142142
response,

0 commit comments

Comments
 (0)