|
| 1 | +// @vitest-environment jsdom |
| 2 | + |
| 3 | +import { |
| 4 | + EnvironmentId, |
| 5 | + ProjectId, |
| 6 | + ThreadId, |
| 7 | + ProviderInstanceId, |
| 8 | + RunId, |
| 9 | + type VcsRef, |
| 10 | +} from "@t3tools/contracts"; |
| 11 | +import { act, createRef, type ReactNode } from "react"; |
| 12 | +import { createRoot } from "react-dom/client"; |
| 13 | +import { expect, it, vi } from "vite-plus/test"; |
| 14 | + |
| 15 | +const state = vi.hoisted(() => ({ |
| 16 | + worktreePath: null as string | null, |
| 17 | + updateMetadata: vi.fn().mockResolvedValue({ _tag: "Success" }), |
| 18 | + stopSession: vi.fn().mockResolvedValue({ _tag: "Success" }), |
| 19 | +})); |
| 20 | +const mainRef: VcsRef = { |
| 21 | + name: "main", |
| 22 | + isDefault: true, |
| 23 | + isRemote: false, |
| 24 | + current: false, |
| 25 | + worktreePath: "/repo", |
| 26 | +}; |
| 27 | +const serverThread = () => ({ |
| 28 | + id: ThreadId.make("thread"), |
| 29 | + environmentId: EnvironmentId.make("local"), |
| 30 | + projectId: ProjectId.make("project"), |
| 31 | + branch: "topic", |
| 32 | + worktreePath: state.worktreePath, |
| 33 | + pullRequests: [], |
| 34 | + runtime: { |
| 35 | + status: "running", |
| 36 | + activeRunId: RunId.make("run"), |
| 37 | + providerInstanceId: ProviderInstanceId.make("codex"), |
| 38 | + providerName: null, |
| 39 | + lastError: null, |
| 40 | + updatedAt: "2026-10-16T00:00:00Z", |
| 41 | + }, |
| 42 | +}); |
| 43 | +vi.mock("../state/entities", () => ({ |
| 44 | + useThreadShell: () => serverThread(), |
| 45 | + readThreadShell: () => serverThread(), |
| 46 | + useProject: () => ({ id: ProjectId.make("project"), workspaceRoot: "/repo" }), |
| 47 | +})); |
| 48 | +vi.mock("../composerDraftStore", () => ({ |
| 49 | + useComposerDraftStore: (select: (store: unknown) => unknown) => |
| 50 | + select({ |
| 51 | + getDraftThreadByRef: () => ({ |
| 52 | + environmentId: "local", |
| 53 | + projectId: "project", |
| 54 | + branch: "topic", |
| 55 | + worktreePath: "/repo/worktree", |
| 56 | + envMode: "worktree", |
| 57 | + }), |
| 58 | + setDraftThreadContext: vi.fn(), |
| 59 | + }), |
| 60 | +})); |
| 61 | +vi.mock("../state/session", () => ({ |
| 62 | + useEnvironmentScope: () => true, |
| 63 | + readEnvironmentScope: () => true, |
| 64 | +})); |
| 65 | +vi.mock("../state/threads", () => ({ |
| 66 | + threadEnvironment: { updateMetadata: "update", stopSession: "stop" }, |
| 67 | +})); |
| 68 | +vi.mock("../state/use-atom-command", () => ({ |
| 69 | + useAtomCommand: (command: string) => |
| 70 | + command === "stop" ? state.stopSession : state.updateMetadata, |
| 71 | +})); |
| 72 | +vi.mock("../state/vcs", () => ({ vcsEnvironment: { status: vi.fn(), listRefs: vi.fn() } })); |
| 73 | +vi.mock("../state/query", () => ({ |
| 74 | + useEnvironmentQuery: () => ({ data: { refName: "topic", refs: [mainRef] }, refresh: vi.fn() }), |
| 75 | +})); |
| 76 | +vi.mock("../state/queries", () => ({ |
| 77 | + usePaginatedBranches: () => ({ |
| 78 | + refs: [mainRef], |
| 79 | + data: { nextCursor: null, totalCount: 1 }, |
| 80 | + isPending: false, |
| 81 | + }), |
| 82 | +})); |
| 83 | +vi.mock("../hooks/useSupportsMultiplePullRequests", () => ({ |
| 84 | + useSupportsMultiplePullRequests: () => false, |
| 85 | +})); |
| 86 | +vi.mock("../lib/openPullRequestLink", () => ({ useOpenPrLink: () => vi.fn() })); |
| 87 | +vi.mock("./chat/composerEventScope", () => ({ useComposerMenuProps: () => ({}) })); |
| 88 | +vi.mock("./chat/ThreadDetailsPrRows", () => ({ ThreadDetailsPrRows: () => null })); |
| 89 | +vi.mock("../rightPanelStore", () => ({ useRightPanelStore: { getState: vi.fn() } })); |
| 90 | +vi.mock("./ThreadStatusIndicators", () => ({ |
| 91 | + ThreadPullRequestBadgeControl: () => null, |
| 92 | + prStatusIndicator: () => null, |
| 93 | + resolveThreadPullRequestBadge: () => null, |
| 94 | + useLinkedThreadPullRequest: () => null, |
| 95 | +})); |
| 96 | +// jsdom has no layout measurements for the virtualized list. |
| 97 | +vi.mock("@legendapp/list/react", () => ({ |
| 98 | + LegendList: ({ |
| 99 | + data, |
| 100 | + renderItem, |
| 101 | + }: { |
| 102 | + data: string[]; |
| 103 | + renderItem: (row: { item: string; index: number }) => ReactNode; |
| 104 | + }) => data.map((item, index) => <div key={item}>{renderItem({ item, index })}</div>), |
| 105 | +})); |
| 106 | + |
| 107 | +import { |
| 108 | + BranchToolbarBranchSelector, |
| 109 | + type BranchToolbarBranchSelectorHandle, |
| 110 | +} from "./BranchToolbarBranchSelector"; |
| 111 | + |
| 112 | +it.each(["toolbar", "panel"] as const)( |
| 113 | + "keeps the main checkout selectable in %s with a running server thread and retained worktree draft", |
| 114 | + async (displayMode) => { |
| 115 | + vi.clearAllMocks(); |
| 116 | + state.worktreePath = "/repo/worktree"; |
| 117 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 118 | + const container = document.createElement("div"); |
| 119 | + document.body.append(container); |
| 120 | + const root = createRoot(container); |
| 121 | + const ref = createRef<BranchToolbarBranchSelectorHandle>(); |
| 122 | + const renderSelector = () => |
| 123 | + root.render( |
| 124 | + <BranchToolbarBranchSelector |
| 125 | + displayMode={displayMode} |
| 126 | + ref={ref} |
| 127 | + environmentId={EnvironmentId.make("local")} |
| 128 | + threadId={ThreadId.make("thread")} |
| 129 | + envLocked |
| 130 | + startFromOrigin={false} |
| 131 | + onStartFromOriginChange={vi.fn()} |
| 132 | + />, |
| 133 | + ); |
| 134 | + try { |
| 135 | + await act(async () => renderSelector()); |
| 136 | + await act(async () => ref.current?.open()); |
| 137 | + expect(document.querySelector('[role="option"]')?.getAttribute("aria-disabled")).toBe("true"); |
| 138 | + expect(document.body.textContent).toContain("Stop the current turn to switch worktrees."); |
| 139 | + state.worktreePath = null; |
| 140 | + await act(async () => renderSelector()); |
| 141 | + const main = document.querySelector<HTMLElement>('[role="option"]'); |
| 142 | + expect(main?.textContent).toContain("main"); |
| 143 | + expect(main?.getAttribute("aria-disabled")).not.toBe("true"); |
| 144 | + expect(document.body.textContent).not.toContain("Stop the current turn to switch worktrees."); |
| 145 | + await act(async () => main?.click()); |
| 146 | + expect(state.updateMetadata).toHaveBeenCalledWith({ |
| 147 | + environmentId: "local", |
| 148 | + input: { threadId: "thread", branch: "main", worktreePath: null }, |
| 149 | + }); |
| 150 | + expect(state.stopSession).not.toHaveBeenCalled(); |
| 151 | + } finally { |
| 152 | + await act(async () => root.unmount()); |
| 153 | + container.remove(); |
| 154 | + state.worktreePath = null; |
| 155 | + vi.unstubAllGlobals(); |
| 156 | + } |
| 157 | + }, |
| 158 | +); |
0 commit comments