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

BranchToolbar.logic.test.ts

397 lines11.5 KB

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