11import { afterEach , beforeEach , expect , test } from "bun:test" ;
22import { Window } from "happy-dom" ;
33import { act } from "react" ;
4- import { createRoot , type Root } from "react-dom/client" ;
4+ import type { Root } from "react-dom/client" ;
55import { useCodexAccountPool , type CodexAccountPoolController } from "../src/hooks/useCodexAccountPool" ;
66
77/**
@@ -10,12 +10,16 @@ import { useCodexAccountPool, type CodexAccountPoolController } from "../src/hoo
1010 * substring checks is not proven at all.
1111 */
1212
13- const globals = [ "document" , "window" , "navigator" , "localStorage" , "fetch" , "IS_REACT_ACT_ENVIRONMENT" ] as const ;
13+ // NOTE: `fetch` is deliberately absent. A sibling suite installs its own fetch router
14+ // inside individual tests without restoring it, so writing a captured fetch back here
15+ // would clobber that router and make results depend on file order.
16+ const globals = [ "document" , "window" , "navigator" , "localStorage" , "IS_REACT_ACT_ENVIRONMENT" ] as const ;
1417let previous : Record < ( typeof globals ) [ number ] , unknown > ;
1518let win : Window ;
1619let host : HTMLElement ;
1720let root : Root | null = null ;
1821let calls : string [ ] = [ ] ;
22+ let originalFetch : typeof globalThis . fetch ;
1923let accounts : unknown [ ] = [ ] ;
2024let threshold = 80 ;
2125
@@ -30,8 +34,9 @@ beforeEach(() => {
3034 } ) ;
3135 ( globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT ?: boolean } ) . IS_REACT_ACT_ENVIRONMENT = true ;
3236
37+ originalFetch = globalThis . fetch ;
3338 calls = [ ] ;
34- accounts = [ { id :
"a1" , email :
"[email protected] " , isMain :
true , hasCredential :
true , quota :
null } ] ; 39+ accounts = [ { id : "a1" , email : "account-one " , isMain : true , hasCredential : true , quota : null } ] ;
3540 Object . defineProperty ( globalThis , "fetch" , {
3641 configurable : true ,
3742 value : async ( url : string , init ?: RequestInit ) => {
@@ -52,14 +57,22 @@ beforeEach(() => {
5257} ) ;
5358
5459afterEach ( async ( ) => {
60+ // Unmount first, while this file's window/timers are still the active globals:
61+ // otherwise React cleanup runs against a swapped-out window and the controller's
62+ // 30s interval outlives the suite, firing into whatever runs next.
5563 if ( root ) {
5664 const current = root ;
5765 await act ( async ( ) => { current . unmount ( ) ; } ) ;
5866 root = null ;
5967 }
68+ await act ( async ( ) => { await new Promise ( ( r ) => setTimeout ( r , 0 ) ) ; } ) ;
6069 for ( const key of globals ) {
6170 Object . defineProperty ( globalThis , key , { configurable : true , value : previous [ key ] } ) ;
6271 }
72+ Object . defineProperty ( globalThis , "fetch" , { configurable : true , value : originalFetch } ) ;
73+ // Tear the window down: leaving it alive kept this file's timers and document
74+ // reachable, which made a sibling suite in the same process fail depending on order.
75+ await win . happyDOM ?. close ?.( ) ;
6376} ) ;
6477
6578/** Mounts the hook and exposes the live controller. */
@@ -69,6 +82,8 @@ async function mountController(enabled = true) {
6982 seen . current = useCodexAccountPool ( "" , enabled ) ;
7083 return null ;
7184 }
85+ // Lazy import: see the note on the Root type import above.
86+ const { createRoot } = await import ( "react-dom/client" ) ;
7287 await act ( async ( ) => {
7388 root = createRoot ( host ) ;
7489 root . render ( < Probe /> ) ;
@@ -115,6 +130,12 @@ test("two pause holders both have to release before polling resumes", async () =
115130 expect ( calls . length ) . toBe ( afterPause ) ;
116131} ) ;
117132
133+ test ( "the last genuine threshold read is cached for surfaces that mount later" , async ( ) => {
134+ const seen = await mountController ( ) ;
135+ // A real /active read succeeded during the initial load.
136+ expect ( seen . current ! . readLastThreshold ( ) ) . toBe ( 80 ) ;
137+ } ) ;
138+
118139test ( "subscribing never fabricates a server read" , async ( ) => {
119140 const seen = await mountController ( ) ;
120141 const received : unknown [ ] = [ ] ;
@@ -127,10 +148,11 @@ test("subscribing never fabricates a server read", async () => {
127148 } ) ;
128149 } ) ;
129150
130- // A replay-on-subscribe was tried and rejected: useCodexAutoSwitch treats every
131- // acceptActiveRead as belonging to a read that genuinely started at that revision, so
132- // a synthesised accept corrupted its editing/saving disposition and overwrote drafts
133- // the user was typing. Subscribing is therefore silent; the next load notifies.
151+ // Subscribing stays silent: useCodexAutoSwitch treats every acceptActiveRead as
152+ // belonging to a read that genuinely started at that revision, so synthesising one
153+ // corrupts its editing/saving disposition and overwrites drafts. Late surfaces seed
154+ // themselves through readLastThreshold() + hydrateServerValue() instead, which applies
155+ // only while uninitialized.
134156 expect ( received ) . toEqual ( [ ] ) ;
135157
136158 // And a real load does reach the subscriber.
@@ -141,8 +163,8 @@ test("subscribing never fabricates a server read", async () => {
141163test ( "a mutation updates the one shared controller state" , async ( ) => {
142164 const seen = await mountController ( ) ;
143165 accounts = [
144- { id :
"a1" , email :
"[email protected] " , isMain :
true , hasCredential :
true , quota :
null } , 145- { id :
"a2" , email :
"[email protected] " , isMain :
false , hasCredential :
true , quota :
null } , 166+ { id : "a1" , email : "account-one " , isMain : true , hasCredential : true , quota : null } ,
167+ { id : "a2" , email : "account-two " , isMain : false , hasCredential : true , quota : null } ,
146168 ] ;
147169
148170 await act ( async ( ) => { await seen . current ! . switchAccount ( "a2" ) ; } ) ;
0 commit comments