Skip to content

test: migrate network tool tests to unit tests - #2754

Merged
OrKoN merged 8 commits into
ChromeDevTools:mainfrom
maheshsingh20:test/network-unit-tests
Sep 22, 2026
Merged

OrKoN merged 8 commits into
ChromeDevTools:mainfrom
maheshsingh20:test/network-unit-tests

Conversation

@maheshsingh20

Copy link
Copy Markdown
Contributor

Description

This PR migrates tests/tools/network.test.ts off withMcpContext to fast mock-based unit tests using createHandlerMocks():

  • Tool handler unit tests: Converted list_network_requests and get_network_request handlers in tests/tools/network.test.ts to mock unit tests verifying parameter parsing, default fallbacks, DevTools UI selection resolution, and delegation to page and response.
  • Harness & snapshot cleanup: Removed obsolete snapshot file tests/tools/network.test.js.snapshot and withMcpContext / serverHooks() overhead, reducing test duration from ~45s to ~5s.
  • Coverage preserved: Genuine real-network behavior (request isolation per navigation, preserved navigations, redirects, and request lookup across navigations) is fully covered in tests/collectors/PageCollector.test.ts (NetworkCollector) and tests/formatters/NetworkFormatter.test.ts.

Test Plan

  • npm run build
  • node scripts/test.js tests/tools/network.test.ts
  • node scripts/test.js tests/collectors/PageCollector.test.ts
  • node scripts/test.js tests/formatters/NetworkFormatter.test.ts
  • npm run check-format
  • npm run docs:generate

- Convert list_network_requests and get_network_request handlers to mock unit tests using createHandlerMocks

- Remove obsolete snapshot file and withMcpContext overhead
@maheshsingh20

Copy link
Copy Markdown
Contributor Author

Hi @OrKoN,

Following up from #2729, this PR migrates tests/tools/network.test.ts off withMcpContext to fast mock-based unit tests using createHandlerMocks():

  1. Tool handler unit tests: Converted list_network_requests and get_network_request handlers to unit tests with createHandlerMocks(), verifying parameter validation, defaults, DevTools UI selection resolution (cdpRequestId -> networkRequestIdInDevToolsUI), and delegation to page and response.
  2. Coverage preserved: Genuine real-network behavior (request isolation across navigations, preserved navigation history, redirect handling, and request retrieval) is fully covered in tests/collectors/PageCollector.test.ts (NetworkCollector) and tests/formatters/NetworkFormatter.test.ts.
  3. Snapshot & harness cleanup: Removed obsolete snapshot file tests/tools/network.test.js.snapshot and withMcpContext / serverHooks() overhead, dropping test run time from ~45s to ~5s.

All tests and formatting checks pass cleanly. Could you please take a look when you have a moment? Thanks!

@samiyac
samiyac requested a review from OrKoN September 16, 2026 08:14
@OrKoN

OrKoN commented Sep 16, 2026

Copy link
Copy Markdown
Collaborator

Summary of Changes

Converting tests/tools/network.test.ts from real-browser (withMcpContext) snapshot tests into mock-based unit tests using createHandlerMocks aligns well with AGENTS.md for tool handler tests. The new unit tests thoroughly cover parameter parsing, cdpRequestId resolution via DevTools data, and fallback error messages.


Missed E2E / Integration Test Coverage

Because the deleted tests in tests/tools/network.test.ts previously acted as end-to-end tests across multiple layers (Puppeteer/CDP → NetworkCollector → McpPage → McpResponse → NetworkFormatter), removing them without adding corresponding tests elsewhere leaves 4 coverage gaps:

1. Inline Response Body Eviction (<not available anymore>)

  • What was tested: In should get request from previous navigations, navigating across pages (/one → /two → /three) caused Chrome/CDP to evict the network response buffer for reqid=1 (/one). Calling get_network_request for reqid=1 triggered a live CDP rejection on httpResponse.buffer(), verifying that NetworkFormatter.#getFormattedResponseBody (src/formatters/NetworkFormatter.ts:233-254) catches the error and outputs ### Response Body\n<not available anymore> (line 252).
  • Why it's missed: tests/formatters/NetworkFormatter.test.ts:422-463 only tests missing bodies when saving to files (requestFilePath / responseFilePath), which hits NetworkFormatter.#saveResponseBodyToFile (<Response body not available anymore>). With this patch, line 252 (<not available anymore> for inline response bodies) has zero test coverage across the codebase.

2. McpResponse → McpPage Wiring for includePreservedRequests

  • What was tested: End-to-end verification that passing includePreservedRequests: true vs undefined to response.setIncludeNetworkRequests(...) causes McpResponse.#handleNetworkRequestList (src/McpResponse.ts:674-676) to forward includePreservedRequests to McpPage.getNetworkRequests(includePreservedRequests) (src/McpPage.ts:376-378), which in turn queries NetworkCollector.getData(includePreservedRequests).
  • Why it's missed:
    • In tests/McpResponse.test.ts:419-491, every network test stubs context.getSelectedMcpPage().getNetworkRequests = () => [...] without checking the includePreservedRequests argument passed to it.
    • tests/McpPage.test.ts has no unit tests for getNetworkRequests or getNetworkRequestById.

