Open History to see the last commit
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 };
36}
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 });
42}
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("resolveQuickAction pushes without committing when the tree is clean and ahead", () => {
132 const withUpstream = resolveQuickAction(
133 status({ refName: "main", aheadCount: 1 }),
134 false,
135 true,
136 );
137 assert.deepInclude(withUpstream, { kind: "run_action", action: "push", label: "Push" });
138
139 const withoutUpstream = resolveQuickAction(
140 status({ refName: "main", aheadCount: 1, hasUpstream: false }),
141 false,
142 true,
143 );
144 assert.deepInclude(withoutUpstream, { kind: "run_action", action: "push", label: "Push" });
145 });
146
147 it("offers a feature branch only to actions that commit the changes", () => {
148 const reason = (action: DefaultBranchConfirmableAction, hasWorkingTreeChanges: boolean) =>
149 resolveFeatureBranchUnavailableReason({ action, branchName: "main", hasWorkingTreeChanges });
150 assert.isNull(reason("commit_push", true));
151 assert.isNull(reason("commit_push_pr", true));
152 assert.isNotNull(reason("commit_push", false));
153 assert.isNotNull(reason("push", true));
154 assert.isNotNull(reason("create_pr", true));
155 });
156});
157
158describe("when: local changes are behind upstream", () => {
159 it("keeps the commit action available without pushing, with or without an open PR", () => {
160 const behind = status({ hasWorkingTreeChanges: true, behindCount: 2 });
161 const commit = {
162 label: "Commit",
163 disabled: false,
164 kind: "run_action",
165 action: "commit",
166 } as const;
167 assert.deepEqual(resolveQuickAction(behind, false), commit);
168 assert.equal(
169 pushDisabledReason({ ...behind, aheadCount: 1 }),
170 "Branch is behind upstream. Pull/rebase before pushing.",
171 );
172 assert.deepEqual(
173 resolveQuickAction(
174 {
175 ...behind,
176 pr: {
177 number: 13,
178 title: "Open PR",
179 url: "https://example.com/pr/13",
180 baseRef: "main",
181 headRef: "feature/test",
182 state: "open",
183 },
184 },
185 false,
186 ),
187 commit,
188 );
189 });
190});
191
192describe("when: ref has no upstream configured", () => {
193 it("resolveQuickAction runs push and create PR when no upstream and commits are ahead", () => {
194 const quick = resolveQuickAction(
195 status({
196 hasUpstream: false,
197 aheadCount: 2,
198 pr: null,
199 }),
200 false,
201 );
202 assert.deepInclude(quick, {
203 kind: "run_action",
204 action: "create_pr",
205 label: "Push & create PR",
206 disabled: false,
207 });
208 });
209
210 it("resolveQuickAction publishes when no origin remote exists", () => {
211 const quick = resolveQuickAction(
212 status({
213 hasUpstream: false,
214 aheadCount: 2,
215 pr: null,
216 }),
217 false,
218 false,
219 false,
220 );
221 assert.deepEqual(quick, {
222 kind: "open_publish",
223 label: "Publish repository",
224 disabled: false,
225 });
226 });
227});
228
229describe("when: the ref is on GitLab", () => {
230 const gitLab = {
231 sourceControlProvider: {
232 kind: SourceControlProviderKind.make("gitlab"),
233 name: "GitLab",
234 baseUrl: "https://gitlab.com",
235 },
236 };
237
238 it("names merge requests in actions and the default-ref dialog", () => {
239 const ahead = status({ ...gitLab, hasUpstream: false, aheadCount: 2 });
240 assert.equal(resolveQuickAction(ahead, false).label, "Push & create MR");
241 assert.equal(
242 resolveQuickAction(status({ ...gitLab, hasWorkingTreeChanges: true }), false).label,
243 "Commit, push & MR",
244 );
245 assert.equal(buildMenuItems(ahead, false).find((item) => item.id === "pr")?.label, "Create MR");
246
247 const copy = resolveDefaultBranchActionDialogCopy({
248 action: "create_pr",
249 branchName: "main",
250 includesCommit: false,
251 terminology: sourceControlClients.get("gitlab").changeRequest,
252 });
253 assert.equal(copy.title, "Push & create MR from default ref?");
254 assert.include(copy.description, "create a merge request on");
255 });
256});
257
258describe("requiresDefaultBranchConfirmation", () => {
259 it("requires confirmation for push actions on default ref", () => {
260 assert.isFalse(requiresDefaultBranchConfirmation("commit", true));
261 assert.isTrue(requiresDefaultBranchConfirmation("push", true));
262 assert.isTrue(requiresDefaultBranchConfirmation("create_pr", true));
263 assert.isTrue(requiresDefaultBranchConfirmation("commit_push", true));
264 assert.isTrue(requiresDefaultBranchConfirmation("commit_push_pr", true));
265 assert.isFalse(requiresDefaultBranchConfirmation("commit_push", false));
266 assert.isFalse(requiresDefaultBranchConfirmation("push", false));
267 });
268});
269
270describe("resolveLiveThreadBranchUpdate", () => {
271 it("returns a branch update when live git status differs from stored thread metadata", () => {
272 const update = resolveLiveThreadBranchUpdate({
273 threadBranch: "feature/old-ref",
274 gitStatus: status({ refName: "effect-atom" }),
275 });
276
277 assert.deepEqual(update, {
278 branch: "effect-atom",
279 });
280 });
281
282 it("returns null when git status is detached HEAD but the thread already has a ref", () => {
283 const update = resolveLiveThreadBranchUpdate({
284 threadBranch: "effect-atom",
285 gitStatus: status({ refName: null }),
286 });
287
288 assert.equal(update, null);
289 });
290
291 it("does not regress a semantic thread ref back to a temporary worktree ref", () => {
292 const update = resolveLiveThreadBranchUpdate({
293 threadBranch: "t3code/github-query-rate-limit",
294 gitStatus: status({ refName: "t3code/bda76797" }),
295 });
296
297 assert.equal(update, null);
298 });
299});
300
301describe("resolveAutoFeatureBranchName", () => {
302 it("increments suffix when the preferred ref name already exists", () => {
303 const ref = resolveAutoFeatureBranchName(
304 ["main", "feature/fix-toast-copy", "feature/fix-toast-copy-2"],
305 "fix toast copy",
306 );
307 assert.equal(ref, "feature/fix-toast-copy-3");
308 });
309
310 it("treats existing ref names as case-insensitive for collision checks", () => {
311 const ref = resolveAutoFeatureBranchName(["Feature/Ticket-1"], "feature/ticket-1");
312 assert.equal(ref, "feature/ticket-1-2");
313 });
314});
315
316describe("resolveChangesTotals", () => {
317 it("does not let an empty branchChanges hide a dirty working tree", () => {
318 assert.deepInclude(
319 resolveChangesTotals(
320 status({
321 hasWorkingTreeChanges: true,
322 branchChanges: { baseRef: "main", insertions: 0, deletions: 0 },
323 workingTree: {
324 files: [
325 { path: "a.ts", insertions: 4, deletions: 1 },
326 { path: "README.md", insertions: 1, deletions: 0 },
327 ],
328 insertions: 5,
329 deletions: 1,
330 },
331 }),
332 ),
333 {
334 insertions: 5,
335 deletions: 1,
336 breakdown: {
337 code: { additions: 4, deletions: 1 },
338 tests: { additions: 0, deletions: 0 },
339 mocks: { additions: 0, deletions: 0 },
340 generated: { additions: 0, deletions: 0 },
341 docs: { additions: 1, deletions: 0 },
342 },
343 },
344 );
345 });
346
347 it("keeps non-zero branchChanges over the working tree", () => {
348 assert.deepEqual(
349 resolveChangesTotals(
350 status({
351 branchChanges: { baseRef: "origin/master", insertions: 20, deletions: 3 },
352 workingTree: {
353 files: [{ path: "a.ts", insertions: 1, deletions: 0 }],
354 insertions: 1,
355 deletions: 0,
356 },
357 }),
358 ),
359 { insertions: 20, deletions: 3, baseRef: "origin/master", breakdown: null },
360 );
361 });
362
363 it("uses the panel's branch comparison when it has file stats", () => {
364 const dirty = status({
365 branchChanges: { baseRef: "main", insertions: 20, deletions: 3 },
366 });
367 const source = {
368 id: "branch-range",
369 kind: "branch-range",
370 title: "Changes vs release",
371 baseRef: "release",
372 headRef: "feature/test",
373 diff: "",
374 diffHash: "hash",
375 truncated: false,
376 } as const;
377 assert.deepEqual(
378 resolveChangesTotals(dirty, {
379 ...source,
380 files: [
381 { path: "a.ts", previousPath: null, additions: 4, deletions: 1 },
382 { path: "a.test.ts", previousPath: null, additions: 2, deletions: 0 },
383 ],
384 }),
385 {
386 insertions: 6,
387 deletions: 1,
388 baseRef: "release",
389 breakdown: {
390 code: { additions: 4, deletions: 1 },
391 tests: { additions: 2, deletions: 0 },
392 mocks: { additions: 0, deletions: 0 },
393 generated: { additions: 0, deletions: 0 },
394 docs: { additions: 0, deletions: 0 },
395 },
396 },
397 );
398 assert.deepEqual(resolveChangesTotals(dirty, source), {
399 insertions: 20,
400 deletions: 3,
401 baseRef: "main",
402 breakdown: null,
403 });
404 });
405
406 it("falls back to the working tree when branchChanges is omitted", () => {
407 assert.deepInclude(
408 resolveChangesTotals(
409 status({
410 workingTree: {
411 files: [{ path: "a.ts", insertions: 2, deletions: 0 }],
412 insertions: 2,
413 deletions: 0,
414 },
415 }),
416 ),
417 { insertions: 2, deletions: 0 },
418 );
419 });
420});