refactor(chat): close the credits manual-refetch path and drop the unsafe cast

Closed
SW
3mo ago
+44-10

Implements item F4b of #1949 — the second 4xx on every cold page load.

The bug

GET api.recoupable.dev/api/accounts/undefined/credits → 400 {"error":"id must be a valid UUID"}, reproduced on both cold loads in #1949. Unlike F4a this request does carry a valid bearer token, so it is a distinct race: auth had resolved, accountId had not.

The chain:

  1. useCredits.ts L20 gates the query with react-query's enabled: authenticated && !!userData?.account_id — correct, but refetch() deliberately ignores enabled.
  2. usePayment.tsx L10 exposes that refetch as refetchCredits.
  3. useVercelChat.ts calls await refetchCredits() in onFinish.
  4. When it lands before userData.account_id resolves, the userData?.account_id as string cast lets undefined interpolate straight into the URL.

The cast is what made this invisible: it asserts a value the code does not have, so the type system had nothing to say about the exact case that fails.

The fix

Two lines of defense, matching where the two problems actually are:

  • getAccountCredits refuses to build the request when the id is missing or the literal string "undefined". This is where the interpolation happens, so guarding here means no future caller can reintroduce it.
  • useCredits drops the as string cast and handles the missing id explicitly, so the failure is a typed guard rather than a lie to the compiler.

The request is refused before fetch, so the api never sees it. Behaviour for callers is unchanged: an errored query leaves creditsData undefined and usePayment already falls back to credits: 0 (creditsData?.remaining_credits || 0), which is what the 400 produced anyway — minus the failed request.

Tests

RED before GREEN — 3 new cases failed with Cannot read properties of undefined before the guard existed, extending the existing getAccountCredits suite:

accountIdBehaviour
undefinedthrows, fetch never called
"undefined" (literal)throws, fetch never called
""throws, fetch never called

Each asserts expect(mockFetch).not.toHaveBeenCalled() — the point is not that it throws, it is that no request goes out.

Test Files  92 passed (92)
     Tests  395 passed (395)

tsc --noEmit clean for the changed files. ESLint clean, 0 problems.

Relationship to #1950

Same cold load, same issue, independent fixes in different files — no dependency, no shared lines, either can merge first. #1950 removes the 401/404 from stream resume; this removes the 400 from credits. Both are needed for #1949's F4 Done-when (a cold load of / produces zero 4xx responses and no error toast).

Verification status

🔄 Preview verification pending — needs a DevTools network trace on the deployed preview confirming no /credits request carries undefined. Results to follow as a comment.


Summary by cubic

Stops sending /credits requests with an unresolved account id to remove the 400 on cold page loads. Fixes F4b in #1949 by guarding both the API helper and the query hook.

  • Bug Fixes
    • getAccountCredits: validate accountId; throw if missing or the string "undefined" so no fetch occurs.
    • useCredits: use skipToken with enabled from @tanstack/react-query to block both automatic and manual refetches when accountId is missing; remove the as string cast.
    • Tests: add cases for undefined, "undefined", and empty string; assert fetch is not called.