Skip to content

fix(tests): the four red unit tests, and the constraint one of them found - #676

Merged
thinkclay merged 1 commit into
mainfrom
fix/red-unit-tests
Sep 1, 2026
Merged

thinkclay merged 1 commit into
mainfrom
fix/red-unit-tests

Conversation

@thinkclay

Copy link
Copy Markdown
Contributor

pnpm test:unit was red on scf-core, which blocks cutting a release. Four failures: one stale test, one test asserting a contract I had already changed and not updated, and — behind the last two — a schema defect that has been there since the table was created.

useApplicationStatusChange — a stub that could not notice reality

The test built its own query client: { invalidateQueries } and nothing else. #648 taught the hook to patch the cached list optimistically, so it started calling getQueriesData, which the stand-in did not have. TypeError: queryClient.getQueriesData is not a function.

Swapped for a real QueryClient. The cache API is real, invalidateQueries is spied on the real method, and the optimistic patch and its rollback can now be asserted against actual cache contents. Two tests added for exactly that, because neither was covered:

  • the move lands in every cached filter combination, not just the one on screen (this is the thing feat(ats): a stage move can be undone, because it has not been sent yet #648 was for — each filter combination is its own cache entry, and a move has to land in all of them or the row snaps back when the user changes a filter)
  • a failed write rolls the row back, so the board never keeps showing a stage the server rejected

Same class of problem as the retry stub in #650.

…and a test asserting the contract I changed

That test was called "executes valid non-critical transitions immediately" and asserted the mutation had fired by the time changeStatus resolved. #648 deliberately stopped that — a non-critical move is held for UNDO_WINDOW_MS so Undo has something to cancel.

I changed the behaviour and did not update the test. It kept "passing" in the suite only because it died earlier, on the stub. It now pins both halves of the real contract: nothing sent during the window, sent after.

ApplicationsFilters — the button moved

Asserted a Clear Filters button. #624 moved clear-all out of the filter body and onto the chip strip (onClearAll, gated on activeFilterCount > 0), so no such button exists in this component any more.

The assertion is still worth having, so it moves to filters.test.ts, next to the module the screen's onClearAll actually calls — covering EMPTY_FILTERS, that it leaves the chip strip empty, and the gate. A breadcrumb is left where it used to live.

office-role-access — not a stale test

This one was real, and it is the interesting finding. It failed expected { code: 'PGRST116', … } to be null on a .single(), because the local database held 26 duplicated (user, role, scope) pairs.

core.role_assignments has carried this since it was created:

UNIQUE (role_id, user_id, scope_org_id, scope_team_id)

It has never prevented a duplicate. Two NULLs are not equal in a default UNIQUE constraint, and the table's own CHECK guarantees every legal row has at least one NULL scope column:

CHECK ( (scope_org_id IS NULL     AND scope_team_id IS NULL)      -- platform
     OR (scope_org_id IS NOT NULL AND scope_team_id IS NULL)      -- org
     OR (scope_org_id IS NULL     AND scope_team_id IS NOT NULL) ) -- team

So no two rows ever compare as equal and the index waves all of them through. Consequences:

  1. Revoking a role is unreliable — delete "the" assignment and any duplicate stays behind, and the user keeps the role.
  2. Every seed ON CONFLICT … DO NOTHING on this table silently no-opped. 002_seed-users.sql:200 says -- Skip if role assignment already exists. It could never match; each seed run appended another copy.
  3. Any .single() read 500s once a duplicate exists.

Migration 348 collapses the duplicates (oldest wins, ordered created_at, id so it is deterministic) and recreates the constraint as UNIQUE NULLS NOT DISTINCT — PG 15+, and the database is on 17.6. That is the semantics it always meant.

Verified locally:

duplicate insert  →  ERROR: duplicate key value violates unique constraint
                     "role_assignments_role_user_scope_key"
seed 002 re-run   →  INSERT 0 0     (was: appending another copy every time)
duplicates left   →  0

Filed separately as #675. Its acceptance list is deliberately unfinished: the migration deletes rows, and production has not had it applied. Production very likely holds duplicates too and the counts should be inspected before it is run there — that is a call for a human, not something to slip into a merge.

Verification

scf-core 1148 / 1148, --skip-nx-cache
Full test:unit 7 / 7 projects, --skip-nx-cache
typecheck 12 / 12
lint 10 / 10

The cache mattered: the first full run read 6 of 7 projects from Nx cache, which is exactly how a red suite stays invisible.

🤖 Generated with Claude Code

…ound

`pnpm test:unit` was red on scf-core. Working through the four failures found
one stale test, one test asserting a contract I had already changed, and a
schema defect that had been there since the table was created.

**useApplicationStatusChange** — the test built its own query client,
`{ invalidateQueries }` and nothing else. #648 taught the hook to patch the
cached list optimistically, so it started calling `getQueriesData`, which the
stand-in did not have. Swapped for a real QueryClient: the cache API is real,
`invalidateQueries` is spied on the real method, and the optimistic patch and
its rollback can be asserted against actual cache contents. Two tests added for
exactly that, since neither was covered.

The same test was also named "executes valid non-critical transitions
immediately" and asserted the mutation had fired by the time `changeStatus`
resolved. #648 deliberately stopped that — a non-critical move is held for
UNDO_WINDOW_MS so Undo has something to cancel. I changed the behaviour and did
not update the test; it kept "passing" only because it died earlier, on the
stub. Now it pins both halves: nothing sent during the window, sent after.

**ApplicationsFilters** — asserted a "Clear Filters" button. #624 moved
clear-all out of the filter body and onto the chip strip, so no such button
exists here any more. The assertion is still worth having, so it moves to
filters.test.ts next to the module the screen's `onClearAll` actually calls,
covering EMPTY_FILTERS and the `activeFilterCount > 0` gate.

**office-role-access** — this one was not a stale test. It failed with
PGRST116 on a `.single()`, because the local database held 26 duplicated
(user, role, scope) pairs. The cause:

  UNIQUE (role_id, user_id, scope_org_id, scope_team_id)

has never prevented a duplicate. Two NULLs are not equal in a default UNIQUE
constraint, and the table's own CHECK guarantees every legal row has at least
one NULL scope column — so no two rows ever compare as equal. Every seed's
`ON CONFLICT ... DO NOTHING` on this table silently no-opped and appended
another copy instead, and revoking a role by deleting "the" assignment would
leave a duplicate behind and the user still holding it.

Migration 348 collapses the duplicates (oldest wins, deterministic) and
recreates the constraint as UNIQUE NULLS NOT DISTINCT, which is what it always
meant. Verified locally: a duplicate insert is now rejected, and re-running the
002 seed insert reports INSERT 0 0 rather than appending.

Filed as #675, whose acceptance list is deliberately unfinished — the migration
deletes rows, and production has not had it applied.

scf-core 1148/1148. Typecheck 12/12. Lint 10/10.
@thinkclay
thinkclay merged commit 216f73b into main Sep 1, 2026
5 of 6 checks passed
@thinkclay
thinkclay deleted the fix/red-unit-tests branch September 1, 2026 15:15
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