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
520describe("resolveEnvModeLabel", () => {
521 it("uses explicit workspace labels", () => {
522 expect(resolveEnvModeLabel("local")).toBe("Current checkout");
523 expect(resolveEnvModeLabel("worktree")).toBe("New worktree");
524 });
525});
526
527describe("resolveCurrentWorkspaceLabel", () => {
528 it("describes the main repo checkout when no worktree path is active", () => {
529 expect(resolveCurrentWorkspaceLabel(null)).toBe("Current checkout");
530 });
531
532 it("describes the active checkout as a worktree when one is attached", () => {
533 expect(resolveCurrentWorkspaceLabel("/repo/.t3/worktrees/feature-a")).toBe("Current worktree");
534 });
535});
536
537describe("resolveLockedWorkspaceLabel", () => {
538 it("uses a shorter label for the main repo checkout", () => {
539 expect(resolveLockedWorkspaceLabel(null)).toBe("Local checkout");
540 });
541
542 it("uses a shorter label for an attached worktree", () => {
543 expect(resolveLockedWorkspaceLabel("/repo/.t3/worktrees/feature-a")).toBe("Worktree");
544 });
545});
546
547describe("resolveWorkspaceDisplayName", () => {
548 it("returns the final folder for POSIX and Windows paths", () => {
549 expect(resolveWorkspaceDisplayName("/repo/.t3/worktrees/feature-a")).toBe("feature-a");
550 expect(resolveWorkspaceDisplayName("C:\\code\\project\\feature-b\\")).toBe("feature-b");
551 });
552
553 it("handles missing and root paths", () => {
554 expect(resolveWorkspaceDisplayName(null)).toBeNull();
555 expect(resolveWorkspaceDisplayName("/")).toBe("/");
556 });
557});
558
559describe("deriveLocalBranchNameFromRemoteRef", () => {
560 it("strips the remote prefix from a remote ref", () => {
561 expect(deriveLocalBranchNameFromRemoteRef("origin/feature/demo")).toBe("feature/demo");
562 });
563
564 it("supports remote names that contain slashes", () => {
565 expect(deriveLocalBranchNameFromRemoteRef("my-org/upstream/feature/demo")).toBe(
566 "upstream/feature/demo",
567 );
568 });
569
570 it("returns the original name when ref is malformed", () => {
571 expect(deriveLocalBranchNameFromRemoteRef("origin/")).toBe("origin/");
572 expect(deriveLocalBranchNameFromRemoteRef("/feature/demo")).toBe("/feature/demo");
573 });
574});
575
576describe("dedupeRemoteBranchesWithLocalMatches", () => {
577 it("hides remote refs when the matching local ref exists", () => {
578 const input: VcsRef[] = [
579 {
580 name: "feature/demo",
581 current: false,
582 isDefault: false,
583 worktreePath: null,
584 },
585 {
586 name: "origin/feature/demo",
587 isRemote: true,
588 remoteName: "origin",
589 current: false,
590 isDefault: false,
591 worktreePath: null,
592 },
593 {
594 name: "origin/feature/remote-only",
595 isRemote: true,
596 remoteName: "origin",
597 current: false,
598 isDefault: false,
599 worktreePath: null,
600 },
601 ];
602
603 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
604 "feature/demo",
605 "origin/feature/remote-only",
606 ]);
607 });
608
609 it("keeps all entries when no local match exists for a remote ref", () => {
610 const input: VcsRef[] = [
611 {
612 name: "feature/local",
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/local",
629 "origin/feature/remote-only",
630 ]);
631 });
632
633 it("keeps non-origin remote refs visible even when a matching local ref exists", () => {
634 const input: VcsRef[] = [
635 {
636 name: "feature/demo",
637 current: false,
638 isDefault: false,
639 worktreePath: null,
640 },
641 {
642 name: "my-org/upstream/feature/demo",
643 isRemote: true,
644 remoteName: "my-org/upstream",
645 current: false,
646 isDefault: false,
647 worktreePath: null,
648 },
649 ];
650
651 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
652 "feature/demo",
653 "my-org/upstream/feature/demo",
654 ]);
655 });
656
657 it("keeps non-origin remote refs visible when git tracks with first-slash local naming", () => {
658 const input: VcsRef[] = [
659 {
660 name: "upstream/feature",
661 current: false,
662 isDefault: false,
663 worktreePath: null,
664 },
665 {
666 name: "my-org/upstream/feature",
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 "upstream/feature",
677 "my-org/upstream/feature",
678 ]);
679 });
680});
681
682describe("resolveBranchSelectionTarget", () => {
683 it("reuses an existing secondary worktree for the selected ref", () => {
684 expect(
685 resolveBranchSelectionTarget({
686 activeProjectCwd: "/repo",
687 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
688 refName: {
689 isDefault: false,
690 worktreePath: "/repo/.t3/worktrees/feature-b",
691 },
692 }),
693 ).toEqual({
694 checkoutCwd: "/repo/.t3/worktrees/feature-b",
695 nextWorktreePath: "/repo/.t3/worktrees/feature-b",
696 reuseExistingWorktree: true,
697 });
698 });
699
700 it("switches back to the main repo when the ref already lives there", () => {
701 expect(
702 resolveBranchSelectionTarget({
703 activeProjectCwd: "/repo",
704 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
705 refName: {
706 isDefault: true,
707 worktreePath: "/repo",
708 },
709 }),
710 ).toEqual({
711 checkoutCwd: "/repo",
712 nextWorktreePath: null,
713 reuseExistingWorktree: true,
714 });
715 });
716
717 it("checks out the default ref in the main repo when leaving a secondary worktree", () => {
718 expect(
719 resolveBranchSelectionTarget({
720 activeProjectCwd: "/repo",
721 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
722 refName: {
723 isDefault: true,
724 worktreePath: null,
725 },
726 }),
727 ).toEqual({
728 checkoutCwd: "/repo",
729 nextWorktreePath: null,
730 reuseExistingWorktree: false,
731 });
732 });
733
734 it("keeps checkout in the current worktree for non-default refs", () => {
735 expect(
736 resolveBranchSelectionTarget({
737 activeProjectCwd: "/repo",
738 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
739 refName: {
740 isDefault: false,
741 worktreePath: null,
742 },
743 }),
744 ).toEqual({
745 checkoutCwd: "/repo/.t3/worktrees/feature-a",
746 nextWorktreePath: "/repo/.t3/worktrees/feature-a",
747 reuseExistingWorktree: false,
748 });
749 });
750});
751
752describe("shouldIncludeBranchPickerItem", () => {
753 it("keeps the synthetic checkout PR item visible for gh pr checkout input", () => {
754 expect(
755 shouldIncludeBranchPickerItem({
756 itemValue: "__checkout_pull_request__:1359",
757 normalizedQuery: "gh pr checkout 1359",
758 createBranchItemValue: "__create_new_branch__:gh pr checkout 1359",
759 checkoutPullRequestItemValue: "__checkout_pull_request__:1359",
760 }),
761 ).toBe(true);
762 });
763
764 it("keeps the synthetic create-ref item visible for arbitrary ref input", () => {
765 expect(
766 shouldIncludeBranchPickerItem({
767 itemValue: "__create_new_branch__:feature/demo",
768 normalizedQuery: "feature/demo",
769 createBranchItemValue: "__create_new_branch__:feature/demo",
770 checkoutPullRequestItemValue: null,
771 }),
772 ).toBe(true);
773 });
774
775 it("still filters ordinary ref items by query text", () => {
776 expect(
777 shouldIncludeBranchPickerItem({
778 itemValue: "main",
779 normalizedQuery: "gh pr checkout 1359",
780 createBranchItemValue: "__create_new_branch__:gh pr checkout 1359",
781 checkoutPullRequestItemValue: "__checkout_pull_request__:1359",
782 }),
783 ).toBe(false);
784 });
785
786 // Typing a spaced name must still surface the ref it would have been created
787 // as, or the picker shows nothing at all for that query.
788 it("surfaces an existing ref matching the sanitized query", () => {
789 expect(
790 shouldIncludeBranchPickerItem({
791 itemValue: "new-branch",
792 normalizedQuery: "new branch",
793 createBranchItemValue: null,
794 checkoutPullRequestItemValue: null,
795 }),
796 ).toBe(true);
797 });
798
799 // A partial query has to reach the ref it would have been created as, so
800 // searching "hello w" still finds an existing hello-world.
801 it("surfaces a ref from a partial query containing a space", () => {
802 expect(
803 shouldIncludeBranchPickerItem({
804 itemValue: "hello-world",
805 normalizedQuery: "hello w",
806 createBranchItemValue: null,
807 checkoutPullRequestItemValue: null,
808 }),
809 ).toBe(true);
810 });
811
812 it("excludes refs matching neither the raw nor the sanitized query", () => {
813 expect(
814 shouldIncludeBranchPickerItem({
815 itemValue: "main",
816 normalizedQuery: "new branch",
817 createBranchItemValue: null,
818 checkoutPullRequestItemValue: null,
819 }),
820 ).toBe(false);
821 });
822});
823
824// Git rejects ASCII space and the ASCII control characters in ref names, so a
825// typed name like "new branch" can only ever fail. Replacing exactly those can
826// turn a failing name into a working one without touching a name git already
827// accepts, including one holding non-ASCII whitespace such as U+00A0.
828describe("sanitizeNewRefName", () => {
829 it("replaces a space with a dash", () => {
830 expect(sanitizeNewRefName("new branch")).toBe("new-branch");
831 });
832
833 it("collapses a run of whitespace into a single dash", () => {
834 expect(sanitizeNewRefName("new branch")).toBe("new-branch");
835 });
836
837 it("trims surrounding whitespace instead of turning it into dashes", () => {
838 expect(sanitizeNewRefName(" new branch ")).toBe("new-branch");
839 });
840
841 it("replaces tabs, which git rejects just like spaces", () => {
842 expect(sanitizeNewRefName("new\tbranch")).toBe("new-branch");
843 });
844
845 // git accepts U+00A0, U+2009 and other non-ASCII whitespace in ref names, so
846 // rewriting them would silently create a ref the user never typed.
847 it("preserves whitespace that git accepts", () => {
848 expect(sanitizeNewRefName("new\u00a0branch")).toBe("new\u00a0branch");
849 expect(sanitizeNewRefName("new\u2009branch")).toBe("new\u2009branch");
850 });
851
852 it("keeps slashes so nested ref names survive", () => {
853 expect(sanitizeNewRefName("feature/new thing")).toBe("feature/new-thing");
854 });
855
856 it("preserves case because git ref names are case sensitive", () => {
857 expect(sanitizeNewRefName("Feature/New Thing")).toBe("Feature/New-Thing");
858 });
859
860 it("leaves an already valid ref name untouched", () => {
861 expect(sanitizeNewRefName("feature/login")).toBe("feature/login");
862 });
863
864 it("returns an empty string for whitespace-only input", () => {
865 expect(sanitizeNewRefName(" ")).toBe("");
866 });
867
868 // Scoped deliberately to whitespace: git accepts consecutive dashes, so
869 // collapsing them would rewrite names the user may have typed on purpose.
870 it("does not collapse dashes the user typed", () => {
871 expect(sanitizeNewRefName("new - branch")).toBe("new---branch");
872 expect(sanitizeNewRefName("foo--bar")).toBe("foo--bar");
873 });
874});