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

BranchToolbar.logic.test.ts

410 lines12.0 KB

1import { EnvironmentId, type VcsRef } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3import {
4 dedupeRemoteBranchesWithLocalMatches,
5 deriveLocalBranchNameFromRemoteRef,
6 resolveEnvironmentOptionLabel,
7 resolveBranchSelectionTarget,
8 resolveCurrentWorkspaceLabel,
9 resolveDraftEnvModeAfterBranchChange,
10 resolveEffectiveEnvMode,
11 resolveEnvModeLabel,
12 resolveBranchToolbarValue,
13 resolveLockedWorkspaceLabel,
14 resolveWorkspaceDisplayName,
15 shouldIncludeBranchPickerItem,
16} from "./BranchToolbar.logic";
17​
18const localEnvironmentId = EnvironmentId.make("environment-local");
19const remoteEnvironmentId = EnvironmentId.make("environment-remote");
20​
21describe("resolveDraftEnvModeAfterBranchChange", () => {
22 it("switches to local mode when returning from an existing worktree to the main worktree", () => {
23 expect(
24 resolveDraftEnvModeAfterBranchChange({
25 nextWorktreePath: null,
26 currentWorktreePath: "/repo/.t3/worktrees/feature-a",
27 effectiveEnvMode: "worktree",
28 }),
29 ).toBe("local");
30 });
31​
32 it("keeps new-worktree mode when selecting a base ref before worktree creation", () => {
33 expect(
34 resolveDraftEnvModeAfterBranchChange({
35 nextWorktreePath: null,
36 currentWorktreePath: null,
37 effectiveEnvMode: "worktree",
38 }),
39 ).toBe("worktree");
40 });
41​
42 it("uses worktree mode when selecting a ref already attached to a worktree", () => {
43 expect(
44 resolveDraftEnvModeAfterBranchChange({
45 nextWorktreePath: "/repo/.t3/worktrees/feature-a",
46 currentWorktreePath: null,
47 effectiveEnvMode: "local",
48 }),
49 ).toBe("worktree");
50 });
51});
52​
53describe("resolveBranchToolbarValue", () => {
54 it("defaults new-worktree mode to current git ref when no explicit base ref is set", () => {
55 expect(
56 resolveBranchToolbarValue({
57 envMode: "worktree",
58 activeWorktreePath: null,
59 activeThreadBranch: null,
60 currentGitBranch: "main",
61 }),
62 ).toBe("main");
63 });
64​
65 it("keeps an explicitly selected worktree base ref", () => {
66 expect(
67 resolveBranchToolbarValue({
68 envMode: "worktree",
69 activeWorktreePath: null,
70 activeThreadBranch: "feature/base",
71 currentGitBranch: "main",
72 }),
73 ).toBe("feature/base");
74 });
75​
76 it("shows the actual checked-out ref when not selecting a new worktree base", () => {
77 expect(
78 resolveBranchToolbarValue({
79 envMode: "local",
80 activeWorktreePath: null,
81 activeThreadBranch: "feature/base",
82 currentGitBranch: "main",
83 }),
84 ).toBe("main");
85 });
86});
87​
88describe("resolveEnvironmentOptionLabel", () => {
89 it("prefers the primary environment's machine label", () => {
90 expect(
91 resolveEnvironmentOptionLabel({
92 isPrimary: true,
93 environmentId: localEnvironmentId,
94 runtimeLabel: "Julius's Mac mini",
95 savedLabel: "Local environment",
96 }),
97 ).toBe("Julius's Mac mini");
98 });
99​
100 it("falls back to 'This device' for generic primary labels", () => {
101 expect(
102 resolveEnvironmentOptionLabel({
103 isPrimary: true,
104 environmentId: localEnvironmentId,
105 runtimeLabel: "Local environment",
106 savedLabel: "Local",
107 }),
108 ).toBe("This device");
109 });
110​
111 it("keeps configured labels for non-primary environments", () => {
112 expect(
113 resolveEnvironmentOptionLabel({
114 isPrimary: false,
115 environmentId: remoteEnvironmentId,
116 runtimeLabel: null,
117 savedLabel: "Build box",
118 }),
119 ).toBe("Build box");
120 });
121});
122​
123describe("resolveEffectiveEnvMode", () => {
124 it("treats draft threads already attached to a worktree as current-checkout mode", () => {
125 expect(
126 resolveEffectiveEnvMode({
127 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
128 hasServerThread: false,
129 draftThreadEnvMode: "worktree",
130 }),
131 ).toBe("local");
132 });
133​
134 it("keeps explicit new-worktree mode for draft threads without a worktree path", () => {
135 expect(
136 resolveEffectiveEnvMode({
137 activeWorktreePath: null,
138 hasServerThread: false,
139 draftThreadEnvMode: "worktree",
140 }),
141 ).toBe("worktree");
142 });
143});
144​
145describe("resolveEnvModeLabel", () => {
146 it("uses explicit workspace labels", () => {
147 expect(resolveEnvModeLabel("local")).toBe("Current checkout");
148 expect(resolveEnvModeLabel("worktree")).toBe("New worktree");
149 });
150});
151​
152describe("resolveCurrentWorkspaceLabel", () => {
153 it("describes the main repo checkout when no worktree path is active", () => {
154 expect(resolveCurrentWorkspaceLabel(null)).toBe("Current checkout");
155 });
156​
157 it("describes the active checkout as a worktree when one is attached", () => {
158 expect(resolveCurrentWorkspaceLabel("/repo/.t3/worktrees/feature-a")).toBe("Current worktree");
159 });
160});
161​
162describe("resolveLockedWorkspaceLabel", () => {
163 it("uses a shorter label for the main repo checkout", () => {
164 expect(resolveLockedWorkspaceLabel(null)).toBe("Local checkout");
165 });
166​
167 it("uses a shorter label for an attached worktree", () => {
168 expect(resolveLockedWorkspaceLabel("/repo/.t3/worktrees/feature-a")).toBe("Worktree");
169 });
170});
171​
172describe("resolveWorkspaceDisplayName", () => {
173 it("returns the final folder for POSIX and Windows paths", () => {
174 expect(resolveWorkspaceDisplayName("/repo/.t3/worktrees/feature-a")).toBe("feature-a");
175 expect(resolveWorkspaceDisplayName("C:\\code\\project\\feature-b\\")).toBe("feature-b");
176 });
177​
178 it("handles missing and root paths", () => {
179 expect(resolveWorkspaceDisplayName(null)).toBeNull();
180 expect(resolveWorkspaceDisplayName("/")).toBe("/");
181 });
182});
183​
184describe("deriveLocalBranchNameFromRemoteRef", () => {
185 it("strips the remote prefix from a remote ref", () => {
186 expect(deriveLocalBranchNameFromRemoteRef("origin/feature/demo")).toBe("feature/demo");
187 });
188​
189 it("supports remote names that contain slashes", () => {
190 expect(deriveLocalBranchNameFromRemoteRef("my-org/upstream/feature/demo")).toBe(
191 "upstream/feature/demo",
192 );
193 });
194​
195 it("returns the original name when ref is malformed", () => {
196 expect(deriveLocalBranchNameFromRemoteRef("origin/")).toBe("origin/");
197 expect(deriveLocalBranchNameFromRemoteRef("/feature/demo")).toBe("/feature/demo");
198 });
199});
200​
201describe("dedupeRemoteBranchesWithLocalMatches", () => {
202 it("hides remote refs when the matching local ref exists", () => {
203 const input: VcsRef[] = [
204 {
205 name: "feature/demo",
206 current: false,
207 isDefault: false,
208 worktreePath: null,
209 },
210 {
211 name: "origin/feature/demo",
212 isRemote: true,
213 remoteName: "origin",
214 current: false,
215 isDefault: false,
216 worktreePath: null,
217 },
218 {
219 name: "origin/feature/remote-only",
220 isRemote: true,
221 remoteName: "origin",
222 current: false,
223 isDefault: false,
224 worktreePath: null,
225 },
226 ];
227​
228 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
229 "feature/demo",
230 "origin/feature/remote-only",
231 ]);
232 });
233​
234 it("keeps all entries when no local match exists for a remote ref", () => {
235 const input: VcsRef[] = [
236 {
237 name: "feature/local",
238 current: false,
239 isDefault: false,
240 worktreePath: null,
241 },
242 {
243 name: "origin/feature/remote-only",
244 isRemote: true,
245 remoteName: "origin",
246 current: false,
247 isDefault: false,
248 worktreePath: null,
249 },
250 ];
251​
252 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
253 "feature/local",
254 "origin/feature/remote-only",
255 ]);
256 });
257​
258 it("keeps non-origin remote refs visible even when a matching local ref exists", () => {
259 const input: VcsRef[] = [
260 {
261 name: "feature/demo",
262 current: false,
263 isDefault: false,
264 worktreePath: null,
265 },
266 {
267 name: "my-org/upstream/feature/demo",
268 isRemote: true,
269 remoteName: "my-org/upstream",
270 current: false,
271 isDefault: false,
272 worktreePath: null,
273 },
274 ];
275​
276 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
277 "feature/demo",
278 "my-org/upstream/feature/demo",
279 ]);
280 });
281​
282 it("keeps non-origin remote refs visible when git tracks with first-slash local naming", () => {
283 const input: VcsRef[] = [
284 {
285 name: "upstream/feature",
286 current: false,
287 isDefault: false,
288 worktreePath: null,
289 },
290 {
291 name: "my-org/upstream/feature",
292 isRemote: true,
293 remoteName: "my-org/upstream",
294 current: false,
295 isDefault: false,
296 worktreePath: null,
297 },
298 ];
299​
300 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
301 "upstream/feature",
302 "my-org/upstream/feature",
303 ]);
304 });
305});
306​
307describe("resolveBranchSelectionTarget", () => {
308 it("reuses an existing secondary worktree for the selected ref", () => {
309 expect(
310 resolveBranchSelectionTarget({
311 activeProjectCwd: "/repo",
312 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
313 refName: {
314 isDefault: false,
315 worktreePath: "/repo/.t3/worktrees/feature-b",
316 },
317 }),
318 ).toEqual({
319 checkoutCwd: "/repo/.t3/worktrees/feature-b",
320 nextWorktreePath: "/repo/.t3/worktrees/feature-b",
321 reuseExistingWorktree: true,
322 });
323 });
324​
325 it("switches back to the main repo when the ref already lives there", () => {
326 expect(
327 resolveBranchSelectionTarget({
328 activeProjectCwd: "/repo",
329 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
330 refName: {
331 isDefault: true,
332 worktreePath: "/repo",
333 },
334 }),
335 ).toEqual({
336 checkoutCwd: "/repo",
337 nextWorktreePath: null,
338 reuseExistingWorktree: true,
339 });
340 });
341​
342 it("checks out the default ref in the main repo when leaving a secondary worktree", () => {
343 expect(
344 resolveBranchSelectionTarget({
345 activeProjectCwd: "/repo",
346 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
347 refName: {
348 isDefault: true,
349 worktreePath: null,
350 },
351 }),
352 ).toEqual({
353 checkoutCwd: "/repo",
354 nextWorktreePath: null,
355 reuseExistingWorktree: false,
356 });
357 });
358​
359 it("keeps checkout in the current worktree for non-default refs", () => {
360 expect(
361 resolveBranchSelectionTarget({
362 activeProjectCwd: "/repo",
363 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
364 refName: {
365 isDefault: false,
366 worktreePath: null,
367 },
368 }),
369 ).toEqual({
370 checkoutCwd: "/repo/.t3/worktrees/feature-a",
371 nextWorktreePath: "/repo/.t3/worktrees/feature-a",
372 reuseExistingWorktree: false,
373 });
374 });
375});
376​
377describe("shouldIncludeBranchPickerItem", () => {
378 it("keeps the synthetic checkout PR item visible for gh pr checkout input", () => {
379 expect(
380 shouldIncludeBranchPickerItem({
381 itemValue: "__checkout_pull_request__:1359",
382 normalizedQuery: "gh pr checkout 1359",
383 createBranchItemValue: "__create_new_branch__:gh pr checkout 1359",
384 checkoutPullRequestItemValue: "__checkout_pull_request__:1359",
385 }),
386 ).toBe(true);
387 });
388​
389 it("keeps the synthetic create-ref item visible for arbitrary ref input", () => {
390 expect(
391 shouldIncludeBranchPickerItem({
392 itemValue: "__create_new_branch__:feature/demo",
393 normalizedQuery: "feature/demo",
394 createBranchItemValue: "__create_new_branch__:feature/demo",
395 checkoutPullRequestItemValue: null,
396 }),
397 ).toBe(true);
398 });
399​
400 it("still filters ordinary ref items by query text", () => {
401 expect(
402 shouldIncludeBranchPickerItem({
403 itemValue: "main",
404 normalizedQuery: "gh pr checkout 1359",
405 createBranchItemValue: "__create_new_branch__:gh pr checkout 1359",
406 checkoutPullRequestItemValue: "__checkout_pull_request__:1359",
407 }),
408 ).toBe(false);
409 });
410});