Skip to content

Commit 058dcf0

Browse files
committed
fix(web): hide paths for worktrees not yet created
1 parent d3f8e62 commit 058dcf0

2 files changed

Lines changed: 68 additions & 2 deletions

File tree

‎apps/web/src/components/BranchToolbar.panel.test.tsx‎

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ import { act } from "react";
55
import { createRoot } from "react-dom/client";
66
import { 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+
}));
820
vi.mock("./BranchToolbarBranchSelector", () => ({ BranchToolbarBranchSelector: () => null }));
921
vi.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+
);

‎apps/web/src/components/BranchToolbar.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,10 @@ const RunContextSelector = memo(function RunContextSelector({
198198
: resolveCurrentWorkspaceLabel(activeWorktreePath);
199199
const isPanel = displayMode === "panel";
200200
const isLocked = envLocked || (envModeLocked && (!isPanel || !showEnvironmentPicker));
201-
const workspacePath = forceNewWorktree ? null : (activeWorktreePath ?? workspaceRoot);
201+
const workspacePath =
202+
forceNewWorktree || (effectiveEnvMode === "worktree" && !activeWorktreePath)
203+
? null
204+
: (activeWorktreePath ?? workspaceRoot);
202205
const handleContextMenu = (event: ReactMouseEvent) => {
203206
if (isPanel && workspacePath) {
204207
showWorkspaceContextMenu(event, workspacePath);

0 commit comments

Comments
 (0)