@ -106,6 +106,62 @@ describe('stats route', () => {
expect ( window . SoulSyncWebShellBridge ? . setActivePageChrome ) . toHaveBeenCalledWith ( 'stats' ) ;
} ) ;
it ( 'still renders when listening stats status prefetch fails' , async ( ) = > {
vi . stubGlobal (
'fetch' ,
vi . fn ( async ( input : RequestInfo | URL ) = > {
const url = input instanceof Request ? input.url : String ( input ) ;
if ( url . includes ( '/api/stats/cached' ) ) {
return createResponse ( {
success : true ,
overview : {
total_plays : 24 ,
total_time_ms : 6_600_000 ,
unique_artists : 3 ,
unique_albums : 4 ,
unique_tracks : 12 ,
} ,
top_artists : [ { id : 7 , name : 'Artist A' , play_count : 10 } ] ,
top_albums : [ ] ,
top_tracks : [ ] ,
timeline : [ { date : 'May 10' , plays : 4 } ] ,
genres : [ { genre : 'House' , play_count : 10 , percentage : 80 } ] ,
recent : [ { title : 'Track A' , artist : 'Artist A' , played_at : '2026-05-14T08:00:00Z' } ] ,
health : { total_tracks : 12 , format_breakdown : { FLAC : 12 } } ,
} ) ;
}
if ( url . includes ( '/api/listening-stats/status' ) ) {
return createResponse ( { error : 'status unavailable' } , false , 500 ) ;
}
if ( url . includes ( '/api/stats/db-storage' ) ) {
return createResponse ( {
success : true ,
tables : [ { name : 'tracks' , size : 2048 } ] ,
total_file_size : 4096 ,
method : 'dbstat' ,
} ) ;
}
if ( url . includes ( '/api/stats/library-disk-usage' ) ) {
return createResponse ( {
success : true ,
has_data : true ,
total_bytes : 2048 ,
tracks_with_size : 12 ,
tracks_without_size : 0 ,
by_format : { flac : 2048 } ,
} ) ;
}
return createResponse ( { success : true } ) ;
} ) as unknown as typeof fetch ,
) ;
renderStatsRoute ( ) ;
await waitFor ( ( ) = > expect ( screen . getByTestId ( 'stats-page' ) ) . toBeInTheDocument ( ) ) ;
expect ( await screen . findByText ( 'Listening Stats' ) ) . toBeInTheDocument ( ) ;
expect ( screen . getByText ( 'Not synced yet' ) ) . toBeInTheDocument ( ) ;
} ) ;
it ( 'stores the time range in route search state' , async ( ) = > {
const { history } = renderStatsRoute ( ) ;
@ -125,6 +181,80 @@ describe('stats route', () => {
) ;
} ) ;
it ( 'falls back to streaming when track resolution fails' , async ( ) = > {
window . SoulSyncWebShellBridge = createShellBridge ( {
startStream : vi.fn ( ) ,
} ) ;
vi . stubGlobal (
'fetch' ,
vi . fn ( async ( input : RequestInfo | URL ) = > {
const url = input instanceof Request ? input.url : String ( input ) ;
if ( url . includes ( '/api/stats/cached' ) ) {
return createResponse ( {
success : true ,
overview : {
total_plays : 24 ,
total_time_ms : 6_600_000 ,
unique_artists : 3 ,
unique_albums : 4 ,
unique_tracks : 12 ,
} ,
top_artists : [ { id : 7 , name : 'Artist A' , play_count : 10 } ] ,
top_albums : [ ] ,
top_tracks : [ { name : 'Track A' , artist : 'Artist A' , album : 'Album A' , play_count : 3 } ] ,
timeline : [ { date : 'May 10' , plays : 4 } ] ,
genres : [ { genre : 'House' , play_count : 10 , percentage : 80 } ] ,
recent : [ { title : 'Track A' , artist : 'Artist A' , played_at : '2026-05-14T08:00:00Z' } ] ,
health : { total_tracks : 12 , format_breakdown : { FLAC : 12 } } ,
} ) ;
}
if ( url . includes ( '/api/listening-stats/status' ) ) {
return createResponse ( { stats : { last_poll : '2026-05-14 10:00:00' } } ) ;
}
if ( url . includes ( '/api/stats/resolve-track' ) ) {
return createResponse ( { error : 'resolve unavailable' } , false , 500 ) ;
}
if ( url . includes ( '/api/enhanced-search/stream-track' ) ) {
return createResponse ( {
success : true ,
result : { stream_url : '/api/stream/1' } ,
} ) ;
}
if ( url . includes ( '/api/stats/db-storage' ) ) {
return createResponse ( {
success : true ,
tables : [ { name : 'tracks' , size : 2048 } ] ,
total_file_size : 4096 ,
method : 'dbstat' ,
} ) ;
}
if ( url . includes ( '/api/stats/library-disk-usage' ) ) {
return createResponse ( {
success : true ,
has_data : true ,
total_bytes : 2048 ,
tracks_with_size : 12 ,
tracks_without_size : 0 ,
by_format : { flac : 2048 } ,
} ) ;
}
return createResponse ( { success : true } ) ;
} ) as unknown as typeof fetch ,
) ;
renderStatsRoute ( ) ;
fireEvent . click ( ( await screen . findAllByTitle ( 'Play' ) ) [ 0 ] ) ;
await waitFor ( ( ) = >
expect ( window . SoulSyncWebShellBridge ? . startStream ) . toHaveBeenCalledWith ( {
stream_url : '/api/stream/1' ,
} ) ,
) ;
expect ( window . SoulSyncWebShellBridge ? . playLibraryTrack ) . not . toHaveBeenCalled ( ) ;
} ) ;
it ( 'redirects back home when the page is not allowed' , async ( ) = > {
window . SoulSyncWebShellBridge = createShellBridge ( {
isPageAllowed : vi.fn ( ( pageId ) = > pageId !== 'stats' ) ,