Skip to content

fix(test): repair the shared api test fixtures — 249 failures to 95 - #922

Merged
thinkclay merged 4 commits into
mainfrom
fix/api-test-drift
Sep 26, 2026
Merged

thinkclay merged 4 commits into
mainfrom
fix/api-test-drift

Conversation

@thinkclay

Copy link
Copy Markdown
Contributor

Follow-on from #920, which built the gate. This repairs what the gate was built to let us land incrementally.

Most of ~250 failures were not 250 problems

Five faults in shared plumbing accounted for the bulk. Each presented as a broken endpoint rather than a broken fixture, which is why they survived so long.

1. The magic link carried token_hash, not token

const token = url.searchParams.get("token");   // always null

GoTrue's confirmation link is /auth/confirm?token_hash=…&type=signup. The read found nothing, completeMagicLinkAuth returned null, and every suite built on magic-link registration asserted user !== null and failed. The identical bug sat in both test trees — and it is why auth.test.ts could never write the cached token fixture, which then failed everything calling requireAuthSetup().

One line each. Single biggest cause in this PR.

2. The application fixture invented two columns

createTestApplication inserted custom_answers and type. Neither is a column on core.applications; no migration has ever created either. PostgREST rejects the whole insert when a key does not exist, so every test needing a seeded application died in its fixture instead of its assertions.

3. Production code a test could not reach

4. No token carried the office role

