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 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.each([false, true])(
443 "honors the active-thread preference with resting controls %s",
444 (hostsRestingComposerControls) => {
445 const input = {
446 isDraftHeroState: false,
447 hasActiveProject: true,
448 isGitRepo: true,
449 showEnvironmentIndicator: true,
450 hostsRestingComposerControls,
451 };
452 expect(shouldShowComposerContextStrip({ ...input, persistInActiveThreads: false })).toBe(
453 false,
454 );
455 expect(shouldShowComposerContextStrip({ ...input, persistInActiveThreads: true })).toBe(true);
456 },
457 );
458
459 it("keeps the environment indicator visible for a non-Git project", () => {
460 expect(
461 shouldShowComposerContextStrip({
462 isDraftHeroState: true,
463 persistInActiveThreads: false,
464 hasActiveProject: true,
465 isGitRepo: false,
466 showEnvironmentIndicator: true,
467 hostsRestingComposerControls: false,
468 }),
469 ).toBe(true);
470 });
471
472 it("hides the strip when a non-Git project has nothing to show", () => {
473 expect(
474 shouldShowComposerContextStrip({
475 isDraftHeroState: true,
476 persistInActiveThreads: false,
477 hasActiveProject: true,
478 isGitRepo: false,
479 showEnvironmentIndicator: false,
480 hostsRestingComposerControls: false,
481 }),
482 ).toBe(false);
483 });
484
485 it("keeps the strip for visible resting composer controls in a non-Git thread", () => {
486 expect(
487 shouldShowComposerContextStrip({
488 isDraftHeroState: true,
489 persistInActiveThreads: false,
490 hasActiveProject: true,
491 isGitRepo: false,
492 showEnvironmentIndicator: false,
493 hostsRestingComposerControls: true,
494 }),
495 ).toBe(true);
496 });
497
498 it("shows Git controls without requiring an environment indicator", () => {
499 expect(
500 shouldShowComposerContextStrip({
501 isDraftHeroState: true,
502 persistInActiveThreads: false,
503 hasActiveProject: true,
504 isGitRepo: true,
505 showEnvironmentIndicator: false,
506 hostsRestingComposerControls: false,
507 }),
508 ).toBe(true);
509 });
510});
511
512describe("resolveEffectiveEnvMode", () => {
513 it("treats draft threads already attached to a worktree as current-checkout mode", () => {
514 expect(
515 resolveEffectiveEnvMode({
516 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
517 hasServerThread: false,
518 draftThreadEnvMode: "worktree",
519 }),
520 ).toBe("local");
521 });
522
523 it("keeps explicit new-worktree mode for draft threads without a worktree path", () => {
524 expect(
525 resolveEffectiveEnvMode({
526 activeWorktreePath: null,
527 hasServerThread: false,
528 draftThreadEnvMode: "worktree",
529 }),
530 ).toBe("worktree");
531 });
532
533 it("keeps a server thread in worktree mode while its worktree is still being created", () => {
534 expect(
535 resolveEffectiveEnvMode({
536 activeWorktreePath: null,
537 hasServerThread: true,
538 draftThreadEnvMode: undefined,
539 preparingWorktree: true,
540 }),
541 ).toBe("worktree");
542 expect(
543 resolveEffectiveEnvMode({
544 activeWorktreePath: null,
545 hasServerThread: true,
546 draftThreadEnvMode: undefined,
547 }),
548 ).toBe("local");
549 });
550});
551
552describe("resolveEnvModeLabel", () => {
553 it("uses explicit workspace labels", () => {
554 expect(resolveEnvModeLabel("local")).toBe("Current checkout");
555 expect(resolveEnvModeLabel("worktree")).toBe("New worktree");
556 });
557});
558
559describe("resolveCurrentWorkspaceLabel", () => {
560 it("describes the main repo checkout when no worktree path is active", () => {
561 expect(resolveCurrentWorkspaceLabel(null)).toBe("Current checkout");
562 });
563
564 it("describes the active checkout as a worktree when one is attached", () => {
565 expect(resolveCurrentWorkspaceLabel("/repo/.t3/worktrees/feature-a")).toBe("Current worktree");
566 });
567});
568
569describe("resolveLockedWorkspaceLabel", () => {
570 it("uses a shorter label for the main repo checkout", () => {
571 expect(resolveLockedWorkspaceLabel(null, "local")).toBe("Local checkout");
572 });
573
574 it("uses a shorter label for an attached worktree", () => {
575 expect(resolveLockedWorkspaceLabel("/repo/.t3/worktrees/feature-a", "worktree")).toBe(
576 "Worktree",
577 );
578 });
579
580 it("describes a worktree that is still being created as a new worktree", () => {
581 expect(resolveLockedWorkspaceLabel(null, "worktree")).toBe("New worktree");
582 });
583});
584
585describe("deriveLocalBranchNameFromRemoteRef", () => {
586 it("strips the remote prefix from a remote ref", () => {
587 expect(deriveLocalBranchNameFromRemoteRef("origin/feature/demo")).toBe("feature/demo");
588 });
589
590 it("supports remote names that contain slashes", () => {
591 expect(deriveLocalBranchNameFromRemoteRef("my-org/upstream/feature/demo")).toBe(
592 "upstream/feature/demo",
593 );
594 });
595
596 it("returns the original name when ref is malformed", () => {
597 expect(deriveLocalBranchNameFromRemoteRef("origin/")).toBe("origin/");
598 expect(deriveLocalBranchNameFromRemoteRef("/feature/demo")).toBe("/feature/demo");
599 });
600});
601
602describe("dedupeRemoteBranchesWithLocalMatches", () => {
603 it("hides remote refs when the matching local ref exists", () => {
604 const input: VcsRef[] = [
605 {
606 name: "feature/demo",
607 current: false,
608 isDefault: false,
609 worktreePath: null,
610 },
611 {
612 name: "origin/feature/demo",
613 isRemote: true,
614 remoteName: "origin",
615 current: false,
616 isDefault: false,
617 worktreePath: null,
618 },
619 {
620 name: "origin/feature/remote-only",
621 isRemote: true,
622 remoteName: "origin",
623 current: false,
624 isDefault: false,
625 worktreePath: null,
626 },
627 ];
628
629 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
630 "feature/demo",
631 "origin/feature/remote-only",
632 ]);
633 });
634
635 it("keeps all entries when no local match exists for a remote ref", () => {
636 const input: VcsRef[] = [
637 {
638 name: "feature/local",
639 current: false,
640 isDefault: false,
641 worktreePath: null,
642 },
643 {
644 name: "origin/feature/remote-only",
645 isRemote: true,
646 remoteName: "origin",
647 current: false,
648 isDefault: false,
649 worktreePath: null,
650 },
651 ];
652
653 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
654 "feature/local",
655 "origin/feature/remote-only",
656 ]);
657 });
658
659 it("keeps non-origin remote refs visible even when a matching local ref exists", () => {
660 const input: VcsRef[] = [
661 {
662 name: "feature/demo",
663 current: false,
664 isDefault: false,
665 worktreePath: null,
666 },
667 {
668 name: "my-org/upstream/feature/demo",
669 isRemote: true,
670 remoteName: "my-org/upstream",
671 current: false,
672 isDefault: false,
673 worktreePath: null,
674 },
675 ];
676
677 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
678 "feature/demo",
679 "my-org/upstream/feature/demo",
680 ]);
681 });
682
683 it("keeps non-origin remote refs visible when git tracks with first-slash local naming", () => {
684 const input: VcsRef[] = [
685 {
686 name: "upstream/feature",
687 current: false,
688 isDefault: false,
689 worktreePath: null,
690 },
691 {
692 name: "my-org/upstream/feature",
693 isRemote: true,
694 remoteName: "my-org/upstream",
695 current: false,
696 isDefault: false,
697 worktreePath: null,
698 },
699 ];
700
701 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
702 "upstream/feature",
703 "my-org/upstream/feature",
704 ]);
705 });
706});
707
708describe("resolveBranchSelectionTarget", () => {
709 it("reuses an existing secondary worktree for the selected ref", () => {
710 expect(
711 resolveBranchSelectionTarget({
712 activeProjectCwd: "/repo",
713 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
714 refName: {
715 isDefault: false,
716 worktreePath: "/repo/.t3/worktrees/feature-b",
717 },
718 }),
719 ).toEqual({
720 checkoutCwd: "/repo/.t3/worktrees/feature-b",
721 nextWorktreePath: "/repo/.t3/worktrees/feature-b",
722 reuseExistingWorktree: true,
723 });
724 });
725
726 it("switches back to the main repo when the ref already lives there", () => {
727 expect(
728 resolveBranchSelectionTarget({
729 activeProjectCwd: "/repo",
730 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
731 refName: {
732 isDefault: true,
733 worktreePath: "/repo",
734 },
735 }),
736 ).toEqual({
737 checkoutCwd: "/repo",
738 nextWorktreePath: null,
739 reuseExistingWorktree: true,
740 });
741 });
742
743 it("checks out the default ref in the main repo when leaving a secondary worktree", () => {
744 expect(
745 resolveBranchSelectionTarget({
746 activeProjectCwd: "/repo",
747 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
748 refName: {
749 isDefault: true,
750 worktreePath: null,
751 },
752 }),
753 ).toEqual({
754 checkoutCwd: "/repo",
755 nextWorktreePath: null,
756 reuseExistingWorktree: false,
757 });
758 });
759
760 it("keeps checkout in the current worktree for non-default refs", () => {
761 expect(
762 resolveBranchSelectionTarget({
763 activeProjectCwd: "/repo",
764 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
765 refName: {
766 isDefault: false,
767 worktreePath: null,
768 },
769 }),
770 ).toEqual({
771 checkoutCwd: "/repo/.t3/worktrees/feature-a",
772 nextWorktreePath: "/repo/.t3/worktrees/feature-a",
773 reuseExistingWorktree: false,
774 });
775 });
776});
777
778describe("shouldIncludeBranchPickerItem", () => {
779 it("keeps the synthetic checkout PR item visible for gh pr checkout input", () => {
780 expect(
781 shouldIncludeBranchPickerItem({
782 itemValue: "__checkout_pull_request__:1359",
783 normalizedQuery: "gh pr checkout 1359",
784 createBranchItemValue: "__create_new_branch__:gh pr checkout 1359",
785 checkoutPullRequestItemValue: "__checkout_pull_request__:1359",
786 }),
787 ).toBe(true);
788 });
789
790 it("keeps the synthetic create-ref item visible for arbitrary ref input", () => {
791 expect(
792 shouldIncludeBranchPickerItem({
793 itemValue: "__create_new_branch__:feature/demo",
794 normalizedQuery: "feature/demo",
795 createBranchItemValue: "__create_new_branch__:feature/demo",
796 checkoutPullRequestItemValue: null,
797 }),
798 ).toBe(true);
799 });
800
801 it("still filters ordinary ref items by query text", () => {
802 expect(
803 shouldIncludeBranchPickerItem({
804 itemValue: "main",
805 normalizedQuery: "gh pr checkout 1359",
806 createBranchItemValue: "__create_new_branch__:gh pr checkout 1359",
807 checkoutPullRequestItemValue: "__checkout_pull_request__:1359",
808 }),
809 ).toBe(false);
810 });
811
812 it("surfaces an existing ref matching the sanitized query", () => {
813 expect(
814 shouldIncludeBranchPickerItem({
815 itemValue: "new-branch",
816 normalizedQuery: "new branch",
817 createBranchItemValue: null,
818 checkoutPullRequestItemValue: null,
819 }),
820 ).toBe(true);
821 });
822
823 it("surfaces a ref from a partial query containing a space", () => {
824 expect(
825 shouldIncludeBranchPickerItem({
826 itemValue: "hello-world",
827 normalizedQuery: "hello w",
828 createBranchItemValue: null,
829 checkoutPullRequestItemValue: null,
830 }),
831 ).toBe(true);
832 });
833
834 it("excludes refs matching neither the raw nor the sanitized query", () => {
835 expect(
836 shouldIncludeBranchPickerItem({
837 itemValue: "main",
838 normalizedQuery: "new branch",
839 createBranchItemValue: null,
840 checkoutPullRequestItemValue: null,
841 }),
842 ).toBe(false);
843 });
844});
845
846describe("sanitizeNewRefName", () => {
847 it("replaces a space with a dash", () => {
848 expect(sanitizeNewRefName("new branch")).toBe("new-branch");
849 });
850
851 it("collapses a run of whitespace into a single dash", () => {
852 expect(sanitizeNewRefName("new branch")).toBe("new-branch");
853 });
854
855 it("trims surrounding whitespace instead of turning it into dashes", () => {
856 expect(sanitizeNewRefName(" new branch ")).toBe("new-branch");
857 });
858
859 it("replaces tabs, which git rejects just like spaces", () => {
860 expect(sanitizeNewRefName("new\tbranch")).toBe("new-branch");
861 });
862
863 it("preserves whitespace that git accepts", () => {
864 expect(sanitizeNewRefName("new\u00a0branch")).toBe("new\u00a0branch");
865 expect(sanitizeNewRefName("new\u2009branch")).toBe("new\u2009branch");
866 });
867
868 it("keeps slashes so nested ref names survive", () => {
869 expect(sanitizeNewRefName("feature/new thing")).toBe("feature/new-thing");
870 });
871
872 it("preserves case because git ref names are case sensitive", () => {
873 expect(sanitizeNewRefName("Feature/New Thing")).toBe("Feature/New-Thing");
874 });
875
876 it("leaves an already valid ref name untouched", () => {
877 expect(sanitizeNewRefName("feature/login")).toBe("feature/login");
878 });
879
880 it("returns an empty string for whitespace-only input", () => {
881 expect(sanitizeNewRefName(" ")).toBe("");
882 });
883
884 it("does not collapse dashes the user typed", () => {
885 expect(sanitizeNewRefName("new - branch")).toBe("new---branch");
886 expect(sanitizeNewRefName("foo--bar")).toBe("foo--bar");
887 });
888});