|
| 1 | +import { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts"; |
| 2 | +import { act, type ComponentProps } from "react"; |
| 3 | +import { createRoot, type Root } from "react-dom/client"; |
| 4 | +import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test"; |
| 5 | +import { page, userEvent } from "vite-plus/test/browser"; |
| 6 | + |
| 7 | +import "~/index.css"; |
| 8 | + |
| 9 | +const state = vi.hoisted(() => ({ |
| 10 | + worktreePath: "/tmp/worktree" as string | null, |
| 11 | + showContextMenu: vi.fn(), |
| 12 | + writeTextToClipboard: vi.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock(import("../localApi"), async (original) => ({ |
| 16 | + ...(await original()), |
| 17 | + readLocalApi: (() => ({ contextMenu: { show: state.showContextMenu } })) as never, |
| 18 | +})); |
| 19 | +vi.mock(import("../hooks/useCopyToClipboard"), async (original) => ({ |
| 20 | + ...(await original()), |
| 21 | + writeTextToClipboard: state.writeTextToClipboard, |
| 22 | +})); |
| 23 | +vi.mock(import("./BranchToolbarBranchSelector"), async (original) => ({ |
| 24 | + ...(await original()), |
| 25 | + BranchToolbarBranchSelector: (() => null) as never, |
| 26 | +})); |
| 27 | +vi.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 | + |
| 38 | +import { BranchToolbar } from "./BranchToolbar"; |
| 39 | + |
| 40 | +let root: Root; |
| 41 | +let container: HTMLDivElement; |
| 42 | + |
| 43 | +beforeEach(() => { |
| 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 | + |
| 53 | +afterEach(async () => { |
| 54 | + await act(async () => root.unmount()); |
| 55 | + container.remove(); |
| 56 | + vi.unstubAllGlobals(); |
| 57 | +}); |
| 58 | + |
| 59 | +function 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 | + |
| 79 | +it("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 | + |
| 109 | +it.each([ |
| 110 | + ["local", null, "/tmp/project"], |
| 111 | + ["worktree", null, null], |
| 112 | + ["worktree", "/tmp/worktree", "/tmp/worktree"], |
| 113 | +] as const)( |
| 114 | + "copies only an existing workspace path with mode %s and worktree %s", |
| 115 | + async (envMode, worktreePath, copiedPath) => { |
| 116 | + state.worktreePath = worktreePath; |
| 117 | + await render({ envMode }); |
| 118 | + const event = new MouseEvent("contextmenu", { bubbles: true, cancelable: true }); |
| 119 | + await act(async () => { |
| 120 | + container.querySelector('[aria-label="Run context"]')!.dispatchEvent(event); |
| 121 | + }); |
| 122 | + expect(event.defaultPrevented).toBe(copiedPath !== null); |
| 123 | + if (copiedPath === null) { |
| 124 | + expect(state.showContextMenu).not.toHaveBeenCalled(); |
| 125 | + expect(state.writeTextToClipboard).not.toHaveBeenCalled(); |
| 126 | + } else { |
| 127 | + await vi.waitFor(() => |
| 128 | + expect(state.writeTextToClipboard).toHaveBeenCalledWith(copiedPath, "workspace path"), |
| 129 | + ); |
| 130 | + } |
| 131 | + }, |
| 132 | +); |
0 commit comments