@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 434 lines 13.5 KB
Raw
1 import { EnvironmentId, ProviderInstanceId, RunId, type VcsRef } from "@t3tools/contracts";
2 import { describe, expect, it } from "vite-plus/test";
3 import { dedupeRemoteBranchesWithLocalMatches } from "@t3tools/shared/git";
4 import {
5 deriveLocalBranchNameFromRemoteRef,
6 isWorktreeChangeBlocked,
7 resolveBranchSelectionTarget,
8 resolveDraftEnvModeAfterBranchChange,
9 resolveEffectiveEnvMode,
10 resolveBranchToolbarPrBranch,
11 resolveConfirmedSelectedRef,
12 resolveDefaultWorktreeBaseBranch,
13 resolveLocalCheckoutBranchMismatch,
14 resolveLockedWorkspaceLabel,
15 sanitizeNewRefName,
16 shouldIncludeBranchPickerItem,
17 isBranchHoldCurrent,
18 } from "./BranchToolbar.logic";
19
20 describe("resolveDraftEnvModeAfterBranchChange", () => {
21 it("switches to local mode when returning from an existing worktree to the main worktree", () => {
22 expect(
23 resolveDraftEnvModeAfterBranchChange({
24 nextWorktreePath: null,
25 currentWorktreePath: "/repo/.t3/worktrees/feature-a",
26 effectiveEnvMode: "worktree",
27 }),
28 ).toBe("local");
29 });
30
31 it("uses worktree mode when selecting a ref already attached to a worktree", () => {
32 expect(
33 resolveDraftEnvModeAfterBranchChange({
34 nextWorktreePath: "/repo/.t3/worktrees/feature-a",
35 currentWorktreePath: null,
36 effectiveEnvMode: "local",
37 }),
38 ).toBe("worktree");
39 });
40 });
41
42 describe("resolveBranchToolbarPrBranch", () => {
43 it("hides PR state while an optimistic branch switch is in flight", () => {
44 expect(
45 resolveBranchToolbarPrBranch({
46 activeThreadBranch: "feature/current",
47 resolvedActiveBranch: "feature/next",
48 }),
49 ).toBeNull();
50 });
51 });
52
53 describe("resolveDefaultWorktreeBaseBranch", () => {
54 it("offers nothing while a clone has no refs, then the default branch once they are listed", () => {
55 expect(resolveDefaultWorktreeBaseBranch([])).toBeNull();
56 expect(
57 resolveDefaultWorktreeBaseBranch([
58 { name: "feature", isDefault: false, current: true },
59 { name: "staging", isDefault: true, current: false },
60 ]),
61 ).toBe("staging");
62 expect(
63 resolveDefaultWorktreeBaseBranch([
64 { name: "other", isDefault: false, current: false },
65 { name: "feature", isDefault: false, current: true },
66 ]),
67 ).toBe("feature");
68 });
69 });
70
71 describe("resolveLocalCheckoutBranchMismatch", () => {
72 it("detects when a local thread is associated with a different branch than the checkout", () => {
73 expect(
74 resolveLocalCheckoutBranchMismatch({
75 effectiveEnvMode: "local",
76 activeWorktreePath: null,
77 activeThreadBranch: "feature/thread",
78 currentGitBranch: "feature/current",
79 }),
80 ).toEqual({
81 threadBranch: "feature/thread",
82 currentBranch: "feature/current",
83 });
84 });
85
86 it("ignores dedicated worktrees because their checkout is already thread-scoped", () => {
87 expect(
88 resolveLocalCheckoutBranchMismatch({
89 effectiveEnvMode: "worktree",
90 activeWorktreePath: "/repo/.t3/worktrees/feature-thread",
91 activeThreadBranch: "feature/thread",
92 currentGitBranch: "feature/current",
93 }),
94 ).toBeNull();
95 });
96 });
97
98 describe("resolveEffectiveEnvMode", () => {
99 it("keeps a server thread in worktree mode while its worktree is still being created", () => {
100 expect(
101 resolveEffectiveEnvMode({
102 activeWorktreePath: null,
103 hasServerThread: true,
104 draftThreadEnvMode: undefined,
105 preparingWorktree: true,
106 }),
107 ).toBe("worktree");
108 expect(
109 resolveEffectiveEnvMode({
110 activeWorktreePath: null,
111 hasServerThread: true,
112 draftThreadEnvMode: undefined,
113 }),
114 ).toBe("local");
115 });
116 });
117
118 describe("resolveLockedWorkspaceLabel", () => {
119 it.each([
120 ["/home/me/.t3/worktrees/app/t3-ff466c42", "t3-ff466c42"],
121 ["/home/me/.t3/worktrees/app/t3-ff466c42/", "t3-ff466c42"],
122 ["C:\\Users\\me\\worktrees\\feature-x", "feature-x"],
123 ])("names the worktree at %s", (path, label) => {
124 expect(resolveLockedWorkspaceLabel(path, "worktree")).toBe(label);
125 });
126 });
127
128 describe("deriveLocalBranchNameFromRemoteRef", () => {
129 it("supports remote names that contain slashes", () => {
130 expect(deriveLocalBranchNameFromRemoteRef("my-org/upstream/feature/demo")).toBe(
131 "upstream/feature/demo",
132 );
133 });
134 });
135
136 describe("dedupeRemoteBranchesWithLocalMatches", () => {
137 it.each([
138 ["feature/demo", "origin/feature/demo", true],
139 ["feature/demo", "feature/demo", false],
140 ["demo", "demo", true],
141 ["origin/", "origin/", true],
142 ["/demo", "/demo", true],
143 ["", "", false],
144 ])("filters origin ref %s / %s (hidden: %s)", (localName, remoteName, hidden) => {
145 const input: VcsRef[] = [
146 {
147 name: localName,
148 current: false,
149 isDefault: false,
150 worktreePath: null,
151 },
152 {
153 name: remoteName,
154 isRemote: true,
155 remoteName: "origin",
156 current: false,
157 isDefault: false,
158 worktreePath: null,
159 },
160 {
161 name: "origin/feature/remote-only",
162 isRemote: true,
163 remoteName: "origin",
164 current: false,
165 isDefault: false,
166 worktreePath: null,
167 },
168 ];
169
170 const result = dedupeRemoteBranchesWithLocalMatches(input);
171 const expected = hidden ? [input[0], input[2]] : input;
172 expect(result).toEqual(expected);
173 for (const [index, ref] of result.entries()) {
174 expect(ref).toBe(expected[index]);
175 }
176 });
177
178 it("keeps non-origin remote refs visible when git tracks with first-slash local naming", () => {
179 const input: VcsRef[] = [
180 {
181 name: "upstream/feature",
182 current: false,
183 isDefault: false,
184 worktreePath: null,
185 },
186 {
187 name: "my-org/upstream/feature",
188 isRemote: true,
189 remoteName: "my-org/upstream",
190 current: false,
191 isDefault: false,
192 worktreePath: null,
193 },
194 ];
195
196 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
197 "upstream/feature",
198 "my-org/upstream/feature",
199 ]);
200 });
201 });
202
203 describe("resolveBranchSelectionTarget", () => {
204 it("reuses an existing secondary worktree for the selected ref", () => {
205 expect(
206 resolveBranchSelectionTarget({
207 activeProjectCwd: "/repo/nested-checkout/packages/app",
208 activeProjectCheckoutRoot: "/repo/nested-checkout",
209 activeWorktreePath: null,
210 refName: { isDefault: true, worktreePath: "/repo" },
211 }),
212 ).toEqual({
213 checkoutCwd: "/repo",
214 nextWorktreePath: "/repo",
215 reuseExistingWorktree: true,
216 staysInActiveCheckout: false,
217 });
218 expect(
219 resolveBranchSelectionTarget({
220 activeProjectCwd: "/repo",
221 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
222 refName: {
223 isDefault: false,
224 worktreePath: "/repo/.t3/worktrees/feature-b",
225 },
226 }),
227 ).toEqual({
228 checkoutCwd: "/repo/.t3/worktrees/feature-b",
229 nextWorktreePath: "/repo/.t3/worktrees/feature-b",
230 reuseExistingWorktree: true,
231 staysInActiveCheckout: false,
232 });
233 });
234
235 it("switches back to the main repo when the ref already lives there", () => {
236 // A subdirectory project's own checkout is reported at the repository root.
237 // The checkout root is undefined until the project's status loads.
238 for (const [activeProjectCwd, activeWorktreePath, activeProjectCheckoutRoot] of [
239 ["/repo", "/repo/.t3/worktrees/feature-a", "/repo"],
240 ["/repo/packages/app", "/repo/.t3/worktrees/feature-a", "/repo"],
241 ["/repo/packages/app", null, "/repo"],
242 ["/repo/packages/app", "/repo/.t3/worktrees/feature-a", undefined],
243 ["/repo/packages/app", null, undefined],
244 ] as const) {
245 expect(
246 resolveBranchSelectionTarget({
247 activeProjectCwd,
248 activeProjectCheckoutRoot,
249 activeWorktreePath,
250 refName: {
251 isDefault: true,
252 worktreePath: "/repo",
253 },
254 }),
255 ).toEqual({
256 checkoutCwd: "/repo",
257 nextWorktreePath: null,
258 reuseExistingWorktree: true,
259 // A ref listed in the thread's own checkout may be stale, so it is checked out, not assumed.
260 staysInActiveCheckout: activeWorktreePath === null,
261 });
262 }
263 });
264
265 it("keeps checkout in the current worktree for non-default refs", () => {
266 expect(
267 resolveBranchSelectionTarget({
268 activeProjectCwd: "/repo",
269 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
270 refName: {
271 isDefault: false,
272 worktreePath: null,
273 },
274 }),
275 ).toEqual({
276 checkoutCwd: "/repo/.t3/worktrees/feature-a",
277 nextWorktreePath: "/repo/.t3/worktrees/feature-a",
278 reuseExistingWorktree: false,
279 staysInActiveCheckout: true,
280 });
281 });
282 });
283
284 describe("isWorktreeChangeBlocked", () => {
285 const runtime = (status: "preparing" | "running" | "completed") => ({
286 status,
287 activeRunId: status === "completed" ? null : RunId.make("run"),
288 providerInstanceId: ProviderInstanceId.make("codex"),
289 providerName: null,
290 lastError: null,
291 });
292
293 it.each([
294 ["preparing", "/repo/wt-a", null, true],
295 ["running", "/repo/wt-a", "/repo/wt-b", true],
296 ["running", null, "/repo/wt-b", true],
297 ["running", "/repo/wt-a", "/repo/wt-a", false],
298 ["completed", "/repo/wt-a", null, false],
299 ] as const)(
300 "a %s turn moving %s -> %s is blocked: %s",
301 (status, currentWorktreePath, nextWorktreePath, blocked) => {
302 expect(
303 isWorktreeChangeBlocked({
304 runtime: runtime(status),
305 currentWorktreePath,
306 nextWorktreePath,
307 }),
308 ).toBe(blocked);
309 },
310 );
311 });
312
313 describe("shouldIncludeBranchPickerItem", () => {
314 it("keeps the synthetic checkout PR item visible for gh pr checkout input", () => {
315 expect(
316 shouldIncludeBranchPickerItem({
317 itemValue: "__checkout_pull_request__:1359",
318 normalizedQuery: "gh pr checkout 1359",
319 createBranchItemValue: "__create_new_branch__:gh pr checkout 1359",
320 checkoutPullRequestItemValue: "__checkout_pull_request__:1359",
321 }),
322 ).toBe(true);
323 });
324
325 it("surfaces an existing ref matching the sanitized query", () => {
326 expect(
327 shouldIncludeBranchPickerItem({
328 itemValue: "new-branch",
329 normalizedQuery: "new branch",
330 createBranchItemValue: null,
331 checkoutPullRequestItemValue: null,
332 }),
333 ).toBe(true);
334 });
335 });
336
337 describe("isBranchHoldCurrent", () => {
338 const environmentId = EnvironmentId.make("env-a");
339 const hold = {
340 branch: "feature",
341 environmentId,
342 checkouts: [{ cwd: "/repo", replaces: "main" }],
343 };
344 const current = { canonicalBranch: "main", environmentId, cwd: "/repo" };
345
346 it("keeps showing the checked-out branch while status still reports the old one", () => {
347 expect(isBranchHoldCurrent(hold, current)).toBe(true);
348 });
349
350 it("lets go once status moves, even to a different branch than the checkout", () => {
351 for (const canonicalBranch of ["feature", "other", null]) {
352 expect(isBranchHoldCurrent(hold, { ...current, canonicalBranch })).toBe(false);
353 }
354 });
355
356 it("does not apply to another checkout that happens to be on the old branch", () => {
357 expect(isBranchHoldCurrent(hold, { ...current, cwd: "/other" })).toBe(false);
358 });
359
360 it("follows a thread moving from its worktree to the project root", () => {
361 const moveHold = {
362 branch: "main",
363 environmentId,
364 checkouts: [
365 { cwd: "/repo-feature", replaces: "feature" },
366 { cwd: "/repo", replaces: "dev" },
367 ],
368 };
369 expect(
370 isBranchHoldCurrent(moveHold, {
371 environmentId,
372 cwd: "/repo-feature",
373 canonicalBranch: "feature",
374 }),
375 ).toBe(true);
376 expect(
377 isBranchHoldCurrent(moveHold, { environmentId, cwd: "/repo", canonicalBranch: "dev" }),
378 ).toBe(true);
379 expect(
380 isBranchHoldCurrent(moveHold, { environmentId, cwd: "/repo", canonicalBranch: "main" }),
381 ).toBe(false);
382 });
383
384 it("does not apply to the same path on another environment", () => {
385 expect(
386 isBranchHoldCurrent(hold, { ...current, environmentId: EnvironmentId.make("env-b") }),
387 ).toBe(false);
388 });
389 });
390
391 describe("sanitizeNewRefName", () => {
392 it("collapses a run of whitespace into a single dash", () => {
393 expect(sanitizeNewRefName("new branch")).toBe("new-branch");
394 });
395
396 it("replaces tabs, which git rejects just like spaces", () => {
397 expect(sanitizeNewRefName("new\tbranch")).toBe("new-branch");
398 });
399
400 it("preserves whitespace that git accepts", () => {
401 expect(sanitizeNewRefName("new\u00a0branch")).toBe("new\u00a0branch");
402 expect(sanitizeNewRefName("new\u2009branch")).toBe("new\u2009branch");
403 });
404
405 it("does not collapse dashes the user typed", () => {
406 expect(sanitizeNewRefName("new - branch")).toBe("new---branch");
407 expect(sanitizeNewRefName("foo--bar")).toBe("foo--bar");
408 });
409 });
410
411 describe("resolveConfirmedSelectedRef", () => {
412 const listed: VcsRef = {
413 name: "previous",
414 isRemote: false,
415 current: false,
416 isDefault: false,
417 worktreePath: "/worktree-b",
418 };
419
420 it.each([
421 [
422 "the claimed worktree still has it checked out",
423 [{ ...listed, current: true }],
424 "/worktree-b",
425 ],
426 ["the claimed worktree has moved to another branch", [], null],
427 ["the claimed worktree lists it but not as current", [listed], null],
428 ["the read failed", null, null],
429 ] as const)("%s", (_label, refs, expected) => {
430 expect(resolveConfirmedSelectedRef(listed, refs === null ? null : { refs }).worktreePath).toBe(
431 expected,
432 );
433 });
434 });