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