1- import { beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
1+ import { afterEach , beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
22
33import { EnvironmentId , ThreadId } from "@t3tools/contracts" ;
44
@@ -30,6 +30,13 @@ function entry(overrides: Partial<BrowserHistoryEntry> = {}): BrowserHistoryEntr
3030}
3131
3232beforeEach ( ( ) => readPreparedConnection . mockReturnValue ( null ) ) ;
33+ afterEach ( ( ) => vi . restoreAllMocks ( ) ) ;
34+
35+ function spyOnPersistWrites ( ) {
36+ const storage = useBrowserHistoryStore . persist . getOptions ( ) . storage ;
37+ if ( ! storage ) throw new Error ( "Browser history persistence storage is unavailable." ) ;
38+ return vi . spyOn ( storage , "setItem" ) ;
39+ }
3340
3441describe ( "normalizeHistoryUrl" , ( ) => {
3542 it ( "normalizes bare loopback hosts to http and keeps path/query" , ( ) => {
@@ -81,7 +88,6 @@ describe("upsertHistoryEntry", () => {
8188 const next = upsertHistoryEntry ( full , "http://new.test/" , 9999 ) ;
8289 expect ( next ) . toHaveLength ( BROWSER_HISTORY_MAX_ENTRIES_PER_PROJECT ) ;
8390 expect ( next [ 0 ] ?. url ) . toBe ( "http://new.test/" ) ;
84- // Prepending the new head pushes the LAST fixture entry off the end.
8591 const lastPort = 3000 + BROWSER_HISTORY_MAX_ENTRIES_PER_PROJECT - 1 ;
8692 expect ( next . some ( ( e ) => e . url === `http://localhost:${ lastPort } /` ) ) . toBe ( false ) ;
8793 expect ( next . some ( ( e ) => e . url === "http://localhost:3000/" ) ) . toBe ( true ) ;
@@ -220,17 +226,19 @@ describe("useBrowserHistoryStore", () => {
220226 ] ) ;
221227 } ) ;
222228
223- it ( "ignores invalid urls whether queued pending or recorded post-registration" , ( ) => {
224- recordVisitForThread ( threadRef , "ftp://a.test/" , 1 ) ;
229+ it ( "does not persist when a thread is already registered to the same project" , ( ) => {
225230 useBrowserHistoryStore . getState ( ) . registerThreadProject ( threadRef , "proj-a" ) ;
226- recordVisitForThread ( threadRef , "ftp://a.test/" , 2 ) ;
227- expect ( useBrowserHistoryStore . getState ( ) . byProjectKey ) . toEqual ( { } ) ;
231+ const persist = spyOnPersistWrites ( ) ;
232+
233+ useBrowserHistoryStore . getState ( ) . registerThreadProject ( threadRef , "proj-a" ) ;
234+
235+ expect ( persist ) . not . toHaveBeenCalled ( ) ;
228236 } ) ;
229237
230- it ( "unregisters a thread when project key is null" , ( ) => {
238+ it ( "ignores invalid urls whether queued pending or recorded post-registration" , ( ) => {
239+ recordVisitForThread ( threadRef , "ftp://a.test/" , 1 ) ;
231240 useBrowserHistoryStore . getState ( ) . registerThreadProject ( threadRef , "proj-a" ) ;
232- useBrowserHistoryStore . getState ( ) . registerThreadProject ( threadRef , null ) ;
233- recordVisitForThread ( threadRef , "http://a.test/" , 1 ) ;
241+ recordVisitForThread ( threadRef , "ftp://a.test/" , 2 ) ;
234242 expect ( useBrowserHistoryStore . getState ( ) . byProjectKey ) . toEqual ( { } ) ;
235243 } ) ;
236244
@@ -243,13 +251,17 @@ describe("useBrowserHistoryStore", () => {
243251 expect ( useBrowserHistoryStore . getState ( ) . byProjectKey [ "proj-a" ] ?. [ 0 ] ?. title ) . toBe ( "My App" ) ;
244252 } ) ;
245253
246- it ( "skips the write when the title is already set" , ( ) => {
254+ it ( "does not persist when the title is already set" , ( ) => {
247255 useBrowserHistoryStore . getState ( ) . registerThreadProject ( threadRef , "proj-a" ) ;
248256 recordVisitForThread ( threadRef , "http://a.test/" , 1 ) ;
249257 setTitleForThreadUrl ( threadRef , "http://a.test/" , "My App" ) ;
258+ const persist = spyOnPersistWrites ( ) ;
250259 const byProjectKey = useBrowserHistoryStore . getState ( ) . byProjectKey ;
260+
251261 setTitleForThreadUrl ( threadRef , "http://a.test/" , "My App" ) ;
262+
252263 expect ( useBrowserHistoryStore . getState ( ) . byProjectKey ) . toBe ( byProjectKey ) ;
264+ expect ( persist ) . not . toHaveBeenCalled ( ) ;
253265 } ) ;
254266
255267 it ( "sets a title against a settled url that differs from the stored one only by a trailing slash" , ( ) => {
@@ -353,27 +365,13 @@ describe("pendingVisitsByThreadKey", () => {
353365 expect ( useBrowserHistoryStore . getState ( ) . pendingVisitsByThreadKey ) . toEqual ( { } ) ;
354366 } ) ;
355367
356- it ( "preserves pending visits through a null registration and drains them later" , ( ) => {
357- recordVisitForThread ( threadRef , "http://a.test/" , 1 ) ;
358- useBrowserHistoryStore . getState ( ) . registerThreadProject ( threadRef , null ) ;
359- expect ( Object . values ( useBrowserHistoryStore . getState ( ) . pendingVisitsByThreadKey ) ) . toEqual ( [
360- [ { url : "http://a.test/" , at : 1 , environmentHostname : null } ] ,
361- ] ) ;
362- useBrowserHistoryStore . getState ( ) . registerThreadProject ( threadRef , "proj-a" ) ;
363- expect ( useBrowserHistoryStore . getState ( ) . byProjectKey [ "proj-a" ] ) . toEqual ( [
364- { url : "http://a.test/" , lastVisitedAt : 1 } ,
365- ] ) ;
366- expect ( useBrowserHistoryStore . getState ( ) . pendingVisitsByThreadKey ) . toEqual ( { } ) ;
367- } ) ;
368-
369368 it ( "caps the per-thread pending list at 10, dropping the oldest" , ( ) => {
370369 for ( let i = 0 ; i < 12 ; i ++ ) {
371370 recordVisitForThread ( threadRef , `http://a.test/${ i } ` , i ) ;
372371 }
373372 useBrowserHistoryStore . getState ( ) . registerThreadProject ( threadRef , "proj-a" ) ;
374373 const urls = useBrowserHistoryStore . getState ( ) . byProjectKey [ "proj-a" ] ?. map ( ( e ) => e . url ) ;
375374 expect ( urls ) . toHaveLength ( 10 ) ;
376- // Oldest two (index 0, 1) were dropped; newest (index 11) drained last so it's MRU.
377375 expect ( urls ) . not . toContain ( "http://a.test/0" ) ;
378376 expect ( urls ) . not . toContain ( "http://a.test/1" ) ;
379377 expect ( urls ?. [ 0 ] ) . toBe ( "http://a.test/11" ) ;
@@ -384,10 +382,8 @@ describe("pendingVisitsByThreadKey", () => {
384382 environmentId : EnvironmentId . make ( "env-1" ) ,
385383 threadId : ThreadId . make ( "thread-2" ) ,
386384 } ;
387- // Another thread in the same project records a visit live...
388385 useBrowserHistoryStore . getState ( ) . registerThreadProject ( otherThreadRef , "proj-a" ) ;
389386 recordVisitForThread ( otherThreadRef , "http://newer.test/" , 2000 ) ;
390- // ...while this thread's older visit was queued before it registered.
391387 recordVisitForThread ( threadRef , "http://older.test/" , 1000 ) ;
392388 useBrowserHistoryStore . getState ( ) . registerThreadProject ( threadRef , "proj-a" ) ;
393389
@@ -415,21 +411,6 @@ describe("pendingTitlesByThreadKey", () => {
415411 expect ( useBrowserHistoryStore . getState ( ) . pendingTitlesByThreadKey ) . toEqual ( { } ) ;
416412 } ) ;
417413
418- it ( "preserves pending titles through a null registration and applies them later" , ( ) => {
419- recordVisitForThread ( threadRef , "http://a.test/" , 1 ) ;
420- setTitleForThreadUrl ( threadRef , "http://a.test/" , "My App" ) ;
421- useBrowserHistoryStore . getState ( ) . registerThreadProject ( threadRef , null ) ;
422- expect ( Object . values ( useBrowserHistoryStore . getState ( ) . pendingTitlesByThreadKey ) ) . toEqual ( [
423- [ { url : "http://a.test/" , title : "My App" , environmentHostname : undefined } ] ,
424- ] ) ;
425- useBrowserHistoryStore . getState ( ) . registerThreadProject ( threadRef , "proj-a" ) ;
426- expect ( useBrowserHistoryStore . getState ( ) . byProjectKey [ "proj-a" ] ?. [ 0 ] ) . toMatchObject ( {
427- url : "http://a.test/" ,
428- title : "My App" ,
429- } ) ;
430- expect ( useBrowserHistoryStore . getState ( ) . pendingTitlesByThreadKey ) . toEqual ( { } ) ;
431- } ) ;
432-
433414 it ( "preserves environment host matching while a title is pending" , ( ) => {
434415 recordVisitForThread ( threadRef , "http://localhost:5173/app" , 1 ) ;
435416 setTitleForThreadUrl ( threadRef , "http://192.168.64.2:5173/app" , "Local App" , "192.168.64.2" ) ;
0 commit comments