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

GitActionsControl.logic.test.ts

188 lines5.4 KB

1import type { VcsStatusResult } from "@t3tools/contracts";
2import { assert, describe, it } from "vite-plus/test";
3import {
4 buildMenuItems,
5 requiresDefaultBranchConfirmation,
6 resolveAutoFeatureBranchName,
7 resolveLiveThreadBranchUpdate,
8 resolveQuickAction,
9} from "./GitActionsControl.logic";
10​
11function status(overrides: Partial<VcsStatusResult> = {}): VcsStatusResult {
12 return {
13 isRepo: true,
14 hasPrimaryRemote: true,
15 isDefaultRef: false,
16 refName: "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 };
30​
31describe("when: ref is clean, ahead, and has an open PR", () => {
32 it("resolveQuickAction prefers push", () => {
33 const quick = resolveQuickAction(
34 status({
35 aheadCount: 3,
36 pr: {
37 number: 13,
38 title: "Open PR",
39 url: "https://example.com/pr/13",
40 baseRef: "main",
41 headRef: "feature/test",
42 state: "open",
43 },
44 }),
45 false,
46 );
47 assert.deepInclude(quick, { kind: "run_action", action: "push", label: "Push" });
48 });
49});
50​
51describe("when: ref is clean, up to date, and has no open PR", () => {
52 it("enables create PR when synced with upstream but ahead of default, even with local changes", () => {
53 const syncedFeature = status({
54 aheadCount: 0,
55 behindCount: 0,
56 aheadOfDefaultCount: 1,
57 pr: null,
58 });
59​
60 const quick = resolveQuickAction(syncedFeature, false);
61 assert.deepInclude(quick, {
62 label: "Create PR",
63 disabled: false,
64 kind: "run_action",
65 action: "create_pr",
66 });
67​
68 const items = buildMenuItems(syncedFeature, false);
69 assert.equal(items.find((item) => item.id === "pr")?.disabled, false);
70​
71 const dirtyFeature = { ...syncedFeature, hasWorkingTreeChanges: true };
72 assert.equal(
73 buildMenuItems(dirtyFeature, false).find((item) => item.id === "pr")?.disabled,
74 false,
75 );
76 });
77});
78​
79describe("when: on default ref without open PR", () => {
80 it("resolveQuickAction returns commit and push when local changes exist", () => {
81 const quick = resolveQuickAction(
82 status({ refName: "main", hasWorkingTreeChanges: true }),
83 false,
84 true,
85 );
86 assert.deepInclude(quick, {
87 kind: "run_action",
88 action: "commit_push",
89 label: "Commit & push",
90 disabled: false,
91 });
92 });
93});
94​
95describe("when: ref has no upstream configured", () => {
96 it("resolveQuickAction runs push and create PR when no upstream and commits are ahead", () => {
97 const quick = resolveQuickAction(
98 status({
99 hasUpstream: false,
100 aheadCount: 2,
101 pr: null,
102 }),
103 false,
104 );
105 assert.deepInclude(quick, {
106 kind: "run_action",
107 action: "create_pr",
108 label: "Push & create PR",
109 disabled: false,
110 });
111 });
112​
113 it("resolveQuickAction publishes when no origin remote exists", () => {
114 const quick = resolveQuickAction(
115 status({
116 hasUpstream: false,
117 aheadCount: 2,
118 pr: null,
119 }),
120 false,
121 false,
122 false,
123 );
124 assert.deepEqual(quick, {
125 kind: "open_publish",
126 label: "Publish repository",
127 disabled: false,
128 });
129 });
130});
131​
132describe("requiresDefaultBranchConfirmation", () => {
133 it("requires confirmation for push actions on default ref", () => {
134 assert.isFalse(requiresDefaultBranchConfirmation("commit", true));
135 assert.isTrue(requiresDefaultBranchConfirmation("push", true));
136 assert.isTrue(requiresDefaultBranchConfirmation("create_pr", true));
137 assert.isTrue(requiresDefaultBranchConfirmation("commit_push", true));
138 assert.isTrue(requiresDefaultBranchConfirmation("commit_push_pr", true));
139 assert.isFalse(requiresDefaultBranchConfirmation("commit_push", false));
140 assert.isFalse(requiresDefaultBranchConfirmation("push", false));
141 });
142});
143​
144describe("resolveLiveThreadBranchUpdate", () => {
145 it("returns a branch update when live git status differs from stored thread metadata", () => {
146 const update = resolveLiveThreadBranchUpdate({
147 threadBranch: "feature/old-ref",
148 gitStatus: status({ refName: "effect-atom" }),
149 });
150​
151 assert.deepEqual(update, {
152 branch: "effect-atom",
153 });
154 });
155​
156 it("returns null when git status is detached HEAD but the thread already has a ref", () => {
157 const update = resolveLiveThreadBranchUpdate({
158 threadBranch: "effect-atom",
159 gitStatus: status({ refName: null }),
160 });
161​
162 assert.equal(update, null);
163 });
164​
165 it("does not regress a semantic thread ref back to a temporary worktree ref", () => {
166 const update = resolveLiveThreadBranchUpdate({
167 threadBranch: "t3code/github-query-rate-limit",
168 gitStatus: status({ refName: "t3code/bda76797" }),
169 });
170​
171 assert.equal(update, null);
172 });
173});
174​
175describe("resolveAutoFeatureBranchName", () => {
176 it("increments suffix when the preferred ref name already exists", () => {
177 const ref = resolveAutoFeatureBranchName(
178 ["main", "feature/fix-toast-copy", "feature/fix-toast-copy-2"],
179 "fix toast copy",
180 );
181 assert.equal(ref, "feature/fix-toast-copy-3");
182 });
183​
184 it("treats existing ref names as case-insensitive for collision checks", () => {
185 const ref = resolveAutoFeatureBranchName(["Feature/Ticket-1"], "feature/ticket-1");
186 assert.equal(ref, "feature/ticket-1-2");
187 });
188});