Open History to see the last commit
1import { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts";
2import { act, type ComponentProps } from "react";
3import { createRoot, type Root } from "react-dom/client";
4import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
5import { page, userEvent } from "vite-plus/test/browser";
6
7import "~/index.css";
8
9const state = vi.hoisted(() => ({
10 worktreePath: "/tmp/worktree" as string | null,
11 showContextMenu: vi.fn(),
12 writeTextToClipboard: vi.fn(),
13}));
14
15vi.mock(import("../localApi"), async (original) => ({
16 ...(await original()),
17 readLocalApi: (() => ({ contextMenu: { show: state.showContextMenu } })) as never,
18}));
19vi.mock(import("../hooks/useCopyToClipboard"), async (original) => ({
20 ...(await original()),
21 writeTextToClipboard: state.writeTextToClipboard,
22}));
23vi.mock(import("./BranchToolbarBranchSelector"), async (original) => ({
24 ...(await original()),
25 BranchToolbarBranchSelector: (() => null) as never,
26}));
27vi.mock(import("../state/entities"), async (original) => ({
28 ...(await original()),
29 useQuietThreadShell: (() => ({
30 environmentId: "local",
31 projectId: "project",
32 worktreePath: state.worktreePath,
33 })) as never,
34 useProject: (() => ({ workspaceRoot: "/tmp/project" })) as never,
35 useThreadShellsForProjectRefs: () => [],
36}));
37
38import { BranchToolbar } from "./BranchToolbar";
39
40let root: Root;
41let container: HTMLDivElement;
42
43beforeEach(() => {
44 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
45 state.worktreePath = "/tmp/worktree";
46 state.showContextMenu.mockReset().mockResolvedValue("copy-path");
47 state.writeTextToClipboard.mockReset().mockResolvedValue(true);
48 container = document.createElement("div");
49 document.body.append(container);
50 root = createRoot(container);
51});
52
53afterEach(async () => {
54 await act(async () => root.unmount());
55 container.remove();
56 vi.unstubAllGlobals();
57});
58
59function render(props: Partial<ComponentProps<typeof BranchToolbar>>) {
60 return act(async () =>
61 root.render(
62 <BranchToolbar
63 layout="panel"
64 panelSection="workspace"
65 environmentId={EnvironmentId.make("local")}
66 threadId={ThreadId.make("thread")}
67 showGitControls
68 envMode="local"
69 envLocked={false}
70 startFromOrigin={false}
71 onStartFromOriginChange={vi.fn()}
72 onEnvModeChange={vi.fn()}
73 {...props}
74 />,
75 ),
76 );
77}
78
79it("keeps machine choices usable when the combined row's workspace is locked", async () => {
80 const onEnvironmentChange = vi.fn();
81 await render({
82 onEnvironmentChange,
83 availableEnvironments: ["local", "remote"].map((id) => ({
84 environmentId: EnvironmentId.make(id),
85 projectId: ProjectId.make("project"),
86 label: id,
87 isPrimary: id === "local",
88 machine: "server",
89 })),
90 });
91 const trigger = container.querySelector("button")!;
92 expect(trigger.textContent).toBe("local");
93 expect(container.querySelectorAll("button")).toHaveLength(1);
94
95 // A ctrl-click is a macOS secondary click; it must not open the menu.
96 await act(async () => {
97 trigger.dispatchEvent(new MouseEvent("mousedown", { bubbles: true, ctrlKey: true }));
98 });
99 expect(document.querySelector('[role="menu"]')).toBeNull();
100
101 await userEvent.click(trigger);
102 await expect
103 .element(page.getByRole("menuitemradio", { name: "New worktree" }))
104 .toHaveAttribute("aria-disabled", "true");
105 await userEvent.click(page.getByRole("menuitemradio", { name: "remote" }));
106 expect(onEnvironmentChange).toHaveBeenCalledWith("remote");
107});
108
109it("shows a started thread's primary machine as a static label in the composer", async () => {
110 await render({
111 layout: "composer",
112 showGitControls: false,
113 availableEnvironments: ["local", "remote"].map((id) => ({
114 environmentId: EnvironmentId.make(id),
115 projectId: ProjectId.make("project"),
116 label: id,
117 isPrimary: id === "local",
118 machine: "server",
119 })),
120 });
121 expect(container.textContent).toBe("local");
122 expect(container.querySelector("button")).toBeNull();
123});
124
125it.each([
126 ["local", null, "/tmp/project"],
127 ["worktree", null, null],
128 ["worktree", "/tmp/worktree", "/tmp/worktree"],
129] as const)(
130 "copies only an existing workspace path with mode %s and worktree %s",
131 async (envMode, worktreePath, copiedPath) => {
132 state.worktreePath = worktreePath;
133 await render({ envMode });
134 const event = new MouseEvent("contextmenu", { bubbles: true, cancelable: true });
135 await act(async () => {
136 container.querySelector('[aria-label="Run context"]')!.dispatchEvent(event);
137 });
138 expect(event.defaultPrevented).toBe(copiedPath !== null);
139 if (copiedPath === null) {
140 expect(state.showContextMenu).not.toHaveBeenCalled();
141 expect(state.writeTextToClipboard).not.toHaveBeenCalled();
142 } else {
143 await vi.waitFor(() =>
144 expect(state.writeTextToClipboard).toHaveBeenCalledWith(copiedPath, "workspace path"),
145 );
146 }
147 },
148);