Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

BranchToolbar.logic.test.ts

386 lines11.2 KB

1import { EnvironmentId, type GitBranch } from "@t3tools/contracts";
2import { describe, expect, it } from "vitest";
3import {
4 dedupeRemoteBranchesWithLocalMatches,
5 deriveLocalBranchNameFromRemoteRef,
6 resolveEnvironmentOptionLabel,
7 resolveBranchSelectionTarget,
8 resolveCurrentWorkspaceLabel,
9 resolveDraftEnvModeAfterBranchChange,
10 resolveEffectiveEnvMode,
11 resolveEnvModeLabel,
12 resolveBranchToolbarValue,
13 shouldIncludeBranchPickerItem,
14} from "./BranchToolbar.logic";
15​
16const localEnvironmentId = EnvironmentId.make("environment-local");
17const remoteEnvironmentId = EnvironmentId.make("environment-remote");
18​
19describe("resolveDraftEnvModeAfterBranchChange", () => {
20 it("switches to local mode when returning from an existing worktree to the main worktree", () => {
21 expect(
22 resolveDraftEnvModeAfterBranchChange({
23 nextWorktreePath: null,
24 currentWorktreePath: "/repo/.t3/worktrees/feature-a",
25 effectiveEnvMode: "worktree",
26 }),
27 ).toBe("local");
28 });
29​
30 it("keeps new-worktree mode when selecting a base branch before worktree creation", () => {
31 expect(
32 resolveDraftEnvModeAfterBranchChange({
33 nextWorktreePath: null,
34 currentWorktreePath: null,
35 effectiveEnvMode: "worktree",
36 }),
37 ).toBe("worktree");
38 });
39​
40 it("uses worktree mode when selecting a branch already attached to a worktree", () => {
41 expect(
42 resolveDraftEnvModeAfterBranchChange({
43 nextWorktreePath: "/repo/.t3/worktrees/feature-a",
44 currentWorktreePath: null,
45 effectiveEnvMode: "local",
46 }),
47 ).toBe("worktree");
48 });
49});
50​
51describe("resolveBranchToolbarValue", () => {
52 it("defaults new-worktree mode to current git branch when no explicit base branch is set", () => {
53 expect(
54 resolveBranchToolbarValue({
55 envMode: "worktree",
56 activeWorktreePath: null,
57 activeThreadBranch: null,
58 currentGitBranch: "main",
59 }),
60 ).toBe("main");
61 });
62​
63 it("keeps an explicitly selected worktree base branch", () => {
64 expect(
65 resolveBranchToolbarValue({
66 envMode: "worktree",
67 activeWorktreePath: null,
68 activeThreadBranch: "feature/base",
69 currentGitBranch: "main",
70 }),
71 ).toBe("feature/base");
72 });
73​
74 it("shows the actual checked-out branch when not selecting a new worktree base", () => {
75 expect(
76 resolveBranchToolbarValue({
77 envMode: "local",
78 activeWorktreePath: null,
79 activeThreadBranch: "feature/base",
80 currentGitBranch: "main",
81 }),
82 ).toBe("main");
83 });
84});
85​
86describe("resolveEnvironmentOptionLabel", () => {
87 it("prefers the primary environment's machine label", () => {
88 expect(
89 resolveEnvironmentOptionLabel({
90 isPrimary: true,
91 environmentId: localEnvironmentId,
92 runtimeLabel: "Julius's Mac mini",
93 savedLabel: "Local environment",
94 }),
95 ).toBe("Julius's Mac mini");
96 });
97​
98 it("falls back to 'This device' for generic primary labels", () => {
99 expect(
100 resolveEnvironmentOptionLabel({
101 isPrimary: true,
102 environmentId: localEnvironmentId,
103 runtimeLabel: "Local environment",
104 savedLabel: "Local",
105 }),
106 ).toBe("This device");
107 });
108​
109 it("keeps configured labels for non-primary environments", () => {
110 expect(
111 resolveEnvironmentOptionLabel({
112 isPrimary: false,
113 environmentId: remoteEnvironmentId,
114 runtimeLabel: null,
115 savedLabel: "Build box",
116 }),
117 ).toBe("Build box");
118 });
119});
120​
121describe("resolveEffectiveEnvMode", () => {
122 it("treats draft threads already attached to a worktree as current-checkout mode", () => {
123 expect(
124 resolveEffectiveEnvMode({
125 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
126 hasServerThread: false,
127 draftThreadEnvMode: "worktree",
128 }),
129 ).toBe("local");
130 });
131​
132 it("keeps explicit new-worktree mode for draft threads without a worktree path", () => {
133 expect(
134 resolveEffectiveEnvMode({
135 activeWorktreePath: null,
136 hasServerThread: false,
137 draftThreadEnvMode: "worktree",
138 }),
139 ).toBe("worktree");
140 });
141});
142​
143describe("resolveEnvModeLabel", () => {
144 it("uses explicit workspace labels", () => {
145 expect(resolveEnvModeLabel("local")).toBe("Current checkout");
146 expect(resolveEnvModeLabel("worktree")).toBe("New worktree");
147 });
148});
149​
150describe("resolveCurrentWorkspaceLabel", () => {
151 it("describes the main repo checkout when no worktree path is active", () => {
152 expect(resolveCurrentWorkspaceLabel(null)).toBe("Current checkout");
153 });
154​
155 it("describes the active checkout as a worktree when one is attached", () => {
156 expect(resolveCurrentWorkspaceLabel("/repo/.t3/worktrees/feature-a")).toBe("Current worktree");
157 });
158});
159​
160describe("deriveLocalBranchNameFromRemoteRef", () => {
161 it("strips the remote prefix from a remote ref", () => {
162 expect(deriveLocalBranchNameFromRemoteRef("origin/feature/demo")).toBe("feature/demo");
163 });
164​
165 it("supports remote names that contain slashes", () => {
166 expect(deriveLocalBranchNameFromRemoteRef("my-org/upstream/feature/demo")).toBe(
167 "upstream/feature/demo",
168 );
169 });
170​
171 it("returns the original name when ref is malformed", () => {
172 expect(deriveLocalBranchNameFromRemoteRef("origin/")).toBe("origin/");
173 expect(deriveLocalBranchNameFromRemoteRef("/feature/demo")).toBe("/feature/demo");
174 });
175});
176​
177describe("dedupeRemoteBranchesWithLocalMatches", () => {
178 it("hides remote refs when the matching local branch exists", () => {
179 const input: GitBranch[] = [
180 {
181 name: "feature/demo",
182 current: false,
183 isDefault: false,
184 worktreePath: null,
185 },
186 {
187 name: "origin/feature/demo",
188 isRemote: true,
189 remoteName: "origin",
190 current: false,
191 isDefault: false,
192 worktreePath: null,
193 },
194 {
195 name: "origin/feature/remote-only",
196 isRemote: true,
197 remoteName: "origin",
198 current: false,
199 isDefault: false,
200 worktreePath: null,
201 },
202 ];
203​
204 expect(dedupeRemoteBranchesWithLocalMatches(input).map((branch) => branch.name)).toEqual([
205 "feature/demo",
206 "origin/feature/remote-only",
207 ]);
208 });
209​
210 it("keeps all entries when no local match exists for a remote ref", () => {
211 const input: GitBranch[] = [
212 {
213 name: "feature/local",
214 current: false,
215 isDefault: false,
216 worktreePath: null,
217 },
218 {
219 name: "origin/feature/remote-only",
220 isRemote: true,
221 remoteName: "origin",
222 current: false,
223 isDefault: false,
224 worktreePath: null,
225 },
226 ];
227​
228 expect(dedupeRemoteBranchesWithLocalMatches(input).map((branch) => branch.name)).toEqual([
229 "feature/local",
230 "origin/feature/remote-only",
231 ]);
232 });
233​
234 it("keeps non-origin remote refs visible even when a matching local branch exists", () => {
235 const input: GitBranch[] = [
236 {
237 name: "feature/demo",
238 current: false,
239 isDefault: false,
240 worktreePath: null,
241 },
242 {
243 name: "my-org/upstream/feature/demo",
244 isRemote: true,
245 remoteName: "my-org/upstream",
246 current: false,
247 isDefault: false,
248 worktreePath: null,
249 },
250 ];
251​
252 expect(dedupeRemoteBranchesWithLocalMatches(input).map((branch) => branch.name)).toEqual([
253 "feature/demo",
254 "my-org/upstream/feature/demo",
255 ]);
256 });
257​
258 it("keeps non-origin remote refs visible when git tracks with first-slash local naming", () => {
259 const input: GitBranch[] = [
260 {
261 name: "upstream/feature",
262 current: false,
263 isDefault: false,
264 worktreePath: null,
265 },
266 {
267 name: "my-org/upstream/feature",
268 isRemote: true,
269 remoteName: "my-org/upstream",
270 current: false,
271 isDefault: false,
272 worktreePath: null,
273 },
274 ];
275​
276 expect(dedupeRemoteBranchesWithLocalMatches(input).map((branch) => branch.name)).toEqual([
277 "upstream/feature",
278 "my-org/upstream/feature",
279 ]);
280 });
281});
282​
283describe("resolveBranchSelectionTarget", () => {
284 it("reuses an existing secondary worktree for the selected branch", () => {
285 expect(
286 resolveBranchSelectionTarget({
287 activeProjectCwd: "/repo",
288 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
289 branch: {
290 isDefault: false,
291 worktreePath: "/repo/.t3/worktrees/feature-b",
292 },
293 }),
294 ).toEqual({
295 checkoutCwd: "/repo/.t3/worktrees/feature-b",
296 nextWorktreePath: "/repo/.t3/worktrees/feature-b",
297 reuseExistingWorktree: true,
298 });
299 });
300​
301 it("switches back to the main repo when the branch already lives there", () => {
302 expect(
303 resolveBranchSelectionTarget({
304 activeProjectCwd: "/repo",
305 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
306 branch: {
307 isDefault: true,
308 worktreePath: "/repo",
309 },
310 }),
311 ).toEqual({
312 checkoutCwd: "/repo",
313 nextWorktreePath: null,
314 reuseExistingWorktree: true,
315 });
316 });
317​
318 it("checks out the default branch in the main repo when leaving a secondary worktree", () => {
319 expect(
320 resolveBranchSelectionTarget({
321 activeProjectCwd: "/repo",
322 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
323 branch: {
324 isDefault: true,
325 worktreePath: null,
326 },
327 }),
328 ).toEqual({
329 checkoutCwd: "/repo",
330 nextWorktreePath: null,
331 reuseExistingWorktree: false,
332 });
333 });
334​
335 it("keeps checkout in the current worktree for non-default branches", () => {
336 expect(
337 resolveBranchSelectionTarget({
338 activeProjectCwd: "/repo",
339 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
340 branch: {
341 isDefault: false,
342 worktreePath: null,
343 },
344 }),
345 ).toEqual({
346 checkoutCwd: "/repo/.t3/worktrees/feature-a",
347 nextWorktreePath: "/repo/.t3/worktrees/feature-a",
348 reuseExistingWorktree: false,
349 });
350 });
351});
352​
353describe("shouldIncludeBranchPickerItem", () => {
354 it("keeps the synthetic checkout PR item visible for gh pr checkout input", () => {
355 expect(
356 shouldIncludeBranchPickerItem({
357 itemValue: "__checkout_pull_request__:1359",
358 normalizedQuery: "gh pr checkout 1359",
359 createBranchItemValue: "__create_new_branch__:gh pr checkout 1359",
360 checkoutPullRequestItemValue: "__checkout_pull_request__:1359",
361 }),
362 ).toBe(true);
363 });
364​
365 it("keeps the synthetic create-branch item visible for arbitrary branch input", () => {
366 expect(
367 shouldIncludeBranchPickerItem({
368 itemValue: "__create_new_branch__:feature/demo",
369 normalizedQuery: "feature/demo",
370 createBranchItemValue: "__create_new_branch__:feature/demo",
371 checkoutPullRequestItemValue: null,
372 }),
373 ).toBe(true);
374 });
375​
376 it("still filters ordinary branch items by query text", () => {
377 expect(
378 shouldIncludeBranchPickerItem({
379 itemValue: "main",
380 normalizedQuery: "gh pr checkout 1359",
381 createBranchItemValue: "__create_new_branch__:gh pr checkout 1359",
382 checkoutPullRequestItemValue: "__checkout_pull_request__:1359",
383 }),
384 ).toBe(false);
385 });
386});