Skip to content

Commit c5933a0

Browse files
committed
chore: fix pr comment
1 parent 420399d commit c5933a0

File tree

4 files changed

+57
-86
lines changed

4 files changed

+57
-86
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,8 @@ If you run into any issues, checkout our [troubleshooting guide](./docs/troubles
296296
- [`new_page`](docs/tool-reference.md#new_page)
297297
- [`select_page`](docs/tool-reference.md#select_page)
298298
- [`wait_for`](docs/tool-reference.md#wait_for)
299-
- **Emulation** (3 tools)
299+
- **Emulation** (2 tools)
300300
- [`emulate`](docs/tool-reference.md#emulate)
301-
- [`emulate_geolocation`](docs/tool-reference.md#emulate_geolocation)
302301
- [`resize_page`](docs/tool-reference.md#resize_page)
303302
- **Performance** (3 tools)
304303
- [`performance_analyze_insight`](docs/tool-reference.md#performance_analyze_insight)

docs/tool-reference.md

Lines changed: 4 additions & 13 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)
21+
- **[Emulation](#emulation)** (2 tools)
2222
- [`emulate`](#emulate)
23-
- [`emulate_geolocation`](#emulate_geolocation)
2423
- [`resize_page`](#resize_page)
2524
- **[Performance](#performance)** (3 tools)
2625
- [`performance_analyze_insight`](#performance_analyze_insight)
@@ -196,22 +195,14 @@
196195

197196
**Parameters:**
198197

198+
- **clearGeolocation** (boolean) _(optional)_: Set to true to clear the geolocation override.
199199
- **cpuThrottlingRate** (number) _(optional)_: Represents the CPU slowdown factor. Set the rate to 1 to disable throttling. If omitted, throttling remains unchanged.
200+
- **latitude** (number) _(optional)_: Latitude between -90 and 90 for geolocation emulation. Must be provided together with longitude.
201+
- **longitude** (number) _(optional)_: Longitude between -180 and 180 for geolocation emulation. Must be provided together with latitude.
200202
- **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.
201203

202204
---
203205

204-
### `emulate_geolocation`
205-
206-
**Description:** Emulates geolocation on the selected page. Useful for testing location-based features.
207-
208-
**Parameters:**
209-
210-
- **latitude** (number) _(optional)_: Latitude between -90 and 90. Omit latitude and longitude to clear the override.
211-
- **longitude** (number) _(optional)_: Longitude between -180 and 180. Omit latitude and longitude to clear the override.
212-
213-
---
214-
215206
### `resize_page`
216207

217208
**Description:** Resizes the selected page's window so that the page has specified dimension

src/tools/emulation.ts

Lines changed: 40 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,50 @@ export const emulate = defineTool({
3737
.describe(
3838
'Represents the CPU slowdown factor. Set the rate to 1 to disable throttling. If omitted, throttling remains unchanged.',
3939
),
40+
latitude: zod
41+
.number()
42+
.min(-90)
43+
.max(90)
44+
.optional()
45+
.describe(
46+
'Latitude between -90 and 90 for geolocation emulation. Must be provided together with longitude.',
47+
),
48+
longitude: zod
49+
.number()
50+
.min(-180)
51+
.max(180)
52+
.optional()
53+
.describe(
54+
'Longitude between -180 and 180 for geolocation emulation. Must be provided together with latitude.',
55+
),
56+
clearGeolocation: zod
57+
.boolean()
58+
.optional()
59+
.describe('Set to true to clear the geolocation override.'),
4060
},
4161
handler: async (request, _response, context) => {
4262
const page = context.getSelectedPage();
43-
const networkConditions = request.params.networkConditions;
44-
const cpuThrottlingRate = request.params.cpuThrottlingRate;
63+
const {
64+
networkConditions,
65+
cpuThrottlingRate,
66+
latitude,
67+
longitude,
68+
clearGeolocation,
69+
} = request.params;
4570

4671
if (networkConditions) {
4772
if (networkConditions === 'No emulation') {
4873
await page.emulateNetworkConditions(null);
4974
context.setNetworkConditions(null);
50-
return;
51-
}
52-
53-
if (networkConditions === 'Offline') {
75+
} else if (networkConditions === 'Offline') {
5476
await page.emulateNetworkConditions({
5577
offline: true,
5678
download: 0,
5779
upload: 0,
5880
latency: 0,
5981
});
6082
context.setNetworkConditions('Offline');
61-
return;
62-
}
63-
64-
if (networkConditions in PredefinedNetworkConditions) {
83+
} else if (networkConditions in PredefinedNetworkConditions) {
6584
const networkCondition =
6685
PredefinedNetworkConditions[
6786
networkConditions as keyof typeof PredefinedNetworkConditions
@@ -75,59 +94,19 @@ export const emulate = defineTool({
7594
await page.emulateCPUThrottling(cpuThrottlingRate);
7695
context.setCpuThrottlingRate(cpuThrottlingRate);
7796
}
78-
},
79-
});
8097

81-
export const emulateGeolocation = defineTool({
82-
name: 'emulate_geolocation',
83-
description: `Emulates geolocation on the selected page. Useful for testing location-based features.`,
84-
annotations: {
85-
category: ToolCategory.EMULATION,
86-
readOnlyHint: false,
87-
},
88-
schema: {
89-
latitude: zod
90-
.number()
91-
.min(-90)
92-
.max(90)
93-
.optional()
94-
.describe(
95-
'Latitude between -90 and 90. Omit latitude and longitude to clear the override.',
96-
),
97-
longitude: zod
98-
.number()
99-
.min(-180)
100-
.max(180)
101-
.optional()
102-
.describe(
103-
'Longitude between -180 and 180. Omit latitude and longitude to clear the override.',
104-
),
105-
},
106-
handler: async (request, _response, context) => {
107-
const page = context.getSelectedPage();
108-
const {latitude, longitude} = request.params;
109-
110-
if (latitude === undefined && longitude === undefined) {
111-
// Clear geolocation override
112-
await page.setGeolocation({
113-
latitude: 0,
114-
longitude: 0,
115-
});
98+
if (clearGeolocation) {
99+
await page.setGeolocation({latitude: 0, longitude: 0});
116100
context.setGeolocation(null);
117-
} else if (latitude !== undefined && longitude !== undefined) {
118-
// Set geolocation override
119-
await page.setGeolocation({
120-
latitude,
121-
longitude,
122-
});
123-
context.setGeolocation({
124-
latitude,
125-
longitude,
126-
});
127-
} else {
128-
throw new Error(
129-
'Both latitude and longitude must be provided, or both must be omitted to clear the override.',
130-
);
101+
} else if (latitude !== undefined || longitude !== undefined) {
102+
if (latitude !== undefined && longitude !== undefined) {
103+
await page.setGeolocation({latitude, longitude});
104+
context.setGeolocation({latitude, longitude});
105+
} else {
106+
throw new Error(
107+
'Both latitude and longitude must be provided together for geolocation emulation.',
108+
);
109+
}
131110
}
132111
},
133112
});

tests/tools/emulation.test.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import assert from 'node:assert';
88
import {describe, it} from 'node:test';
99

10-
import {emulate, emulateGeolocation} from '../../src/tools/emulation.js';
10+
import {emulate} from '../../src/tools/emulation.js';
1111
import {withMcpContext} from '../utils.js';
1212

1313
describe('emulation', () => {
@@ -156,7 +156,7 @@ describe('emulation', () => {
156156
describe('geolocation', () => {
157157
it('emulates geolocation with latitude and longitude', async () => {
158158
await withMcpContext(async (response, context) => {
159-
await emulateGeolocation.handler(
159+
await emulate.handler(
160160
{
161161
params: {
162162
latitude: 48.137154,
@@ -173,10 +173,10 @@ describe('emulation', () => {
173173
});
174174
});
175175

176-
it('clears geolocation override when both params are omitted', async () => {
176+
it('clears geolocation override when clearGeolocation is true', async () => {
177177
await withMcpContext(async (response, context) => {
178178
// First set a geolocation
179-
await emulateGeolocation.handler(
179+
await emulate.handler(
180180
{
181181
params: {
182182
latitude: 48.137154,
@@ -189,10 +189,12 @@ describe('emulation', () => {
189189

190190
assert.notStrictEqual(context.getGeolocation(), null);
191191

192-
// Then clear it by omitting both params
193-
await emulateGeolocation.handler(
192+
// Then clear it using clearGeolocation
193+
await emulate.handler(
194194
{
195-
params: {},
195+
params: {
196+
clearGeolocation: true,
197+
},
196198
},
197199
response,
198200
context,
@@ -206,7 +208,7 @@ describe('emulation', () => {
206208
await withMcpContext(async (response, context) => {
207209
await assert.rejects(
208210
async () => {
209-
await emulateGeolocation.handler(
211+
await emulate.handler(
210212
{
211213
params: {
212214
latitude: 48.137154,
@@ -218,15 +220,15 @@ describe('emulation', () => {
218220
},
219221
{
220222
message:
221-
'Both latitude and longitude must be provided, or both must be omitted to clear the override.',
223+
'Both latitude and longitude must be provided together for geolocation emulation.',
222224
},
223225
);
224226
});
225227
});
226228

227229
it('reports correctly for the currently selected page', async () => {
228230
await withMcpContext(async (response, context) => {
229-
await emulateGeolocation.handler(
231+
await emulate.handler(
230232
{
231233
params: {
232234
latitude: 48.137154,

0 commit comments

Comments
 (0)