@@ -7,12 +7,16 @@ const createdClients: string[] = []
77const createdSessions : string [ ] = [ ]
88const enabledAutoAccept : Array < { sessionID : string ; directory : string } > = [ ]
99const optimistic : Array < {
10+ directory ?: string
11+ sessionID ?: string
1012 message : {
1113 agent : string
1214 model : { providerID : string ; modelID : string }
1315 variant ?: string
1416 }
1517} > = [ ]
18+ const optimisticSeeded : boolean [ ] = [ ]
19+ const storedSessions : Record < string , Array < { id : string ; title ?: string } > > = { }
1620const sentShell : string [ ] = [ ]
1721const syncedDirectories : string [ ] = [ ]
1822
@@ -28,7 +32,12 @@ const clientFor = (directory: string) => {
2832 session : {
2933 create : async ( ) => {
3034 createdSessions . push ( directory )
31- return { data : { id : `session-${ createdSessions . length } ` } }
35+ return {
36+ data : {
37+ id : `session-${ createdSessions . length } ` ,
38+ title : `New session ${ createdSessions . length } ` ,
39+ } ,
40+ }
3241 } ,
3342 shell : async ( ) => {
3443 sentShell . push ( directory )
@@ -129,9 +138,16 @@ beforeAll(async () => {
129138 session : {
130139 optimistic : {
131140 add : ( value : {
141+ directory ?: string
142+ sessionID ?: string
132143 message : { agent : string ; model : { providerID : string ; modelID : string } ; variant ?: string }
133144 } ) => {
134145 optimistic . push ( value )
146+ optimisticSeeded . push (
147+ ! ! value . directory &&
148+ ! ! value . sessionID &&
149+ ! ! storedSessions [ value . directory ] ?. find ( ( item ) => item . id === value . sessionID ) ?. title ,
150+ )
135151 } ,
136152 remove : ( ) => undefined ,
137153 } ,
@@ -144,7 +160,21 @@ beforeAll(async () => {
144160 useGlobalSync : ( ) => ( {
145161 child : ( directory : string ) => {
146162 syncedDirectories . push ( directory )
147- return [ { } , ( ) => undefined ]
163+ storedSessions [ directory ] ??= [ ]
164+ return [
165+ { session : storedSessions [ directory ] } ,
166+ ( ...args : unknown [ ] ) => {
167+ if ( args [ 0 ] !== "session" ) return
168+ const next = args [ 1 ]
169+ if ( typeof next === "function" ) {
170+ storedSessions [ directory ] = next ( storedSessions [ directory ] ) as Array < { id : string ; title ?: string } >
171+ return
172+ }
173+ if ( Array . isArray ( next ) ) {
174+ storedSessions [ directory ] = next as Array < { id : string ; title ?: string } >
175+ }
176+ } ,
177+ ]
148178 } ,
149179 } ) ,
150180 } ) )
@@ -170,11 +200,13 @@ beforeEach(() => {
170200 createdSessions . length = 0
171201 enabledAutoAccept . length = 0
172202 optimistic . length = 0
203+ optimisticSeeded . length = 0
173204 params = { }
174205 sentShell . length = 0
175206 syncedDirectories . length = 0
176207 selected = "/repo/worktree-a"
177208 variant = undefined
209+ for ( const key of Object . keys ( storedSessions ) ) delete storedSessions [ key ]
178210} )
179211
180212describe ( "prompt submit worktree selection" , ( ) => {
@@ -207,7 +239,7 @@ describe("prompt submit worktree selection", () => {
207239 expect ( createdClients ) . toEqual ( [ "/repo/worktree-a" , "/repo/worktree-b" ] )
208240 expect ( createdSessions ) . toEqual ( [ "/repo/worktree-a" , "/repo/worktree-b" ] )
209241 expect ( sentShell ) . toEqual ( [ "/repo/worktree-a" , "/repo/worktree-b" ] )
210- expect ( syncedDirectories ) . toEqual ( [ "/repo/worktree-a" , "/repo/worktree-b" ] )
242+ expect ( syncedDirectories ) . toEqual ( [ "/repo/worktree-a" , "/repo/worktree-a" , "/repo/worktree-b" , "/repo/worktree- b"] )
211243 } )
212244
213245 test ( "applies auto-accept to newly created sessions" , async ( ) => {
@@ -271,4 +303,32 @@ describe("prompt submit worktree selection", () => {
271303 } ,
272304 } )
273305 } )
306+
307+ test ( "seeds new sessions before optimistic prompts are added" , async ( ) => {
308+ const submit = createPromptSubmit ( {
309+ info : ( ) => undefined ,
310+ imageAttachments : ( ) => [ ] ,
311+ commentCount : ( ) => 0 ,
312+ autoAccept : ( ) => false ,
313+ mode : ( ) => "normal" ,
314+ working : ( ) => false ,
315+ editor : ( ) => undefined ,
316+ queueScroll : ( ) => undefined ,
317+ promptLength : ( value ) => value . reduce ( ( sum , part ) => sum + ( "content" in part ? part . content . length : 0 ) , 0 ) ,
318+ addToHistory : ( ) => undefined ,
319+ resetHistoryNavigation : ( ) => undefined ,
320+ setMode : ( ) => undefined ,
321+ setPopover : ( ) => undefined ,
322+ newSessionWorktree : ( ) => selected ,
323+ onNewSessionWorktreeReset : ( ) => undefined ,
324+ onSubmit : ( ) => undefined ,
325+ } )
326+
327+ const event = { preventDefault : ( ) => undefined } as unknown as Event
328+
329+ await submit . handleSubmit ( event )
330+
331+ expect ( storedSessions [ "/repo/worktree-a" ] ) . toEqual ( [ { id : "session-1" , title : "New session 1" } ] )
332+ expect ( optimisticSeeded ) . toEqual ( [ true ] )
333+ } )
274334} )
0 commit comments