Skip to content

fix(mobile): contract test sends fetch as the native app does (no Sec-Fetch-*) - #144

Merged
jayleaton merged 1 commit into
mainfrom
fix/contract-native-fetch
Oct 31, 2026
Merged

jayleaton merged 1 commit into
mainfrom
fix/contract-native-fetch

Conversation

@jayleaton

@jayleaton jayleaton commented Oct 31, 2026 •

Copy link
Copy Markdown
Owner

Problem

Same change as jayleaton/sparkrack#36 (merged there). Stacked on #143, which brings better-auth 1.7.

pnpm test:contract couldn't run. Its setup timed out on /healthz because Node's fetch (undici) adds browser Fetch Metadata to every request (Sec-Fetch-Mode: cors). The server's API gate (apiOnly, apps/web/internal/server/middleware.go) treats that as a browser and returns 404, as designed. React Native's fetch (NSURLSession / OkHttp) sends no such headers, so the harness was not testing the API the way the app reaches it. As a result, the better-auth 1.7 change in #143 (getCookie() became async) had never been checked against a real server.

Fix

contract/nativeFetch.ts   fetch over node:http(s), only the headers the caller set (as React Native's fetch sends them)
contract/setup.ts         globalThis.fetch = nativeFetch   (the app's auth + API clients use the global fetch)
contract/globalSetup.ts   /healthz wait uses nativeFetch
server                    unchanged: the browser check still 404s anything with Sec-Fetch-*, Origin, Referer or Accept: text/html

nativeFetch covers what the clients use: method, headers, a buffered body, an AbortSignal, and redirect (follows by default; manual and error are supported). Duplicate Set-Cookie headers are kept.

The first real run also caught a better-auth 1.7 change. The cold-launch test expected the session atom to be filled from the keychain when the client is created. In 1.7, expoClient restores that cache inside the first get-session request's init, before the request is sent. Both versions set only data and leave isPending alone, and the app's screens wait on isPending, so what users see on launch is the same. The test now holds the client's network requests back. It asserts that the atom is filled from the keychain while nothing has been sent (sent === 0), then releases them and checks that get-session confirms it (sent === 1). better-auth 1.7 captures fetch when the client is created, which is why the hold is installed before the relaunch.

Evidence

  • Before (main, harness as it was): Error: contract setup: timed out waiting for the Go server /healthz.
  • After: Test Files 1 passed (1) · Tests 11 passed (11) against a real Go server and Postgres 18. Every /api/auth/* and /api/core/v1/* request was traced going through nativeFetch.
  • Mutation checks (each reverted afterwards):
Mutation Result
Harness fix off (Node's fetch) setup times out on /healthz
apiFetch without await authClient.getCookie() 5 tests fail: signed-in calls to /users/me, PATCH, sessions
Held fetch throws (proves the hold intercepts) the cold-launch test fails
  • Other mobile checks: tsc, expo lint (0 warnings) and pnpm test (18 tests) pass. make check-fast passes.

Merge Danger

Door: two-way.

Blast Radius: test-only. This touches the mobile contract harness, one test and the README. No app code and no server code change.

Local verification

Filled by make ci-local, 2026-10-31T16:43:38Z. Never edit this section by hand.

Head 061622f624732d26123c2489c4298b95e9b5845d · tree 7bba64b01882 · passed
Attested by JJ Eaton on MacBook-Pro-3.local

Ran: On this exact tree (7bba64b) in a 4 vCPU Davit box, after the rebase onto main: apps/mobile-app pnpm install --frozen-lockfile, pnpm typecheck, pnpm lint, pnpm test (18/18), pnpm test:contract (11/11 against a real Go server + Postgres); make secret-scan (local). Only apps/mobile-app/contract changes; the Go tree is main's, which passed make ci as #142's head.

🤖 Generated with Claude Code

Node's fetch (undici) adds Sec-Fetch-Mode: cors, which the API gate
(apiOnly) treats as a browser and 404s, so setup timed out on /healthz.
contract/nativeFetch.ts sends over node:http with only the caller's
headers, as React Native does; the server's browser check is unchanged.

better-auth 1.7 restores the cached session in the first get-session's
init instead of at client creation: the cold-launch test now holds the
network back and proves the keychain fills the session atom first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jayleaton
jayleaton changed the base branch from deps/security-updates to main October 31, 2026 16:43
@jayleaton
jayleaton force-pushed the fix/contract-native-fetch branch from 62a0845 to 061622f Compare October 31, 2026 16:43
@jayleaton
jayleaton merged commit 822bf3a into main Oct 31, 2026
1 check passed
@jayleaton

Copy link
Copy Markdown
Owner Author

Authorized by JJ, merged by Claude (Opus 5.5, CI speed agent) with --admin after make ci-local attested 061622f

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