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

GitActionsControl.logic.test.ts

379 lines11.7 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 type DefaultBranchConfirmableAction,
8 hasUsableGitMenuContent,
9 requiresDefaultBranchConfirmation,
10 resolveChangesTotals,
11 resolveDefaultBranchActionDialogCopy,
12 resolveFeatureBranchUnavailableReason,
13 resolveLiveThreadBranchUpdate,
14 resolveQuickAction,
15 getMenuActionDisabledReason,
16} from "./GitActionsControl.logic";
17​
18function status(overrides: Partial<VcsStatusResult> = {}): VcsStatusResult {
19 return {
20 isRepo: true,
21 hasPrimaryRemote: true,
22 isDefaultRef: false,
23 refName: "feature/test",
24 hasWorkingTreeChanges: false,
25 workingTree: {
26 files: [],
27 insertions: 0,
28 deletions: 0,
29 },
30 hasUpstream: true,
31 aheadCount: 0,
32 behindCount: 0,
33 pr: null,
34 ...overrides,
35 };
37​
38function pushDisabledReason(gitStatus: VcsStatusResult): string | null {
39 const item = buildMenuItems(gitStatus, false).find((menuItem) => menuItem.id === "push");
40 assert.isDefined(item);
41 return getMenuActionDisabledReason({ item, gitStatus, isBusy: false, hasPrimaryRemote: true });
43​
44describe("when: ref is clean, ahead, and has an open PR", () => {
45 it("resolveQuickAction prefers push", () => {
46 const quick = resolveQuickAction(
47 status({
48 aheadCount: 3,
49 pr: {
50 number: 13,
51 title: "Open PR",
52 url: "https://example.com/pr/13",
53 baseRef: "main",
54 headRef: "feature/test",
55 state: "open",
56 },
57 }),
58 false,
59 );
60 assert.deepInclude(quick, { kind: "run_action", action: "push", label: "Push" });
61 });
62});
63​
64describe("when: ref is clean, up to date, and has no open PR", () => {
65 it("enables create PR when synced with upstream but ahead of default, even with local changes", () => {
66 const syncedFeature = status({
67 aheadCount: 0,
68 behindCount: 0,
69 aheadOfDefaultCount: 1,
70 pr: null,
71 });
72​
73 const quick = resolveQuickAction(syncedFeature, false);
74 assert.deepInclude(quick, {
75 label: "Create PR",
76 disabled: false,
77 kind: "run_action",
78 action: "create_pr",
79 });
80​
81 const items = buildMenuItems(syncedFeature, false);
82 assert.equal(items.find((item) => item.id === "pr")?.disabled, false);
83​
84 const dirtyFeature = { ...syncedFeature, hasWorkingTreeChanges: true };
85 assert.equal(
86 buildMenuItems(dirtyFeature, false).find((item) => item.id === "pr")?.disabled,
87 false,
88 );
89 assert.equal(pushDisabledReason(dirtyFeature), "No local commits to push.");
90 });
91​
92 it("offers the actions menu only when something in it is usable", () => {
93 const hasContent = (
94 gitStatus: VcsStatusResult,
95 overrides: Partial<Parameters<typeof hasUsableGitMenuContent>[0]> = {},
96 ) =>
97 hasUsableGitMenuContent({
98 menuItems: buildMenuItems(gitStatus, false, gitStatus.hasPrimaryRemote),
99 canWriteSourceControl: true,
100 canPublishRepository: false,
101 hasNotice: false,
102 ...overrides,
103 });
104 const clean = status();
105 const dirty = status({ hasWorkingTreeChanges: true });
106​
107 assert.isFalse(hasContent(clean));
108 assert.isTrue(hasContent(dirty));
109 assert.isTrue(hasContent(status({ aheadCount: 1 })));
110 assert.isFalse(hasContent(dirty, { canWriteSourceControl: false }));
111 assert.isTrue(hasContent(status({ hasPrimaryRemote: false }), { canPublishRepository: true }));
112 assert.isTrue(hasContent(clean, { hasNotice: true }));
113 });
114});
115​
116describe("when: on default ref without open PR", () => {
117 it("resolveQuickAction returns commit and push when local changes exist", () => {
118 const quick = resolveQuickAction(
119 status({ refName: "main", hasWorkingTreeChanges: true }),
120 false,
121 true,
122 );
123 assert.deepInclude(quick, {
124 kind: "run_action",
125 action: "commit_push",
126 label: "Commit & push",
127 disabled: false,
128 });
129 });
130​
131 it("offers a feature branch only to actions that commit the changes", () => {
132 const reason = (action: DefaultBranchConfirmableAction, hasWorkingTreeChanges: boolean) =>
133 resolveFeatureBranchUnavailableReason({ action, branchName: "main", hasWorkingTreeChanges });
134 assert.isNull(reason("commit_push", true));
135 assert.isNull(reason("commit_push_pr", true));
136 assert.isNotNull(reason("commit_push", false));
137 assert.isNotNull(reason("push", true));
138 assert.isNotNull(reason("create_pr", true));
139 });
140});
141​
142describe("when: local changes are behind upstream", () => {
143 it("keeps the commit action available without pushing, with or without an open PR", () => {
144 const behind = status({ hasWorkingTreeChanges: true, behindCount: 2 });
145 const commit = {
146 label: "Commit",
147 disabled: false,
148 kind: "run_action",
149 action: "commit",
150 } as const;
151 assert.deepEqual(resolveQuickAction(behind, false), commit);
152 assert.equal(
153 pushDisabledReason({ ...behind, aheadCount: 1 }),
154 "Branch is behind upstream. Pull/rebase before pushing.",
155 );
156 assert.deepEqual(
157 resolveQuickAction(
158 {
159 ...behind,
160 pr: {
161 number: 13,
162 title: "Open PR",
163 url: "https://example.com/pr/13",
164 baseRef: "main",
165 headRef: "feature/test",
166 state: "open",
167 },
168 },
169 false,
170 ),
171 commit,
172 );
173 });
174});
175​
176describe("when: ref has no upstream configured", () => {
177 it("resolveQuickAction runs push and create PR when no upstream and commits are ahead", () => {
178 const quick = resolveQuickAction(
179 status({
180 hasUpstream: false,
181 aheadCount: 2,
182 pr: null,
183 }),
184 false,
185 );
186 assert.deepInclude(quick, {
187 kind: "run_action",
188 action: "create_pr",
189 label: "Push & create PR",
190 disabled: false,
191 });
192 });
193​
194 it("resolveQuickAction publishes when no origin remote exists", () => {
195 const quick = resolveQuickAction(
196 status({
197 hasUpstream: false,
198 aheadCount: 2,
199 pr: null,
200 }),
201 false,
202 false,
203 false,
204 );
205 assert.deepEqual(quick, {
206 kind: "open_publish",
207 label: "Publish repository",
208 disabled: false,
209 });
210 });
211});
212​
213describe("when: the ref is on GitLab", () => {
214 const gitLab = {
215 sourceControlProvider: {
216 kind: SourceControlProviderKind.make("gitlab"),
217 name: "GitLab",
218 baseUrl: "https://gitlab.com",
219 },
220 };
221​
222 it("names merge requests in actions and the default-ref dialog", () => {
223 const ahead = status({ ...gitLab, hasUpstream: false, aheadCount: 2 });
224 assert.equal(resolveQuickAction(ahead, false).label, "Push & create MR");
225 assert.equal(
226 resolveQuickAction(status({ ...gitLab, hasWorkingTreeChanges: true }), false).label,
227 "Commit, push & MR",
228 );
229 assert.equal(buildMenuItems(ahead, false).find((item) => item.id === "pr")?.label, "Create MR");
230​
231 const copy = resolveDefaultBranchActionDialogCopy({
232 action: "create_pr",
233 branchName: "main",
234 includesCommit: false,
235 terminology: sourceControlClients.get("gitlab").changeRequest,
236 });
237 assert.equal(copy.title, "Push & create MR from default ref?");
238 assert.include(copy.description, "create a merge request on");
239 });
240});
241​
242describe("requiresDefaultBranchConfirmation", () => {
243 it("requires confirmation for push actions on default ref", () => {
244 assert.isFalse(requiresDefaultBranchConfirmation("commit", true));
245 assert.isTrue(requiresDefaultBranchConfirmation("push", true));
246 assert.isTrue(requiresDefaultBranchConfirmation("create_pr", true));
247 assert.isTrue(requiresDefaultBranchConfirmation("commit_push", true));
248 assert.isTrue(requiresDefaultBranchConfirmation("commit_push_pr", true));
249 assert.isFalse(requiresDefaultBranchConfirmation("commit_push", false));
250 assert.isFalse(requiresDefaultBranchConfirmation("push", false));
251 });
252});
253​
254describe("resolveLiveThreadBranchUpdate", () => {
255 it("returns a branch update when live git status differs from stored thread metadata", () => {
256 const update = resolveLiveThreadBranchUpdate({
257 threadBranch: "feature/old-ref",
258 gitStatus: status({ refName: "effect-atom" }),
259 });
260​
261 assert.deepEqual(update, {
262 branch: "effect-atom",
263 });
264 });
265​
266 it("returns null when git status is detached HEAD but the thread already has a ref", () => {
267 const update = resolveLiveThreadBranchUpdate({
268 threadBranch: "effect-atom",
269 gitStatus: status({ refName: null }),
270 });
271​
272 assert.equal(update, null);
273 });
274​
275 it("does not regress a semantic thread ref back to a temporary worktree ref", () => {
276 const update = resolveLiveThreadBranchUpdate({
277 threadBranch: "t3code/github-query-rate-limit",
278 gitStatus: status({ refName: "t3code/bda76797" }),
279 });
280​
281 assert.equal(update, null);
282 });
283});
284​
285describe("resolveAutoFeatureBranchName", () => {
286 it("increments suffix when the preferred ref name already exists", () => {
287 const ref = resolveAutoFeatureBranchName(
288 ["main", "feature/fix-toast-copy", "feature/fix-toast-copy-2"],
289 "fix toast copy",
290 );
291 assert.equal(ref, "feature/fix-toast-copy-3");
292 });
293​
294 it("treats existing ref names as case-insensitive for collision checks", () => {
295 const ref = resolveAutoFeatureBranchName(["Feature/Ticket-1"], "feature/ticket-1");
296 assert.equal(ref, "feature/ticket-1-2");
297 });
298});
299​
300describe("resolveChangesTotals", () => {
301 it("does not let an empty branchChanges hide a dirty working tree", () => {
302 assert.deepInclude(
303 resolveChangesTotals(
304 status({
305 hasWorkingTreeChanges: true,
306 branchChanges: { baseRef: "main", insertions: 0, deletions: 0 },
307 workingTree: {
308 files: [{ path: "a.ts", insertions: 5, deletions: 1 }],
309 insertions: 5,
310 deletions: 1,
311 },
312 }),
313 ),
314 { insertions: 5, deletions: 1 },
315 );
316 });
317​
318 it("keeps non-zero branchChanges over the working tree", () => {
319 assert.deepEqual(
320 resolveChangesTotals(
321 status({
322 branchChanges: { baseRef: "origin/master", insertions: 20, deletions: 3 },
323 workingTree: {
324 files: [{ path: "a.ts", insertions: 1, deletions: 0 }],
325 insertions: 1,
326 deletions: 0,
327 },
328 }),
329 ),
330 { insertions: 20, deletions: 3, baseRef: "origin/master" },
331 );
332 });
333​
334 it("uses the panel's branch comparison when it has file stats", () => {
335 const dirty = status({
336 branchChanges: { baseRef: "main", insertions: 20, deletions: 3 },
337 });
338 const source = {
339 id: "branch-range",
340 kind: "branch-range",
341 title: "Changes vs release",
342 baseRef: "release",
343 headRef: "feature/test",
344 diff: "",
345 diffHash: "hash",
346 truncated: false,
347 } as const;
348 assert.deepEqual(
349 resolveChangesTotals(dirty, {
350 ...source,
351 files: [
352 { path: "a.ts", previousPath: null, additions: 4, deletions: 1 },
353 { path: "b.ts", previousPath: null, additions: 2, deletions: 0 },
354 ],
355 }),
356 { insertions: 6, deletions: 1, baseRef: "release" },
357 );
358 assert.deepEqual(resolveChangesTotals(dirty, source), {
359 insertions: 20,
360 deletions: 3,
361 baseRef: "main",
362 });
363 });
364​
365 it("falls back to the working tree when branchChanges is omitted", () => {
366 assert.deepInclude(
367 resolveChangesTotals(
368 status({
369 workingTree: {
370 files: [{ path: "a.ts", insertions: 2, deletions: 0 }],
371 insertions: 2,
372 deletions: 0,
373 },
374 }),
375 ),
376 { insertions: 2, deletions: 0 },
377 );
378 });
379});