@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 404 lines 12.5 KB
Raw
1 import { SourceControlProviderKind, type VcsStatusResult } from "@t3tools/contracts";
2 import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
3 import { resolveAutoFeatureBranchName } from "@t3tools/shared/git";
4 import { assert, describe, it } from "vite-plus/test";
5 import {
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
18 function 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
38 function 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
44 describe("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
64 describe("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
116 describe("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
142 describe("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
176 describe("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
213 describe("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
242 describe("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
254 describe("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
285 describe("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
300 describe("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: [
309 { path: "a.ts", insertions: 4, deletions: 1 },
310 { path: "README.md", insertions: 1, deletions: 0 },
311 ],
312 insertions: 5,
313 deletions: 1,
314 },
315 }),
316 ),
317 {
318 insertions: 5,
319 deletions: 1,
320 breakdown: {
321 code: { additions: 4, deletions: 1 },
322 tests: { additions: 0, deletions: 0 },
323 mocks: { additions: 0, deletions: 0 },
324 generated: { additions: 0, deletions: 0 },
325 docs: { additions: 1, deletions: 0 },
326 },
327 },
328 );
329 });
330
331 it("keeps non-zero branchChanges over the working tree", () => {
332 assert.deepEqual(
333 resolveChangesTotals(
334 status({
335 branchChanges: { baseRef: "origin/master", insertions: 20, deletions: 3 },
336 workingTree: {
337 files: [{ path: "a.ts", insertions: 1, deletions: 0 }],
338 insertions: 1,
339 deletions: 0,
340 },
341 }),
342 ),
343 { insertions: 20, deletions: 3, baseRef: "origin/master", breakdown: null },
344 );
345 });
346
347 it("uses the panel's branch comparison when it has file stats", () => {
348 const dirty = status({
349 branchChanges: { baseRef: "main", insertions: 20, deletions: 3 },
350 });
351 const source = {
352 id: "branch-range",
353 kind: "branch-range",
354 title: "Changes vs release",
355 baseRef: "release",
356 headRef: "feature/test",
357 diff: "",
358 diffHash: "hash",
359 truncated: false,
360 } as const;
361 assert.deepEqual(
362 resolveChangesTotals(dirty, {
363 ...source,
364 files: [
365 { path: "a.ts", previousPath: null, additions: 4, deletions: 1 },
366 { path: "a.test.ts", previousPath: null, additions: 2, deletions: 0 },
367 ],
368 }),
369 {
370 insertions: 6,
371 deletions: 1,
372 baseRef: "release",
373 breakdown: {
374 code: { additions: 4, deletions: 1 },
375 tests: { additions: 2, deletions: 0 },
376 mocks: { additions: 0, deletions: 0 },
377 generated: { additions: 0, deletions: 0 },
378 docs: { additions: 0, deletions: 0 },
379 },
380 },
381 );
382 assert.deepEqual(resolveChangesTotals(dirty, source), {
383 insertions: 20,
384 deletions: 3,
385 baseRef: "main",
386 breakdown: null,
387 });
388 });
389
390 it("falls back to the working tree when branchChanges is omitted", () => {
391 assert.deepInclude(
392 resolveChangesTotals(
393 status({
394 workingTree: {
395 files: [{ path: "a.ts", insertions: 2, deletions: 0 }],
396 insertions: 2,
397 deletions: 0,
398 },
399 }),
400 ),
401 { insertions: 2, deletions: 0 },
402 );
403 });
404 });