Open History to see the last commit
1import type { GitStatusResult } from "@t3tools/contracts";
2import { assert, describe, it } from "vitest";
3import {
4 buildGitActionProgressStages,
5 buildMenuItems,
6 requiresDefaultBranchConfirmation,
7 resolveAutoFeatureBranchName,
8 resolveDefaultBranchActionDialogCopy,
9 resolveLiveThreadBranchUpdate,
10 resolveQuickAction,
11 resolveThreadBranchUpdate,
12} from "./GitActionsControl.logic";
13
14function status(overrides: Partial<GitStatusResult> = {}): GitStatusResult {
15 return {
16 branch: "feature/test",
17 hasWorkingTreeChanges: false,
18 workingTree: {
19 files: [],
20 insertions: 0,
21 deletions: 0,
22 },
23 hasUpstream: true,
24 aheadCount: 0,
25 behindCount: 0,
26 pr: null,
27 ...overrides,
28 };
29}
30
31describe("when: branch is clean and has an open PR", () => {
32 it("resolveQuickAction opens the existing PR", () => {
33 const quick = resolveQuickAction(
34 status({
35 pr: {
36 number: 10,
37 title: "Open PR",
38 url: "https://example.com/pr/10",
39 baseBranch: "main",
40 headBranch: "feature/test",
41 state: "open",
42 },
43 }),
44 false,
45 );
46 assert.deepInclude(quick, { kind: "open_pr", label: "View PR", disabled: false });
47 });
48
49 it("buildMenuItems disables commit/push and enables open PR", () => {
50 const items = buildMenuItems(
51 status({
52 pr: {
53 number: 11,
54 title: "Existing PR",
55 url: "https://example.com/pr/11",
56 baseBranch: "main",
57 headBranch: "feature/test",
58 state: "open",
59 },
60 }),
61 false,
62 );
63 assert.deepEqual(items, [
64 {
65 id: "commit",
66 label: "Commit",
67 disabled: true,
68 icon: "commit",
69 kind: "open_dialog",
70 dialogAction: "commit",
71 },
72 {
73 id: "push",
74 label: "Push",
75 disabled: true,
76 icon: "push",
77 kind: "open_dialog",
78 dialogAction: "push",
79 },
80 {
81 id: "pr",
82 label: "View PR",
83 disabled: false,
84 icon: "pr",
85 kind: "open_pr",
86 },
87 ]);
88 });
89});
90
91describe("when: actions are busy", () => {
92 it("resolveQuickAction returns running disabled state", () => {
93 const quick = resolveQuickAction(status(), true);
94 assert.deepInclude(quick, {
95 kind: "show_hint",
96 label: "Commit",
97 disabled: true,
98 hint: "Git action in progress.",
99 });
100 });
101
102 it("buildMenuItems disables all actions", () => {
103 const items = buildMenuItems(status(), true);
104 assert.deepEqual(items, [
105 {
106 id: "commit",
107 label: "Commit",
108 disabled: true,
109 icon: "commit",
110 kind: "open_dialog",
111 dialogAction: "commit",
112 },
113 {
114 id: "push",
115 label: "Push",
116 disabled: true,
117 icon: "push",
118 kind: "open_dialog",
119 dialogAction: "push",
120 },
121 {
122 id: "pr",
123 label: "Create PR",
124 disabled: true,
125 icon: "pr",
126 kind: "open_dialog",
127 dialogAction: "create_pr",
128 },
129 ]);
130 });
131});
132
133describe("when: git status is unavailable", () => {
134 it("resolveQuickAction returns unavailable disabled state", () => {
135 const quick = resolveQuickAction(null, false);
136 assert.deepInclude(quick, {
137 kind: "show_hint",
138 label: "Commit",
139 disabled: true,
140 hint: "Git status is unavailable.",
141 });
142 });
143
144 it("buildMenuItems returns no menu items", () => {
145 const items = buildMenuItems(null, false);
146 assert.deepEqual(items, []);
147 });
148});
149
150describe("when: branch is clean, ahead, and has an open PR", () => {
151 it("resolveQuickAction prefers push", () => {
152 const quick = resolveQuickAction(
153 status({
154 aheadCount: 3,
155 pr: {
156 number: 13,
157 title: "Open PR",
158 url: "https://example.com/pr/13",
159 baseBranch: "main",
160 headBranch: "feature/test",
161 state: "open",
162 },
163 }),
164 false,
165 );
166 assert.deepInclude(quick, { kind: "run_action", action: "push", label: "Push" });
167 });
168
169 it("buildMenuItems enables push and keeps open PR available", () => {
170 const items = buildMenuItems(
171 status({
172 aheadCount: 2,
173 pr: {
174 number: 12,
175 title: "Existing PR",
176 url: "https://example.com/pr/12",
177 baseBranch: "main",
178 headBranch: "feature/test",
179 state: "open",
180 },
181 }),
182 false,
183 );
184 assert.deepEqual(items, [
185 {
186 id: "commit",
187 label: "Commit",
188 disabled: true,
189 icon: "commit",
190 kind: "open_dialog",
191 dialogAction: "commit",
192 },
193 {
194 id: "push",
195 label: "Push",
196 disabled: false,
197 icon: "push",
198 kind: "open_dialog",
199 dialogAction: "push",
200 },
201 {
202 id: "pr",
203 label: "View PR",
204 disabled: false,
205 icon: "pr",
206 kind: "open_pr",
207 },
208 ]);
209 });
210});
211
212describe("when: branch is clean, ahead, and has no open PR", () => {
213 it("resolveQuickAction pushes and creates a PR", () => {
214 const quick = resolveQuickAction(status({ aheadCount: 2, pr: null }), false);
215 assert.deepInclude(quick, {
216 kind: "run_action",
217 action: "create_pr",
218 label: "Push & create PR",
219 });
220 });
221
222 it("buildMenuItems enables push and create PR, with commit disabled", () => {
223 const items = buildMenuItems(status({ aheadCount: 2, pr: null }), false);
224 assert.deepEqual(items, [
225 {
226 id: "commit",
227 label: "Commit",
228 disabled: true,
229 icon: "commit",
230 kind: "open_dialog",
231 dialogAction: "commit",
232 },
233 {
234 id: "push",
235 label: "Push",
236 disabled: false,
237 icon: "push",
238 kind: "open_dialog",
239 dialogAction: "push",
240 },
241 {
242 id: "pr",
243 label: "Create PR",
244 disabled: false,
245 icon: "pr",
246 kind: "open_dialog",
247 dialogAction: "create_pr",
248 },
249 ]);
250 });
251});
252
253describe("when: branch is clean, up to date, and has no open PR", () => {
254 it("resolveQuickAction returns disabled no-action state", () => {
255 const quick = resolveQuickAction(
256 status({ aheadCount: 0, behindCount: 0, hasWorkingTreeChanges: false, pr: null }),
257 false,
258 );
259 assert.deepInclude(quick, { kind: "show_hint", label: "Commit", disabled: true });
260 });
261
262 it("buildMenuItems disables commit, push, and create PR", () => {
263 const items = buildMenuItems(status({ aheadCount: 0, behindCount: 0, pr: null }), false);
264 assert.deepEqual(items, [
265 {
266 id: "commit",
267 label: "Commit",
268 disabled: true,
269 icon: "commit",
270 kind: "open_dialog",
271 dialogAction: "commit",
272 },
273 {
274 id: "push",
275 label: "Push",
276 disabled: true,
277 icon: "push",
278 kind: "open_dialog",
279 dialogAction: "push",
280 },
281 {
282 id: "pr",
283 label: "Create PR",
284 disabled: true,
285 icon: "pr",
286 kind: "open_dialog",
287 dialogAction: "create_pr",
288 },
289 ]);
290 });
291});
292
293describe("when: branch is behind upstream", () => {
294 it("resolveQuickAction returns pull", () => {
295 const quick = resolveQuickAction(status({ behindCount: 2 }), false);
296 assert.deepInclude(quick, { kind: "run_pull", label: "Pull", disabled: false });
297 });
298
299 it("buildMenuItems disables push and create PR", () => {
300 const items = buildMenuItems(status({ behindCount: 1, pr: null }), false);
301 assert.deepEqual(items, [
302 {
303 id: "commit",
304 label: "Commit",
305 disabled: true,
306 icon: "commit",
307 kind: "open_dialog",
308 dialogAction: "commit",
309 },
310 {
311 id: "push",
312 label: "Push",
313 disabled: true,
314 icon: "push",
315 kind: "open_dialog",
316 dialogAction: "push",
317 },
318 {
319 id: "pr",
320 label: "Create PR",
321 disabled: true,
322 icon: "pr",
323 kind: "open_dialog",
324 dialogAction: "create_pr",
325 },
326 ]);
327 });
328});
329
330describe("when: branch has diverged from upstream", () => {
331 it("resolveQuickAction returns a disabled sync hint", () => {
332 const quick = resolveQuickAction(status({ aheadCount: 2, behindCount: 1 }), false);
333 assert.deepEqual(quick, {
334 label: "Sync branch",
335 disabled: true,
336 kind: "show_hint",
337 hint: "Branch has diverged from upstream. Rebase/merge first.",
338 });
339 });
340});
341
342describe("when: working tree has local changes", () => {
343 it("resolveQuickAction returns commit, push, and create PR", () => {
344 const quick = resolveQuickAction(status({ hasWorkingTreeChanges: true }), false);
345 assert.deepInclude(quick, {
346 kind: "run_action",
347 action: "commit_push_pr",
348 label: "Commit, push & PR",
349 });
350 });
351
352 it("resolveQuickAction falls back to commit when no origin remote exists", () => {
353 const quick = resolveQuickAction(
354 status({ hasWorkingTreeChanges: true, hasUpstream: false }),
355 false,
356 false,
357 false,
358 );
359 assert.deepInclude(quick, {
360 kind: "run_action",
361 action: "commit",
362 label: "Commit",
363 disabled: false,
364 });
365 });
366
367 it("resolveQuickAction returns commit and push when open PR exists", () => {
368 const quick = resolveQuickAction(
369 status({
370 hasWorkingTreeChanges: true,
371 pr: {
372 number: 16,
373 title: "Existing PR",
374 url: "https://example.com/pr/16",
375 baseBranch: "main",
376 headBranch: "feature/test",
377 state: "open",
378 },
379 }),
380 false,
381 );
382 assert.deepInclude(quick, {
383 kind: "run_action",
384 action: "commit_push",
385 label: "Commit & push",
386 });
387 });
388
389 it("buildMenuItems enables commit and disables push and PR", () => {
390 const items = buildMenuItems(status({ hasWorkingTreeChanges: true }), false);
391 assert.deepEqual(items, [
392 {
393 id: "commit",
394 label: "Commit",
395 disabled: false,
396 icon: "commit",
397 kind: "open_dialog",
398 dialogAction: "commit",
399 },
400 {
401 id: "push",
402 label: "Push",
403 disabled: true,
404 icon: "push",
405 kind: "open_dialog",
406 dialogAction: "push",
407 },
408 {
409 id: "pr",
410 label: "Create PR",
411 disabled: true,
412 icon: "pr",
413 kind: "open_dialog",
414 dialogAction: "create_pr",
415 },
416 ]);
417 });
418});
419
420describe("when: on default branch without open PR", () => {
421 it("resolveQuickAction returns commit and push when local changes exist", () => {
422 const quick = resolveQuickAction(
423 status({ branch: "main", hasWorkingTreeChanges: true }),
424 false,
425 true,
426 );
427 assert.deepInclude(quick, {
428 kind: "run_action",
429 action: "commit_push",
430 label: "Commit & push",
431 disabled: false,
432 });
433 });
434
435 it("resolveQuickAction returns push when branch is ahead", () => {
436 const quick = resolveQuickAction(
437 status({ branch: "main", aheadCount: 2, pr: null }),
438 false,
439 true,
440 );
441 assert.deepInclude(quick, {
442 kind: "run_action",
443 action: "commit_push",
444 label: "Push",
445 disabled: false,
446 });
447 });
448});
449
450describe("when: working tree has local changes and branch is behind upstream", () => {
451 it("resolveQuickAction still prefers commit, push, and create PR", () => {
452 const quick = resolveQuickAction(
453 status({ hasWorkingTreeChanges: true, behindCount: 1 }),
454 false,
455 );
456 assert.deepInclude(quick, {
457 kind: "run_action",
458 action: "commit_push_pr",
459 label: "Commit, push & PR",
460 });
461 });
462
463 it("buildMenuItems enables commit and keeps push and PR disabled", () => {
464 const items = buildMenuItems(status({ hasWorkingTreeChanges: true, behindCount: 2 }), false);
465 assert.deepEqual(items, [
466 {
467 id: "commit",
468 label: "Commit",
469 disabled: false,
470 icon: "commit",
471 kind: "open_dialog",
472 dialogAction: "commit",
473 },
474 {
475 id: "push",
476 label: "Push",
477 disabled: true,
478 icon: "push",
479 kind: "open_dialog",
480 dialogAction: "push",
481 },
482 {
483 id: "pr",
484 label: "Create PR",
485 disabled: true,
486 icon: "pr",
487 kind: "open_dialog",
488 dialogAction: "create_pr",
489 },
490 ]);
491 });
492});
493
494describe("when: HEAD is detached and there are no local changes", () => {
495 it("resolveQuickAction shows detached head hint", () => {
496 const quick = resolveQuickAction(
497 status({ branch: null, hasWorkingTreeChanges: false, hasUpstream: false }),
498 false,
499 );
500 assert.deepInclude(quick, { kind: "show_hint", label: "Commit", disabled: true });
501 });
502
503 it("buildMenuItems keeps commit, push, and PR disabled", () => {
504 const items = buildMenuItems(status({ branch: null, hasWorkingTreeChanges: false }), false);
505 assert.deepEqual(items, [
506 {
507 id: "commit",
508 label: "Commit",
509 disabled: true,
510 icon: "commit",
511 kind: "open_dialog",
512 dialogAction: "commit",
513 },
514 {
515 id: "push",
516 label: "Push",
517 disabled: true,
518 icon: "push",
519 kind: "open_dialog",
520 dialogAction: "push",
521 },
522 {
523 id: "pr",
524 label: "Create PR",
525 disabled: true,
526 icon: "pr",
527 kind: "open_dialog",
528 dialogAction: "create_pr",
529 },
530 ]);
531 });
532});
533
534describe("when: branch has no upstream configured", () => {
535 it("resolveQuickAction is disabled when clean, no upstream, and no local commits are ahead", () => {
536 const quick = resolveQuickAction(
537 status({ hasUpstream: false, pr: null, aheadCount: 0 }),
538 false,
539 );
540 assert.deepInclude(quick, {
541 kind: "show_hint",
542 label: "Push",
543 hint: "No local commits to push.",
544 disabled: true,
545 });
546 });
547
548 it("resolveQuickAction opens PR when clean, no upstream, no local commits are ahead, and PR exists", () => {
549 const quick = resolveQuickAction(
550 status({
551 hasUpstream: false,
552 aheadCount: 0,
553 pr: {
554 number: 14,
555 title: "Existing PR",
556 url: "https://example.com/pr/14",
557 baseBranch: "main",
558 headBranch: "feature/test",
559 state: "open",
560 },
561 }),
562 false,
563 );
564 assert.deepInclude(quick, {
565 kind: "open_pr",
566 label: "View PR",
567 disabled: false,
568 });
569 });
570
571 it("resolveQuickAction runs push when clean, no upstream, and local commits are ahead", () => {
572 const quick = resolveQuickAction(
573 status({
574 hasUpstream: false,
575 aheadCount: 1,
576 pr: {
577 number: 15,
578 title: "Existing PR",
579 url: "https://example.com/pr/15",
580 baseBranch: "main",
581 headBranch: "feature/test",
582 state: "open",
583 },
584 }),
585 false,
586 );
587 assert.deepInclude(quick, {
588 kind: "run_action",
589 action: "push",
590 label: "Push",
591 disabled: false,
592 });
593 });
594
595 it("buildMenuItems disables push and create PR when no commits are ahead", () => {
596 const items = buildMenuItems(status({ hasUpstream: false, pr: null, aheadCount: 0 }), false);
597 assert.deepEqual(items, [
598 {
599 id: "commit",
600 label: "Commit",
601 disabled: true,
602 icon: "commit",
603 kind: "open_dialog",
604 dialogAction: "commit",
605 },
606 {
607 id: "push",
608 label: "Push",
609 disabled: true,
610 icon: "push",
611 kind: "open_dialog",
612 dialogAction: "push",
613 },
614 {
615 id: "pr",
616 label: "Create PR",
617 disabled: true,
618 icon: "pr",
619 kind: "open_dialog",
620 dialogAction: "create_pr",
621 },
622 ]);
623 });
624
625 it("resolveQuickAction runs push and create PR when no upstream and commits are ahead", () => {
626 const quick = resolveQuickAction(
627 status({
628 hasUpstream: false,
629 aheadCount: 2,
630 pr: null,
631 }),
632 false,
633 );
634 assert.deepInclude(quick, {
635 kind: "run_action",
636 action: "create_pr",
637 label: "Push & create PR",
638 disabled: false,
639 });
640 });
641
642 it("resolveQuickAction disables push-and-pr flows when no origin remote exists", () => {
643 const quick = resolveQuickAction(
644 status({
645 hasUpstream: false,
646 aheadCount: 2,
647 pr: null,
648 }),
649 false,
650 false,
651 false,
652 );
653 assert.deepEqual(quick, {
654 kind: "show_hint",
655 label: "Push",
656 hint: 'Add an "origin" remote before pushing or creating a PR.',
657 disabled: true,
658 });
659 });
660
661 it("buildMenuItems enables create PR when no upstream and commits are ahead", () => {
662 const items = buildMenuItems(status({ hasUpstream: false, pr: null, aheadCount: 2 }), false);
663 assert.deepEqual(items, [
664 {
665 id: "commit",
666 label: "Commit",
667 disabled: true,
668 icon: "commit",
669 kind: "open_dialog",
670 dialogAction: "commit",
671 },
672 {
673 id: "push",
674 label: "Push",
675 disabled: false,
676 icon: "push",
677 kind: "open_dialog",
678 dialogAction: "push",
679 },
680 {
681 id: "pr",
682 label: "Create PR",
683 disabled: false,
684 icon: "pr",
685 kind: "open_dialog",
686 dialogAction: "create_pr",
687 },
688 ]);
689 });
690
691 it("buildMenuItems disables push and create PR when no origin remote exists", () => {
692 const items = buildMenuItems(
693 status({ hasUpstream: false, pr: null, aheadCount: 2 }),
694 false,
695 false,
696 );
697 assert.deepEqual(items, [
698 {
699 id: "commit",
700 label: "Commit",
701 disabled: true,
702 icon: "commit",
703 kind: "open_dialog",
704 dialogAction: "commit",
705 },
706 {
707 id: "push",
708 label: "Push",
709 disabled: true,
710 icon: "push",
711 kind: "open_dialog",
712 dialogAction: "push",
713 },
714 {
715 id: "pr",
716 label: "Create PR",
717 disabled: true,
718 icon: "pr",
719 kind: "open_dialog",
720 dialogAction: "create_pr",
721 },
722 ]);
723 });
724
725 it("resolveQuickAction is disabled on default branch when no upstream exists and no commits are ahead", () => {
726 const quick = resolveQuickAction(
727 status({
728 branch: "main",
729 hasUpstream: false,
730 aheadCount: 0,
731 pr: null,
732 }),
733 false,
734 true,
735 );
736 assert.deepInclude(quick, {
737 kind: "show_hint",
738 label: "Push",
739 hint: "No local commits to push.",
740 disabled: true,
741 });
742 });
743
744 it("resolveQuickAction uses push-only on default branch when no upstream exists and commits are ahead", () => {
745 const quick = resolveQuickAction(
746 status({
747 branch: "main",
748 hasUpstream: false,
749 aheadCount: 1,
750 pr: null,
751 }),
752 false,
753 true,
754 );
755 assert.deepInclude(quick, {
756 kind: "run_action",
757 action: "commit_push",
758 label: "Push",
759 disabled: false,
760 });
761 });
762
763 it("buildMenuItems still disables push and create PR when branch is behind", () => {
764 const items = buildMenuItems(
765 status({
766 hasUpstream: false,
767 behindCount: 1,
768 aheadCount: 0,
769 pr: null,
770 }),
771 false,
772 );
773 assert.deepEqual(items, [
774 {
775 id: "commit",
776 label: "Commit",
777 disabled: true,
778 icon: "commit",
779 kind: "open_dialog",
780 dialogAction: "commit",
781 },
782 {
783 id: "push",
784 label: "Push",
785 disabled: true,
786 icon: "push",
787 kind: "open_dialog",
788 dialogAction: "push",
789 },
790 {
791 id: "pr",
792 label: "Create PR",
793 disabled: true,
794 icon: "pr",
795 kind: "open_dialog",
796 dialogAction: "create_pr",
797 },
798 ]);
799 });
800});
801
802describe("requiresDefaultBranchConfirmation", () => {
803 it("requires confirmation for push actions on default branch", () => {
804 assert.isFalse(requiresDefaultBranchConfirmation("commit", true));
805 assert.isTrue(requiresDefaultBranchConfirmation("push", true));
806 assert.isTrue(requiresDefaultBranchConfirmation("create_pr", true));
807 assert.isTrue(requiresDefaultBranchConfirmation("commit_push", true));
808 assert.isTrue(requiresDefaultBranchConfirmation("commit_push_pr", true));
809 assert.isFalse(requiresDefaultBranchConfirmation("commit_push", false));
810 assert.isFalse(requiresDefaultBranchConfirmation("push", false));
811 });
812});
813
814describe("resolveDefaultBranchActionDialogCopy", () => {
815 it("uses push-only copy when pushing without a commit", () => {
816 const copy = resolveDefaultBranchActionDialogCopy({
817 action: "commit_push",
818 branchName: "main",
819 includesCommit: false,
820 });
821
822 assert.deepEqual(copy, {
823 title: "Push to default branch?",
824 description:
825 'This action will push local commits on "main". You can continue on this branch or create a feature branch and run the same action there.',
826 continueLabel: "Push to main",
827 });
828 });
829
830 it("uses push-and-pr copy when creating a PR without a commit", () => {
831 const copy = resolveDefaultBranchActionDialogCopy({
832 action: "commit_push_pr",
833 branchName: "main",
834 includesCommit: false,
835 });
836
837 assert.deepEqual(copy, {
838 title: "Push & create PR from default branch?",
839 description:
840 'This action will push local commits and create a PR on "main". You can continue on this branch or create a feature branch and run the same action there.',
841 continueLabel: "Push & create PR",
842 });
843 });
844
845 it("keeps commit copy when the action includes a commit", () => {
846 const copy = resolveDefaultBranchActionDialogCopy({
847 action: "commit_push_pr",
848 branchName: "main",
849 includesCommit: true,
850 });
851
852 assert.deepEqual(copy, {
853 title: "Commit, push & create PR from default branch?",
854 description:
855 'This action will commit, push, and create a PR on "main". You can continue on this branch or create a feature branch and run the same action there.',
856 continueLabel: "Commit, push & create PR",
857 });
858 });
859});
860
861describe("buildGitActionProgressStages", () => {
862 it("shows only push progress for explicit push actions", () => {
863 const stages = buildGitActionProgressStages({
864 action: "push",
865 hasCustomCommitMessage: false,
866 hasWorkingTreeChanges: false,
867 pushTarget: "origin/feature/test",
868 });
869 assert.deepEqual(stages, ["Pushing to origin/feature/test..."]);
870 });
871
872 it("shows push and PR progress for create-pr actions that still need a push", () => {
873 const stages = buildGitActionProgressStages({
874 action: "create_pr",
875 hasCustomCommitMessage: false,
876 hasWorkingTreeChanges: false,
877 pushTarget: "origin/feature/test",
878 shouldPushBeforePr: true,
879 });
880 assert.deepEqual(stages, ["Pushing to origin/feature/test...", "Creating PR..."]);
881 });
882
883 it("shows only PR progress when create-pr can skip the push", () => {
884 const stages = buildGitActionProgressStages({
885 action: "create_pr",
886 hasCustomCommitMessage: false,
887 hasWorkingTreeChanges: false,
888 shouldPushBeforePr: false,
889 });
890 assert.deepEqual(stages, ["Creating PR..."]);
891 });
892
893 it("includes commit stages for commit+push when working tree is dirty", () => {
894 const stages = buildGitActionProgressStages({
895 action: "commit_push",
896 hasCustomCommitMessage: false,
897 hasWorkingTreeChanges: true,
898 pushTarget: "origin/feature/test",
899 });
900 assert.deepEqual(stages, [
901 "Generating commit message...",
902 "Committing...",
903 "Pushing to origin/feature/test...",
904 ]);
905 });
906});
907
908describe("resolveThreadBranchUpdate", () => {
909 it("returns a branch update when the action created a new branch", () => {
910 const update = resolveThreadBranchUpdate({
911 action: "commit_push_pr",
912 branch: {
913 status: "created",
914 name: "feature/fix-toast-copy",
915 },
916 commit: {
917 status: "created",
918 commitSha: "89abcdef01234567",
919 subject: "feat: add branch sync",
920 },
921 push: { status: "pushed", branch: "feature/fix-toast-copy" },
922 pr: { status: "skipped_not_requested" },
923 toast: {
924 title: "Pushed 89abcde to origin/feature/fix-toast-copy",
925 cta: { kind: "none" },
926 },
927 });
928
929 assert.deepEqual(update, {
930 branch: "feature/fix-toast-copy",
931 });
932 });
933
934 it("returns null when the action stayed on the existing branch", () => {
935 const update = resolveThreadBranchUpdate({
936 action: "commit_push",
937 branch: {
938 status: "skipped_not_requested",
939 },
940 commit: {
941 status: "created",
942 commitSha: "89abcdef01234567",
943 subject: "feat: add branch sync",
944 },
945 push: { status: "pushed", branch: "feature/fix-toast-copy" },
946 pr: { status: "skipped_not_requested" },
947 toast: {
948 title: "Pushed 89abcde to origin/feature/fix-toast-copy",
949 cta: { kind: "none" },
950 },
951 });
952
953 assert.equal(update, null);
954 });
955});
956
957describe("resolveLiveThreadBranchUpdate", () => {
958 it("returns a branch update when live git status differs from stored thread metadata", () => {
959 const update = resolveLiveThreadBranchUpdate({
960 threadBranch: "feature/old-branch",
961 gitStatus: status({ branch: "effect-atom" }),
962 });
963
964 assert.deepEqual(update, {
965 branch: "effect-atom",
966 });
967 });
968
969 it("returns null when live git status is unavailable", () => {
970 const update = resolveLiveThreadBranchUpdate({
971 threadBranch: "feature/old-branch",
972 gitStatus: null,
973 });
974
975 assert.equal(update, null);
976 });
977
978 it("returns null when the stored thread branch already matches git status", () => {
979 const update = resolveLiveThreadBranchUpdate({
980 threadBranch: "effect-atom",
981 gitStatus: status({ branch: "effect-atom" }),
982 });
983
984 assert.equal(update, null);
985 });
986
987 it("returns null when git status is detached HEAD but the thread already has a branch", () => {
988 const update = resolveLiveThreadBranchUpdate({
989 threadBranch: "effect-atom",
990 gitStatus: status({ branch: null }),
991 });
992
993 assert.equal(update, null);
994 });
995});
996
997describe("resolveAutoFeatureBranchName", () => {
998 it("uses semantic preferred branch names when available", () => {
999 const branch = resolveAutoFeatureBranchName(["main", "feature/other"], "fix toast copy");
1000 assert.equal(branch, "feature/fix-toast-copy");
1001 });
1002
1003 it("normalizes preferred names that already include a branch namespace", () => {
1004 const branch = resolveAutoFeatureBranchName(["main"], "feature/refine-toolbar-actions");
1005 assert.equal(branch, "feature/refine-toolbar-actions");
1006 });
1007
1008 it("increments suffix when the preferred branch name already exists", () => {
1009 const branch = resolveAutoFeatureBranchName(
1010 ["main", "feature/fix-toast-copy", "feature/fix-toast-copy-2"],
1011 "fix toast copy",
1012 );
1013 assert.equal(branch, "feature/fix-toast-copy-3");
1014 });
1015
1016 it("treats existing branch names as case-insensitive for collision checks", () => {
1017 const branch = resolveAutoFeatureBranchName(["Feature/Ticket-1"], "feature/ticket-1");
1018 assert.equal(branch, "feature/ticket-1-2");
1019 });
1020
1021 it("falls back to feature/update when no preferred name is provided", () => {
1022 const branch = resolveAutoFeatureBranchName(["main"]);
1023 assert.equal(branch, "feature/update");
1024 });
1025});