@@ -33,6 +33,7 @@ describe("Worktree Commands", () => {
3333 let eventBus : EventBus ;
3434 let db : ReturnType < typeof openDatabase > ;
3535 let workspaceId : string ;
36+ let tempPaths : string [ ] ;
3637
3738 beforeEach ( async ( ) => {
3839 repoDir = join (
@@ -51,6 +52,7 @@ describe("Worktree Commands", () => {
5152 runMigrations ( db ) ;
5253 eventBus = new EventBus ( ) ;
5354 workspaceMgr = new WorkspaceManager ( { db, eventBus } ) ;
55+ tempPaths = [ ] ;
5456
5557 const workspace = await workspaceMgr . open ( { path : repoDir } ) ;
5658 workspaceId = workspace . id ;
@@ -71,6 +73,7 @@ describe("Worktree Commands", () => {
7173 afterEach ( async ( ) => {
7274 await rm ( repoDir , { recursive : true , force : true } ) ;
7375 await rm ( otherRepoDir , { recursive : true , force : true } ) ;
76+ await Promise . all ( tempPaths . map ( ( tempPath ) => rm ( tempPath , { recursive : true , force : true } ) ) ) ;
7477 } ) ;
7578
7679 it ( "returns status for a worktree belonging to the workspace repo" , async ( ) => {
@@ -118,4 +121,83 @@ describe("Worktree Commands", () => {
118121 expect ( result . ok ) . toBe ( false ) ;
119122 expect ( result . error ?. code ) . toBe ( "worktree_not_found" ) ;
120123 } ) ;
124+
125+ it ( "emits worktreeChanged after create and remove" , async ( ) => {
126+ const createdPath = join (
127+ tmpdir ( ) ,
128+ `worktree-command-created-${ Date . now ( ) } -${ Math . random ( ) . toString ( 36 ) . slice ( 2 ) } `
129+ ) ;
130+ const linkedPath = join (
131+ tmpdir ( ) ,
132+ `worktree-command-linked-${ Date . now ( ) } -${ Math . random ( ) . toString ( 36 ) . slice ( 2 ) } `
133+ ) ;
134+ tempPaths . push ( createdPath , linkedPath ) ;
135+
136+ await execFileAsync ( "git" , [ "branch" , "feature/existing-worktree" ] , { cwd : repoDir } ) ;
137+ await execFileAsync ( "git" , [ "worktree" , "add" , linkedPath , "feature/existing-worktree" ] , {
138+ cwd : repoDir ,
139+ } ) ;
140+ const linkedWorkspace = await workspaceMgr . open ( { path : linkedPath } ) ;
141+ await execFileAsync ( "git" , [ "branch" , "feature/worktree-manager" ] , { cwd : repoDir } ) ;
142+
143+ const emitted : Array < { workspaceId : string ; worktreeChanged ?: boolean } > = [ ] ;
144+ eventBus . on ( "git.state.changed" , ( event ) => emitted . push ( event ) ) ;
145+
146+ const createResult = await dispatch (
147+ {
148+ kind : "command" ,
149+ id : "worktree-create-event" ,
150+ op : "worktree.create" ,
151+ args : {
152+ workspaceId,
153+ branch : "feature/worktree-manager" ,
154+ path : createdPath ,
155+ } ,
156+ } ,
157+ ctx
158+ ) ;
159+
160+ expect ( createResult . ok ) . toBe ( true ) ;
161+ expect ( emitted ) . toEqual (
162+ expect . arrayContaining ( [
163+ expect . objectContaining ( {
164+ workspaceId,
165+ worktreeChanged : true ,
166+ } ) ,
167+ expect . objectContaining ( {
168+ workspaceId : linkedWorkspace . id ,
169+ worktreeChanged : true ,
170+ } ) ,
171+ ] )
172+ ) ;
173+
174+ emitted . length = 0 ;
175+
176+ const removeResult = await dispatch (
177+ {
178+ kind : "command" ,
179+ id : "worktree-remove-event" ,
180+ op : "worktree.remove" ,
181+ args : {
182+ workspaceId,
183+ worktreePath : createdPath ,
184+ } ,
185+ } ,
186+ ctx
187+ ) ;
188+
189+ expect ( removeResult . ok ) . toBe ( true ) ;
190+ expect ( emitted ) . toEqual (
191+ expect . arrayContaining ( [
192+ expect . objectContaining ( {
193+ workspaceId,
194+ worktreeChanged : true ,
195+ } ) ,
196+ expect . objectContaining ( {
197+ workspaceId : linkedWorkspace . id ,
198+ worktreeChanged : true ,
199+ } ) ,
200+ ] )
201+ ) ;
202+ } ) ;
121203} ) ;
0 commit comments