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

GitActionsControl.logic.test.ts

249 lines7.4 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 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 };
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: local changes are behind upstream", () => {
98 it("keeps the commit action available without pushing, with or without an open PR", () => {
99 const behind = status({ hasWorkingTreeChanges: true, behindCount: 2 });
100 const commit = {
101 label: "Commit",
102 disabled: false,
103 kind: "run_action",
104 action: "commit",
105 } as const;
106 assert.deepEqual(resolveQuickAction(behind, false), commit);
107 assert.deepEqual(
108 resolveQuickAction(
109 {
110 ...behind,
111 pr: {
112 number: 13,
113 title: "Open PR",
114 url: "https://example.com/pr/13",
115 baseRef: "main",
116 headRef: "feature/test",
117 state: "open",
118 },
119 },
120 false,
121 ),
122 commit,
123 );
124 });
125});
126​
127describe("when: ref has no upstream configured", () => {
128 it("resolveQuickAction runs push and create PR when no upstream and commits are ahead", () => {
129 const quick = resolveQuickAction(
130 status({
131 hasUpstream: false,
132 aheadCount: 2,
133 pr: null,
134 }),
135 false,
136 );
137 assert.deepInclude(quick, {
138 kind: "run_action",
139 action: "create_pr",
140 label: "Push & create PR",
141 disabled: false,
142 });
143 });
144​
145 it("resolveQuickAction publishes when no origin remote exists", () => {
146 const quick = resolveQuickAction(
147 status({
148 hasUpstream: false,
149 aheadCount: 2,
150 pr: null,
151 }),
152 false,
153 false,
154 false,
155 );
156 assert.deepEqual(quick, {
157 kind: "open_publish",
158 label: "Publish repository",
159 disabled: false,
160 });
161 });
162});
163​
164describe("when: the ref is on GitLab", () => {
165 const gitLab = {
166 sourceControlProvider: {
167 kind: SourceControlProviderKind.make("gitlab"),
168 name: "GitLab",
169 baseUrl: "https://gitlab.com",
170 },
171 };
172​
173 it("names merge requests in actions and the default-ref dialog", () => {
174 const ahead = status({ ...gitLab, hasUpstream: false, aheadCount: 2 });
175 assert.equal(resolveQuickAction(ahead, false).label, "Push & create MR");
176 assert.equal(
177 resolveQuickAction(status({ ...gitLab, hasWorkingTreeChanges: true }), false).label,
178 "Commit, push & MR",
179 );
180 assert.equal(buildMenuItems(ahead, false).find((item) => item.id === "pr")?.label, "Create MR");
181​
182 const copy = resolveDefaultBranchActionDialogCopy({
183 action: "create_pr",
184 branchName: "main",
185 includesCommit: false,
186 terminology: sourceControlClients.get("gitlab").changeRequest,
187 });
188 assert.equal(copy.title, "Push & create MR from default ref?");
189 assert.include(copy.description, "create a merge request on");
190 });
191});
192​
193describe("requiresDefaultBranchConfirmation", () => {
194 it("requires confirmation for push actions on default ref", () => {
195 assert.isFalse(requiresDefaultBranchConfirmation("commit", true));
196 assert.isTrue(requiresDefaultBranchConfirmation("push", true));
197 assert.isTrue(requiresDefaultBranchConfirmation("create_pr", true));
198 assert.isTrue(requiresDefaultBranchConfirmation("commit_push", true));
199 assert.isTrue(requiresDefaultBranchConfirmation("commit_push_pr", true));
200 assert.isFalse(requiresDefaultBranchConfirmation("commit_push", false));
201 assert.isFalse(requiresDefaultBranchConfirmation("push", false));
202 });
203});
204​
205describe("resolveLiveThreadBranchUpdate", () => {
206 it("returns a branch update when live git status differs from stored thread metadata", () => {
207 const update = resolveLiveThreadBranchUpdate({
208 threadBranch: "feature/old-ref",
209 gitStatus: status({ refName: "effect-atom" }),
210 });
211​
212 assert.deepEqual(update, {
213 branch: "effect-atom",
214 });
215 });
216​
217 it("returns null when git status is detached HEAD but the thread already has a ref", () => {
218 const update = resolveLiveThreadBranchUpdate({
219 threadBranch: "effect-atom",
220 gitStatus: status({ refName: null }),
221 });
222​
223 assert.equal(update, null);
224 });
225​
226 it("does not regress a semantic thread ref back to a temporary worktree ref", () => {
227 const update = resolveLiveThreadBranchUpdate({
228 threadBranch: "t3code/github-query-rate-limit",
229 gitStatus: status({ refName: "t3code/bda76797" }),
230 });
231​
232 assert.equal(update, null);
233 });
234});
235​
236describe("resolveAutoFeatureBranchName", () => {
237 it("increments suffix when the preferred ref name already exists", () => {
238 const ref = resolveAutoFeatureBranchName(
239 ["main", "feature/fix-toast-copy", "feature/fix-toast-copy-2"],
240 "fix toast copy",
241 );
242 assert.equal(ref, "feature/fix-toast-copy-3");
243 });
244​
245 it("treats existing ref names as case-insensitive for collision checks", () => {
246 const ref = resolveAutoFeatureBranchName(["Feature/Ticket-1"], "feature/ticket-1");
247 assert.equal(ref, "feature/ticket-1-2");
248 });
249});