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

BranchToolbar.logic.test.ts

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