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