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

GitActionsControl.logic.test.ts

727 lines18.7 KB

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