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

BranchToolbar.logic.test.ts

836 lines24.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 resolveLocalCheckoutBranchMismatch,
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("shows the origin ref when a new worktree will start from origin", () => {
182 expect(
183 resolveBranchTriggerLabel({
184 activeWorktreePath: null,
185 effectiveEnvMode: "worktree",
186 resolvedActiveBranch: "main",
187 resolvedActiveBranchIsRemote: false,
188 startFromOrigin: true,
189 }),
190 ).toBe("From origin/main");
191 });
192​
193 it("shows the origin ref 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 origin/feature/demo");
203 });
204​
205 it("shows the local ref when start from origin 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 duplicate the origin prefix 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 an origin ref 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("resolveLocalCheckoutBranchMismatch", () => {
305 it("detects when a local thread is associated with a different branch than the checkout", () => {
306 expect(
307 resolveLocalCheckoutBranchMismatch({
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 });
317 });
318​
319 it("ignores matching local checkout state", () => {
320 expect(
321 resolveLocalCheckoutBranchMismatch({
322 effectiveEnvMode: "local",
323 activeWorktreePath: null,
324 activeThreadBranch: "feature/thread",
325 currentGitBranch: "feature/thread",
326 }),
327 ).toBeNull();
328 });
329​
330 it("ignores dedicated worktrees because their checkout is already thread-scoped", () => {
331 expect(
332 resolveLocalCheckoutBranchMismatch({
333 effectiveEnvMode: "worktree",
334 activeWorktreePath: "/repo/.t3/worktrees/feature-thread",
335 activeThreadBranch: "feature/thread",
336 currentGitBranch: "feature/current",
337 }),
338 ).toBeNull();
339 });
340​
341 it("ignores new-worktree base selection before a worktree exists", () => {
342 expect(
343 resolveLocalCheckoutBranchMismatch({
344 effectiveEnvMode: "worktree",
345 activeWorktreePath: null,
346 activeThreadBranch: "feature/base",
347 currentGitBranch: "main",
348 }),
349 ).toBeNull();
350 });
351});
352​
353describe("resolveEnvironmentOptionLabel", () => {
354 it("prefers the primary environment's machine label", () => {
355 expect(
356 resolveEnvironmentOptionLabel({
357 isPrimary: true,
358 environmentId: localEnvironmentId,
359 runtimeLabel: "Julius's Mac mini",
360 savedLabel: "Local environment",
361 }),
362 ).toBe("Julius's Mac mini");
363 });
364​
365 it("falls back to 'This device' for generic primary labels", () => {
366 expect(
367 resolveEnvironmentOptionLabel({
368 isPrimary: true,
369 environmentId: localEnvironmentId,
370 runtimeLabel: "Local environment",
371 savedLabel: "Local",
372 }),
373 ).toBe("This device");
374 });
375​
376 it("keeps configured labels for non-primary environments", () => {
377 expect(
378 resolveEnvironmentOptionLabel({
379 isPrimary: false,
380 environmentId: remoteEnvironmentId,
381 runtimeLabel: null,
382 savedLabel: "Build box",
383 }),
384 ).toBe("Build box");
385 });
386});
387​
388describe("shouldShowEnvironmentIndicator", () => {
389 it("shows the indicator whenever multiple environments are pickable", () => {
390 expect(
391 shouldShowEnvironmentIndicator({
392 activeEnvironment: { isPrimary: true },
393 canPickEnvironment: true,
394 }),
395 ).toBe(true);
396 });
397​
398 it("shows a sole remote environment so the user knows where the project runs", () => {
399 expect(
400 shouldShowEnvironmentIndicator({
401 activeEnvironment: { isPrimary: false },
402 canPickEnvironment: false,
403 }),
404 ).toBe(true);
405 });
406​
407 it("hides a sole primary (this-device) environment", () => {
408 expect(
409 shouldShowEnvironmentIndicator({
410 activeEnvironment: { isPrimary: true },
411 canPickEnvironment: false,
412 }),
413 ).toBe(false);
414 });
415​
416 it("hides the indicator when the active environment is unknown", () => {
417 expect(
418 shouldShowEnvironmentIndicator({
419 activeEnvironment: null,
420 canPickEnvironment: false,
421 }),
422 ).toBe(false);
423 });
424});
425​
426describe("shouldShowComposerContextStrip", () => {
427 it("keeps the environment indicator visible for a non-Git project", () => {
428 expect(
429 shouldShowComposerContextStrip({
430 hasActiveProject: true,
431 isGitRepo: false,
432 showEnvironmentIndicator: true,
433 hostsRestingComposerControls: false,
434 }),
435 ).toBe(true);
436 });
437​
438 it("hides the strip when a non-Git project has nothing to show", () => {
439 expect(
440 shouldShowComposerContextStrip({
441 hasActiveProject: true,
442 isGitRepo: false,
443 showEnvironmentIndicator: false,
444 hostsRestingComposerControls: false,
445 }),
446 ).toBe(false);
447 });
448​
449 it("keeps the strip for visible resting composer controls in a non-Git thread", () => {
450 expect(
451 shouldShowComposerContextStrip({
452 hasActiveProject: true,
453 isGitRepo: false,
454 showEnvironmentIndicator: false,
455 hostsRestingComposerControls: true,
456 }),
457 ).toBe(true);
458 });
459​
460 it("shows Git controls without requiring an environment indicator", () => {
461 expect(
462 shouldShowComposerContextStrip({
463 hasActiveProject: true,
464 isGitRepo: true,
465 showEnvironmentIndicator: false,
466 hostsRestingComposerControls: false,
467 }),
468 ).toBe(true);
469 });
470});
471​
472describe("resolveEffectiveEnvMode", () => {
473 it("treats draft threads already attached to a worktree as current-checkout mode", () => {
474 expect(
475 resolveEffectiveEnvMode({
476 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
477 hasServerThread: false,
478 draftThreadEnvMode: "worktree",
479 }),
480 ).toBe("local");
481 });
482​
483 it("keeps explicit new-worktree mode for draft threads without a worktree path", () => {
484 expect(
485 resolveEffectiveEnvMode({
486 activeWorktreePath: null,
487 hasServerThread: false,
488 draftThreadEnvMode: "worktree",
489 }),
490 ).toBe("worktree");
491 });
492});
493​
494describe("resolveEnvModeLabel", () => {
495 it("uses explicit workspace labels", () => {
496 expect(resolveEnvModeLabel("local")).toBe("Current checkout");
497 expect(resolveEnvModeLabel("worktree")).toBe("New worktree");
498 });
499});
500​
501describe("resolveCurrentWorkspaceLabel", () => {
502 it("describes the main repo checkout when no worktree path is active", () => {
503 expect(resolveCurrentWorkspaceLabel(null)).toBe("Current checkout");
504 });
505​
506 it("describes the active checkout as a worktree when one is attached", () => {
507 expect(resolveCurrentWorkspaceLabel("/repo/.t3/worktrees/feature-a")).toBe("Current worktree");
508 });
509});
510​
511describe("resolveLockedWorkspaceLabel", () => {
512 it("uses a shorter label for the main repo checkout", () => {
513 expect(resolveLockedWorkspaceLabel(null)).toBe("Local checkout");
514 });
515​
516 it("uses a shorter label for an attached worktree", () => {
517 expect(resolveLockedWorkspaceLabel("/repo/.t3/worktrees/feature-a")).toBe("Worktree");
518 });
519});
520​
521describe("deriveLocalBranchNameFromRemoteRef", () => {
522 it("strips the remote prefix from a remote ref", () => {
523 expect(deriveLocalBranchNameFromRemoteRef("origin/feature/demo")).toBe("feature/demo");
524 });
525​
526 it("supports remote names that contain slashes", () => {
527 expect(deriveLocalBranchNameFromRemoteRef("my-org/upstream/feature/demo")).toBe(
528 "upstream/feature/demo",
529 );
530 });
531​
532 it("returns the original name when ref is malformed", () => {
533 expect(deriveLocalBranchNameFromRemoteRef("origin/")).toBe("origin/");
534 expect(deriveLocalBranchNameFromRemoteRef("/feature/demo")).toBe("/feature/demo");
535 });
536});
537​
538describe("dedupeRemoteBranchesWithLocalMatches", () => {
539 it("hides remote refs when the matching local ref exists", () => {
540 const input: VcsRef[] = [
541 {
542 name: "feature/demo",
543 current: false,
544 isDefault: false,
545 worktreePath: null,
546 },
547 {
548 name: "origin/feature/demo",
549 isRemote: true,
550 remoteName: "origin",
551 current: false,
552 isDefault: false,
553 worktreePath: null,
554 },
555 {
556 name: "origin/feature/remote-only",
557 isRemote: true,
558 remoteName: "origin",
559 current: false,
560 isDefault: false,
561 worktreePath: null,
562 },
563 ];
564​
565 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
566 "feature/demo",
567 "origin/feature/remote-only",
568 ]);
569 });
570​
571 it("keeps all entries when no local match exists for a remote ref", () => {
572 const input: VcsRef[] = [
573 {
574 name: "feature/local",
575 current: false,
576 isDefault: false,
577 worktreePath: null,
578 },
579 {
580 name: "origin/feature/remote-only",
581 isRemote: true,
582 remoteName: "origin",
583 current: false,
584 isDefault: false,
585 worktreePath: null,
586 },
587 ];
588​
589 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
590 "feature/local",
591 "origin/feature/remote-only",
592 ]);
593 });
594​
595 it("keeps non-origin remote refs visible even when a matching local ref exists", () => {
596 const input: VcsRef[] = [
597 {
598 name: "feature/demo",
599 current: false,
600 isDefault: false,
601 worktreePath: null,
602 },
603 {
604 name: "my-org/upstream/feature/demo",
605 isRemote: true,
606 remoteName: "my-org/upstream",
607 current: false,
608 isDefault: false,
609 worktreePath: null,
610 },
611 ];
612​
613 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
614 "feature/demo",
615 "my-org/upstream/feature/demo",
616 ]);
617 });
618​
619 it("keeps non-origin remote refs visible when git tracks with first-slash local naming", () => {
620 const input: VcsRef[] = [
621 {
622 name: "upstream/feature",
623 current: false,
624 isDefault: false,
625 worktreePath: null,
626 },
627 {
628 name: "my-org/upstream/feature",
629 isRemote: true,
630 remoteName: "my-org/upstream",
631 current: false,
632 isDefault: false,
633 worktreePath: null,
634 },
635 ];
636​
637 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
638 "upstream/feature",
639 "my-org/upstream/feature",
640 ]);
641 });
642});
643​
644describe("resolveBranchSelectionTarget", () => {
645 it("reuses an existing secondary worktree for the selected ref", () => {
646 expect(
647 resolveBranchSelectionTarget({
648 activeProjectCwd: "/repo",
649 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
650 refName: {
651 isDefault: false,
652 worktreePath: "/repo/.t3/worktrees/feature-b",
653 },
654 }),
655 ).toEqual({
656 checkoutCwd: "/repo/.t3/worktrees/feature-b",
657 nextWorktreePath: "/repo/.t3/worktrees/feature-b",
658 reuseExistingWorktree: true,
659 });
660 });
661​
662 it("switches back to the main repo when the ref already lives there", () => {
663 expect(
664 resolveBranchSelectionTarget({
665 activeProjectCwd: "/repo",
666 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
667 refName: {
668 isDefault: true,
669 worktreePath: "/repo",
670 },
671 }),
672 ).toEqual({
673 checkoutCwd: "/repo",
674 nextWorktreePath: null,
675 reuseExistingWorktree: true,
676 });
677 });
678​
679 it("checks out the default ref in the main repo when leaving a secondary worktree", () => {
680 expect(
681 resolveBranchSelectionTarget({
682 activeProjectCwd: "/repo",
683 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
684 refName: {
685 isDefault: true,
686 worktreePath: null,
687 },
688 }),
689 ).toEqual({
690 checkoutCwd: "/repo",
691 nextWorktreePath: null,
692 reuseExistingWorktree: false,
693 });
694 });
695​
696 it("keeps checkout in the current worktree for non-default refs", () => {
697 expect(
698 resolveBranchSelectionTarget({
699 activeProjectCwd: "/repo",
700 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
701 refName: {
702 isDefault: false,
703 worktreePath: null,
704 },
705 }),
706 ).toEqual({
707 checkoutCwd: "/repo/.t3/worktrees/feature-a",
708 nextWorktreePath: "/repo/.t3/worktrees/feature-a",
709 reuseExistingWorktree: false,
710 });
711 });
712});
713​
714describe("shouldIncludeBranchPickerItem", () => {
715 it("keeps the synthetic checkout PR item visible for gh pr checkout input", () => {
716 expect(
717 shouldIncludeBranchPickerItem({
718 itemValue: "__checkout_pull_request__:1359",
719 normalizedQuery: "gh pr checkout 1359",
720 createBranchItemValue: "__create_new_branch__:gh pr checkout 1359",
721 checkoutPullRequestItemValue: "__checkout_pull_request__:1359",
722 }),
723 ).toBe(true);
724 });
725​
726 it("keeps the synthetic create-ref item visible for arbitrary ref input", () => {
727 expect(
728 shouldIncludeBranchPickerItem({
729 itemValue: "__create_new_branch__:feature/demo",
730 normalizedQuery: "feature/demo",
731 createBranchItemValue: "__create_new_branch__:feature/demo",
732 checkoutPullRequestItemValue: null,
733 }),
734 ).toBe(true);
735 });
736​
737 it("still filters ordinary ref items by query text", () => {
738 expect(
739 shouldIncludeBranchPickerItem({
740 itemValue: "main",
741 normalizedQuery: "gh pr checkout 1359",
742 createBranchItemValue: "__create_new_branch__:gh pr checkout 1359",
743 checkoutPullRequestItemValue: "__checkout_pull_request__:1359",
744 }),
745 ).toBe(false);
746 });
747​
748 // Typing a spaced name must still surface the ref it would have been created
749 // as, or the picker shows nothing at all for that query.
750 it("surfaces an existing ref matching the sanitized query", () => {
751 expect(
752 shouldIncludeBranchPickerItem({
753 itemValue: "new-branch",
754 normalizedQuery: "new branch",
755 createBranchItemValue: null,
756 checkoutPullRequestItemValue: null,
757 }),
758 ).toBe(true);
759 });
760​
761 // A partial query has to reach the ref it would have been created as, so
762 // searching "hello w" still finds an existing hello-world.
763 it("surfaces a ref from a partial query containing a space", () => {
764 expect(
765 shouldIncludeBranchPickerItem({
766 itemValue: "hello-world",
767 normalizedQuery: "hello w",
768 createBranchItemValue: null,
769 checkoutPullRequestItemValue: null,
770 }),
771 ).toBe(true);
772 });
773​
774 it("excludes refs matching neither the raw nor the sanitized query", () => {
775 expect(
776 shouldIncludeBranchPickerItem({
777 itemValue: "main",
778 normalizedQuery: "new branch",
779 createBranchItemValue: null,
780 checkoutPullRequestItemValue: null,
781 }),
782 ).toBe(false);
783 });
784});
785​
786// Git rejects ASCII space and the ASCII control characters in ref names, so a
787// typed name like "new branch" can only ever fail. Replacing exactly those can
788// turn a failing name into a working one without touching a name git already
789// accepts, including one holding non-ASCII whitespace such as U+00A0.
790describe("sanitizeNewRefName", () => {
791 it("replaces a space with a dash", () => {
792 expect(sanitizeNewRefName("new branch")).toBe("new-branch");
793 });
794​
795 it("collapses a run of whitespace into a single dash", () => {
796 expect(sanitizeNewRefName("new branch")).toBe("new-branch");
797 });
798​
799 it("trims surrounding whitespace instead of turning it into dashes", () => {
800 expect(sanitizeNewRefName(" new branch ")).toBe("new-branch");
801 });
802​
803 it("replaces tabs, which git rejects just like spaces", () => {
804 expect(sanitizeNewRefName("new\tbranch")).toBe("new-branch");
805 });
806​
807 // git accepts U+00A0, U+2009 and other non-ASCII whitespace in ref names, so
808 // rewriting them would silently create a ref the user never typed.
809 it("preserves whitespace that git accepts", () => {
810 expect(sanitizeNewRefName("new\u00a0branch")).toBe("new\u00a0branch");
811 expect(sanitizeNewRefName("new\u2009branch")).toBe("new\u2009branch");
812 });
813​
814 it("keeps slashes so nested ref names survive", () => {
815 expect(sanitizeNewRefName("feature/new thing")).toBe("feature/new-thing");
816 });
817​
818 it("preserves case because git ref names are case sensitive", () => {
819 expect(sanitizeNewRefName("Feature/New Thing")).toBe("Feature/New-Thing");
820 });
821​
822 it("leaves an already valid ref name untouched", () => {
823 expect(sanitizeNewRefName("feature/login")).toBe("feature/login");
824 });
825​
826 it("returns an empty string for whitespace-only input", () => {
827 expect(sanitizeNewRefName(" ")).toBe("");
828 });
829​
830 // Scoped deliberately to whitespace: git accepts consecutive dashes, so
831 // collapsing them would rewrite names the user may have typed on purpose.
832 it("does not collapse dashes the user typed", () => {
833 expect(sanitizeNewRefName("new - branch")).toBe("new---branch");
834 expect(sanitizeNewRefName("foo--bar")).toBe("foo--bar");
835 });
836});