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

BranchToolbar.logic.test.ts

732 lines21.2 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 shouldIncludeBranchPickerItem,
20 shouldShowComposerContextStrip,
21 shouldShowEnvironmentIndicator,
22} from "./BranchToolbar.logic";
23​
24const localEnvironmentId = EnvironmentId.make("environment-local");
25const remoteEnvironmentId = EnvironmentId.make("environment-remote");
26​
27describe("resolvePreviousWorktreeSeed", () => {
28 it("picks the most recently updated worktree thread", () => {
29 expect(
30 resolvePreviousWorktreeSeed({
31 threads: [
32 {
33 branch: "t3/older",
34 worktreePath: "/repo/.t3/worktrees/older",
35 updatedAt: "2026-07-20T00:00:00.000Z",
36 },
37 {
38 branch: "t3/newer",
39 worktreePath: "/repo/.t3/worktrees/newer",
40 updatedAt: "2026-07-22T00:00:00.000Z",
41 },
42 { branch: "main", worktreePath: null, updatedAt: "2026-07-23T00:00:00.000Z" },
43 ],
44 currentWorktreePath: null,
45 }),
46 ).toEqual({ branch: "t3/newer", worktreePath: "/repo/.t3/worktrees/newer" });
47 });
48​
49 it("skips the worktree the composer already points at", () => {
50 expect(
51 resolvePreviousWorktreeSeed({
52 threads: [
53 {
54 branch: "t3/current",
55 worktreePath: "/repo/.t3/worktrees/current",
56 updatedAt: "2026-07-22T00:00:00.000Z",
57 },
58 ],
59 currentWorktreePath: "/repo/.t3/worktrees/current",
60 }),
61 ).toBeNull();
62 });
63​
64 it("returns null when no thread has a worktree", () => {
65 expect(
66 resolvePreviousWorktreeSeed({
67 threads: [{ branch: "main", worktreePath: null, updatedAt: "2026-07-22T00:00:00.000Z" }],
68 currentWorktreePath: null,
69 }),
70 ).toBeNull();
71 });
72​
73 it("ignores archived threads and threads with unparseable timestamps", () => {
74 expect(
75 resolvePreviousWorktreeSeed({
76 threads: [
77 {
78 branch: "t3/archived",
79 worktreePath: "/repo/.t3/worktrees/archived",
80 updatedAt: "2026-07-23T00:00:00.000Z",
81 archivedAt: "2026-07-23T01:00:00.000Z",
82 },
83 {
84 branch: "t3/garbage-timestamp",
85 worktreePath: "/repo/.t3/worktrees/garbage",
86 updatedAt: "not-a-date",
87 },
88 {
89 branch: "t3/live",
90 worktreePath: "/repo/.t3/worktrees/live",
91 updatedAt: "2026-07-21T00:00:00.000Z",
92 archivedAt: null,
93 },
94 ],
95 currentWorktreePath: null,
96 }),
97 ).toEqual({ branch: "t3/live", worktreePath: "/repo/.t3/worktrees/live" });
98 });
99});
100​
101describe("resolvePreviousWorktreeLabel", () => {
102 it("includes the branch when known", () => {
103 expect(resolvePreviousWorktreeLabel({ branch: "t3/fix-thing", worktreePath: "/wt" })).toBe(
104 "Previous worktree (t3/fix-thing)",
105 );
106 expect(resolvePreviousWorktreeLabel({ branch: null, worktreePath: "/wt" })).toBe(
107 "Previous worktree",
108 );
109 });
110});
111​
112describe("resolveDraftEnvModeAfterBranchChange", () => {
113 it("switches to local mode when returning from an existing worktree to the main worktree", () => {
114 expect(
115 resolveDraftEnvModeAfterBranchChange({
116 nextWorktreePath: null,
117 currentWorktreePath: "/repo/.t3/worktrees/feature-a",
118 effectiveEnvMode: "worktree",
119 }),
120 ).toBe("local");
121 });
122​
123 it("keeps new-worktree mode when selecting a base ref before worktree creation", () => {
124 expect(
125 resolveDraftEnvModeAfterBranchChange({
126 nextWorktreePath: null,
127 currentWorktreePath: null,
128 effectiveEnvMode: "worktree",
129 }),
130 ).toBe("worktree");
131 });
132​
133 it("uses worktree mode when selecting a ref already attached to a worktree", () => {
134 expect(
135 resolveDraftEnvModeAfterBranchChange({
136 nextWorktreePath: "/repo/.t3/worktrees/feature-a",
137 currentWorktreePath: null,
138 effectiveEnvMode: "local",
139 }),
140 ).toBe("worktree");
141 });
142});
143​
144describe("resolveBranchToolbarValue", () => {
145 it("defaults new-worktree mode to current git ref when no explicit base ref is set", () => {
146 expect(
147 resolveBranchToolbarValue({
148 envMode: "worktree",
149 activeWorktreePath: null,
150 activeThreadBranch: null,
151 currentGitBranch: "main",
152 }),
153 ).toBe("main");
154 });
155​
156 it("keeps an explicitly selected worktree base ref", () => {
157 expect(
158 resolveBranchToolbarValue({
159 envMode: "worktree",
160 activeWorktreePath: null,
161 activeThreadBranch: "feature/base",
162 currentGitBranch: "main",
163 }),
164 ).toBe("feature/base");
165 });
166​
167 it("shows the actual checked-out ref when not selecting a new worktree base", () => {
168 expect(
169 resolveBranchToolbarValue({
170 envMode: "local",
171 activeWorktreePath: null,
172 activeThreadBranch: "feature/base",
173 currentGitBranch: "main",
174 }),
175 ).toBe("main");
176 });
177});
178​
179describe("resolveBranchTriggerLabel", () => {
180 it("shows the origin ref when a new worktree will start from origin", () => {
181 expect(
182 resolveBranchTriggerLabel({
183 activeWorktreePath: null,
184 effectiveEnvMode: "worktree",
185 resolvedActiveBranch: "main",
186 resolvedActiveBranchIsRemote: false,
187 startFromOrigin: true,
188 }),
189 ).toBe("From origin/main");
190 });
191​
192 it("shows the origin ref for local branch names that contain slashes", () => {
193 expect(
194 resolveBranchTriggerLabel({
195 activeWorktreePath: null,
196 effectiveEnvMode: "worktree",
197 resolvedActiveBranch: "feature/demo",
198 resolvedActiveBranchIsRemote: false,
199 startFromOrigin: true,
200 }),
201 ).toBe("From origin/feature/demo");
202 });
203​
204 it("shows the local ref when start from origin is disabled", () => {
205 expect(
206 resolveBranchTriggerLabel({
207 activeWorktreePath: null,
208 effectiveEnvMode: "worktree",
209 resolvedActiveBranch: "main",
210 resolvedActiveBranchIsRemote: false,
211 startFromOrigin: false,
212 }),
213 ).toBe("From main");
214 });
215​
216 it("does not duplicate the origin prefix for an explicit remote ref", () => {
217 expect(
218 resolveBranchTriggerLabel({
219 activeWorktreePath: null,
220 effectiveEnvMode: "worktree",
221 resolvedActiveBranch: "origin/feature/demo",
222 resolvedActiveBranchIsRemote: true,
223 startFromOrigin: true,
224 }),
225 ).toBe("From origin/feature/demo");
226 });
227​
228 it("preserves an explicit ref from a non-origin remote", () => {
229 expect(
230 resolveBranchTriggerLabel({
231 activeWorktreePath: null,
232 effectiveEnvMode: "worktree",
233 resolvedActiveBranch: "upstream/feature/demo",
234 resolvedActiveBranchIsRemote: true,
235 startFromOrigin: true,
236 }),
237 ).toBe("From upstream/feature/demo");
238 });
239​
240 it("keeps current-checkout labels and empty state unchanged", () => {
241 expect(
242 resolveBranchTriggerLabel({
243 activeWorktreePath: null,
244 effectiveEnvMode: "local",
245 resolvedActiveBranch: "main",
246 resolvedActiveBranchIsRemote: false,
247 startFromOrigin: true,
248 }),
249 ).toBe("main");
250 expect(
251 resolveBranchTriggerLabel({
252 activeWorktreePath: null,
253 effectiveEnvMode: "worktree",
254 resolvedActiveBranch: null,
255 resolvedActiveBranchIsRemote: null,
256 startFromOrigin: true,
257 }),
258 ).toBe("Select ref");
259 });
260​
261 it("does not fabricate an origin ref while branch metadata is loading", () => {
262 expect(
263 resolveBranchTriggerLabel({
264 activeWorktreePath: null,
265 effectiveEnvMode: "worktree",
266 resolvedActiveBranch: "upstream/feature/demo",
267 resolvedActiveBranchIsRemote: null,
268 startFromOrigin: true,
269 }),
270 ).toBe("From upstream/feature/demo");
271 });
272});
273​
274describe("resolveBranchToolbarPrBranch", () => {
275 it("uses the explicit thread branch when it matches the displayed branch", () => {
276 expect(
277 resolveBranchToolbarPrBranch({
278 activeThreadBranch: "feature/current",
279 resolvedActiveBranch: "feature/current",
280 }),
281 ).toBe("feature/current");
282 });
283​
284 it("hides PR state while an optimistic branch switch is in flight", () => {
285 expect(
286 resolveBranchToolbarPrBranch({
287 activeThreadBranch: "feature/current",
288 resolvedActiveBranch: "feature/next",
289 }),
290 ).toBeNull();
291 });
292​
293 it("does not infer PR state without an explicit thread branch", () => {
294 expect(
295 resolveBranchToolbarPrBranch({
296 activeThreadBranch: null,
297 resolvedActiveBranch: "feature/current",
298 }),
299 ).toBeNull();
300 });
301});
302​
303describe("resolveLocalCheckoutBranchMismatch", () => {
304 it("detects when a local thread is associated with a different branch than the checkout", () => {
305 expect(
306 resolveLocalCheckoutBranchMismatch({
307 effectiveEnvMode: "local",
308 activeWorktreePath: null,
309 activeThreadBranch: "feature/thread",
310 currentGitBranch: "feature/current",
311 }),
312 ).toEqual({
313 threadBranch: "feature/thread",
314 currentBranch: "feature/current",
315 });
316 });
317​
318 it("ignores matching local checkout state", () => {
319 expect(
320 resolveLocalCheckoutBranchMismatch({
321 effectiveEnvMode: "local",
322 activeWorktreePath: null,
323 activeThreadBranch: "feature/thread",
324 currentGitBranch: "feature/thread",
325 }),
326 ).toBeNull();
327 });
328​
329 it("ignores dedicated worktrees because their checkout is already thread-scoped", () => {
330 expect(
331 resolveLocalCheckoutBranchMismatch({
332 effectiveEnvMode: "worktree",
333 activeWorktreePath: "/repo/.t3/worktrees/feature-thread",
334 activeThreadBranch: "feature/thread",
335 currentGitBranch: "feature/current",
336 }),
337 ).toBeNull();
338 });
339​
340 it("ignores new-worktree base selection before a worktree exists", () => {
341 expect(
342 resolveLocalCheckoutBranchMismatch({
343 effectiveEnvMode: "worktree",
344 activeWorktreePath: null,
345 activeThreadBranch: "feature/base",
346 currentGitBranch: "main",
347 }),
348 ).toBeNull();
349 });
350});
351​
352describe("resolveEnvironmentOptionLabel", () => {
353 it("prefers the primary environment's machine label", () => {
354 expect(
355 resolveEnvironmentOptionLabel({
356 isPrimary: true,
357 environmentId: localEnvironmentId,
358 runtimeLabel: "Julius's Mac mini",
359 savedLabel: "Local environment",
360 }),
361 ).toBe("Julius's Mac mini");
362 });
363​
364 it("falls back to 'This device' for generic primary labels", () => {
365 expect(
366 resolveEnvironmentOptionLabel({
367 isPrimary: true,
368 environmentId: localEnvironmentId,
369 runtimeLabel: "Local environment",
370 savedLabel: "Local",
371 }),
372 ).toBe("This device");
373 });
374​
375 it("keeps configured labels for non-primary environments", () => {
376 expect(
377 resolveEnvironmentOptionLabel({
378 isPrimary: false,
379 environmentId: remoteEnvironmentId,
380 runtimeLabel: null,
381 savedLabel: "Build box",
382 }),
383 ).toBe("Build box");
384 });
385});
386​
387describe("shouldShowEnvironmentIndicator", () => {
388 it("shows the indicator whenever multiple environments are pickable", () => {
389 expect(
390 shouldShowEnvironmentIndicator({
391 activeEnvironment: { isPrimary: true },
392 canPickEnvironment: true,
393 }),
394 ).toBe(true);
395 });
396​
397 it("shows a sole remote environment so the user knows where the project runs", () => {
398 expect(
399 shouldShowEnvironmentIndicator({
400 activeEnvironment: { isPrimary: false },
401 canPickEnvironment: false,
402 }),
403 ).toBe(true);
404 });
405​
406 it("hides a sole primary (this-device) environment", () => {
407 expect(
408 shouldShowEnvironmentIndicator({
409 activeEnvironment: { isPrimary: true },
410 canPickEnvironment: false,
411 }),
412 ).toBe(false);
413 });
414​
415 it("hides the indicator when the active environment is unknown", () => {
416 expect(
417 shouldShowEnvironmentIndicator({
418 activeEnvironment: null,
419 canPickEnvironment: false,
420 }),
421 ).toBe(false);
422 });
423});
424​
425describe("shouldShowComposerContextStrip", () => {
426 it("keeps the environment indicator visible for a non-Git project", () => {
427 expect(
428 shouldShowComposerContextStrip({
429 hasActiveProject: true,
430 isGitRepo: false,
431 showEnvironmentIndicator: true,
432 }),
433 ).toBe(true);
434 });
435​
436 it("hides the strip when a non-Git project has no environment indicator", () => {
437 expect(
438 shouldShowComposerContextStrip({
439 hasActiveProject: true,
440 isGitRepo: false,
441 showEnvironmentIndicator: false,
442 }),
443 ).toBe(false);
444 });
445​
446 it("shows Git controls without requiring an environment indicator", () => {
447 expect(
448 shouldShowComposerContextStrip({
449 hasActiveProject: true,
450 isGitRepo: true,
451 showEnvironmentIndicator: false,
452 }),
453 ).toBe(true);
454 });
455});
456​
457describe("resolveEffectiveEnvMode", () => {
458 it("treats draft threads already attached to a worktree as current-checkout mode", () => {
459 expect(
460 resolveEffectiveEnvMode({
461 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
462 hasServerThread: false,
463 draftThreadEnvMode: "worktree",
464 }),
465 ).toBe("local");
466 });
467​
468 it("keeps explicit new-worktree mode for draft threads without a worktree path", () => {
469 expect(
470 resolveEffectiveEnvMode({
471 activeWorktreePath: null,
472 hasServerThread: false,
473 draftThreadEnvMode: "worktree",
474 }),
475 ).toBe("worktree");
476 });
477});
478​
479describe("resolveEnvModeLabel", () => {
480 it("uses explicit workspace labels", () => {
481 expect(resolveEnvModeLabel("local")).toBe("Current checkout");
482 expect(resolveEnvModeLabel("worktree")).toBe("New worktree");
483 });
484});
485​
486describe("resolveCurrentWorkspaceLabel", () => {
487 it("describes the main repo checkout when no worktree path is active", () => {
488 expect(resolveCurrentWorkspaceLabel(null)).toBe("Current checkout");
489 });
490​
491 it("describes the active checkout as a worktree when one is attached", () => {
492 expect(resolveCurrentWorkspaceLabel("/repo/.t3/worktrees/feature-a")).toBe("Current worktree");
493 });
494});
495​
496describe("resolveLockedWorkspaceLabel", () => {
497 it("uses a shorter label for the main repo checkout", () => {
498 expect(resolveLockedWorkspaceLabel(null)).toBe("Local checkout");
499 });
500​
501 it("uses a shorter label for an attached worktree", () => {
502 expect(resolveLockedWorkspaceLabel("/repo/.t3/worktrees/feature-a")).toBe("Worktree");
503 });
504});
505​
506describe("deriveLocalBranchNameFromRemoteRef", () => {
507 it("strips the remote prefix from a remote ref", () => {
508 expect(deriveLocalBranchNameFromRemoteRef("origin/feature/demo")).toBe("feature/demo");
509 });
510​
511 it("supports remote names that contain slashes", () => {
512 expect(deriveLocalBranchNameFromRemoteRef("my-org/upstream/feature/demo")).toBe(
513 "upstream/feature/demo",
514 );
515 });
516​
517 it("returns the original name when ref is malformed", () => {
518 expect(deriveLocalBranchNameFromRemoteRef("origin/")).toBe("origin/");
519 expect(deriveLocalBranchNameFromRemoteRef("/feature/demo")).toBe("/feature/demo");
520 });
521});
522​
523describe("dedupeRemoteBranchesWithLocalMatches", () => {
524 it("hides remote refs when the matching local ref exists", () => {
525 const input: VcsRef[] = [
526 {
527 name: "feature/demo",
528 current: false,
529 isDefault: false,
530 worktreePath: null,
531 },
532 {
533 name: "origin/feature/demo",
534 isRemote: true,
535 remoteName: "origin",
536 current: false,
537 isDefault: false,
538 worktreePath: null,
539 },
540 {
541 name: "origin/feature/remote-only",
542 isRemote: true,
543 remoteName: "origin",
544 current: false,
545 isDefault: false,
546 worktreePath: null,
547 },
548 ];
549​
550 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
551 "feature/demo",
552 "origin/feature/remote-only",
553 ]);
554 });
555​
556 it("keeps all entries when no local match exists for a remote ref", () => {
557 const input: VcsRef[] = [
558 {
559 name: "feature/local",
560 current: false,
561 isDefault: false,
562 worktreePath: null,
563 },
564 {
565 name: "origin/feature/remote-only",
566 isRemote: true,
567 remoteName: "origin",
568 current: false,
569 isDefault: false,
570 worktreePath: null,
571 },
572 ];
573​
574 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
575 "feature/local",
576 "origin/feature/remote-only",
577 ]);
578 });
579​
580 it("keeps non-origin remote refs visible even when a matching local ref exists", () => {
581 const input: VcsRef[] = [
582 {
583 name: "feature/demo",
584 current: false,
585 isDefault: false,
586 worktreePath: null,
587 },
588 {
589 name: "my-org/upstream/feature/demo",
590 isRemote: true,
591 remoteName: "my-org/upstream",
592 current: false,
593 isDefault: false,
594 worktreePath: null,
595 },
596 ];
597​
598 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
599 "feature/demo",
600 "my-org/upstream/feature/demo",
601 ]);
602 });
603​
604 it("keeps non-origin remote refs visible when git tracks with first-slash local naming", () => {
605 const input: VcsRef[] = [
606 {
607 name: "upstream/feature",
608 current: false,
609 isDefault: false,
610 worktreePath: null,
611 },
612 {
613 name: "my-org/upstream/feature",
614 isRemote: true,
615 remoteName: "my-org/upstream",
616 current: false,
617 isDefault: false,
618 worktreePath: null,
619 },
620 ];
621​
622 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
623 "upstream/feature",
624 "my-org/upstream/feature",
625 ]);
626 });
627});
628​
629describe("resolveBranchSelectionTarget", () => {
630 it("reuses an existing secondary worktree for the selected ref", () => {
631 expect(
632 resolveBranchSelectionTarget({
633 activeProjectCwd: "/repo",
634 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
635 refName: {
636 isDefault: false,
637 worktreePath: "/repo/.t3/worktrees/feature-b",
638 },
639 }),
640 ).toEqual({
641 checkoutCwd: "/repo/.t3/worktrees/feature-b",
642 nextWorktreePath: "/repo/.t3/worktrees/feature-b",
643 reuseExistingWorktree: true,
644 });
645 });
646​
647 it("switches back to the main repo when the ref already lives there", () => {
648 expect(
649 resolveBranchSelectionTarget({
650 activeProjectCwd: "/repo",
651 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
652 refName: {
653 isDefault: true,
654 worktreePath: "/repo",
655 },
656 }),
657 ).toEqual({
658 checkoutCwd: "/repo",
659 nextWorktreePath: null,
660 reuseExistingWorktree: true,
661 });
662 });
663​
664 it("checks out the default ref in the main repo when leaving a secondary worktree", () => {
665 expect(
666 resolveBranchSelectionTarget({
667 activeProjectCwd: "/repo",
668 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
669 refName: {
670 isDefault: true,
671 worktreePath: null,
672 },
673 }),
674 ).toEqual({
675 checkoutCwd: "/repo",
676 nextWorktreePath: null,
677 reuseExistingWorktree: false,
678 });
679 });
680​
681 it("keeps checkout in the current worktree for non-default refs", () => {
682 expect(
683 resolveBranchSelectionTarget({
684 activeProjectCwd: "/repo",
685 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
686 refName: {
687 isDefault: false,
688 worktreePath: null,
689 },
690 }),
691 ).toEqual({
692 checkoutCwd: "/repo/.t3/worktrees/feature-a",
693 nextWorktreePath: "/repo/.t3/worktrees/feature-a",
694 reuseExistingWorktree: false,
695 });
696 });
697});
698​
699describe("shouldIncludeBranchPickerItem", () => {
700 it("keeps the synthetic checkout PR item visible for gh pr checkout input", () => {
701 expect(
702 shouldIncludeBranchPickerItem({
703 itemValue: "__checkout_pull_request__:1359",
704 normalizedQuery: "gh pr checkout 1359",
705 createBranchItemValue: "__create_new_branch__:gh pr checkout 1359",
706 checkoutPullRequestItemValue: "__checkout_pull_request__:1359",
707 }),
708 ).toBe(true);
709 });
710​
711 it("keeps the synthetic create-ref item visible for arbitrary ref input", () => {
712 expect(
713 shouldIncludeBranchPickerItem({
714 itemValue: "__create_new_branch__:feature/demo",
715 normalizedQuery: "feature/demo",
716 createBranchItemValue: "__create_new_branch__:feature/demo",
717 checkoutPullRequestItemValue: null,
718 }),
719 ).toBe(true);
720 });
721​
722 it("still filters ordinary ref items by query text", () => {
723 expect(
724 shouldIncludeBranchPickerItem({
725 itemValue: "main",
726 normalizedQuery: "gh pr checkout 1359",
727 createBranchItemValue: "__create_new_branch__:gh pr checkout 1359",
728 checkoutPullRequestItemValue: "__checkout_pull_request__:1359",
729 }),
730 ).toBe(false);
731 });
732});