|
3 | 3 | * Copyright 2025 Google LLC |
4 | 4 | * SPDX-License-Identifier: Apache-2.0 |
5 | 5 | */ |
6 | | -import {describe, it} from 'node:test'; |
| 6 | +import { describe, it } from 'node:test'; |
7 | 7 | import assert from 'assert'; |
8 | 8 |
|
9 | | -import {getMockRequest, html, withBrowser} from './utils.js'; |
| 9 | +import { getMockRequest, html, withBrowser } from './utils.js'; |
10 | 10 |
|
11 | 11 | describe('McpResponse', () => { |
12 | 12 | it('list pages', async () => { |
@@ -120,7 +120,7 @@ Navigation timeout set to 100000 ms`, |
120 | 120 | }); |
121 | 121 | it('adds image when image is attached', async () => { |
122 | 122 | await withBrowser(async (response, context) => { |
123 | | - response.attachImage({data: 'imageBase64', mimeType: 'image/png'}); |
| 123 | + response.attachImage({ data: 'imageBase64', mimeType: 'image/png' }); |
124 | 124 | const result = await response.handle('test', context); |
125 | 125 | assert.strictEqual(result[0].text, `# test response`); |
126 | 126 | assert.equal(result[1].type, 'image'); |
@@ -181,12 +181,11 @@ Call browser_handle_dialog to handle it before continuing.`, |
181 | 181 | return [getMockRequest()]; |
182 | 182 | }; |
183 | 183 | const result = await response.handle('test', context); |
184 | | - assert.strictEqual( |
185 | | - result[0].text, |
186 | | - `# test response |
187 | | -## Network requests |
188 | | -http://example.com GET [pending]`, |
| 184 | + const text = result[0].text as string; |
| 185 | + assert.ok( |
| 186 | + text.includes(`## Network requests`), |
189 | 187 | ); |
| 188 | + assert.ok(text.includes('http://example.com GET [pending]')); |
190 | 189 | }); |
191 | 190 | }); |
192 | 191 | it('does not include network requests when setting is false', async () => { |
@@ -217,6 +216,7 @@ Status: [pending] |
217 | 216 | ### Request Headers |
218 | 217 | - content-size:10 |
219 | 218 | ## Network requests |
| 219 | +Showing 1-1 of 1. |
220 | 220 | http://example.com GET [pending]`, |
221 | 221 | ); |
222 | 222 | }); |
@@ -261,3 +261,70 @@ Log>`), |
261 | 261 | }); |
262 | 262 | }); |
263 | 263 | }); |
| 264 | + |
| 265 | +describe('McpResponse network pagination', () => { |
| 266 | + it('returns all requests when pagination is not provided', async () => { |
| 267 | + await withBrowser(async (response, context) => { |
| 268 | + const requests = Array.from({ length: 5 }, () => getMockRequest()); |
| 269 | + context.getNetworkRequests = () => requests; |
| 270 | + response.setIncludeNetworkRequests(true); |
| 271 | + const result = await response.handle('test', context); |
| 272 | + const text = (result[0].text as string).toString(); |
| 273 | + assert.ok(text.includes('Showing 1-5 of 5.')); |
| 274 | + assert.ok(!text.includes('Next:')); |
| 275 | + assert.ok(!text.includes('Prev:')); |
| 276 | + }); |
| 277 | + }); |
| 278 | + |
| 279 | + it('returns first page by default', async () => { |
| 280 | + await withBrowser(async (response, context) => { |
| 281 | + const requests = Array.from({ length: 30 }, (_, idx) => |
| 282 | + getMockRequest({ method: `GET-${idx}` }), |
| 283 | + ); |
| 284 | + context.getNetworkRequests = () => { |
| 285 | + return requests; |
| 286 | + }; |
| 287 | + response.setIncludeNetworkRequests(true, { pageSize: 10 }); |
| 288 | + const result = await response.handle('test', context); |
| 289 | + const text = (result[0].text as string).toString(); |
| 290 | + assert.ok(text.includes('Showing 1-10 of 30.')); |
| 291 | + assert.ok(text.includes('Next: 10')); |
| 292 | + assert.ok(!text.includes('Prev:')); |
| 293 | + }); |
| 294 | + }); |
| 295 | + |
| 296 | + it('returns subsequent page when token provided', async () => { |
| 297 | + await withBrowser(async (response, context) => { |
| 298 | + const requests = Array.from({ length: 25 }, (_, idx) => |
| 299 | + getMockRequest({ method: `GET-${idx}` }), |
| 300 | + ); |
| 301 | + context.getNetworkRequests = () => requests; |
| 302 | + response.setIncludeNetworkRequests(true, { |
| 303 | + pageSize: 10, |
| 304 | + pageToken: '10', |
| 305 | + }); |
| 306 | + const result = await response.handle('test', context); |
| 307 | + const text = (result[0].text as string).toString(); |
| 308 | + assert.ok(text.includes('Showing 11-20 of 25.')); |
| 309 | + assert.ok(text.includes('Next: 20')); |
| 310 | + assert.ok(text.includes('Prev: 0')); |
| 311 | + }); |
| 312 | + }); |
| 313 | + |
| 314 | + it('handles invalid token by showing first page', async () => { |
| 315 | + await withBrowser(async (response, context) => { |
| 316 | + const requests = Array.from({ length: 5 }, () => getMockRequest()); |
| 317 | + context.getNetworkRequests = () => requests; |
| 318 | + response.setIncludeNetworkRequests(true, { |
| 319 | + pageSize: 2, |
| 320 | + pageToken: 'invalid', |
| 321 | + }); |
| 322 | + const result = await response.handle('test', context); |
| 323 | + const text = (result[0].text as string).toString(); |
| 324 | + assert.ok( |
| 325 | + text.includes('Invalid page token provided. Showing first page.'), |
| 326 | + ); |
| 327 | + assert.ok(text.includes('Showing 1-2 of 5.')); |
| 328 | + }); |
| 329 | + }); |
| 330 | +}); |
0 commit comments