3. Real Browser Event Ordering for HTTP 302 Redirects + Client JS Redirects

  • What was tested: In list requests from previous navigations from redirects, a real browser navigated to /redirect (HTTP 302 to /redirected), which then executed <script>document.location.href = '/redirected-page'</script>.
  • Why it's missed: While tests/collectors/PageCollector.test.ts:327-402 tests NetworkCollector with synthetic page.emit('request', ...) and page.emit('framenavigated', ...) calls, it relies on assumed event ordering. In real Chrome/Puppeteer:
    • An HTTP 302 redirect emits multiple request events with isNavigationRequest() === true before a single framenavigated event fires.
    • NetworkCollector.splitAfterNavigation() (src/collectors/PageCollector.ts:393-412) uses findLastIndex(req => req.isNavigationRequest()) to split buckets.
    • Without this test, there is no real-browser test verifying that Puppeteer's actual request / framenavigated event lifecycle during server-side + client-side redirects integrates properly with NetworkCollector.

4. Orphaned Dead Code in McpPage

  • McpPage.setUpNetworkCollectorForTesting() (src/McpPage.ts:1014-1030) was created specifically for tests/tools/network.test.ts to filter out favicon.ico requests during real browser navigations. With this patch, setUpNetworkCollectorForTesting() is no longer called anywhere in the repository.

Recommendations

  1. Add a unit test in tests/formatters/NetworkFormatter.test.ts for toStringDetailed() with fetchData: true (and no responseFilePath) when response.buffer() rejects, asserting ### Response Body\n<not available anymore>.
  2. Add unit tests in tests/McpResponse.test.ts and tests/McpPage.test.ts verifying that McpResponse forwards includePreservedRequests to page.getNetworkRequests(includePreservedRequests) and that McpPage.getNetworkRequests / McpPage.getNetworkRequestById delegate to networkCollector.
  3. Either keep one lightweight real-browser integration test in tests/collectors/PageCollector.test.ts (or tests/McpPage.test.ts) for real navigation/redirect collection, or remove McpPage.setUpNetworkCollectorForTesting() so it doesn't remain dead code.

@OrKoN OrKoN left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes per my previous comment.

maheshsingh20 and others added 2 commits September 16, 2026 20:45
- Test NetworkFormatter inline body eviction with <not available anymore>
- Test McpResponse forwarding includePreservedRequests to page.getNetworkRequests
- Test McpPage getNetworkRequests and getNetworkRequestById delegation
- Retain real browser redirect integration test using setUpNetworkCollectorForTesting
@maheshsingh20

Copy link
Copy Markdown
Contributor Author

@OrKoN Thank you for the detailed feedback! I have addressed all 4 coverage gaps:

  1. Inline Response Body Eviction: Added unit test in tests/formatters/NetworkFormatter.test.ts verifying that when httpResponse.buffer() throws and no responseFilePath is provided, NetworkFormatter outputs ### Response Body\n<not available anymore> and sets toJSONDetailed().responseBody to '<not available anymore>'.
  2. McpResponse & McpPage Delegation Wiring:
    • Added unit test in tests/McpResponse.test.ts verifying response.handle() forwards includePreservedRequests to page.getNetworkRequests().
    • Added unit tests in tests/McpPage.test.ts verifying delegation of getNetworkRequests and getNetworkRequestById to networkCollector.
    • Initialized emulationSettings in createMockMcpPage() in tests/mocks.ts to support mock page formatting.
  3. Real Browser Redirect Integration: Added a real-browser test in tests/McpPage.test.ts testing request collection across an HTTP 302 redirect followed by a client-side JavaScript redirect.
  4. setUpNetworkCollectorForTesting: Utilized await mcpPage.setUpNetworkCollectorForTesting() in the redirect integration test so it remains covered.

All formatting checks and tests are passing. Ready for another look!

@OrKoN
OrKoN self-requested a review September 17, 2026 07:51
Comment thread tests/tools/network.test.ts Outdated
Comment thread tests/mocks.ts Outdated
maheshsingh20 and others added 4 commits September 17, 2026 16:35
- Stub page.emulationSettings directly in McpResponse test
- Remove emulationSettings initialization from createMockMcpPage in tests/mocks.ts
- Restore Copyright 2025 in tests/tools/network.test.ts
@maheshsingh20

Copy link
Copy Markdown
Contributor Author

@OrKoN The merge conflict with main has been resolved (updated tests/tools/network.test.ts to pass args to listNetworkRequests and getNetworkRequest per #2659). Build and formatting checks are all green. Could you please approve the workflow runs and take another look?

@OrKoN
OrKoN enabled auto-merge September 22, 2026 09:36
@OrKoN
OrKoN added this pull request to the merge queue Sep 22, 2026
Merged via the queue into ChromeDevTools:main with commit a3e8bfa Sep 22, 2026
20 checks passed
@maheshsingh20

Copy link
Copy Markdown
Contributor Author

Hi @OrKoN ,
I am really excited for new task, please share next step and guidelines.

@maheshsingh20
maheshsingh20 deleted the test/network-unit-tests branch September 23, 2026 17:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants