Skip to content

test(build): make the fake fetch method-aware and header-readable - #297

Merged
netravnen merged 1 commit into
dev-nextfrom
test/browser-shim-fetch-fidelity
Aug 19, 2026
Merged

netravnen merged 1 commit into
dev-nextfrom
test/browser-shim-fetch-fidelity

Conversation

@netravnen

Copy link
Copy Markdown
Contributor

The node:test harness could not express the cases needed to test
lib/admincom-common.js's retry/backoff wrappers, which is why that code
shipped with a defect nothing caught.

Two limits caused it. The fake response exposed only headers.forEach(),
while fetchWithRetry reads response.headers.get('retry-after') -- so any
503 fixture threw TypeError before reaching the logic under test. And the
fake fetch was async (url) =>, dropping the init argument entirely, so a
test could not observe the request method or count requests. Since a
retried request and a single one return the same value, request count was
the only observable that distinguishes them, and it was unavailable.

This closes both so the following commit can test the retry policy, and
pins the harness's own capabilities: a gap in the harness is invisible in
a green run, and produces confident, empty coverage.

Changes:

  • Extracted makeFakeResponse(status, body, headers) with a real
    case-insensitive headers.get()/has()/forEach(), correct ok derivation,
    and json()/text().
  • makeFakeFetch now takes (url, init) and appends { url, method, body,
    headers } to a per-load call log, returned from loadScript as
    fetchCalls.
  • fetchMap entries may now be a response descriptor
    ({ __response: true, status, body, headers }) to model failures and
    header-bearing responses; a bare JSON body still means 200, unchanged.
  • Added tests/browser-shim.test.js asserting these capabilities directly.

Security:

  • N/A -- test harness only, never inlined into a generated .user.js.

Testing:

  • node --test from user.js/: 480 tests, 479 pass, 1 skipped (live tests
    are opt-in), up from 476/475.
  • All 476 pre-existing tests pass unchanged, confirming the bare-body
    fetchMap form and the 404-on-unmapped-URL behavior are preserved.

Backwards Compatibility:

  • Purely additive for existing callers: the descriptor form is opt-in via
    an explicit __response marker, and loadScript's return value gains a
    property rather than changing one.

Assisted-by: Claude:claude-opus-5


Stack created with GitHub Stacks CLI • Give Feedback 💬

@netravnen
netravnen force-pushed the test/browser-shim-fetch-fidelity branch from f11585b to 0fd347f Compare August 19, 2026 22:27
Base automatically changed from fix/dp-whitelist-hostname-validation to dev-next August 19, 2026 22:28
The node:test harness could not express the cases needed to test
lib/admincom-common.js's retry/backoff wrappers, which is why that code
shipped with a defect nothing caught.

Two limits caused it. The fake response exposed only headers.forEach(),
while fetchWithRetry reads response.headers.get('retry-after') -- so any
503 fixture threw TypeError before reaching the logic under test. And the
fake fetch was `async (url) =>`, dropping the init argument entirely, so a
test could not observe the request method or count requests. Since a
retried request and a single one return the same value, request count was
the only observable that distinguishes them, and it was unavailable.

This closes both so the following commit can test the retry policy, and
pins the harness's own capabilities: a gap in the harness is invisible in
a green run, and produces confident, empty coverage.

Changes:
- Extracted makeFakeResponse(status, body, headers) with a real
  case-insensitive headers.get()/has()/forEach(), correct ok derivation,
  and json()/text().
- makeFakeFetch now takes (url, init) and appends { url, method, body,
  headers } to a per-load call log, returned from loadScript as
  `fetchCalls`.
- fetchMap entries may now be a response descriptor
  ({ __response: true, status, body, headers }) to model failures and
  header-bearing responses; a bare JSON body still means 200, unchanged.
- Added tests/browser-shim.test.js asserting these capabilities directly.

Security:
- N/A -- test harness only, never inlined into a generated .user.js.

Testing:
- node --test from user.js/: 480 tests, 479 pass, 1 skipped (live tests
  are opt-in), up from 476/475.
- All 476 pre-existing tests pass unchanged, confirming the bare-body
  fetchMap form and the 404-on-unmapped-URL behavior are preserved.

Backwards Compatibility:
- Purely additive for existing callers: the descriptor form is opt-in via
  an explicit __response marker, and loadScript's return value gains a
  property rather than changing one.

Assisted-by: Claude:claude-opus-5
@netravnen
netravnen force-pushed the test/browser-shim-fetch-fidelity branch from 0fd347f to c2f41e1 Compare August 19, 2026 22:28
@netravnen
netravnen marked this pull request as ready for review August 19, 2026 22:29
@netravnen
netravnen merged commit 1acfafe into dev-next Aug 19, 2026
2 checks passed
@netravnen
netravnen deleted the test/browser-shim-fetch-fidelity branch August 20, 2026 09:39
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.

1 participant