Open History to see the last commit
1import { EnvironmentId, type VcsRef } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3import {
4 dedupeRemoteBranchesWithLocalMatches,
5 deriveLocalBranchNameFromRemoteRef,
6 resolveEnvironmentOptionLabel,
7 resolveBranchSelectionTarget,
8 resolveCurrentWorkspaceLabel,
9 resolveDraftEnvModeAfterBranchChange,
10 resolveEffectiveEnvMode,
11 resolveEnvModeLabel,
12 resolveBranchTriggerLabel,
13 resolveBranchToolbarPrBranch,
14 resolveBranchToolbarValue,
15 resolveLockedWorkspaceLabel,
16 resolveLocalCheckoutBranchMismatch,
17 resolvePreviousWorktreeLabel,
18 resolvePreviousWorktreeSeed,
19 shouldIncludeBranchPickerItem,
20 shouldShowEnvironmentIndicator,
21} from "./BranchToolbar.logic";
22
23const localEnvironmentId = EnvironmentId.make("environment-local");
24const remoteEnvironmentId = EnvironmentId.make("environment-remote");
25
26describe("resolvePreviousWorktreeSeed", () => {
27 it("picks the most recently updated worktree thread", () => {
28 expect(
29 resolvePreviousWorktreeSeed({
30 threads: [
31 {
32 branch: "t3/older",
33 worktreePath: "/repo/.t3/worktrees/older",
34 updatedAt: "2026-07-20T00:00:00.000Z",
35 },
36 {
37 branch: "t3/newer",
38 worktreePath: "/repo/.t3/worktrees/newer",
39 updatedAt: "2026-07-22T00:00:00.000Z",
40 },
41 { branch: "main", worktreePath: null, updatedAt: "2026-07-23T00:00:00.000Z" },
42 ],
43 currentWorktreePath: null,
44 }),
45 ).toEqual({ branch: "t3/newer", worktreePath: "/repo/.t3/worktrees/newer" });
46 });
47
48 it("skips the worktree the composer already points at", () => {
49 expect(
50 resolvePreviousWorktreeSeed({
51 threads: [
52 {
53 branch: "t3/current",
54 worktreePath: "/repo/.t3/worktrees/current",
55 updatedAt: "2026-07-22T00:00:00.000Z",
56 },
57 ],
58 currentWorktreePath: "/repo/.t3/worktrees/current",
59 }),
60 ).toBeNull();
61 });
62
63 it("returns null when no thread has a worktree", () => {
64 expect(
65 resolvePreviousWorktreeSeed({
66 threads: [{ branch: "main", worktreePath: null, updatedAt: "2026-07-22T00:00:00.000Z" }],
67 currentWorktreePath: null,
68 }),
69 ).toBeNull();
70 });
71
72 it("ignores archived threads and threads with unparseable timestamps", () => {
73 expect(
74 resolvePreviousWorktreeSeed({
75 threads: [
76 {
77 branch: "t3/archived",
78 worktreePath: "/repo/.t3/worktrees/archived",
79 updatedAt: "2026-07-23T00:00:00.000Z",
80 archivedAt: "2026-07-23T01:00:00.000Z",
81 },
82 {
83 branch: "t3/garbage-timestamp",
84 worktreePath: "/repo/.t3/worktrees/garbage",
85 updatedAt: "not-a-date",
86 },
87 {
88 branch: "t3/live",
89 worktreePath: "/repo/.t3/worktrees/live",
90 updatedAt: "2026-07-21T00:00:00.000Z",
91 archivedAt: null,
92 },
93 ],
94 currentWorktreePath: null,
95 }),
96 ).toEqual({ branch: "t3/live", worktreePath: "/repo/.t3/worktrees/live" });
97 });
98});
99
100describe("resolvePreviousWorktreeLabel", () => {
101 it("includes the branch when known", () => {
102 expect(resolvePreviousWorktreeLabel({ branch: "t3/fix-thing", worktreePath: "/wt" })).toBe(
103 "Previous worktree (t3/fix-thing)",
104 );
105 expect(resolvePreviousWorktreeLabel({ branch: null, worktreePath: "/wt" })).toBe(
106 "Previous worktree",
107 );
108 });
109});
110
111describe("resolveDraftEnvModeAfterBranchChange", () => {
112 it("switches to local mode when returning from an existing worktree to the main worktree", () => {
113 expect(
114 resolveDraftEnvModeAfterBranchChange({
115 nextWorktreePath: null,
116 currentWorktreePath: "/repo/.t3/worktrees/feature-a",
117 effectiveEnvMode: "worktree",
118 }),
119 ).toBe("local");
120 });
121
122 it("keeps new-worktree mode when selecting a base ref before worktree creation", () => {
123 expect(
124 resolveDraftEnvModeAfterBranchChange({
125 nextWorktreePath: null,
126 currentWorktreePath: null,
127 effectiveEnvMode: "worktree",
128 }),
129 ).toBe("worktree");
130 });
131
132 it("uses worktree mode when selecting a ref already attached to a worktree", () => {
133 expect(
134 resolveDraftEnvModeAfterBranchChange({
135 nextWorktreePath: "/repo/.t3/worktrees/feature-a",
136 currentWorktreePath: null,
137 effectiveEnvMode: "local",
138 }),
139 ).toBe("worktree");
140 });
141});
142
143describe("resolveBranchToolbarValue", () => {
144 it("defaults new-worktree mode to current git ref when no explicit base ref is set", () => {
145 expect(
146 resolveBranchToolbarValue({
147 envMode: "worktree",
148 activeWorktreePath: null,
149 activeThreadBranch: null,
150 currentGitBranch: "main",
151 }),
152 ).toBe("main");
153 });
154
155 it("keeps an explicitly selected worktree base ref", () => {
156 expect(
157 resolveBranchToolbarValue({
158 envMode: "worktree",
159 activeWorktreePath: null,
160 activeThreadBranch: "feature/base",
161 currentGitBranch: "main",
162 }),
163 ).toBe("feature/base");
164 });
165
166 it("shows the actual checked-out ref when not selecting a new worktree base", () => {
167 expect(
168 resolveBranchToolbarValue({
169 envMode: "local",
170 activeWorktreePath: null,
171 activeThreadBranch: "feature/base",
172 currentGitBranch: "main",
173 }),
174 ).toBe("main");
175 });
176});
177
178describe("resolveBranchTriggerLabel", () => {
179 it("shows the origin ref when a new worktree will start from origin", () => {
180 expect(
181 resolveBranchTriggerLabel({
182 activeWorktreePath: null,
183 effectiveEnvMode: "worktree",
184 resolvedActiveBranch: "main",
185 resolvedActiveBranchIsRemote: false,
186 startFromOrigin: true,
187 }),
188 ).toBe("From origin/main");
189 });
190
191 it("shows the origin ref for local branch names that contain slashes", () => {
192 expect(
193 resolveBranchTriggerLabel({
194 activeWorktreePath: null,
195 effectiveEnvMode: "worktree",
196 resolvedActiveBranch: "feature/demo",
197 resolvedActiveBranchIsRemote: false,
198 startFromOrigin: true,
199 }),
200 ).toBe("From origin/feature/demo");
201 });
202
203 it("shows the local ref when start from origin is disabled", () => {
204 expect(
205 resolveBranchTriggerLabel({
206 activeWorktreePath: null,
207 effectiveEnvMode: "worktree",
208 resolvedActiveBranch: "main",
209 resolvedActiveBranchIsRemote: false,
210 startFromOrigin: false,
211 }),
212 ).toBe("From main");
213 });
214
215 it("does not duplicate the origin prefix for an explicit remote ref", () => {
216 expect(
217 resolveBranchTriggerLabel({
218 activeWorktreePath: null,
219 effectiveEnvMode: "worktree",
220 resolvedActiveBranch: "origin/feature/demo",
221 resolvedActiveBranchIsRemote: true,
222 startFromOrigin: true,
223 }),
224 ).toBe("From origin/feature/demo");
225 });
226
227 it("preserves an explicit ref from a non-origin remote", () => {
228 expect(
229 resolveBranchTriggerLabel({
230 activeWorktreePath: null,
231 effectiveEnvMode: "worktree",
232 resolvedActiveBranch: "upstream/feature/demo",
233 resolvedActiveBranchIsRemote: true,
234 startFromOrigin: true,
235 }),
236 ).toBe("From upstream/feature/demo");
237 });
238
239 it("keeps current-checkout labels and empty state unchanged", () => {
240 expect(
241 resolveBranchTriggerLabel({
242 activeWorktreePath: null,
243 effectiveEnvMode: "local",
244 resolvedActiveBranch: "main",
245 resolvedActiveBranchIsRemote: false,
246 startFromOrigin: true,
247 }),
248 ).toBe("main");
249 expect(
250 resolveBranchTriggerLabel({
251 activeWorktreePath: null,
252 effectiveEnvMode: "worktree",
253 resolvedActiveBranch: null,
254 resolvedActiveBranchIsRemote: null,
255 startFromOrigin: true,
256 }),
257 ).toBe("Select ref");
258 });
259
260 it("does not fabricate an origin ref while branch metadata is loading", () => {
261 expect(
262 resolveBranchTriggerLabel({
263 activeWorktreePath: null,
264 effectiveEnvMode: "worktree",
265 resolvedActiveBranch: "upstream/feature/demo",
266 resolvedActiveBranchIsRemote: null,
267 startFromOrigin: true,
268 }),
269 ).toBe("From upstream/feature/demo");
270 });
271});
272
273describe("resolveBranchToolbarPrBranch", () => {
274 it("uses the explicit thread branch when it matches the displayed branch", () => {
275 expect(
276 resolveBranchToolbarPrBranch({
277 activeThreadBranch: "feature/current",
278 resolvedActiveBranch: "feature/current",
279 }),
280 ).toBe("feature/current");
281 });
282
283 it("hides PR state while an optimistic branch switch is in flight", () => {
284 expect(
285 resolveBranchToolbarPrBranch({
286 activeThreadBranch: "feature/current",
287 resolvedActiveBranch: "feature/next",
288 }),
289 ).toBeNull();
290 });
291
292 it("does not infer PR state without an explicit thread branch", () => {
293 expect(
294 resolveBranchToolbarPrBranch({
295 activeThreadBranch: null,
296 resolvedActiveBranch: "feature/current",
297 }),
298 ).toBeNull();
299 });
300});
301
302describe("resolveLocalCheckoutBranchMismatch", () => {
303 it("detects when a local thread is associated with a different branch than the checkout", () => {
304 expect(
305 resolveLocalCheckoutBranchMismatch({
306 effectiveEnvMode: "local",
307 activeWorktreePath: null,
308 activeThreadBranch: "feature/thread",
309 currentGitBranch: "feature/current",
310 }),
311 ).toEqual({
312 threadBranch: "feature/thread",
313 currentBranch: "feature/current",
314 });
315 });
316
317 it("ignores matching local checkout state", () => {
318 expect(
319 resolveLocalCheckoutBranchMismatch({
320 effectiveEnvMode: "local",
321 activeWorktreePath: null,
322 activeThreadBranch: "feature/thread",
323 currentGitBranch: "feature/thread",
324 }),
325 ).toBeNull();
326 });
327
328 it("ignores dedicated worktrees because their checkout is already thread-scoped", () => {
329 expect(
330 resolveLocalCheckoutBranchMismatch({
331 effectiveEnvMode: "worktree",
332 activeWorktreePath: "/repo/.t3/worktrees/feature-thread",
333 activeThreadBranch: "feature/thread",
334 currentGitBranch: "feature/current",
335 }),
336 ).toBeNull();
337 });
338
339 it("ignores new-worktree base selection before a worktree exists", () => {
340 expect(
341 resolveLocalCheckoutBranchMismatch({
342 effectiveEnvMode: "worktree",
343 activeWorktreePath: null,
344 activeThreadBranch: "feature/base",
345 currentGitBranch: "main",
346 }),
347 ).toBeNull();
348 });
349});
350
351describe("resolveEnvironmentOptionLabel", () => {
352 it("prefers the primary environment's machine label", () => {
353 expect(
354 resolveEnvironmentOptionLabel({
355 isPrimary: true,
356 environmentId: localEnvironmentId,
357 runtimeLabel: "Julius's Mac mini",
358 savedLabel: "Local environment",
359 }),
360 ).toBe("Julius's Mac mini");
361 });
362
363 it("falls back to 'This device' for generic primary labels", () => {
364 expect(
365 resolveEnvironmentOptionLabel({
366 isPrimary: true,
367 environmentId: localEnvironmentId,
368 runtimeLabel: "Local environment",
369 savedLabel: "Local",
370 }),
371 ).toBe("This device");
372 });
373
374 it("keeps configured labels for non-primary environments", () => {
375 expect(
376 resolveEnvironmentOptionLabel({
377 isPrimary: false,
378 environmentId: remoteEnvironmentId,
379 runtimeLabel: null,
380 savedLabel: "Build box",
381 }),
382 ).toBe("Build box");
383 });
384});
385
386describe("shouldShowEnvironmentIndicator", () => {
387 it("shows the indicator whenever multiple environments are pickable", () => {
388 expect(
389 shouldShowEnvironmentIndicator({
390 activeEnvironment: { isPrimary: true },
391 canPickEnvironment: true,
392 }),
393 ).toBe(true);
394 });
395
396 it("shows a sole remote environment so the user knows where the project runs", () => {
397 expect(
398 shouldShowEnvironmentIndicator({
399 activeEnvironment: { isPrimary: false },
400 canPickEnvironment: false,
401 }),
402 ).toBe(true);
403 });
404
405 it("hides a sole primary (this-device) environment", () => {
406 expect(
407 shouldShowEnvironmentIndicator({
408 activeEnvironment: { isPrimary: true },
409 canPickEnvironment: false,
410 }),
411 ).toBe(false);
412 });
413
414 it("hides the indicator when the active environment is unknown", () => {
415 expect(
416 shouldShowEnvironmentIndicator({
417 activeEnvironment: null,
418 canPickEnvironment: false,
419 }),
420 ).toBe(false);
421 });
422});
423
424describe("resolveEffectiveEnvMode", () => {
425 it("treats draft threads already attached to a worktree as current-checkout mode", () => {
426 expect(
427 resolveEffectiveEnvMode({
428 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
429 hasServerThread: false,
430 draftThreadEnvMode: "worktree",
431 }),
432 ).toBe("local");
433 });
434
435 it("keeps explicit new-worktree mode for draft threads without a worktree path", () => {
436 expect(
437 resolveEffectiveEnvMode({
438 activeWorktreePath: null,
439 hasServerThread: false,
440 draftThreadEnvMode: "worktree",
441 }),
442 ).toBe("worktree");
443 });
444});
445
446describe("resolveEnvModeLabel", () => {
447 it("uses explicit workspace labels", () => {
448 expect(resolveEnvModeLabel("local")).toBe("Current checkout");
449 expect(resolveEnvModeLabel("worktree")).toBe("New worktree");
450 });
451});
452
453describe("resolveCurrentWorkspaceLabel", () => {
454 it("describes the main repo checkout when no worktree path is active", () => {
455 expect(resolveCurrentWorkspaceLabel(null)).toBe("Current checkout");
456 });
457
458 it("describes the active checkout as a worktree when one is attached", () => {
459 expect(resolveCurrentWorkspaceLabel("/repo/.t3/worktrees/feature-a")).toBe("Current worktree");
460 });
461});
462
463describe("resolveLockedWorkspaceLabel", () => {
464 it("uses a shorter label for the main repo checkout", () => {
465 expect(resolveLockedWorkspaceLabel(null)).toBe("Local checkout");
466 });
467
468 it("uses a shorter label for an attached worktree", () => {
469 expect(resolveLockedWorkspaceLabel("/repo/.t3/worktrees/feature-a")).toBe("Worktree");
470 });
471});
472
473describe("deriveLocalBranchNameFromRemoteRef", () => {
474 it("strips the remote prefix from a remote ref", () => {
475 expect(deriveLocalBranchNameFromRemoteRef("origin/feature/demo")).toBe("feature/demo");
476 });
477
478 it("supports remote names that contain slashes", () => {
479 expect(deriveLocalBranchNameFromRemoteRef("my-org/upstream/feature/demo")).toBe(
480 "upstream/feature/demo",
481 );
482 });
483
484 it("returns the original name when ref is malformed", () => {
485 expect(deriveLocalBranchNameFromRemoteRef("origin/")).toBe("origin/");
486 expect(deriveLocalBranchNameFromRemoteRef("/feature/demo")).toBe("/feature/demo");
487 });
488});
489
490describe("dedupeRemoteBranchesWithLocalMatches", () => {
491 it("hides remote refs when the matching local ref exists", () => {
492 const input: VcsRef[] = [
493 {
494 name: "feature/demo",
495 current: false,
496 isDefault: false,
497 worktreePath: null,
498 },
499 {
500 name: "origin/feature/demo",
501 isRemote: true,
502 remoteName: "origin",
503 current: false,
504 isDefault: false,
505 worktreePath: null,
506 },
507 {
508 name: "origin/feature/remote-only",
509 isRemote: true,
510 remoteName: "origin",
511 current: false,
512 isDefault: false,
513 worktreePath: null,
514 },
515 ];
516
517 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
518 "feature/demo",
519 "origin/feature/remote-only",
520 ]);
521 });
522
523 it("keeps all entries when no local match exists for a remote ref", () => {
524 const input: VcsRef[] = [
525 {
526 name: "feature/local",
527 current: false,
528 isDefault: false,
529 worktreePath: null,
530 },
531 {
532 name: "origin/feature/remote-only",
533 isRemote: true,
534 remoteName: "origin",
535 current: false,
536 isDefault: false,
537 worktreePath: null,
538 },
539 ];
540
541 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
542 "feature/local",
543 "origin/feature/remote-only",
544 ]);
545 });
546
547 it("keeps non-origin remote refs visible even when a matching local ref exists", () => {
548 const input: VcsRef[] = [
549 {
550 name: "feature/demo",
551 current: false,
552 isDefault: false,
553 worktreePath: null,
554 },
555 {
556 name: "my-org/upstream/feature/demo",
557 isRemote: true,
558 remoteName: "my-org/upstream",
559 current: false,
560 isDefault: false,
561 worktreePath: null,
562 },
563 ];
564
565 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
566 "feature/demo",
567 "my-org/upstream/feature/demo",
568 ]);
569 });
570
571 it("keeps non-origin remote refs visible when git tracks with first-slash local naming", () => {
572 const input: VcsRef[] = [
573 {
574 name: "upstream/feature",
575 current: false,
576 isDefault: false,
577 worktreePath: null,
578 },
579 {
580 name: "my-org/upstream/feature",
581 isRemote: true,
582 remoteName: "my-org/upstream",
583 current: false,
584 isDefault: false,
585 worktreePath: null,
586 },
587 ];
588
589 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
590 "upstream/feature",
591 "my-org/upstream/feature",
592 ]);
593 });
594});
595
596describe("resolveBranchSelectionTarget", () => {
597 it("reuses an existing secondary worktree for the selected ref", () => {
598 expect(
599 resolveBranchSelectionTarget({
600 activeProjectCwd: "/repo",
601 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
602 refName: {
603 isDefault: false,
604 worktreePath: "/repo/.t3/worktrees/feature-b",
605 },
606 }),
607 ).toEqual({
608 checkoutCwd: "/repo/.t3/worktrees/feature-b",
609 nextWorktreePath: "/repo/.t3/worktrees/feature-b",
610 reuseExistingWorktree: true,
611 });
612 });
613
614 it("switches back to the main repo when the ref already lives there", () => {
615 expect(
616 resolveBranchSelectionTarget({
617 activeProjectCwd: "/repo",
618 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
619 refName: {
620 isDefault: true,
621 worktreePath: "/repo",
622 },
623 }),
624 ).toEqual({
625 checkoutCwd: "/repo",
626 nextWorktreePath: null,
627 reuseExistingWorktree: true,
628 });
629 });
630
631 it("checks out the default ref in the main repo when leaving a secondary worktree", () => {
632 expect(
633 resolveBranchSelectionTarget({
634 activeProjectCwd: "/repo",
635 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
636 refName: {
637 isDefault: true,
638 worktreePath: null,
639 },
640 }),
641 ).toEqual({
642 checkoutCwd: "/repo",
643 nextWorktreePath: null,
644 reuseExistingWorktree: false,
645 });
646 });
647
648 it("keeps checkout in the current worktree for non-default refs", () => {
649 expect(
650 resolveBranchSelectionTarget({
651 activeProjectCwd: "/repo",
652 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
653 refName: {
654 isDefault: false,
655 worktreePath: null,
656 },
657 }),
658 ).toEqual({
659 checkoutCwd: "/repo/.t3/worktrees/feature-a",
660 nextWorktreePath: "/repo/.t3/worktrees/feature-a",
661 reuseExistingWorktree: false,
662 });
663 });
664});
665
666describe("shouldIncludeBranchPickerItem", () => {
667 it("keeps the synthetic checkout PR item visible for gh pr checkout input", () => {
668 expect(
669 shouldIncludeBranchPickerItem({
670 itemValue: "__checkout_pull_request__:1359",
671 normalizedQuery: "gh pr checkout 1359",
672 createBranchItemValue: "__create_new_branch__:gh pr checkout 1359",
673 checkoutPullRequestItemValue: "__checkout_pull_request__:1359",
674 }),
675 ).toBe(true);
676 });
677
678 it("keeps the synthetic create-ref item visible for arbitrary ref input", () => {
679 expect(
680 shouldIncludeBranchPickerItem({
681 itemValue: "__create_new_branch__:feature/demo",
682 normalizedQuery: "feature/demo",
683 createBranchItemValue: "__create_new_branch__:feature/demo",
684 checkoutPullRequestItemValue: null,
685 }),
686 ).toBe(true);
687 });
688
689 it("still filters ordinary ref items by query text", () => {
690 expect(
691 shouldIncludeBranchPickerItem({
692 itemValue: "main",
693 normalizedQuery: "gh pr checkout 1359",
694 createBranchItemValue: "__create_new_branch__:gh pr checkout 1359",
695 checkoutPullRequestItemValue: "__checkout_pull_request__:1359",
696 }),
697 ).toBe(false);
698 });
699});