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

BranchToolbar.logic.test.ts

279 lines8.4 KB

1import { type VcsRef } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3import { dedupeRemoteBranchesWithLocalMatches } from "@t3tools/shared/git";
4import {
5 deriveLocalBranchNameFromRemoteRef,
6 resolveBranchSelectionTarget,
7 resolveDraftEnvModeAfterBranchChange,
8 resolveEffectiveEnvMode,
9 resolveBranchToolbarPrBranch,
10 resolveLocalCheckoutBranchMismatch,
11 sanitizeNewRefName,
12 shouldIncludeBranchPickerItem,
13} from "./BranchToolbar.logic";
14​
15describe("resolveDraftEnvModeAfterBranchChange", () => {
16 it("switches to local mode when returning from an existing worktree to the main worktree", () => {
17 expect(
18 resolveDraftEnvModeAfterBranchChange({
19 nextWorktreePath: null,
20 currentWorktreePath: "/repo/.t3/worktrees/feature-a",
21 effectiveEnvMode: "worktree",
22 }),
23 ).toBe("local");
24 });
25​
26 it("uses worktree mode when selecting a ref already attached to a worktree", () => {
27 expect(
28 resolveDraftEnvModeAfterBranchChange({
29 nextWorktreePath: "/repo/.t3/worktrees/feature-a",
30 currentWorktreePath: null,
31 effectiveEnvMode: "local",
32 }),
33 ).toBe("worktree");
34 });
35});
36​
37describe("resolveBranchToolbarPrBranch", () => {
38 it("hides PR state while an optimistic branch switch is in flight", () => {
39 expect(
40 resolveBranchToolbarPrBranch({
41 activeThreadBranch: "feature/current",
42 resolvedActiveBranch: "feature/next",
43 }),
44 ).toBeNull();
45 });
46});
47​
48describe("resolveLocalCheckoutBranchMismatch", () => {
49 it("detects when a local thread is associated with a different branch than the checkout", () => {
50 expect(
51 resolveLocalCheckoutBranchMismatch({
52 effectiveEnvMode: "local",
53 activeWorktreePath: null,
54 activeThreadBranch: "feature/thread",
55 currentGitBranch: "feature/current",
56 }),
57 ).toEqual({
58 threadBranch: "feature/thread",
59 currentBranch: "feature/current",
60 });
61 });
62​
63 it("ignores dedicated worktrees because their checkout is already thread-scoped", () => {
64 expect(
65 resolveLocalCheckoutBranchMismatch({
66 effectiveEnvMode: "worktree",
67 activeWorktreePath: "/repo/.t3/worktrees/feature-thread",
68 activeThreadBranch: "feature/thread",
69 currentGitBranch: "feature/current",
70 }),
71 ).toBeNull();
72 });
73});
74​
75describe("resolveEffectiveEnvMode", () => {
76 it("keeps a server thread in worktree mode while its worktree is still being created", () => {
77 expect(
78 resolveEffectiveEnvMode({
79 activeWorktreePath: null,
80 hasServerThread: true,
81 draftThreadEnvMode: undefined,
82 preparingWorktree: true,
83 }),
84 ).toBe("worktree");
85 expect(
86 resolveEffectiveEnvMode({
87 activeWorktreePath: null,
88 hasServerThread: true,
89 draftThreadEnvMode: undefined,
90 }),
91 ).toBe("local");
92 });
93});
94​
95describe("deriveLocalBranchNameFromRemoteRef", () => {
96 it("supports remote names that contain slashes", () => {
97 expect(deriveLocalBranchNameFromRemoteRef("my-org/upstream/feature/demo")).toBe(
98 "upstream/feature/demo",
99 );
100 });
101});
102​
103describe("dedupeRemoteBranchesWithLocalMatches", () => {
104 it("hides remote refs when the matching local ref exists", () => {
105 const input: VcsRef[] = [
106 {
107 name: "feature/demo",
108 current: false,
109 isDefault: false,
110 worktreePath: null,
111 },
112 {
113 name: "origin/feature/demo",
114 isRemote: true,
115 remoteName: "origin",
116 current: false,
117 isDefault: false,
118 worktreePath: null,
119 },
120 {
121 name: "origin/feature/remote-only",
122 isRemote: true,
123 remoteName: "origin",
124 current: false,
125 isDefault: false,
126 worktreePath: null,
127 },
128 ];
129​
130 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
131 "feature/demo",
132 "origin/feature/remote-only",
133 ]);
134 });
135​
136 it("keeps non-origin remote refs visible when git tracks with first-slash local naming", () => {
137 const input: VcsRef[] = [
138 {
139 name: "upstream/feature",
140 current: false,
141 isDefault: false,
142 worktreePath: null,
143 },
144 {
145 name: "my-org/upstream/feature",
146 isRemote: true,
147 remoteName: "my-org/upstream",
148 current: false,
149 isDefault: false,
150 worktreePath: null,
151 },
152 ];
153​
154 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
155 "upstream/feature",
156 "my-org/upstream/feature",
157 ]);
158 });
159});
160​
161describe("resolveBranchSelectionTarget", () => {
162 it("reuses an existing secondary worktree for the selected ref", () => {
163 expect(
164 resolveBranchSelectionTarget({
165 activeProjectCwd: "/repo/nested-checkout/packages/app",
166 activeProjectCheckoutRoot: "/repo/nested-checkout",
167 activeWorktreePath: null,
168 refName: { isDefault: true, worktreePath: "/repo" },
169 }),
170 ).toEqual({
171 checkoutCwd: "/repo",
172 nextWorktreePath: "/repo",
173 reuseExistingWorktree: true,
174 });
175 expect(
176 resolveBranchSelectionTarget({
177 activeProjectCwd: "/repo",
178 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
179 refName: {
180 isDefault: false,
181 worktreePath: "/repo/.t3/worktrees/feature-b",
182 },
183 }),
184 ).toEqual({
185 checkoutCwd: "/repo/.t3/worktrees/feature-b",
186 nextWorktreePath: "/repo/.t3/worktrees/feature-b",
187 reuseExistingWorktree: true,
188 });
189 });
190​
191 it("switches back to the main repo when the ref already lives there", () => {
192 // A subdirectory project's own checkout is reported at the repository root.
193 // The checkout root is undefined until the project's status loads.
194 for (const [activeProjectCwd, activeWorktreePath, activeProjectCheckoutRoot] of [
195 ["/repo", "/repo/.t3/worktrees/feature-a", "/repo"],
196 ["/repo/packages/app", "/repo/.t3/worktrees/feature-a", "/repo"],
197 ["/repo/packages/app", null, "/repo"],
198 ["/repo/packages/app", "/repo/.t3/worktrees/feature-a", undefined],
199 ["/repo/packages/app", null, undefined],
200 ] as const) {
201 expect(
202 resolveBranchSelectionTarget({
203 activeProjectCwd,
204 activeProjectCheckoutRoot,
205 activeWorktreePath,
206 refName: {
207 isDefault: true,
208 worktreePath: "/repo",
209 },
210 }),
211 ).toEqual({
212 checkoutCwd: "/repo",
213 nextWorktreePath: null,
214 reuseExistingWorktree: true,
215 });
216 }
217 });
218​
219 it("keeps checkout in the current worktree for non-default refs", () => {
220 expect(
221 resolveBranchSelectionTarget({
222 activeProjectCwd: "/repo",
223 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
224 refName: {
225 isDefault: false,
226 worktreePath: null,
227 },
228 }),
229 ).toEqual({
230 checkoutCwd: "/repo/.t3/worktrees/feature-a",
231 nextWorktreePath: "/repo/.t3/worktrees/feature-a",
232 reuseExistingWorktree: false,
233 });
234 });
235});
236​
237describe("shouldIncludeBranchPickerItem", () => {
238 it("keeps the synthetic checkout PR item visible for gh pr checkout input", () => {
239 expect(
240 shouldIncludeBranchPickerItem({
241 itemValue: "__checkout_pull_request__:1359",
242 normalizedQuery: "gh pr checkout 1359",
243 createBranchItemValue: "__create_new_branch__:gh pr checkout 1359",
244 checkoutPullRequestItemValue: "__checkout_pull_request__:1359",
245 }),
246 ).toBe(true);
247 });
248​
249 it("surfaces an existing ref matching the sanitized query", () => {
250 expect(
251 shouldIncludeBranchPickerItem({
252 itemValue: "new-branch",
253 normalizedQuery: "new branch",
254 createBranchItemValue: null,
255 checkoutPullRequestItemValue: null,
256 }),
257 ).toBe(true);
258 });
259});
260​
261describe("sanitizeNewRefName", () => {
262 it("collapses a run of whitespace into a single dash", () => {
263 expect(sanitizeNewRefName("new branch")).toBe("new-branch");
264 });
265​
266 it("replaces tabs, which git rejects just like spaces", () => {
267 expect(sanitizeNewRefName("new\tbranch")).toBe("new-branch");
268 });
269​
270 it("preserves whitespace that git accepts", () => {
271 expect(sanitizeNewRefName("new\u00a0branch")).toBe("new\u00a0branch");
272 expect(sanitizeNewRefName("new\u2009branch")).toBe("new\u2009branch");
273 });
274​
275 it("does not collapse dashes the user typed", () => {
276 expect(sanitizeNewRefName("new - branch")).toBe("new---branch");
277 expect(sanitizeNewRefName("foo--bar")).toBe("foo--bar");
278 });
279});