TEST_USERS.admin is worker/platform only (#478), so office suites got a 403 and read it as a broken endpoint. There is an office entry now — [email protected], seeded with the role by 002_seed-users.sql.

5. An ordering dependency between test files

requireAuthSetup() threw "Run auth.test.ts first". deno test walks a directory alphabetically, so every suite sorting before auth.test.ts failed for no reason of its own — and running any single file alone failed the same way, which made the tree hostile to the exact activity needed to repair it. It mints the tokens itself now.

Proof it is order-independent: delete tests/fixtures/tokens.json, run office-jobs-rest.test.ts alone → 3 passed.

Result

tree before after
functions/api/__tests__ 344 passed, 72 failed 387 passed, 29 failed
tests/routers/ 122 passed, 177 failed 233 passed, 66 failed
total failures 249 95

GATED.txt grows 3 → 19 files, 152 tests that now fail the build if they regress — verified passing together, and from a deleted fixture.

What is left, honestly

95 failures remain and they are not another shared cause — I checked by grouping every remaining error message. They are per-suite assertion drift, and most suites exercise the legacy tRPC layer (29 of 38 files in tests/routers/ call callTRPCEndpoint; tRPC still serves, it is not 404). Worst first:

suite failing
work-logs 18
profileCompletion 10
payments 6
education 5
15 others 1–3 each

Whether those are worth repairing or retiring is a call about the tRPC→REST migration, not a mechanical fix — so I have not guessed at it.

One environmental gap is also left unfixed: the magic-link endpoint answers 500 Configuration Error: Magic link redirect target is not configured because the local edge runtime has no site URL set. That is stack config, not code — the code side is fixed (it reads per request now).

🤖 Generated with Claude Code

@thinkclay

Copy link
Copy Markdown
Contributor Author

CI rejected five of the files I added to the gate, and it was right to.

I gated on local results and called it evidence. Five files — auth, auth-session-regression, ccpa, onet, resume — passed on my machine and failed immediately in CI: 40 of 152 tests, every one Failed to get or create auth token. They mint their users through the magic-link flow, which completes against a local stack that has a warm mailbox and users left by earlier runs, and does not complete on the fresh stack CI builds.

That is precisely the failure mode the gate exists to catch, and I walked into it by measuring in the wrong environment. They are out of the list.

The gate is now 14 files / 74 tests, verified after pnpm supa db reset — as close to CI's conditions as I can get locally. GATED.txt now carries that warning above the list, in the file, because the next person adding a line will be standing exactly where I was.

The headline numbers survive re-measurement

I re-ran both trees on the clean database, since if the gate list was measured on a dirty stack the totals might be too:

tree dirty (as reported) clean (verified)
functions/api/__tests__ 387 passed, 29 failed 387 passed, 29 failed
tests/routers/ 233 passed, 66 failed 232 passed, 67 failed

One test apart. So the 249 → 95 claim holds; only the gate list was wrong.

One thing this surfaced that is worth its own issue

The magic-link path cannot complete on a clean stack, and the endpoint answers 500 Configuration Error: Magic link redirect target is not configured wherever no site URL is set. That blocks five otherwise-healthy suites from ever being gated — ~40 tests — and it is stack configuration rather than test drift. Happy to file it if you want it tracked separately from the per-suite work.

@github-actions

Copy link
Copy Markdown
Contributor

📊 Performance Benchmark Results


thinkclay and others added 3 commits September 26, 2026 19:16
…tead of 3

Most of the ~250 failing api tests were not 250 problems. Four faults in
shared plumbing accounted for the bulk of them, and each looked like a broken
endpoint rather than a broken fixture.

## The magic link carried `token_hash`, not `token`

`completeMagicLinkAuth` read `url.searchParams.get("token")`. GoTrue's
confirmation link is `/auth/confirm?token_hash=...&type=signup`, so the read
found nothing, the helper returned null, and every suite built on magic-link
registration asserted `user !== null` and failed. The same bug sat in both
test trees, and it is why `tests/routers/auth.test.ts` could never produce the
cached token fixture — which in turn failed every suite calling
`requireAuthSetup()` with "Auth setup is incomplete".

One line each. It is the single biggest cause in this change.

## The application fixture invented two columns

`createTestApplication` inserted `custom_answers` and `type`. Neither is a
column on `core.applications`; no migration has ever created either. PostgREST
rejects the whole insert when a key does not exist, so every test needing a
seeded application died in its fixture rather than in its assertions.

## Production code that could not be reached by a test

Two modules read their configuration in a way that works under the edge
runtime and cannot work under `deno test`:

  - `checkApplicationAccess` built its own service client internally, so the
    stub its unit tests pass as the first argument never reached the lookup.
    It takes the client as a parameter now; the production call site passes
    `getServiceClient()` explicitly, so the service-role read (#608, #649) is
    unchanged.
  - `auth.ts` captured the magic-link redirect target in a module-level const,
    freezing the environment as it stood at import. Read per request now.

Both are small changes that make the modules configurable by their callers.
Neither alters what the edge runtime does.

## No token with the office role

`TEST_USERS.admin` carries `worker/platform` only (#478), so office suites
reaching for it got a 403 and read it as a broken endpoint. There is an
`office` entry now — `[email protected]`, seeded with the role by
`002_seed-users.sql` — cached alongside the others, and the two office REST
suites use it.

## Result

    functions/api/__tests__   344 passed,  72 failed  ->  387 passed, 29 failed
    tests/routers/            122 passed, 177 failed  ->  221 passed, 78 failed

249 failures to 107. `GATED.txt` grows from 3 files to 19 — 152 tests that
now fail the build if they regress, verified passing together.

What remains is per-suite assertion drift rather than shared plumbing, worst
first: work-logs, profileCompletion, applications, payments, education. One
known environmental gap is also left: the magic-link endpoint answers
`500 Configuration Error` because the local edge runtime has no site URL
configured, which is a stack-config question rather than a code one.

Co-Authored-By: Claude Opus 5 <[email protected]>
`requireAuthSetup()` threw "Auth setup is incomplete. Run auth.test.ts first"
— an ordering dependency between test FILES. `deno test` walks a directory
alphabetically, so every suite whose name sorts before `auth.test.ts` ran
before the suite that writes the token fixture and failed for no reason of its
own. Running one file on its own failed the same way, which made the tree
hostile to work on a single suite at a time — the exact thing anyone repairing
it needs to do.

It mints the tokens itself now when the fixture is missing or stale: password
sign-in for the seeded accounts, no mailbox round-trip, including the `office`
entry added alongside. Suites that mean to exercise the magic-link path still
call it directly.

Proof it is not order-dependent any more: delete `tests/fixtures/tokens.json`
and run `office-jobs-rest.test.ts` alone — 3 passed, where it previously
failed with the message above.

    tests/routers/   221 passed, 78 failed  ->  233 passed, 66 failed

`GATED.txt` is unchanged at 19 files: this fixed tests inside suites that
still fail for their own reasons, so no additional file went fully green.

Co-Authored-By: Claude Opus 5 <[email protected]>
I added five files to GATED.txt on the strength of a local green run and CI
rejected them immediately: 40 of 152 tests, every one of them "Failed to get
or create auth token".

The five — auth, auth-session-regression, ccpa, onet, resume — mint their
users through the magic-link flow. That flow completes against my local stack,
which has a warm mailbox and users left by earlier runs, and does not complete
on the fresh stack CI builds. Local green was not evidence, and I treated it
as evidence.

They come back out. The gate is 14 files, 74 tests, verified after
`pnpm supa db reset` — a stack as close to CI's as I can make locally.

GATED.txt now says this in the file itself, above the list, because the next
person to add a line will be standing exactly where I was.

The headline numbers in this branch survive the same re-measurement on a clean
database, so they were sound; it was only the gate list that was wrong:

    functions/api/__tests__   387 passed, 29 failed   (unchanged)
    tests/routers/            232 passed, 67 failed   (was 233/66 dirty)

Also recorded: the magic-link endpoint answers `500 Configuration Error:
Magic link redirect target is not configured` wherever no site URL is set,
which is the environmental half of why those five cannot be gated yet.

Co-Authored-By: Claude Opus 5 <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

📊 Performance Benchmark Results


`Lint REST API Code` has been cancelled at exactly 5:00 on every run since
#924, which surfaces as `##[error]The operation was canceled` with no failing
check named, and then fails `All Tests Passed`.

It is a job timeout, not a lint failure. #924 added a "Type Check (ratchet)"
step that runs `deno check` across all 25 edge functions — roughly four
minutes on its own — to a job whose `timeout-minutes` was 5, already partly
spent on lint and format.

Reproduced on main at 28e1550, the commit that added the step:

    Lint REST API Code | cancelled | 22:57:47Z -> 23:03:03Z
    All Tests Passed   | failure

So this is not specific to any branch — every PR touching packages/supabase
has been red for it since. 15 minutes, with the reasoning in the file so the
next person to add a step to this job knows what it costs.

The ratchet itself is fine: run locally on this branch it reports 380 errors,
within baseline (api 334, unchanged).

Co-Authored-By: Claude Opus 5 <[email protected]>
@thinkclay

Copy link
Copy Markdown
Contributor Author

Lint REST API Code is not failing because of this branch. I said it looked like flake, then said it was my stale branch. Both were wrong. Third time, with evidence:

It is a job timeout. #924 added a "Type Check (ratchet)" step that runs deno check across all 25 edge functions — about four minutes on its own — to a job whose timeout-minutes was 5, already partly spent on lint and format. The job hits the ceiling at exactly 5:00 and is cancelled, which shows up as ##[error]The operation was canceled with no failing check named, and then fails All Tests Passed.

Reproduced on main at 28e155051, the commit that added the step:

Lint REST API Code | cancelled | 22:57:47Z -> 23:03:03Z   (5m16s)
All Tests Passed   | failure

So every PR touching packages/supabase has been red for this since #924 landed — nothing to do with the test drift work.

Fixed here in 7dbd48d: 15 minutes, with the reasoning in the workflow so the next person adding a step to that job knows what it costs. Happy to split it into its own PR if you would rather keep this one to test fixtures.

The ratchet itself is healthy — run locally on this branch it reports 380 errors, within baseline (api 334, unchanged), so nothing in these production edits makes types worse.

Run API Tests has been green since the gate was trimmed.

@github-actions

Copy link
Copy Markdown
Contributor

📊 Performance Benchmark Results


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