Open History to see the last commit
1import type { GitBranch } from "@t3tools/contracts";
2import { describe, expect, it } from "vitest";
3import {
4 dedupeRemoteBranchesWithLocalMatches,
5 deriveLocalBranchNameFromRemoteRef,
6 resolveBranchSelectionTarget,
7 resolveDraftEnvModeAfterBranchChange,
8 resolveBranchToolbarValue,
9 shouldIncludeBranchPickerItem,
10} from "./BranchToolbar.logic";
11
12describe("resolveDraftEnvModeAfterBranchChange", () => {
13 it("switches to local mode when returning from an existing worktree to the main worktree", () => {
14 expect(
15 resolveDraftEnvModeAfterBranchChange({
16 nextWorktreePath: null,
17 currentWorktreePath: "/repo/.t3/worktrees/feature-a",
18 effectiveEnvMode: "worktree",
19 }),
20 ).toBe("local");
21 });
22
23 it("keeps new-worktree mode when selecting a base branch before worktree creation", () => {
24 expect(
25 resolveDraftEnvModeAfterBranchChange({
26 nextWorktreePath: null,
27 currentWorktreePath: null,
28 effectiveEnvMode: "worktree",
29 }),
30 ).toBe("worktree");
31 });
32
33 it("uses worktree mode when selecting a branch already attached to a worktree", () => {
34 expect(
35 resolveDraftEnvModeAfterBranchChange({
36 nextWorktreePath: "/repo/.t3/worktrees/feature-a",
37 currentWorktreePath: null,
38 effectiveEnvMode: "local",
39 }),
40 ).toBe("worktree");
41 });
42});
43
44describe("resolveBranchToolbarValue", () => {
45 it("defaults new-worktree mode to current git branch when no explicit base branch is set", () => {
46 expect(
47 resolveBranchToolbarValue({
48 envMode: "worktree",
49 activeWorktreePath: null,
50 activeThreadBranch: null,
51 currentGitBranch: "main",
52 }),
53 ).toBe("main");
54 });
55
56 it("keeps an explicitly selected worktree base branch", () => {
57 expect(
58 resolveBranchToolbarValue({
59 envMode: "worktree",
60 activeWorktreePath: null,
61 activeThreadBranch: "feature/base",
62 currentGitBranch: "main",
63 }),
64 ).toBe("feature/base");
65 });
66
67 it("shows the actual checked-out branch when not selecting a new worktree base", () => {
68 expect(
69 resolveBranchToolbarValue({
70 envMode: "local",
71 activeWorktreePath: null,
72 activeThreadBranch: "feature/base",
73 currentGitBranch: "main",
74 }),
75 ).toBe("main");
76 });
77});
78
79describe("deriveLocalBranchNameFromRemoteRef", () => {
80 it("strips the remote prefix from a remote ref", () => {
81 expect(deriveLocalBranchNameFromRemoteRef("origin/feature/demo")).toBe("feature/demo");
82 });
83
84 it("supports remote names that contain slashes", () => {
85 expect(deriveLocalBranchNameFromRemoteRef("my-org/upstream/feature/demo")).toBe(
86 "upstream/feature/demo",
87 );
88 });
89
90 it("returns the original name when ref is malformed", () => {
91 expect(deriveLocalBranchNameFromRemoteRef("origin/")).toBe("origin/");
92 expect(deriveLocalBranchNameFromRemoteRef("/feature/demo")).toBe("/feature/demo");
93 });
94});
95
96describe("dedupeRemoteBranchesWithLocalMatches", () => {
97 it("hides remote refs when the matching local branch exists", () => {
98 const input: GitBranch[] = [
99 {
100 name: "feature/demo",
101 current: false,
102 isDefault: false,
103 worktreePath: null,
104 },
105 {
106 name: "origin/feature/demo",
107 isRemote: true,
108 remoteName: "origin",
109 current: false,
110 isDefault: false,
111 worktreePath: null,
112 },
113 {
114 name: "origin/feature/remote-only",
115 isRemote: true,
116 remoteName: "origin",
117 current: false,
118 isDefault: false,
119 worktreePath: null,
120 },
121 ];
122
123 expect(dedupeRemoteBranchesWithLocalMatches(input).map((branch) => branch.name)).toEqual([
124 "feature/demo",
125 "origin/feature/remote-only",
126 ]);
127 });
128
129 it("keeps all entries when no local match exists for a remote ref", () => {
130 const input: GitBranch[] = [
131 {
132 name: "feature/local",
133 current: false,
134 isDefault: false,
135 worktreePath: null,
136 },
137 {
138 name: "origin/feature/remote-only",
139 isRemote: true,
140 remoteName: "origin",
141 current: false,
142 isDefault: false,
143 worktreePath: null,
144 },
145 ];
146
147 expect(dedupeRemoteBranchesWithLocalMatches(input).map((branch) => branch.name)).toEqual([
148 "feature/local",
149 "origin/feature/remote-only",
150 ]);
151 });
152
153 it("keeps non-origin remote refs visible even when a matching local branch exists", () => {
154 const input: GitBranch[] = [
155 {
156 name: "feature/demo",
157 current: false,
158 isDefault: false,
159 worktreePath: null,
160 },
161 {
162 name: "my-org/upstream/feature/demo",
163 isRemote: true,
164 remoteName: "my-org/upstream",
165 current: false,
166 isDefault: false,
167 worktreePath: null,
168 },
169 ];
170
171 expect(dedupeRemoteBranchesWithLocalMatches(input).map((branch) => branch.name)).toEqual([
172 "feature/demo",
173 "my-org/upstream/feature/demo",
174 ]);
175 });
176
177 it("keeps non-origin remote refs visible when git tracks with first-slash local naming", () => {
178 const input: GitBranch[] = [
179 {
180 name: "upstream/feature",
181 current: false,
182 isDefault: false,
183 worktreePath: null,
184 },
185 {
186 name: "my-org/upstream/feature",
187 isRemote: true,
188 remoteName: "my-org/upstream",
189 current: false,
190 isDefault: false,
191 worktreePath: null,
192 },
193 ];
194
195 expect(dedupeRemoteBranchesWithLocalMatches(input).map((branch) => branch.name)).toEqual([
196 "upstream/feature",
197 "my-org/upstream/feature",
198 ]);
199 });
200});
201
202describe("resolveBranchSelectionTarget", () => {
203 it("reuses an existing secondary worktree for the selected branch", () => {
204 expect(
205 resolveBranchSelectionTarget({
206 activeProjectCwd: "/repo",
207 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
208 branch: {
209 isDefault: false,
210 worktreePath: "/repo/.t3/worktrees/feature-b",
211 },
212 }),
213 ).toEqual({
214 checkoutCwd: "/repo/.t3/worktrees/feature-b",
215 nextWorktreePath: "/repo/.t3/worktrees/feature-b",
216 reuseExistingWorktree: true,
217 });
218 });
219
220 it("switches back to the main repo when the branch already lives there", () => {
221 expect(
222 resolveBranchSelectionTarget({
223 activeProjectCwd: "/repo",
224 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
225 branch: {
226 isDefault: true,
227 worktreePath: "/repo",
228 },
229 }),
230 ).toEqual({
231 checkoutCwd: "/repo",
232 nextWorktreePath: null,
233 reuseExistingWorktree: true,
234 });
235 });
236
237 it("checks out the default branch in the main repo when leaving a secondary worktree", () => {
238 expect(
239 resolveBranchSelectionTarget({
240 activeProjectCwd: "/repo",
241 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
242 branch: {
243 isDefault: true,
244 worktreePath: null,
245 },
246 }),
247 ).toEqual({
248 checkoutCwd: "/repo",
249 nextWorktreePath: null,
250 reuseExistingWorktree: false,
251 });
252 });
253
254 it("keeps checkout in the current worktree for non-default branches", () => {
255 expect(
256 resolveBranchSelectionTarget({
257 activeProjectCwd: "/repo",
258 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
259 branch: {
260 isDefault: false,
261 worktreePath: null,
262 },
263 }),
264 ).toEqual({
265 checkoutCwd: "/repo/.t3/worktrees/feature-a",
266 nextWorktreePath: "/repo/.t3/worktrees/feature-a",
267 reuseExistingWorktree: false,
268 });
269 });
270});
271
272describe("shouldIncludeBranchPickerItem", () => {
273 it("keeps the synthetic checkout PR item visible for gh pr checkout input", () => {
274 expect(
275 shouldIncludeBranchPickerItem({
276 itemValue: "__checkout_pull_request__:1359",
277 normalizedQuery: "gh pr checkout 1359",
278 createBranchItemValue: "__create_new_branch__:gh pr checkout 1359",
279 checkoutPullRequestItemValue: "__checkout_pull_request__:1359",
280 }),
281 ).toBe(true);
282 });
283
284 it("keeps the synthetic create-branch item visible for arbitrary branch input", () => {
285 expect(
286 shouldIncludeBranchPickerItem({
287 itemValue: "__create_new_branch__:feature/demo",
288 normalizedQuery: "feature/demo",
289 createBranchItemValue: "__create_new_branch__:feature/demo",
290 checkoutPullRequestItemValue: null,
291 }),
292 ).toBe(true);
293 });
294
295 it("still filters ordinary branch items by query text", () => {
296 expect(
297 shouldIncludeBranchPickerItem({
298 itemValue: "main",
299 normalizedQuery: "gh pr checkout 1359",
300 createBranchItemValue: "__create_new_branch__:gh pr checkout 1359",
301 checkoutPullRequestItemValue: "__checkout_pull_request__:1359",
302 }),
303 ).toBe(false);
304 });
305});