@@ -578,6 +578,64 @@ describe("WorkspaceService", () => {
578578 } ) ;
579579 } ) ;
580580
581+ describe ( "pending creations" , ( ) => {
582+ afterEach ( ( ) => {
583+ vi . mocked ( getCurrentBranch ) . mockReset ( ) ;
584+ } ) ;
585+
586+ function localInput ( taskId : string ) : CreateWorkspaceInput {
587+ return {
588+ taskId,
589+ mainRepoPath : "/repo" ,
590+ folderId : "folder-1" ,
591+ folderPath : "/repo" ,
592+ mode : "local" ,
593+ branch : "feature/x" ,
594+ } ;
595+ }
596+
597+ it ( "tracks in-flight creations and clears them when creation settles" , async ( ) => {
598+ let rejectBranch : ( error : Error ) => void = ( ) => { } ;
599+ vi . mocked ( getCurrentBranch ) . mockReturnValue (
600+ new Promise ( ( _ , reject ) => {
601+ rejectBranch = reject ;
602+ } ) ,
603+ ) ;
604+
605+ const pending = service . createWorkspace ( localInput ( "task-1" ) ) ;
606+ expect ( service . pendingCreationCount ) . toBe ( 1 ) ;
607+
608+ rejectBranch ( new Error ( "boom" ) ) ;
609+ await expect ( pending ) . rejects . toThrow ( "boom" ) ;
610+ expect ( service . pendingCreationCount ) . toBe ( 0 ) ;
611+ } ) ;
612+
613+ it ( "waitForPendingCreations resolves even when creations reject" , async ( ) => {
614+ const rejectors : Array < ( error : Error ) => void > = [ ] ;
615+ const deferredBranch = ( ) =>
616+ new Promise < string | null > ( ( _ , reject ) => {
617+ rejectors . push ( reject ) ;
618+ } ) ;
619+ vi . mocked ( getCurrentBranch )
620+ . mockReturnValueOnce ( deferredBranch ( ) )
621+ . mockReturnValueOnce ( deferredBranch ( ) ) ;
622+
623+ const first = service . createWorkspace ( localInput ( "task-1" ) ) ;
624+ const second = service . createWorkspace ( localInput ( "task-2" ) ) ;
625+ expect ( service . pendingCreationCount ) . toBe ( 2 ) ;
626+
627+ const wait = service . waitForPendingCreations ( ) ;
628+ for ( const reject of rejectors ) {
629+ reject ( new Error ( "boom" ) ) ;
630+ }
631+
632+ await expect ( wait ) . resolves . toBeUndefined ( ) ;
633+ await expect ( first ) . rejects . toThrow ( "boom" ) ;
634+ await expect ( second ) . rejects . toThrow ( "boom" ) ;
635+ expect ( service . pendingCreationCount ) . toBe ( 0 ) ;
636+ } ) ;
637+ } ) ;
638+
581639 describe ( "worktree path resolved from the stored row" , ( ) => {
582640 const tempDirs : string [ ] = [ ] ;
583641
0 commit comments