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