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

GitActionsControl.logic.test.ts

300 lines8.8 KB

1import { SourceControlProviderKind, type VcsStatusResult } from "@t3tools/contracts";
2import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
3import { resolveAutoFeatureBranchName } from "@t3tools/shared/git";
4import { assert, describe, it } from "vite-plus/test";
5import {
6 buildMenuItems,
7 requiresDefaultBranchConfirmation,
8 resolveChangesTotals,
9 resolveDefaultBranchActionDialogCopy,
10 resolveLiveThreadBranchUpdate,
11 resolveQuickAction,
12} from "./GitActionsControl.logic";
13​
14function status(overrides: Partial<VcsStatusResult> = {}): VcsStatusResult {
15 return {
16 isRepo: true,
17 hasPrimaryRemote: true,
18 isDefaultRef: false,
19 refName: "feature/test",
20 hasWorkingTreeChanges: false,
21 workingTree: {
22 files: [],
23 insertions: 0,
24 deletions: 0,
25 },
26 hasUpstream: true,
27 aheadCount: 0,
28 behindCount: 0,
29 pr: null,
30 ...overrides,
31 };
33​
34describe("when: ref is clean, ahead, and has an open PR", () => {
35 it("resolveQuickAction prefers push", () => {
36 const quick = resolveQuickAction(
37 status({
38 aheadCount: 3,
39 pr: {
40 number: 13,
41 title: "Open PR",
42 url: "https://example.com/pr/13",
43 baseRef: "main",
44 headRef: "feature/test",
45 state: "open",
46 },
47 }),
48 false,
49 );
50 assert.deepInclude(quick, { kind: "run_action", action: "push", label: "Push" });
51 });
52});
53​
54describe("when: ref is clean, up to date, and has no open PR", () => {
55 it("enables create PR when synced with upstream but ahead of default, even with local changes", () => {
56 const syncedFeature = status({
57 aheadCount: 0,
58 behindCount: 0,
59 aheadOfDefaultCount: 1,
60 pr: null,
61 });
62​
63 const quick = resolveQuickAction(syncedFeature, false);
64 assert.deepInclude(quick, {
65 label: "Create PR",
66 disabled: false,
67 kind: "run_action",
68 action: "create_pr",
69 });
70​
71 const items = buildMenuItems(syncedFeature, false);
72 assert.equal(items.find((item) => item.id === "pr")?.disabled, false);
73​
74 const dirtyFeature = { ...syncedFeature, hasWorkingTreeChanges: true };
75 assert.equal(
76 buildMenuItems(dirtyFeature, false).find((item) => item.id === "pr")?.disabled,
77 false,
78 );
79 });
80});
81​
82describe("when: on default ref without open PR", () => {
83 it("resolveQuickAction returns commit and push when local changes exist", () => {
84 const quick = resolveQuickAction(
85 status({ refName: "main", hasWorkingTreeChanges: true }),
86 false,
87 true,
88 );
89 assert.deepInclude(quick, {
90 kind: "run_action",
91 action: "commit_push",
92 label: "Commit & push",
93 disabled: false,
94 });
95 });
96});
97​
98describe("when: local changes are behind upstream", () => {
99 it("keeps the commit action available without pushing, with or without an open PR", () => {
100 const behind = status({ hasWorkingTreeChanges: true, behindCount: 2 });
101 const commit = {
102 label: "Commit",
103 disabled: false,
104 kind: "run_action",
105 action: "commit",
106 } as const;
107 assert.deepEqual(resolveQuickAction(behind, false), commit);
108 assert.deepEqual(
109 resolveQuickAction(
110 {
111 ...behind,
112 pr: {
113 number: 13,
114 title: "Open PR",
115 url: "https://example.com/pr/13",
116 baseRef: "main",
117 headRef: "feature/test",
118 state: "open",
119 },
120 },
121 false,
122 ),
123 commit,
124 );
125 });
126});
127​
128describe("when: ref has no upstream configured", () => {
129 it("resolveQuickAction runs push and create PR when no upstream and commits are ahead", () => {
130 const quick = resolveQuickAction(
131 status({
132 hasUpstream: false,
133 aheadCount: 2,
134 pr: null,
135 }),
136 false,
137 );
138 assert.deepInclude(quick, {
139 kind: "run_action",
140 action: "create_pr",
141 label: "Push & create PR",
142 disabled: false,
143 });
144 });
145​
146 it("resolveQuickAction publishes when no origin remote exists", () => {
147 const quick = resolveQuickAction(
148 status({
149 hasUpstream: false,
150 aheadCount: 2,
151 pr: null,
152 }),
153 false,
154 false,
155 false,
156 );
157 assert.deepEqual(quick, {
158 kind: "open_publish",
159 label: "Publish repository",
160 disabled: false,
161 });
162 });
163});
164​
165describe("when: the ref is on GitLab", () => {
166 const gitLab = {
167 sourceControlProvider: {
168 kind: SourceControlProviderKind.make("gitlab"),
169 name: "GitLab",
170 baseUrl: "https://gitlab.com",
171 },
172 };
173​
174 it("names merge requests in actions and the default-ref dialog", () => {
175 const ahead = status({ ...gitLab, hasUpstream: false, aheadCount: 2 });
176 assert.equal(resolveQuickAction(ahead, false).label, "Push & create MR");
177 assert.equal(
178 resolveQuickAction(status({ ...gitLab, hasWorkingTreeChanges: true }), false).label,
179 "Commit, push & MR",
180 );
181 assert.equal(buildMenuItems(ahead, false).find((item) => item.id === "pr")?.label, "Create MR");
182​
183 const copy = resolveDefaultBranchActionDialogCopy({
184 action: "create_pr",
185 branchName: "main",
186 includesCommit: false,
187 terminology: sourceControlClients.get("gitlab").changeRequest,
188 });
189 assert.equal(copy.title, "Push & create MR from default ref?");
190 assert.include(copy.description, "create a merge request on");
191 });
192});
193​
194describe("requiresDefaultBranchConfirmation", () => {
195 it("requires confirmation for push actions on default ref", () => {
196 assert.isFalse(requiresDefaultBranchConfirmation("commit", true));
197 assert.isTrue(requiresDefaultBranchConfirmation("push", true));
198 assert.isTrue(requiresDefaultBranchConfirmation("create_pr", true));
199 assert.isTrue(requiresDefaultBranchConfirmation("commit_push", true));
200 assert.isTrue(requiresDefaultBranchConfirmation("commit_push_pr", true));
201 assert.isFalse(requiresDefaultBranchConfirmation("commit_push", false));
202 assert.isFalse(requiresDefaultBranchConfirmation("push", false));
203 });
204});
205​
206describe("resolveLiveThreadBranchUpdate", () => {
207 it("returns a branch update when live git status differs from stored thread metadata", () => {
208 const update = resolveLiveThreadBranchUpdate({
209 threadBranch: "feature/old-ref",
210 gitStatus: status({ refName: "effect-atom" }),
211 });
212​
213 assert.deepEqual(update, {
214 branch: "effect-atom",
215 });
216 });
217​
218 it("returns null when git status is detached HEAD but the thread already has a ref", () => {
219 const update = resolveLiveThreadBranchUpdate({
220 threadBranch: "effect-atom",
221 gitStatus: status({ refName: null }),
222 });
223​
224 assert.equal(update, null);
225 });
226​
227 it("does not regress a semantic thread ref back to a temporary worktree ref", () => {
228 const update = resolveLiveThreadBranchUpdate({
229 threadBranch: "t3code/github-query-rate-limit",
230 gitStatus: status({ refName: "t3code/bda76797" }),
231 });
232​
233 assert.equal(update, null);
234 });
235});
236​
237describe("resolveAutoFeatureBranchName", () => {
238 it("increments suffix when the preferred ref name already exists", () => {
239 const ref = resolveAutoFeatureBranchName(
240 ["main", "feature/fix-toast-copy", "feature/fix-toast-copy-2"],
241 "fix toast copy",
242 );
243 assert.equal(ref, "feature/fix-toast-copy-3");
244 });
245​
246 it("treats existing ref names as case-insensitive for collision checks", () => {
247 const ref = resolveAutoFeatureBranchName(["Feature/Ticket-1"], "feature/ticket-1");
248 assert.equal(ref, "feature/ticket-1-2");
249 });
250});
251​
252describe("resolveChangesTotals", () => {
253 it("does not let an empty branchChanges hide a dirty working tree", () => {
254 assert.deepInclude(
255 resolveChangesTotals(
256 status({
257 hasWorkingTreeChanges: true,
258 branchChanges: { baseRef: "main", insertions: 0, deletions: 0 },
259 workingTree: {
260 files: [{ path: "a.ts", insertions: 5, deletions: 1 }],
261 insertions: 5,
262 deletions: 1,
263 },
264 }),
265 ),
266 { insertions: 5, deletions: 1 },
267 );
268 });
269​
270 it("keeps non-zero branchChanges over the working tree", () => {
271 assert.deepEqual(
272 resolveChangesTotals(
273 status({
274 branchChanges: { baseRef: "origin/master", insertions: 20, deletions: 3 },
275 workingTree: {
276 files: [{ path: "a.ts", insertions: 1, deletions: 0 }],
277 insertions: 1,
278 deletions: 0,
279 },
280 }),
281 ),
282 { insertions: 20, deletions: 3, baseRef: "origin/master" },
283 );
284 });
285​
286 it("falls back to the working tree when branchChanges is omitted", () => {
287 assert.deepInclude(
288 resolveChangesTotals(
289 status({
290 workingTree: {
291 files: [{ path: "a.ts", insertions: 2, deletions: 0 }],
292 insertions: 2,
293 deletions: 0,
294 },
295 }),
296 ),
297 { insertions: 2, deletions: 0 },
298 );
299 });
300});