test(web): make Libraries scan-polling tests deterministic (fixes #157) #169

Merged
timothy merged 1 commits from fix/157-flaky-scan-polling into main 2026-07-07 16:02:32 +02:00
Owner

Test-only fix for the flaky Libraries scan-polling family in web/src/App.test.tsx (flaked on main runs 569 and 573 today, plus once locally).

Root cause: the tests mock window.setInterval to capture the poll handler and tick it manually, but useLibrariesScreenQuery registers that interval in a passive effect gated on hasActiveScans, which flips only after an async fetch resolves outside act. findBy* resolves on the DOM mutation and can beat the passive-effect flush under CI load — so the captured-handlers array was still empty and the tick silently no-opped. That explains both CI signatures exactly (scan-status count stuck at 1; 75% label never removed).

Fix: a runPollTick() helper that (a) flushes pending effects with an empty act, (b) asserts at least one handler was captured (future regressions fail loudly instead of silently no-opping), and (c) runs the handlers inside act so all state commits land before assertions. Applied to all five tests with the manual-tick pattern; post-tick waitFors became plain synchronous expects — nothing depends on wall-clock scheduling anymore. Also tightened one test's interval mock to the ≥10s poll cadence so it doesn't capture waitFor's internal 50ms interval.

Verification: 20 full App.test.tsx runs under 6-way CPU contention — 0 failures; full suite 202/202; typecheck + eslint clean. No product code touched.

Fixes #157.

🤖 Generated with Claude Code

Test-only fix for the flaky Libraries scan-polling family in `web/src/App.test.tsx` (flaked on main runs 569 and 573 today, plus once locally). **Root cause**: the tests mock `window.setInterval` to capture the poll handler and tick it manually, but `useLibrariesScreenQuery` registers that interval in a passive effect gated on `hasActiveScans`, which flips only after an async fetch resolves outside `act`. `findBy*` resolves on the DOM mutation and can beat the passive-effect flush under CI load — so the captured-handlers array was still empty and the tick silently no-opped. That explains both CI signatures exactly (scan-status count stuck at 1; `75%` label never removed). **Fix**: a `runPollTick()` helper that (a) flushes pending effects with an empty `act`, (b) asserts at least one handler was captured (future regressions fail loudly instead of silently no-opping), and (c) runs the handlers inside `act` so all state commits land before assertions. Applied to all five tests with the manual-tick pattern; post-tick `waitFor`s became plain synchronous expects — nothing depends on wall-clock scheduling anymore. Also tightened one test's interval mock to the ≥10s poll cadence so it doesn't capture `waitFor`'s internal 50ms interval. **Verification**: 20 full `App.test.tsx` runs under 6-way CPU contention — 0 failures; full suite 202/202; typecheck + eslint clean. No product code touched. Fixes #157. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
timothy added 1 commit 2026-07-07 15:53:28 +02:00
test(web): make Libraries scan-polling tests deterministic (fixes #157)
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 4m28s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 4m43s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
7c4cf55715
The five Libraries polling tests mock window.setInterval to capture the
poll handler, but that handler is registered in a passive useEffect that
only runs after an out-of-act state commit (the async fetch that flips
hasActiveScans). findBy*/waitFor resolve on the DOM mutation via
MutationObserver, which can win the race against React's passive-effect
flush under CI load - leaving intervalHandlers empty when the test
invokes it. The forEach was then a silent no-op: no poll fetch, so
'route delta' timed out at scan-status count > 1, and 'stops polling'
never cleared the 75% label.

Fix: new runPollTick helper flushes pending effects with an empty
act(), asserts the poll handler is actually registered (turning any
future regression into a clear failure instead of a timeout), and
invokes the handlers inside act() so fetch microtasks and state updates
are fully applied before assertions run. All post-tick waitFor blocks
become plain synchronous expects - nothing depends on wall-clock
scheduling anymore. Also aligned the 'stops polling' setInterval mock
with its siblings' >=10000ms cadence filter so it captures exactly the
poll handler, not waitFor's internal 50ms interval callback.

Verified: 20x full App.test.tsx loop under CPU contention, 0 failures;
full vitest suite 202 passed; tsc + eslint clean.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
timothy merged commit 117afa310e into main 2026-07-07 16:02:32 +02:00
timothy deleted branch fix/157-flaky-scan-polling 2026-07-07 16:02:32 +02:00
Sign in to join this conversation.