@@ -69,8 +69,11 @@ mockModulePreservingExports('../../../utils/config.ts', {
6969 enableConfigs : mock ( ( ) => { } ) ,
7070} )
7171
72+ const mockSwitchSession = mock ( ( ) => { } )
73+
7274mockModulePreservingExports ( '../../../bootstrap/state.ts' , {
7375 setOriginalCwd : mock ( ( ) => { } ) ,
76+ switchSession : mockSwitchSession ,
7477 addSlowOperation : mock ( ( ) => { } ) ,
7578} )
7679
@@ -222,6 +225,7 @@ describe('AcpAgent', () => {
222225 delete process . env . ACP_PERMISSION_MODE
223226 delete process . env . CLAUDE_CODE_ACP_ALLOW_BYPASS_PERMISSIONS
224227 mockSetModel . mockClear ( )
228+ mockSwitchSession . mockClear ( )
225229 mockSubmitMessage . mockReset ( )
226230 mockSubmitMessage . mockImplementation ( async function * ( _input : string ) { } )
227231 mockGetMainLoopModel . mockClear ( )
@@ -1157,4 +1161,66 @@ describe('AcpAgent', () => {
11571161 expect ( commit . input ) . toEqual ( { hint : '[message]' } )
11581162 } )
11591163 } )
1164+
1165+ describe ( 'sessionId alignment with global state' , ( ) => {
1166+ test ( 'newSession calls switchSession with the generated sessionId' , async ( ) => {
1167+ const agent = new AcpAgent ( makeConn ( ) )
1168+ const res = await agent . newSession ( { cwd : '/tmp' } as any )
1169+ expect ( mockSwitchSession ) . toHaveBeenCalledWith ( res . sessionId )
1170+ } )
1171+
1172+ test ( 'resumeSession calls switchSession with the requested sessionId' , async ( ) => {
1173+ const agent = new AcpAgent ( makeConn ( ) )
1174+ const requestedId = 'resume-test-session-id'
1175+ await agent . unstable_resumeSession ( {
1176+ sessionId : requestedId ,
1177+ cwd : '/tmp' ,
1178+ mcpServers : [ ] ,
1179+ } as any )
1180+
1181+ expect ( mockSwitchSession ) . toHaveBeenCalledWith ( requestedId )
1182+ } )
1183+
1184+ test ( 'loadSession calls switchSession with the requested sessionId' , async ( ) => {
1185+ const agent = new AcpAgent ( makeConn ( ) )
1186+ const requestedId = 'load-test-session-id'
1187+ await agent . loadSession ( {
1188+ sessionId : requestedId ,
1189+ cwd : '/tmp' ,
1190+ mcpServers : [ ] ,
1191+ } as any )
1192+
1193+ expect ( mockSwitchSession ) . toHaveBeenCalledWith ( requestedId )
1194+ } )
1195+
1196+ test ( 'resumeSession with existing session still calls switchSession' , async ( ) => {
1197+ const agent = new AcpAgent ( makeConn ( ) )
1198+ const { sessionId } = await agent . newSession ( { cwd : '/tmp' } as any )
1199+ mockSwitchSession . mockClear ( )
1200+
1201+ // Resume the same session — should still align global state
1202+ await agent . unstable_resumeSession ( {
1203+ sessionId,
1204+ cwd : '/tmp' ,
1205+ mcpServers : [ ] ,
1206+ } as any )
1207+
1208+ expect ( mockSwitchSession ) . toHaveBeenCalledWith ( sessionId )
1209+ } )
1210+
1211+ test ( 'prompt does not trigger additional switchSession for multi-session' , async ( ) => {
1212+ const agent = new AcpAgent ( makeConn ( ) )
1213+ await agent . newSession ( { cwd : '/tmp' } as any )
1214+ await agent . newSession ( { cwd : '/tmp' } as any )
1215+ mockSwitchSession . mockClear ( )
1216+
1217+ // Prompts should not call switchSession — alignment happens at session creation
1218+ const s1 = agent . sessions . keys ( ) . next ( ) . value
1219+ await agent . prompt ( {
1220+ sessionId : s1 ,
1221+ prompt : [ { type : 'text' , text : 'hello' } ] ,
1222+ } as any )
1223+ expect ( mockSwitchSession ) . not . toHaveBeenCalled ( )
1224+ } )
1225+ } )
11601226} )
0 commit comments