Skip to content

test: migrate page URL validation tests to mocks - #3182

Merged
OrKoN merged 2 commits into
ChromeDevTools:mainfrom
Patrick-SCH03:test/migrate-page-url-tests-to-mocks
Oct 16, 2026
Merged

OrKoN merged 2 commits into
ChromeDevTools:mainfrom
Patrick-SCH03:test/migrate-page-url-tests-to-mocks

Conversation

@Patrick-SCH03

Copy link
Copy Markdown
Contributor

Part of #2639. Picks up #2758 (closed as stale) and applies the review feedback from it.

The list_pages handler test and the URL validation tests for new_page and navigate_page only check that the handler validates its input and stops early, so they don't need a real browser. This migrates them in place to createHandlerMocks, following AGENTS.md:

  • list_pages › list pages: asserts setIncludePages(true), setListThirdPartyDeveloperTools() and setListWebMcpTools().
  • new_page › javascript/data/vbscript URLs, file: URLs, unparsable URL, chrome:/chrome-untrusted: URLs, chrome-extension: URLs: assert the exact error and that context.newPage is never called, so no tab is opened for a rejected URL.
  • navigate_page › the same five cases: assert the exact error and that page.pptrPage.goto is never called.

As requested in #2758, the flag-dependent cases pass the option directly (createHandlerMocks({javascriptEvaluation: false}), createHandlerMocks({fileNavigations: false})) instead of parsing CLI flags, and every migrated test destructures args from createHandlerMocks() instead of calling createMockParsedArguments().

The rest of the file still uses the real browser and is unchanged: closed-page recovery, extension pages and service workers, side panels, dialogs, chrome://newtab/, background/focus, isolated contexts, history navigation, beforeunload, initScript, resize and get_tab_id. validateUrl itself stays covered by tests/utils/url.test.ts.

Testing

  • npm run build && NODE_TEST_REPORTER=spec node scripts/test.js tests/tools/pages.test.ts: 63/63 pass (63 before as well). list pages goes from about 650 ms to about 35 ms; the URL validation tests from 35–100 ms to 20–40 ms.
  • The 11 migrated tests also pass with PUPPETEER_EXECUTABLE_PATH pointing at a nonexistent binary, so none of them starts Chrome.
  • Sanity check that they still catch regressions (each change made all the targeted tests fail, then reverted):
    • removing setListWebMcpTools() from list_pages fails list pages;
    • moving validateUrl after context.newPage in new_page fails all five new_page cases on notCalled(context.newPage);
    • passing javascriptEvaluation/fileNavigations as undefined and categoryExtensions: true to validateUrl fails the javascript, file and chrome-extension cases in both tools;
    • removing the validateUrl call from navigate_page fails all five navigate_page cases.
  • eslint and prettier are clean; only tests/tools/pages.test.ts changes.

Part of ChromeDevTools#2639. Picks up ChromeDevTools#2758.

Moves the list_pages handler test and the new_page/navigate_page URL
validation tests in tests/tools/pages.test.ts from a real browser to
createHandlerMocks. Flag-dependent cases pass the option directly
(createHandlerMocks({javascriptEvaluation: false}) and
{fileNavigations: false}) instead of parsing CLI flags, and args is
destructured from createHandlerMocks(). Rejected URLs now assert that
no tab is opened (context.newPage) and no navigation happens
(page.pptrPage.goto).
@OrKoN
OrKoN self-requested a review October 16, 2026 08:07

@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.

One more improvement:

  1. tests/tools/pages.test.ts (all five navigate_page cases): assert sinon.assert.notCalled(page.waitForEventsAfterAction) instead of notCalled(page.pptrPage.goto). goto runs inside the callback passed to page.waitForEventsAfterAction(), which is a stub on the mock McpPage and never invokes the callback, so the current assertion passes even when validation is removed; waitForEventsAfterAction is the first call the handler makes after validateUrl().

Nits

  1. tests/tools/pages.test.ts (navigate_page cases): also add sinon.assert.notCalled(page.pptrPage.evaluateOnNewDocument) in one case with initScript set, or drop the goto assertion entirely; evaluateOnNewDocument is the only pptrPage call the handler makes directly before the action callback.

goto runs inside the waitForEventsAfterAction callback, which the mock
page never invokes, so asserting goto was not called proved nothing.
Assert that waitForEventsAfterAction is not called instead, and that the
initScript is not registered for a rejected URL.
@Patrick-SCH03

Copy link
Copy Markdown
Contributor Author

Thanks, good catch. Done in 5fd68f6: all five navigate_page cases now assert sinon.assert.notCalled(page.waitForEventsAfterAction), and the file: case also passes an initScript and asserts sinon.assert.notCalled(page.pptrPage.evaluateOnNewDocument). The goto assertions are gone. With the validateUrl call in navigate_page disabled, all five cases fail.

@OrKoN
OrKoN enabled auto-merge October 16, 2026 08:47
@OrKoN
OrKoN added this pull request to the merge queue Oct 16, 2026
Merged via the queue into ChromeDevTools:main with commit ed8935e Oct 16, 2026
14 checks passed
zjdCander pushed a commit to zjdCander/chrome-devtools-mcp that referenced this pull request Oct 19, 2026
## Summary

Add a **Pre-submit checklist** section to `AGENTS.md`. Each item comes
from a recent review finding or a landed bug fix in this repository.

The checklist has five groups:

- **Scope and PR description**: one change per PR, title and commit type
match, description lists every behavior change and matches the diff,
`npm run gen` and `npm run format` ran.
- **Design**: fix in the lowest layer that owns the data, fix upstream
(Puppeteer, devtools-frontend) first, one helper for repeated code,
options objects instead of positional `undefined` arguments, validation
in Zod schemas.
- **Correctness and edge cases**: no stale state after a failed call,
artifacts written before a throw, abandoned async continuations, mutex
guards, file removal races, `dispose()` releases every resource, secrets
never reach logs, path parameters validated even when they parse as
URLs.
- **Tests**: every new function has a test, no vacuous assertions,
`notCalled` targets the first call after validation, exact-argument
assertions, reusable stubs in `tests/mocks.ts`, bounded timeouts, no
second CDP session for event waits.
- **Documentation and output**: tool descriptions state when a parameter
is ignored, skills stay generic, one-sentence inline comments.

Sources: review comments on ChromeDevTools#2679, ChromeDevTools#2797, ChromeDevTools#2859, ChromeDevTools#2881, ChromeDevTools#2901, ChromeDevTools#3048,
ChromeDevTools#3082, ChromeDevTools#3109, ChromeDevTools#3120, ChromeDevTools#3132, ChromeDevTools#3134, ChromeDevTools#3139, ChromeDevTools#3148, ChromeDevTools#3156, ChromeDevTools#3158, ChromeDevTools#3161,
ChromeDevTools#3182, ChromeDevTools#3183, ChromeDevTools#3186, ChromeDevTools#3187, ChromeDevTools#3188, ChromeDevTools#3189, ChromeDevTools#3192, ChromeDevTools#3196, ChromeDevTools#3197, ChromeDevTools#3198,
ChromeDevTools#3203, ChromeDevTools#3204, and fixes ChromeDevTools#3150, ChromeDevTools#3160, ChromeDevTools#3185, ChromeDevTools#3202, ChromeDevTools#3207, ChromeDevTools#3208.

Existing content in `AGENTS.md` is unchanged.

Closes ChromeDevTools#3222
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