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

BranchToolbar.logic.test.ts

256 lines7.4 KB

1import { type VcsRef } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3import {
4 dedupeRemoteBranchesWithLocalMatches,
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",
166 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
167 refName: {
168 isDefault: false,
169 worktreePath: "/repo/.t3/worktrees/feature-b",
170 },
171 }),
172 ).toEqual({
173 checkoutCwd: "/repo/.t3/worktrees/feature-b",
174 nextWorktreePath: "/repo/.t3/worktrees/feature-b",
175 reuseExistingWorktree: true,
176 });
177 });
178​
179 it("switches back to the main repo when the ref already lives there", () => {
180 expect(
181 resolveBranchSelectionTarget({
182 activeProjectCwd: "/repo",
183 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
184 refName: {
185 isDefault: true,
186 worktreePath: "/repo",
187 },
188 }),
189 ).toEqual({
190 checkoutCwd: "/repo",
191 nextWorktreePath: null,
192 reuseExistingWorktree: true,
193 });
194 });
195​
196 it("keeps checkout in the current worktree for non-default refs", () => {
197 expect(
198 resolveBranchSelectionTarget({
199 activeProjectCwd: "/repo",
200 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
201 refName: {
202 isDefault: false,
203 worktreePath: null,
204 },
205 }),
206 ).toEqual({
207 checkoutCwd: "/repo/.t3/worktrees/feature-a",
208 nextWorktreePath: "/repo/.t3/worktrees/feature-a",
209 reuseExistingWorktree: false,
210 });
211 });
212});
213​
214describe("shouldIncludeBranchPickerItem", () => {
215 it("keeps the synthetic checkout PR item visible for gh pr checkout input", () => {
216 expect(
217 shouldIncludeBranchPickerItem({
218 itemValue: "__checkout_pull_request__:1359",
219 normalizedQuery: "gh pr checkout 1359",
220 createBranchItemValue: "__create_new_branch__:gh pr checkout 1359",
221 checkoutPullRequestItemValue: "__checkout_pull_request__:1359",
222 }),
223 ).toBe(true);
224 });
225​
226 it("surfaces an existing ref matching the sanitized query", () => {
227 expect(
228 shouldIncludeBranchPickerItem({
229 itemValue: "new-branch",
230 normalizedQuery: "new branch",
231 createBranchItemValue: null,
232 checkoutPullRequestItemValue: null,
233 }),
234 ).toBe(true);
235 });
236});
237​
238describe("sanitizeNewRefName", () => {
239 it("collapses a run of whitespace into a single dash", () => {
240 expect(sanitizeNewRefName("new branch")).toBe("new-branch");
241 });
242​
243 it("replaces tabs, which git rejects just like spaces", () => {
244 expect(sanitizeNewRefName("new\tbranch")).toBe("new-branch");
245 });
246​
247 it("preserves whitespace that git accepts", () => {
248 expect(sanitizeNewRefName("new\u00a0branch")).toBe("new\u00a0branch");
249 expect(sanitizeNewRefName("new\u2009branch")).toBe("new\u2009branch");
250 });
251​
252 it("does not collapse dashes the user typed", () => {
253 expect(sanitizeNewRefName("new - branch")).toBe("new---branch");
254 expect(sanitizeNewRefName("foo--bar")).toBe("foo--bar");
255 });
256});