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

BranchToolbar.logic.test.ts

288 lines8.7 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.each([
105 ["feature/demo", "origin/feature/demo", true],
106 ["feature/demo", "feature/demo", false],
107 ["demo", "demo", true],
108 ["origin/", "origin/", true],
109 ["/demo", "/demo", true],
110 ["", "", false],
111 ])("filters origin ref %s / %s (hidden: %s)", (localName, remoteName, hidden) => {
112 const input: VcsRef[] = [
113 {
114 name: localName,
115 current: false,
116 isDefault: false,
117 worktreePath: null,
118 },
119 {
120 name: remoteName,
121 isRemote: true,
122 remoteName: "origin",
123 current: false,
124 isDefault: false,
125 worktreePath: null,
126 },
127 {
128 name: "origin/feature/remote-only",
129 isRemote: true,
130 remoteName: "origin",
131 current: false,
132 isDefault: false,
133 worktreePath: null,
134 },
135 ];
136​
137 const result = dedupeRemoteBranchesWithLocalMatches(input);
138 const expected = hidden ? [input[0], input[2]] : input;
139 expect(result).toEqual(expected);
140 for (const [index, ref] of result.entries()) {
141 expect(ref).toBe(expected[index]);
142 }
143 });
144​
145 it("keeps non-origin remote refs visible when git tracks with first-slash local naming", () => {
146 const input: VcsRef[] = [
147 {
148 name: "upstream/feature",
149 current: false,
150 isDefault: false,
151 worktreePath: null,
152 },
153 {
154 name: "my-org/upstream/feature",
155 isRemote: true,
156 remoteName: "my-org/upstream",
157 current: false,
158 isDefault: false,
159 worktreePath: null,
160 },
161 ];
162​
163 expect(dedupeRemoteBranchesWithLocalMatches(input).map((ref) => ref.name)).toEqual([
164 "upstream/feature",
165 "my-org/upstream/feature",
166 ]);
167 });
168});
169​
170describe("resolveBranchSelectionTarget", () => {
171 it("reuses an existing secondary worktree for the selected ref", () => {
172 expect(
173 resolveBranchSelectionTarget({
174 activeProjectCwd: "/repo/nested-checkout/packages/app",
175 activeProjectCheckoutRoot: "/repo/nested-checkout",
176 activeWorktreePath: null,
177 refName: { isDefault: true, worktreePath: "/repo" },
178 }),
179 ).toEqual({
180 checkoutCwd: "/repo",
181 nextWorktreePath: "/repo",
182 reuseExistingWorktree: true,
183 });
184 expect(
185 resolveBranchSelectionTarget({
186 activeProjectCwd: "/repo",
187 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
188 refName: {
189 isDefault: false,
190 worktreePath: "/repo/.t3/worktrees/feature-b",
191 },
192 }),
193 ).toEqual({
194 checkoutCwd: "/repo/.t3/worktrees/feature-b",
195 nextWorktreePath: "/repo/.t3/worktrees/feature-b",
196 reuseExistingWorktree: true,
197 });
198 });
199​
200 it("switches back to the main repo when the ref already lives there", () => {
201 // A subdirectory project's own checkout is reported at the repository root.
202 // The checkout root is undefined until the project's status loads.
203 for (const [activeProjectCwd, activeWorktreePath, activeProjectCheckoutRoot] of [
204 ["/repo", "/repo/.t3/worktrees/feature-a", "/repo"],
205 ["/repo/packages/app", "/repo/.t3/worktrees/feature-a", "/repo"],
206 ["/repo/packages/app", null, "/repo"],
207 ["/repo/packages/app", "/repo/.t3/worktrees/feature-a", undefined],
208 ["/repo/packages/app", null, undefined],
209 ] as const) {
210 expect(
211 resolveBranchSelectionTarget({
212 activeProjectCwd,
213 activeProjectCheckoutRoot,
214 activeWorktreePath,
215 refName: {
216 isDefault: true,
217 worktreePath: "/repo",
218 },
219 }),
220 ).toEqual({
221 checkoutCwd: "/repo",
222 nextWorktreePath: null,
223 reuseExistingWorktree: true,
224 });
225 }
226 });
227​
228 it("keeps checkout in the current worktree for non-default refs", () => {
229 expect(
230 resolveBranchSelectionTarget({
231 activeProjectCwd: "/repo",
232 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
233 refName: {
234 isDefault: false,
235 worktreePath: null,
236 },
237 }),
238 ).toEqual({
239 checkoutCwd: "/repo/.t3/worktrees/feature-a",
240 nextWorktreePath: "/repo/.t3/worktrees/feature-a",
241 reuseExistingWorktree: false,
242 });
243 });
244});
245​
246describe("shouldIncludeBranchPickerItem", () => {
247 it("keeps the synthetic checkout PR item visible for gh pr checkout input", () => {
248 expect(
249 shouldIncludeBranchPickerItem({
250 itemValue: "__checkout_pull_request__:1359",
251 normalizedQuery: "gh pr checkout 1359",
252 createBranchItemValue: "__create_new_branch__:gh pr checkout 1359",
253 checkoutPullRequestItemValue: "__checkout_pull_request__:1359",
254 }),
255 ).toBe(true);
256 });
257​
258 it("surfaces an existing ref matching the sanitized query", () => {
259 expect(
260 shouldIncludeBranchPickerItem({
261 itemValue: "new-branch",
262 normalizedQuery: "new branch",
263 createBranchItemValue: null,
264 checkoutPullRequestItemValue: null,
265 }),
266 ).toBe(true);
267 });
268});
269​
270describe("sanitizeNewRefName", () => {
271 it("collapses a run of whitespace into a single dash", () => {
272 expect(sanitizeNewRefName("new branch")).toBe("new-branch");
273 });
274​
275 it("replaces tabs, which git rejects just like spaces", () => {
276 expect(sanitizeNewRefName("new\tbranch")).toBe("new-branch");
277 });
278​
279 it("preserves whitespace that git accepts", () => {
280 expect(sanitizeNewRefName("new\u00a0branch")).toBe("new\u00a0branch");
281 expect(sanitizeNewRefName("new\u2009branch")).toBe("new\u2009branch");
282 });
283​
284 it("does not collapse dashes the user typed", () => {
285 expect(sanitizeNewRefName("new - branch")).toBe("new---branch");
286 expect(sanitizeNewRefName("foo--bar")).toBe("foo--bar");
287 });
288});