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

BranchToolbar.logic.test.ts

919 lines26.9 KB

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 resolveCheckoutBranchMismatch,
17 resolvePreviousWorktreeLabel,
18 resolvePreviousWorktreeSeed,
19 sanitizeNewRefName,
20 shouldIncludeBranchPickerItem,
21 shouldShowComposerContextStrip,
22 shouldShowEnvironmentIndicator,
23} from "./BranchToolbar.logic";
24​
25const localEnvironmentId = EnvironmentId.make("environment-local");
26const remoteEnvironmentId = EnvironmentId.make("environment-remote");
27​
28describe("resolvePreviousWorktreeSeed", () => {
29 it("picks the most recently updated worktree thread", () => {
30 expect(
31 resolvePreviousWorktreeSeed({
32 threads: [
33 {
34 branch: "t3/older",
35 worktreePath: "/repo/.t3/worktrees/older",
36 updatedAt: "2026-07-20T00:00:00.000Z",
37 },
38 {
39 branch: "t3/newer",
40 worktreePath: "/repo/.t3/worktrees/newer",
41 updatedAt: "2026-07-22T00:00:00.000Z",
42 },
43 { branch: "main", worktreePath: null, updatedAt: "2026-07-23T00:00:00.000Z" },
44 ],
45 currentWorktreePath: null,
46 }),
47 ).toEqual({ branch: "t3/newer", worktreePath: "/repo/.t3/worktrees/newer" });
48 });
49​
50 it("skips the worktree the composer already points at", () => {
51 expect(
52 resolvePreviousWorktreeSeed({
53 threads: [
54 {
55 branch: "t3/current",
56 worktreePath: "/repo/.t3/worktrees/current",
57 updatedAt: "2026-07-22T00:00:00.000Z",
58 },
59 ],
60 currentWorktreePath: "/repo/.t3/worktrees/current",
61 }),
62 ).toBeNull();
63 });
64​
65 it("returns null when no thread has a worktree", () => {
66 expect(
67 resolvePreviousWorktreeSeed({
68 threads: [{ branch: "main", worktreePath: null, updatedAt: "2026-07-22T00:00:00.000Z" }],
69 currentWorktreePath: null,
70 }),
71 ).toBeNull();
72 });
73​
74 it("ignores archived threads and threads with unparseable timestamps", () => {
75 expect(
76 resolvePreviousWorktreeSeed({
77 threads: [
78 {
79 branch: "t3/archived",
80 worktreePath: "/repo/.t3/worktrees/archived",
81 updatedAt: "2026-07-23T00:00:00.000Z",
82 archivedAt: "2026-07-23T01:00:00.000Z",
83 },
84 {
85 branch: "t3/garbage-timestamp",
86 worktreePath: "/repo/.t3/worktrees/garbage",
87 updatedAt: "not-a-date",
88 },
89 {
90 branch: "t3/live",
91 worktreePath: "/repo/.t3/worktrees/live",
92 updatedAt: "2026-07-21T00:00:00.000Z",
93 archivedAt: null,
94 },
95 ],
96 currentWorktreePath: null,
97 }),
98 ).toEqual({ branch: "t3/live", worktreePath: "/repo/.t3/worktrees/live" });
99 });
100});
101​
102describe("resolvePreviousWorktreeLabel", () => {
103 it("includes the branch when known", () => {
104 expect(resolvePreviousWorktreeLabel({ branch: "t3/fix-thing", worktreePath: "/wt" })).toBe(
105 "Previous worktree (t3/fix-thing)",
106 );
107 expect(resolvePreviousWorktreeLabel({ branch: null, worktreePath: "/wt" })).toBe(
108 "Previous worktree",
109 );
110 });
111});
112​
113describe("resolveDraftEnvModeAfterBranchChange", () => {
114 it("switches to local mode when returning from an existing worktree to the main worktree", () => {
115 expect(
116 resolveDraftEnvModeAfterBranchChange({
117 nextWorktreePath: null,
118 currentWorktreePath: "/repo/.t3/worktrees/feature-a",
119 effectiveEnvMode: "worktree",
120 }),
121 ).toBe("local");
122 });
123​
124 it("keeps new-worktree mode when selecting a base ref before worktree creation", () => {
125 expect(
126 resolveDraftEnvModeAfterBranchChange({
127 nextWorktreePath: null,
128 currentWorktreePath: null,
129 effectiveEnvMode: "worktree",
130 }),
131 ).toBe("worktree");
132 });
133​
134 it("uses worktree mode when selecting a ref already attached to a worktree", () => {
135 expect(
136 resolveDraftEnvModeAfterBranchChange({
137 nextWorktreePath: "/repo/.t3/worktrees/feature-a",
138 currentWorktreePath: null,
139 effectiveEnvMode: "local",
140 }),
141 ).toBe("worktree");
142 });
143});
144​
145describe("resolveBranchToolbarValue", () => {
146 it("defaults new-worktree mode to current git ref when no explicit base ref is set", () => {
147 expect(
148 resolveBranchToolbarValue({
149 envMode: "worktree",
150 activeWorktreePath: null,
151 activeThreadBranch: null,
152 currentGitBranch: "main",
153 }),
154 ).toBe("main");
155 });
156​
157 it("keeps an explicitly selected worktree base ref", () => {
158 expect(
159 resolveBranchToolbarValue({
160 envMode: "worktree",
161 activeWorktreePath: null,
162 activeThreadBranch: "feature/base",
163 currentGitBranch: "main",
164 }),
165 ).toBe("feature/base");
166 });
167​
168 it("shows the actual checked-out ref when not selecting a new worktree base", () => {
169 expect(
170 resolveBranchToolbarValue({
171 envMode: "local",
172 activeWorktreePath: null,
173 activeThreadBranch: "feature/base",
174 currentGitBranch: "main",
175 }),
176 ).toBe("main");
177 });
178});
179​
180describe("resolveBranchTriggerLabel", () => {
181 it("marks the remote base when a new worktree will start from remote", () => {
182 expect(
183 resolveBranchTriggerLabel({
184 activeWorktreePath: null,
185 effectiveEnvMode: "worktree",
186 resolvedActiveBranch: "main",
187 resolvedActiveBranchIsRemote: false,
188 startFromOrigin: true,
189 }),
190 ).toBe("From main (remote)");
191 });
192​
193 it("marks the remote base for local branch names that contain slashes", () => {
194 expect(
195 resolveBranchTriggerLabel({
196 activeWorktreePath: null,
197 effectiveEnvMode: "worktree",
198 resolvedActiveBranch: "feature/demo",
199 resolvedActiveBranchIsRemote: false,
200 startFromOrigin: true,
201 }),
202 ).toBe("From feature/demo (remote)");
203 });
204​
205 it("shows the local ref when start from remote is disabled", () => {
206 expect(
207 resolveBranchTriggerLabel({
208 activeWorktreePath: null,
209 effectiveEnvMode: "worktree",
210 resolvedActiveBranch: "main",
211 resolvedActiveBranchIsRemote: false,
212 startFromOrigin: false,
213 }),
214 ).toBe("From main");
215 });
216​
217 it("does not add a remote marker for an explicit remote ref", () => {
218 expect(
219 resolveBranchTriggerLabel({
220 activeWorktreePath: null,
221 effectiveEnvMode: "worktree",
222 resolvedActiveBranch: "origin/feature/demo",
223 resolvedActiveBranchIsRemote: true,
224 startFromOrigin: true,
225 }),
226 ).toBe("From origin/feature/demo");
227 });
228​
229 it("preserves an explicit ref from a non-origin remote", () => {
230 expect(
231 resolveBranchTriggerLabel({
232 activeWorktreePath: null,
233 effectiveEnvMode: "worktree",
234 resolvedActiveBranch: "upstream/feature/demo",
235 resolvedActiveBranchIsRemote: true,
236 startFromOrigin: true,
237 }),
238 ).toBe("From upstream/feature/demo");
239 });
240​
241 it("keeps current-checkout labels and empty state unchanged", () => {
242 expect(
243 resolveBranchTriggerLabel({
244 activeWorktreePath: null,
245 effectiveEnvMode: "local",
246 resolvedActiveBranch: "main",
247 resolvedActiveBranchIsRemote: false,
248 startFromOrigin: true,
249 }),
250 ).toBe("main");
251 expect(
252 resolveBranchTriggerLabel({
253 activeWorktreePath: null,
254 effectiveEnvMode: "worktree",
255 resolvedActiveBranch: null,
256 resolvedActiveBranchIsRemote: null,
257 startFromOrigin: true,
258 }),
259 ).toBe("Select ref");
260 });
261​
262 it("does not fabricate a remote marker while branch metadata is loading", () => {
263 expect(
264 resolveBranchTriggerLabel({
265 activeWorktreePath: null,
266 effectiveEnvMode: "worktree",
267 resolvedActiveBranch: "upstream/feature/demo",
268 resolvedActiveBranchIsRemote: null,
269 startFromOrigin: true,
270 }),
271 ).toBe("From upstream/feature/demo");
272 });
273});
274​
275describe("resolveBranchToolbarPrBranch", () => {
276 it("uses the explicit thread branch when it matches the displayed branch", () => {
277 expect(
278 resolveBranchToolbarPrBranch({
279 activeThreadBranch: "feature/current",
280 resolvedActiveBranch: "feature/current",
281 }),
282 ).toBe("feature/current");
283 });
284​
285 it("hides PR state while an optimistic branch switch is in flight", () => {
286 expect(
287 resolveBranchToolbarPrBranch({
288 activeThreadBranch: "feature/current",
289 resolvedActiveBranch: "feature/next",
290 }),
291 ).toBeNull();
292 });
293​
294 it("does not infer PR state without an explicit thread branch", () => {
295 expect(
296 resolveBranchToolbarPrBranch({
297 activeThreadBranch: null,
298 resolvedActiveBranch: "feature/current",
299 }),
300 ).toBeNull();
301 });
302});
303​
304describe("resolveCheckoutBranchMismatch", () => {
305 it("detects when a local thread is associated with a different branch than the checkout", () => {
306 expect(
307 resolveCheckoutBranchMismatch({
308 effectiveEnvMode: "local",
309 activeWorktreePath: null,
310 activeThreadBranch: "feature/thread",
311 currentGitBranch: "feature/current",
312 }),
313 ).toEqual({
314 threadBranch: "feature/thread",
315 currentBranch: "feature/current",
316 canRestoreThreadBranch: true,
317 });
318 });
319​
320 it("ignores matching local checkout state", () => {
321 expect(
322 resolveCheckoutBranchMismatch({
323 effectiveEnvMode: "local",
324 activeWorktreePath: null,
325 activeThreadBranch: "feature/thread",
326 currentGitBranch: "feature/thread",
327 }),
328 ).toBeNull();
329 });
330​
331 it("warns about worktree branch drift without restore, but ignores a pending rename status", () => {
332 const worktree = {
333 effectiveEnvMode: "worktree" as const,
334 activeWorktreePath: "/repo/.t3/worktrees/feature-thread",
335 activeThreadBranch: "t3code/fix-sidebar",
336 };
337​
338 expect(
339 resolveCheckoutBranchMismatch({
340 ...worktree,
341 currentGitBranch: "t3code/1a2b3c4d",
342 }),
343 ).toBeNull();
344 expect(
345 resolveCheckoutBranchMismatch({
346 ...worktree,
347 currentGitBranch: "feature/checkout",
348 }),
349 ).toEqual({
350 threadBranch: worktree.activeThreadBranch,
351 currentBranch: "feature/checkout",
352 canRestoreThreadBranch: false,
353 });
354 });
355​
356 it("ignores new-worktree base selection before a worktree exists", () => {
357 expect(
358 resolveCheckoutBranchMismatch({
359 effectiveEnvMode: "worktree",
360 activeWorktreePath: null,
361 activeThreadBranch: "feature/base",
362 currentGitBranch: "main",
363 }),
364 ).toBeNull();
365 });
366});
367​
368describe("resolveEnvironmentOptionLabel", () => {
369 it("prefers the primary environment's machine label", () => {
370 expect(
371 resolveEnvironmentOptionLabel({
372 isPrimary: true,
373 environmentId: localEnvironmentId,
374 runtimeLabel: "Julius's Mac mini",
375 savedLabel: "Local environment",
376 }),
377 ).toBe("Julius's Mac mini");
378 });
379​
380 it("falls back to 'This device' for generic primary labels", () => {
381 expect(
382 resolveEnvironmentOptionLabel({
383 isPrimary: true,
384 environmentId: localEnvironmentId,
385 runtimeLabel: "Local environment",
386 savedLabel: "Local",
387 }),
388 ).toBe("This device");
389 });
390​
391 it("keeps configured labels for non-primary environments", () => {
392 expect(
393 resolveEnvironmentOptionLabel({
394 isPrimary: false,
395 environmentId: remoteEnvironmentId,
396 runtimeLabel: null,
397 savedLabel: "Build box",
398 }),
399 ).toBe("Build box");
400 });
401});
402​
403describe("shouldShowEnvironmentIndicator", () => {
404 it("shows the indicator whenever multiple environments are pickable", () => {
405 expect(
406 shouldShowEnvironmentIndicator({
407 activeEnvironment: { isPrimary: true },
408 canPickEnvironment: true,
409 }),
410 ).toBe(true);
411 });
412​
413 it("shows a sole remote environment so the user knows where the project runs", () => {
414 expect(
415 shouldShowEnvironmentIndicator({
416 activeEnvironment: { isPrimary: false },
417 canPickEnvironment: false,
418 }),
419 ).toBe(true);
420 });
421​
422 it("hides a sole primary (this-device) environment", () => {
423 expect(
424 shouldShowEnvironmentIndicator({
425 activeEnvironment: { isPrimary: true },
426 canPickEnvironment: false,
427 }),
428 ).toBe(false);
429 });
430​
431 it("hides the indicator when the active environment is unknown", () => {
432 expect(
433 shouldShowEnvironmentIndicator({
434 activeEnvironment: null,
435 canPickEnvironment: false,
436 }),
437 ).toBe(false);
438 });
439});
440​
441describe("shouldShowComposerContextStrip", () => {
442 it("keeps a sole primary server update reachable in a non-Git draft", () => {
443 const environment = {
444 activeEnvironment: { isPrimary: true },
445 canPickEnvironment: false,
446 serverUpdateAvailable: true,
447 };
448 const showEnvironmentIndicator = shouldShowEnvironmentIndicator(environment);
449 expect(showEnvironmentIndicator).toBe(true);
450 expect(
451 shouldShowComposerContextStrip({
452 isDraftHeroState: true,
453 persistInActiveThreads: false,
454 hasActiveProject: true,
455 isGitRepo: false,
456 showEnvironmentIndicator,
457 hostsRestingComposerControls: false,
458 }),
459 ).toBe(true);
460 expect(
461 shouldShowComposerContextStrip({
462 isDraftHeroState: false,
463 persistInActiveThreads: false,
464 hasActiveProject: true,
465 isGitRepo: false,
466 showEnvironmentIndicator,
467 serverUpdateAvailable: true,
468 hostsRestingComposerControls: false,
469 }),
470 ).toBe(true);
471 });
472​
473 it.each([false, true])(
474 "honors the active-thread preference with resting controls %s",
475 (hostsRestingComposerControls) => {
476 const input = {
477 isDraftHeroState: false,
478 hasActiveProject: true,
479 isGitRepo: true,
480 showEnvironmentIndicator: true,
481 hostsRestingComposerControls,
482 };
483 expect(shouldShowComposerContextStrip({ ...input, persistInActiveThreads: false })).toBe(
484 false,
485 );
486 expect(shouldShowComposerContextStrip({ ...input, persistInActiveThreads: true })).toBe(true);
487 },
488 );
489​
490 it("keeps the environment indicator visible for a non-Git project", () => {
491 expect(
492 shouldShowComposerContextStrip({
493 isDraftHeroState: true,
494 persistInActiveThreads: false,
495 hasActiveProject: true,
496 isGitRepo: false,
497 showEnvironmentIndicator: true,
498 hostsRestingComposerControls: false,
499 }),
500 ).toBe(true);
501 });
502​
503 it("hides the strip when a non-Git project has nothing to show", () => {
504 expect(
505 shouldShowComposerContextStrip({
506 isDraftHeroState: true,
507 persistInActiveThreads: false,
508 hasActiveProject: true,
509 isGitRepo: false,
510 showEnvironmentIndicator: false,
511 hostsRestingComposerControls: false,
512 }),
513 ).toBe(false);
514 });
515​
516 it("keeps the strip for visible resting composer controls in a non-Git thread", () => {
517 expect(
518 shouldShowComposerContextStrip({
519 isDraftHeroState: true,
520 persistInActiveThreads: false,
521 hasActiveProject: true,
522 isGitRepo: false,
523 showEnvironmentIndicator: false,
524 hostsRestingComposerControls: true,
525 }),
526 ).toBe(true);
527 });
528​
529 it("shows Git controls without requiring an environment indicator", () => {
530 expect(
531 shouldShowComposerContextStrip({
532 isDraftHeroState: true,
533 persistInActiveThreads: false,
534 hasActiveProject: true,
535 isGitRepo: true,
536 showEnvironmentIndicator: false,
537 hostsRestingComposerControls: false,
538 }),
539 ).toBe(true);
540 });
541});
542​
543describe("resolveEffectiveEnvMode", () => {
544 it("treats draft threads already attached to a worktree as current-checkout mode", () => {
545 expect(
546 resolveEffectiveEnvMode({
547 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
548 hasServerThread: false,
549 draftThreadEnvMode: "worktree",
550 }),
551 ).toBe("local");
552 });
553​
554 it("keeps explicit new-worktree mode for draft threads without a worktree path", () => {
555 expect(
556 resolveEffectiveEnvMode({
557 activeWorktreePath: null,
558 hasServerThread: false,
559 draftThreadEnvMode: "worktree",
560 }),
561 ).toBe("worktree");
562 });
563​
564 it("keeps a server thread in worktree mode while its worktree is still being created", () => {
565 expect(
566 resolveEffectiveEnvMode({
567 activeWorktreePath: null,
568 hasServerThread: true,
569 draftThreadEnvMode: undefined,
570 preparingWorktree: true,
571 }),
572 ).toBe("worktree");
573 expect(
574 resolveEffectiveEnvMode({
575 activeWorktreePath: null,
576 hasServerThread: true,
577 draftThreadEnvMode: undefined,
578 }),
579 ).toBe("local");
580 });
581});
582​
583describe("resolveEnvModeLabel", () => {
584 it("uses explicit workspace labels", () => {
585 expect(resolveEnvModeLabel("local")).toBe("Current checkout");
586 expect(resolveEnvModeLabel("worktree")).toBe("New worktree");
587 });
588});
589​
590describe("resolveCurrentWorkspaceLabel", () => {
591 it("describes the main repo checkout when no worktree path is active", () => {
592 expect(resolveCurrentWorkspaceLabel(null)).toBe("Current checkout");
593 });
594​
595 it("describes the active checkout as a worktree when one is attached", () => {
596 expect(resolveCurrentWorkspaceLabel("/repo/.t3/worktrees/feature-a")).toBe("Current worktree");
597 });
598});
599​
600describe("resolveLockedWorkspaceLabel", () => {
601 it("uses a shorter label for the main repo checkout", () => {
602 expect(resolveLockedWorkspaceLabel(null, "local")).toBe("Local checkout");
603 });
604​
605 it("uses a shorter label for an attached worktree", () => {
606 expect(resolveLockedWorkspaceLabel("/repo/.t3/worktrees/feature-a", "worktree")).toBe(
607 "Worktree",
608 );
609 });
610​
611 it("describes a worktree that is still being created as a new worktree", () => {
612 expect(resolveLockedWorkspaceLabel(null, "worktree")).toBe("New worktree");
613 });
614});
615​
616describe("deriveLocalBranchNameFromRemoteRef", () => {
617 it("strips the remote prefix from a remote ref", () => {
618 expect(deriveLocalBranchNameFromRemoteRef("origin/feature/demo")).toBe("feature/demo");
619 });
620​
621 it("supports remote names that contain slashes", () => {
622 expect(deriveLocalBranchNameFromRemoteRef("my-org/upstream/feature/demo")).toBe(
623 "upstream/feature/demo",
624 );
625 });
626​
627 it("returns the original name when ref is malformed", () => {
628 expect(deriveLocalBranchNameFromRemoteRef("origin/")).toBe("origin/");
629 expect(deriveLocalBranchNameFromRemoteRef("/feature/demo")).toBe("/feature/demo");
630 });
631});
632​
633describe("dedupeRemoteBranchesWithLocalMatches", () => {
634 it("hides remote refs when the matching local ref exists", () => {
635 const input: VcsRef[] = [
636 {
637 name: "feature/demo",
638 current: false,
639 isDefault: false,
640 worktreePath: null,
641 },
642 {
643 name: "origin/feature/demo",
644 isRemote: true,
645 remoteName: "origin",
646 current: false,
647 isDefault: false,
648 worktreePath: null,
649 },
650 {
651 name: "origin/feature/remote-only",
652 isRemote: true,
653 remoteName: "origin",
654 current: false,
655 isDefault: false,
656 worktreePath: null,
657 },
658 ];
659​
660 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
661 "feature/demo",
662 "origin/feature/remote-only",
663 ]);
664 });
665​
666 it("keeps all entries when no local match exists for a remote ref", () => {
667 const input: VcsRef[] = [
668 {
669 name: "feature/local",
670 current: false,
671 isDefault: false,
672 worktreePath: null,
673 },
674 {
675 name: "origin/feature/remote-only",
676 isRemote: true,
677 remoteName: "origin",
678 current: false,
679 isDefault: false,
680 worktreePath: null,
681 },
682 ];
683​
684 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
685 "feature/local",
686 "origin/feature/remote-only",
687 ]);
688 });
689​
690 it("keeps non-origin remote refs visible even when a matching local ref exists", () => {
691 const input: VcsRef[] = [
692 {
693 name: "feature/demo",
694 current: false,
695 isDefault: false,
696 worktreePath: null,
697 },
698 {
699 name: "my-org/upstream/feature/demo",
700 isRemote: true,
701 remoteName: "my-org/upstream",
702 current: false,
703 isDefault: false,
704 worktreePath: null,
705 },
706 ];
707​
708 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
709 "feature/demo",
710 "my-org/upstream/feature/demo",
711 ]);
712 });
713​
714 it("keeps non-origin remote refs visible when git tracks with first-slash local naming", () => {
715 const input: VcsRef[] = [
716 {
717 name: "upstream/feature",
718 current: false,
719 isDefault: false,
720 worktreePath: null,
721 },
722 {
723 name: "my-org/upstream/feature",
724 isRemote: true,
725 remoteName: "my-org/upstream",
726 current: false,
727 isDefault: false,
728 worktreePath: null,
729 },
730 ];
731​
732 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
733 "upstream/feature",
734 "my-org/upstream/feature",
735 ]);
736 });
737});
738​
739describe("resolveBranchSelectionTarget", () => {
740 it("reuses an existing secondary worktree for the selected ref", () => {
741 expect(
742 resolveBranchSelectionTarget({
743 activeProjectCwd: "/repo",
744 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
745 refName: {
746 isDefault: false,
747 worktreePath: "/repo/.t3/worktrees/feature-b",
748 },
749 }),
750 ).toEqual({
751 checkoutCwd: "/repo/.t3/worktrees/feature-b",
752 nextWorktreePath: "/repo/.t3/worktrees/feature-b",
753 reuseExistingWorktree: true,
754 });
755 });
756​
757 it("switches back to the main repo when the ref already lives there", () => {
758 expect(
759 resolveBranchSelectionTarget({
760 activeProjectCwd: "/repo",
761 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
762 refName: {
763 isDefault: true,
764 worktreePath: "/repo",
765 },
766 }),
767 ).toEqual({
768 checkoutCwd: "/repo",
769 nextWorktreePath: null,
770 reuseExistingWorktree: true,
771 });
772 });
773​
774 it("checks out the default ref in the main repo when leaving a secondary worktree", () => {
775 expect(
776 resolveBranchSelectionTarget({
777 activeProjectCwd: "/repo",
778 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
779 refName: {
780 isDefault: true,
781 worktreePath: null,
782 },
783 }),
784 ).toEqual({
785 checkoutCwd: "/repo",
786 nextWorktreePath: null,
787 reuseExistingWorktree: false,
788 });
789 });
790​
791 it("keeps checkout in the current worktree for non-default refs", () => {
792 expect(
793 resolveBranchSelectionTarget({
794 activeProjectCwd: "/repo",
795 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
796 refName: {
797 isDefault: false,
798 worktreePath: null,
799 },
800 }),
801 ).toEqual({
802 checkoutCwd: "/repo/.t3/worktrees/feature-a",
803 nextWorktreePath: "/repo/.t3/worktrees/feature-a",
804 reuseExistingWorktree: false,
805 });
806 });
807});
808​
809describe("shouldIncludeBranchPickerItem", () => {
810 it("keeps the synthetic checkout PR item visible for gh pr checkout input", () => {
811 expect(
812 shouldIncludeBranchPickerItem({
813 itemValue: "__checkout_pull_request__:1359",
814 normalizedQuery: "gh pr checkout 1359",
815 createBranchItemValue: "__create_new_branch__:gh pr checkout 1359",
816 checkoutPullRequestItemValue: "__checkout_pull_request__:1359",
817 }),
818 ).toBe(true);
819 });
820​
821 it("keeps the synthetic create-ref item visible for arbitrary ref input", () => {
822 expect(
823 shouldIncludeBranchPickerItem({
824 itemValue: "__create_new_branch__:feature/demo",
825 normalizedQuery: "feature/demo",
826 createBranchItemValue: "__create_new_branch__:feature/demo",
827 checkoutPullRequestItemValue: null,
828 }),
829 ).toBe(true);
830 });
831​
832 it("still filters ordinary ref items by query text", () => {
833 expect(
834 shouldIncludeBranchPickerItem({
835 itemValue: "main",
836 normalizedQuery: "gh pr checkout 1359",
837 createBranchItemValue: "__create_new_branch__:gh pr checkout 1359",
838 checkoutPullRequestItemValue: "__checkout_pull_request__:1359",
839 }),
840 ).toBe(false);
841 });
842​
843 it("surfaces an existing ref matching the sanitized query", () => {
844 expect(
845 shouldIncludeBranchPickerItem({
846 itemValue: "new-branch",
847 normalizedQuery: "new branch",
848 createBranchItemValue: null,
849 checkoutPullRequestItemValue: null,
850 }),
851 ).toBe(true);
852 });
853​
854 it("surfaces a ref from a partial query containing a space", () => {
855 expect(
856 shouldIncludeBranchPickerItem({
857 itemValue: "hello-world",
858 normalizedQuery: "hello w",
859 createBranchItemValue: null,
860 checkoutPullRequestItemValue: null,
861 }),
862 ).toBe(true);
863 });
864​
865 it("excludes refs matching neither the raw nor the sanitized query", () => {
866 expect(
867 shouldIncludeBranchPickerItem({
868 itemValue: "main",
869 normalizedQuery: "new branch",
870 createBranchItemValue: null,
871 checkoutPullRequestItemValue: null,
872 }),
873 ).toBe(false);
874 });
875});
876​
877describe("sanitizeNewRefName", () => {
878 it("replaces a space with a dash", () => {
879 expect(sanitizeNewRefName("new branch")).toBe("new-branch");
880 });
881​
882 it("collapses a run of whitespace into a single dash", () => {
883 expect(sanitizeNewRefName("new branch")).toBe("new-branch");
884 });
885​
886 it("trims surrounding whitespace instead of turning it into dashes", () => {
887 expect(sanitizeNewRefName(" new branch ")).toBe("new-branch");
888 });
889​
890 it("replaces tabs, which git rejects just like spaces", () => {
891 expect(sanitizeNewRefName("new\tbranch")).toBe("new-branch");
892 });
893​
894 it("preserves whitespace that git accepts", () => {
895 expect(sanitizeNewRefName("new\u00a0branch")).toBe("new\u00a0branch");
896 expect(sanitizeNewRefName("new\u2009branch")).toBe("new\u2009branch");
897 });
898​
899 it("keeps slashes so nested ref names survive", () => {
900 expect(sanitizeNewRefName("feature/new thing")).toBe("feature/new-thing");
901 });
902​
903 it("preserves case because git ref names are case sensitive", () => {
904 expect(sanitizeNewRefName("Feature/New Thing")).toBe("Feature/New-Thing");
905 });
906​
907 it("leaves an already valid ref name untouched", () => {
908 expect(sanitizeNewRefName("feature/login")).toBe("feature/login");
909 });
910​
911 it("returns an empty string for whitespace-only input", () => {
912 expect(sanitizeNewRefName(" ")).toBe("");
913 });
914​
915 it("does not collapse dashes the user typed", () => {
916 expect(sanitizeNewRefName("new - branch")).toBe("new---branch");
917 expect(sanitizeNewRefName("foo--bar")).toBe("foo--bar");
918 });
919});