ReviewOS

craft-native/craft

test(sdk): window-routing reruns reuse retained handles across DOM fixtures

#269
Closed glennmichael123 opened this 10 days ago ·
10 days ago

Found during the post-merge soak for #220. This is outside that issue's location lane.

Reproduction

cd packages/typescript
bun test src/__tests__/window-routing.test.ts --rerun-each=100 --randomize --seed=254

Observed on Bun 1.4.1: 1,354 pass / 46 fail. The failing assertion is window-routing.test.ts:158: the current fake DOM has no craft:window:resize listener after the retained handle is closed. A full SDK soak also reproduced it twice in 5,660 tests.

Cause

Several tests derive an id from Date.now(). Fast --rerun-each runs can reuse the same millisecond id. windowManager is a module singleton whose retained _windows map survives each test fixture, while beforeEach clears and replaces the fake window listener buckets. When create() finds the stale handle, _markOpen() calls _setupEventListeners(), but the handle still has a non-empty _domListeners array pointing at the previous fake window and declines to subscribe to the current one.

This appears to be test isolation, not a production reopen regression: production does not replace globalThis.window between calls. The normal one-pass suite and CI pass.

Suggested fix

Give each rerun a collision-proof id and/or expose a test-only way to reset the singleton registry between fixtures. The latter would also keep future routing tests from inheriting retained handles. Add a repeated-run check for this file to prove the fix.

Sign in to comment on this issue.