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