@@ -673,6 +673,55 @@ const app = loadAppCore();
673673 pass ( name ) ;
674674} ) ( ) ;
675675
676+ ( function testMessageEvictionKeepsActiveOlderSession ( ) {
677+ const name = 'message eviction keeps active older session loaded' ;
678+ const testApp = loadAppCore ( ) ;
679+ testApp . state . sessions = Array . from ( { length : 11 } , ( _ , index ) => ( {
680+ id : `s${ index + 1 } ` ,
681+ title : `Session ${ index + 1 } ` ,
682+ created : 1000 + index ,
683+ lastMessageAt : 1000 + index ,
684+ messages : [ { id : `m${ index + 1 } ` , role : 'user' , content : 'hi' , created : 1000 + index } ] ,
685+ } ) ) ;
686+ testApp . state . activeSessionId = 's1' ;
687+
688+ testApp . saveSessions ( ) ;
689+
690+ const active = testApp . state . sessions . find ( ( session ) => session . id === 's1' ) ;
691+ if ( ! active || active . _serverOnly || active . messages . length !== 1 ) {
692+ fail ( name , 'active older session was evicted and would render blank' ) ;
693+ return ;
694+ }
695+ const loaded = testApp . state . sessions . filter ( ( session ) => session . messages . length > 0 && ! session . _serverOnly ) ;
696+ if ( loaded . length !== 10 ) {
697+ fail ( name , `expected exactly 10 loaded sessions, got ${ loaded . length } ` ) ;
698+ return ;
699+ }
700+ pass ( name ) ;
701+ } ) ( ) ;
702+
703+ ( function testMessageEvictionUsesRecentActivity ( ) {
704+ const name = 'message eviction prefers recent activity over creation time' ;
705+ const testApp = loadAppCore ( ) ;
706+ testApp . state . sessions = Array . from ( { length : 11 } , ( _ , index ) => ( {
707+ id : `s${ index + 1 } ` ,
708+ title : `Session ${ index + 1 } ` ,
709+ created : 1000 + index ,
710+ lastMessageAt : 1000 + index ,
711+ messages : [ { id : `m${ index + 1 } ` , role : 'user' , content : 'hi' , created : 1000 + index } ] ,
712+ } ) ) ;
713+ testApp . state . sessions [ 0 ] . lastMessageAt = 10_000 ;
714+
715+ testApp . saveSessions ( ) ;
716+
717+ const recentlyActive = testApp . state . sessions [ 0 ] ;
718+ if ( recentlyActive . _serverOnly || recentlyActive . messages . length !== 1 ) {
719+ fail ( name , 'recently active older-created session was evicted' ) ;
720+ return ;
721+ }
722+ pass ( name ) ;
723+ } ) ( ) ;
724+
676725if ( failures > 0 ) {
677726 process . exit ( 1 ) ;
678727}
0 commit comments