@@ -5,6 +5,18 @@ import { act } from "react";
55import { createRoot } from "react-dom/client" ;
66import { expect , it , vi } from "vite-plus/test" ;
77
8+ const state = vi . hoisted ( ( ) => ( {
9+ worktreePath : "/tmp/worktree" as string | null ,
10+ showContextMenu : vi . fn ( ) . mockResolvedValue ( "copy-path" ) ,
11+ writeTextToClipboard : vi . fn ( ) . mockResolvedValue ( true ) ,
12+ } ) ) ;
13+
14+ vi . mock ( "../localApi" , ( ) => ( {
15+ readLocalApi : ( ) => ( { contextMenu : { show : state . showContextMenu } } ) ,
16+ } ) ) ;
17+ vi . mock ( "../hooks/useCopyToClipboard" , ( ) => ( {
18+ writeTextToClipboard : state . writeTextToClipboard ,
19+ } ) ) ;
820vi . mock ( "./BranchToolbarBranchSelector" , ( ) => ( { BranchToolbarBranchSelector : ( ) => null } ) ) ;
921vi . mock ( "../composerDraftStore" , ( ) => ( {
1022 useComposerDraftStore : ( select : ( store : unknown ) => unknown ) =>
@@ -14,7 +26,7 @@ vi.mock("../state/entities", () => ({
1426 useThreadShell : ( ) => ( {
1527 environmentId : "local" ,
1628 projectId : "project" ,
17- worktreePath : "/tmp/worktree" ,
29+ worktreePath : state . worktreePath ,
1830 } ) ,
1931 useProject : ( ) => ( { workspaceRoot : "/tmp/project" } ) ,
2032 useThreadShellsForProjectRefs : ( ) => [ ] ,
@@ -73,3 +85,54 @@ it("keeps machine choices usable when the combined row's workspace is locked", a
7385 vi . unstubAllGlobals ( ) ;
7486 }
7587} ) ;
88+
89+ it . each ( [
90+ [ "local" , null , "/tmp/project" ] ,
91+ [ "worktree" , null , null ] ,
92+ [ "worktree" , "/tmp/worktree" , "/tmp/worktree" ] ,
93+ ] as const ) (
94+ "copies only an existing workspace path with mode %s and worktree %s" ,
95+ async ( envMode , worktreePath , copiedPath ) => {
96+ vi . stubGlobal ( "IS_REACT_ACT_ENVIRONMENT" , true ) ;
97+ state . worktreePath = worktreePath ;
98+ state . showContextMenu . mockClear ( ) ;
99+ state . writeTextToClipboard . mockClear ( ) ;
100+ const container = document . createElement ( "div" ) ;
101+ document . body . append ( container ) ;
102+ const root = createRoot ( container ) ;
103+ try {
104+ await act ( async ( ) => {
105+ root . render (
106+ < BranchToolbar
107+ layout = "panel"
108+ panelSection = "workspace"
109+ environmentId = { EnvironmentId . make ( "local" ) }
110+ threadId = { ThreadId . make ( "thread" ) }
111+ showGitControls
112+ envMode = { envMode }
113+ envLocked = { false }
114+ startFromOrigin = { false }
115+ onStartFromOriginChange = { vi . fn ( ) }
116+ onEnvModeChange = { vi . fn ( ) }
117+ /> ,
118+ ) ;
119+ } ) ;
120+ const event = new MouseEvent ( "contextmenu" , { bubbles : true , cancelable : true } ) ;
121+ await act ( async ( ) => {
122+ container . querySelector ( '[aria-label="Run context"]' ) ! . dispatchEvent ( event ) ;
123+ } ) ;
124+ expect ( event . defaultPrevented ) . toBe ( copiedPath !== null ) ;
125+ if ( copiedPath === null ) {
126+ expect ( state . showContextMenu ) . not . toHaveBeenCalled ( ) ;
127+ expect ( state . writeTextToClipboard ) . not . toHaveBeenCalled ( ) ;
128+ } else {
129+ expect ( state . writeTextToClipboard ) . toHaveBeenCalledWith ( copiedPath , "workspace path" ) ;
130+ }
131+ } finally {
132+ await act ( async ( ) => root . unmount ( ) ) ;
133+ container . remove ( ) ;
134+ state . worktreePath = "/tmp/worktree" ;
135+ vi . unstubAllGlobals ( ) ;
136+ }
137+ } ,
138+ ) ;
0 commit comments