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 resolveQuickAction,
8 summarizeGitResult,
9} from "./GitActionsControl.logic";
10
11function status(overrides: Partial<GitStatusResult> = {}): GitStatusResult {
12 return {
13 branch: "feature/test",
14 hasWorkingTreeChanges: false,
15 workingTree: {
16 files: [],
17 insertions: 0,
18 deletions: 0,
19 },
20 hasUpstream: true,
21 aheadCount: 0,
22 behindCount: 0,
23 pr: null,
24 ...overrides,
25 };
26}
27
28describe("when: branch is clean and has an open PR", () => {
29 it("resolveQuickAction opens the existing PR", () => {
30 const quick = resolveQuickAction(
31 status({
32 pr: {
33 number: 10,
34 title: "Open PR",
35 url: "https://example.com/pr/10",
36 baseBranch: "main",
37 headBranch: "feature/test",
38 state: "open",
39 },
40 }),
41 false,
42 );
43 assert.deepInclude(quick, { kind: "open_pr", label: "Open PR", disabled: false });
44 });
45
46 it("buildMenuItems disables commit/push and enables open PR", () => {
47 const items = buildMenuItems(
48 status({
49 pr: {
50 number: 11,
51 title: "Existing PR",
52 url: "https://example.com/pr/11",
53 baseBranch: "main",
54 headBranch: "feature/test",
55 state: "open",
56 },
57 }),
58 false,
59 );
60 assert.deepEqual(items, [
61 {
62 id: "commit",
63 label: "Commit",
64 disabled: true,
65 icon: "commit",
66 kind: "open_dialog",
67 dialogAction: "commit",
68 },
69 {
70 id: "push",
71 label: "Push",
72 disabled: true,
73 icon: "push",
74 kind: "open_dialog",
75 dialogAction: "push",
76 },
77 {
78 id: "pr",
79 label: "Open PR",
80 disabled: false,
81 icon: "pr",
82 kind: "open_pr",
83 },
84 ]);
85 });
86});
87
88describe("when: actions are busy", () => {
89 it("resolveQuickAction returns running disabled state", () => {
90 const quick = resolveQuickAction(status(), true);
91 assert.deepInclude(quick, {
92 kind: "show_hint",
93 label: "Commit",
94 disabled: true,
95 hint: "Git action in progress.",
96 });
97 });
98
99 it("buildMenuItems disables all actions", () => {
100 const items = buildMenuItems(status(), true);
101 assert.deepEqual(items, [
102 {
103 id: "commit",
104 label: "Commit",
105 disabled: true,
106 icon: "commit",
107 kind: "open_dialog",
108 dialogAction: "commit",
109 },
110 {
111 id: "push",
112 label: "Push",
113 disabled: true,
114 icon: "push",
115 kind: "open_dialog",
116 dialogAction: "push",
117 },
118 {
119 id: "pr",
120 label: "Create PR",
121 disabled: true,
122 icon: "pr",
123 kind: "open_dialog",
124 dialogAction: "create_pr",
125 },
126 ]);
127 });
128});
129
130describe("when: git status is unavailable", () => {
131 it("resolveQuickAction returns unavailable disabled state", () => {
132 const quick = resolveQuickAction(null, false);
133 assert.deepInclude(quick, {
134 kind: "show_hint",
135 label: "Commit",
136 disabled: true,
137 hint: "Git status is unavailable.",
138 });
139 });
140
141 it("buildMenuItems returns no menu items", () => {
142 const items = buildMenuItems(null, false);
143 assert.deepEqual(items, []);
144 });
145});
146
147describe("when: branch is clean, ahead, and has an open PR", () => {
148 it("resolveQuickAction prefers push", () => {
149 const quick = resolveQuickAction(
150 status({
151 aheadCount: 3,
152 pr: {
153 number: 13,
154 title: "Open PR",
155 url: "https://example.com/pr/13",
156 baseBranch: "main",
157 headBranch: "feature/test",
158 state: "open",
159 },
160 }),
161 false,
162 );
163 assert.deepInclude(quick, { kind: "run_action", action: "commit_push", label: "Push" });
164 });
165
166 it("buildMenuItems enables push and keeps open PR available", () => {
167 const items = buildMenuItems(
168 status({
169 aheadCount: 2,
170 pr: {
171 number: 12,
172 title: "Existing PR",
173 url: "https://example.com/pr/12",
174 baseBranch: "main",
175 headBranch: "feature/test",
176 state: "open",
177 },
178 }),
179 false,
180 );
181 assert.deepEqual(items, [
182 {
183 id: "commit",
184 label: "Commit",
185 disabled: true,
186 icon: "commit",
187 kind: "open_dialog",
188 dialogAction: "commit",
189 },
190 {
191 id: "push",
192 label: "Push",
193 disabled: false,
194 icon: "push",
195 kind: "open_dialog",
196 dialogAction: "push",
197 },
198 {
199 id: "pr",
200 label: "Open PR",
201 disabled: false,
202 icon: "pr",
203 kind: "open_pr",
204 },
205 ]);
206 });
207});
208
209describe("when: branch is clean, ahead, and has no open PR", () => {
210 it("resolveQuickAction pushes and creates a PR", () => {
211 const quick = resolveQuickAction(status({ aheadCount: 2, pr: null }), false);
212 assert.deepInclude(quick, {
213 kind: "run_action",
214 action: "commit_push_pr",
215 label: "Push & create PR",
216 });
217 });
218
219 it("buildMenuItems enables push and create PR, with commit disabled", () => {
220 const items = buildMenuItems(status({ aheadCount: 2, pr: null }), false);
221 assert.deepEqual(items, [
222 {
223 id: "commit",
224 label: "Commit",
225 disabled: true,
226 icon: "commit",
227 kind: "open_dialog",
228 dialogAction: "commit",
229 },
230 {
231 id: "push",
232 label: "Push",
233 disabled: false,
234 icon: "push",
235 kind: "open_dialog",
236 dialogAction: "push",
237 },
238 {
239 id: "pr",
240 label: "Create PR",
241 disabled: false,
242 icon: "pr",
243 kind: "open_dialog",
244 dialogAction: "create_pr",
245 },
246 ]);
247 });
248});
249
250describe("when: branch is clean, up to date, and has no open PR", () => {
251 it("resolveQuickAction returns disabled no-action state", () => {
252 const quick = resolveQuickAction(
253 status({ aheadCount: 0, behindCount: 0, hasWorkingTreeChanges: false, pr: null }),
254 false,
255 );
256 assert.deepInclude(quick, { kind: "show_hint", label: "Commit", disabled: true });
257 });
258
259 it("buildMenuItems disables commit, push, and create PR", () => {
260 const items = buildMenuItems(status({ aheadCount: 0, behindCount: 0, pr: null }), false);
261 assert.deepEqual(items, [
262 {
263 id: "commit",
264 label: "Commit",
265 disabled: true,
266 icon: "commit",
267 kind: "open_dialog",
268 dialogAction: "commit",
269 },
270 {
271 id: "push",
272 label: "Push",
273 disabled: true,
274 icon: "push",
275 kind: "open_dialog",
276 dialogAction: "push",
277 },
278 {
279 id: "pr",
280 label: "Create PR",
281 disabled: true,
282 icon: "pr",
283 kind: "open_dialog",
284 dialogAction: "create_pr",
285 },
286 ]);
287 });
288});
289
290describe("when: branch is behind upstream", () => {
291 it("resolveQuickAction returns pull", () => {
292 const quick = resolveQuickAction(status({ behindCount: 2 }), false);
293 assert.deepInclude(quick, { kind: "run_pull", label: "Pull", disabled: false });
294 });
295
296 it("buildMenuItems disables push and create PR", () => {
297 const items = buildMenuItems(status({ behindCount: 1, pr: null }), false);
298 assert.deepEqual(items, [
299 {
300 id: "commit",
301 label: "Commit",
302 disabled: true,
303 icon: "commit",
304 kind: "open_dialog",
305 dialogAction: "commit",
306 },
307 {
308 id: "push",
309 label: "Push",
310 disabled: true,
311 icon: "push",
312 kind: "open_dialog",
313 dialogAction: "push",
314 },
315 {
316 id: "pr",
317 label: "Create PR",
318 disabled: true,
319 icon: "pr",
320 kind: "open_dialog",
321 dialogAction: "create_pr",
322 },
323 ]);
324 });
325});
326
327describe("when: branch has diverged from upstream", () => {
328 it("resolveQuickAction returns a disabled sync hint", () => {
329 const quick = resolveQuickAction(status({ aheadCount: 2, behindCount: 1 }), false);
330 assert.deepEqual(quick, {
331 label: "Sync branch",
332 disabled: true,
333 kind: "show_hint",
334 hint: "Branch has diverged from upstream. Rebase/merge first.",
335 });
336 });
337});
338
339describe("when: working tree has local changes", () => {
340 it("resolveQuickAction returns commit, push, and create PR", () => {
341 const quick = resolveQuickAction(status({ hasWorkingTreeChanges: true }), false);
342 assert.deepInclude(quick, {
343 kind: "run_action",
344 action: "commit_push_pr",
345 label: "Commit, push & create PR",
346 });
347 });
348
349 it("resolveQuickAction returns commit and push when open PR exists", () => {
350 const quick = resolveQuickAction(
351 status({
352 hasWorkingTreeChanges: true,
353 pr: {
354 number: 16,
355 title: "Existing PR",
356 url: "https://example.com/pr/16",
357 baseBranch: "main",
358 headBranch: "feature/test",
359 state: "open",
360 },
361 }),
362 false,
363 );
364 assert.deepInclude(quick, {
365 kind: "run_action",
366 action: "commit_push",
367 label: "Commit & push",
368 });
369 });
370
371 it("buildMenuItems enables commit and disables push and PR", () => {
372 const items = buildMenuItems(status({ hasWorkingTreeChanges: true }), false);
373 assert.deepEqual(items, [
374 {
375 id: "commit",
376 label: "Commit",
377 disabled: false,
378 icon: "commit",
379 kind: "open_dialog",
380 dialogAction: "commit",
381 },
382 {
383 id: "push",
384 label: "Push",
385 disabled: true,
386 icon: "push",
387 kind: "open_dialog",
388 dialogAction: "push",
389 },
390 {
391 id: "pr",
392 label: "Create PR",
393 disabled: true,
394 icon: "pr",
395 kind: "open_dialog",
396 dialogAction: "create_pr",
397 },
398 ]);
399 });
400});
401
402describe("when: on default branch without open PR", () => {
403 it("resolveQuickAction returns commit and push when local changes exist", () => {
404 const quick = resolveQuickAction(
405 status({ branch: "main", hasWorkingTreeChanges: true }),
406 false,
407 true,
408 );
409 assert.deepInclude(quick, {
410 kind: "run_action",
411 action: "commit_push",
412 label: "Commit & push",
413 disabled: false,
414 });
415 });
416
417 it("resolveQuickAction returns push when branch is ahead", () => {
418 const quick = resolveQuickAction(
419 status({ branch: "main", aheadCount: 2, pr: null }),
420 false,
421 true,
422 );
423 assert.deepInclude(quick, {
424 kind: "run_action",
425 action: "commit_push",
426 label: "Push",
427 disabled: false,
428 });
429 });
430});
431
432describe("when: working tree has local changes and branch is behind upstream", () => {
433 it("resolveQuickAction still prefers commit, push, and create PR", () => {
434 const quick = resolveQuickAction(
435 status({ hasWorkingTreeChanges: true, behindCount: 1 }),
436 false,
437 );
438 assert.deepInclude(quick, {
439 kind: "run_action",
440 action: "commit_push_pr",
441 label: "Commit, push & create PR",
442 });
443 });
444
445 it("buildMenuItems enables commit and keeps push and PR disabled", () => {
446 const items = buildMenuItems(status({ hasWorkingTreeChanges: true, behindCount: 2 }), false);
447 assert.deepEqual(items, [
448 {
449 id: "commit",
450 label: "Commit",
451 disabled: false,
452 icon: "commit",
453 kind: "open_dialog",
454 dialogAction: "commit",
455 },
456 {
457 id: "push",
458 label: "Push",
459 disabled: true,
460 icon: "push",
461 kind: "open_dialog",
462 dialogAction: "push",
463 },
464 {
465 id: "pr",
466 label: "Create PR",
467 disabled: true,
468 icon: "pr",
469 kind: "open_dialog",
470 dialogAction: "create_pr",
471 },
472 ]);
473 });
474});
475
476describe("when: HEAD is detached and there are no local changes", () => {
477 it("resolveQuickAction shows detached head hint", () => {
478 const quick = resolveQuickAction(
479 status({ branch: null, hasWorkingTreeChanges: false, hasUpstream: false }),
480 false,
481 );
482 assert.deepInclude(quick, { kind: "show_hint", label: "Commit", disabled: true });
483 });
484
485 it("buildMenuItems keeps commit, push, and PR disabled", () => {
486 const items = buildMenuItems(status({ branch: null, hasWorkingTreeChanges: false }), false);
487 assert.deepEqual(items, [
488 {
489 id: "commit",
490 label: "Commit",
491 disabled: true,
492 icon: "commit",
493 kind: "open_dialog",
494 dialogAction: "commit",
495 },
496 {
497 id: "push",
498 label: "Push",
499 disabled: true,
500 icon: "push",
501 kind: "open_dialog",
502 dialogAction: "push",
503 },
504 {
505 id: "pr",
506 label: "Create PR",
507 disabled: true,
508 icon: "pr",
509 kind: "open_dialog",
510 dialogAction: "create_pr",
511 },
512 ]);
513 });
514});
515
516describe("when: branch has no upstream configured", () => {
517 it("resolveQuickAction returns disabled commit state when clean and no commits are ahead", () => {
518 const quick = resolveQuickAction(
519 status({ hasUpstream: false, pr: null, aheadCount: 0 }),
520 false,
521 );
522 assert.deepInclude(quick, {
523 kind: "show_hint",
524 label: "Commit",
525 disabled: true,
526 });
527 });
528
529 it("resolveQuickAction remains disabled when clean, no upstream, and no local commits ahead", () => {
530 const quick = resolveQuickAction(
531 status({
532 hasUpstream: false,
533 aheadCount: 0,
534 pr: {
535 number: 14,
536 title: "Existing PR",
537 url: "https://example.com/pr/14",
538 baseBranch: "main",
539 headBranch: "feature/test",
540 state: "open",
541 },
542 }),
543 false,
544 );
545 assert.deepInclude(quick, {
546 kind: "show_hint",
547 label: "Commit",
548 disabled: true,
549 });
550 });
551
552 it("resolveQuickAction runs push when clean, no upstream, and local commits are ahead", () => {
553 const quick = resolveQuickAction(
554 status({
555 hasUpstream: false,
556 aheadCount: 1,
557 pr: {
558 number: 15,
559 title: "Existing PR",
560 url: "https://example.com/pr/15",
561 baseBranch: "main",
562 headBranch: "feature/test",
563 state: "open",
564 },
565 }),
566 false,
567 );
568 assert.deepInclude(quick, {
569 kind: "run_action",
570 action: "commit_push",
571 label: "Push",
572 disabled: false,
573 });
574 });
575
576 it("buildMenuItems disables push and create PR when no commits are ahead", () => {
577 const items = buildMenuItems(
578 status({ hasUpstream: false, pr: null, aheadCount: 0 }),
579 false,
580 );
581 assert.deepEqual(items, [
582 {
583 id: "commit",
584 label: "Commit",
585 disabled: true,
586 icon: "commit",
587 kind: "open_dialog",
588 dialogAction: "commit",
589 },
590 {
591 id: "push",
592 label: "Push",
593 disabled: true,
594 icon: "push",
595 kind: "open_dialog",
596 dialogAction: "push",
597 },
598 {
599 id: "pr",
600 label: "Create PR",
601 disabled: true,
602 icon: "pr",
603 kind: "open_dialog",
604 dialogAction: "create_pr",
605 },
606 ]);
607 });
608});
609
610describe("requiresDefaultBranchConfirmation", () => {
611 it("requires confirmation only for push actions on default branch", () => {
612 assert.isFalse(requiresDefaultBranchConfirmation("commit", true));
613 assert.isTrue(requiresDefaultBranchConfirmation("commit_push", true));
614 assert.isTrue(requiresDefaultBranchConfirmation("commit_push_pr", true));
615 assert.isFalse(requiresDefaultBranchConfirmation("commit_push", false));
616 });
617});
618
619describe("buildGitActionProgressStages", () => {
620 it("shows only push progress when push-only is forced", () => {
621 const stages = buildGitActionProgressStages({
622 action: "commit_push",
623 hasCustomCommitMessage: false,
624 hasWorkingTreeChanges: true,
625 forcePushOnly: true,
626 pushTarget: "origin/feature/test",
627 });
628 assert.deepEqual(stages, ["Pushing to origin/feature/test..."]);
629 });
630
631 it("includes commit stages for commit+push when working tree is dirty", () => {
632 const stages = buildGitActionProgressStages({
633 action: "commit_push",
634 hasCustomCommitMessage: false,
635 hasWorkingTreeChanges: true,
636 pushTarget: "origin/feature/test",
637 });
638 assert.deepEqual(stages, [
639 "Generating commit message...",
640 "Committing...",
641 "Pushing to origin/feature/test...",
642 ]);
643 });
644});
645
646describe("summarizeGitResult", () => {
647 it("returns commit-focused toast for commit action", () => {
648 const result = summarizeGitResult({
649 action: "commit",
650 commit: {
651 status: "created",
652 commitSha: "0123456789abcdef",
653 subject: "feat: add optimistic UI for git action button",
654 },
655 push: { status: "skipped_not_requested" },
656 pr: { status: "skipped_not_requested" },
657 });
658
659 assert.deepEqual(result, {
660 title: "Committed 0123456",
661 description: "feat: add optimistic UI for git action button",
662 });
663 });
664
665 it("returns push-focused toast for push action", () => {
666 const result = summarizeGitResult({
667 action: "commit_push",
668 commit: {
669 status: "created",
670 commitSha: "abcdef0123456789",
671 subject: "fix: tighten quick action tooltip hover handling",
672 },
673 push: {
674 status: "pushed",
675 branch: "foo",
676 upstreamBranch: "origin/foo",
677 },
678 pr: { status: "skipped_not_requested" },
679 });
680
681 assert.deepEqual(result, {
682 title: "Pushed abcdef0 to origin/foo",
683 description: "fix: tighten quick action tooltip hover handling",
684 });
685 });
686
687 it("returns PR-focused toast for created PR action", () => {
688 const result = summarizeGitResult({
689 action: "commit_push_pr",
690 commit: {
691 status: "created",
692 commitSha: "89abcdef01234567",
693 subject: "feat: ship github shortcuts",
694 },
695 push: {
696 status: "pushed",
697 branch: "foo",
698 },
699 pr: {
700 status: "created",
701 number: 42,
702 title: "feat: ship github shortcuts and improve PR CTA in success toast",
703 },
704 });
705
706 assert.deepEqual(result, {
707 title: "Created PR #42",
708 description: "feat: ship github shortcuts and improve PR CTA in success toast",
709 });
710 });
711
712 it("truncates long description text", () => {
713 const result = summarizeGitResult({
714 action: "commit_push_pr",
715 commit: {
716 status: "created",
717 commitSha: "89abcdef01234567",
718 subject: "short subject",
719 },
720 push: { status: "pushed", branch: "foo" },
721 pr: {
722 status: "created",
723 number: 99,
724 title:
725 "feat: this title is intentionally extremely long so we can validate that toast descriptions are truncated with an ellipsis suffix",
726 },
727 });
728
729 assert.deepEqual(result, {
730 title: "Created PR #99",
731 description: "feat: this title is intentionally extremely long so we can validate t...",
732 });
733 });
734});