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

BranchToolbar.logic.test.ts

269 lines7.5 KB

1import type { GitBranch } from "@t3tools/contracts";
2import { describe, expect, it } from "vitest";
3import {
4 dedupeRemoteBranchesWithLocalMatches,
5 deriveLocalBranchNameFromRemoteRef,
6 resolveBranchSelectionTarget,
7 resolveDraftEnvModeAfterBranchChange,
8 resolveBranchToolbarValue,
9} from "./BranchToolbar.logic";
10​
11describe("resolveDraftEnvModeAfterBranchChange", () => {
12 it("switches to local mode when returning from an existing worktree to the main worktree", () => {
13 expect(
14 resolveDraftEnvModeAfterBranchChange({
15 nextWorktreePath: null,
16 currentWorktreePath: "/repo/.t3/worktrees/feature-a",
17 effectiveEnvMode: "worktree",
18 }),
19 ).toBe("local");
20 });
21​
22 it("keeps new-worktree mode when selecting a base branch before worktree creation", () => {
23 expect(
24 resolveDraftEnvModeAfterBranchChange({
25 nextWorktreePath: null,
26 currentWorktreePath: null,
27 effectiveEnvMode: "worktree",
28 }),
29 ).toBe("worktree");
30 });
31​
32 it("uses worktree mode when selecting a branch already attached to a worktree", () => {
33 expect(
34 resolveDraftEnvModeAfterBranchChange({
35 nextWorktreePath: "/repo/.t3/worktrees/feature-a",
36 currentWorktreePath: null,
37 effectiveEnvMode: "local",
38 }),
39 ).toBe("worktree");
40 });
41});
42​
43describe("resolveBranchToolbarValue", () => {
44 it("defaults new-worktree mode to current git branch when no explicit base branch is set", () => {
45 expect(
46 resolveBranchToolbarValue({
47 envMode: "worktree",
48 activeWorktreePath: null,
49 activeThreadBranch: null,
50 currentGitBranch: "main",
51 }),
52 ).toBe("main");
53 });
54​
55 it("keeps an explicitly selected worktree base branch", () => {
56 expect(
57 resolveBranchToolbarValue({
58 envMode: "worktree",
59 activeWorktreePath: null,
60 activeThreadBranch: "feature/base",
61 currentGitBranch: "main",
62 }),
63 ).toBe("feature/base");
64 });
65​
66 it("shows the actual checked-out branch when not selecting a new worktree base", () => {
67 expect(
68 resolveBranchToolbarValue({
69 envMode: "local",
70 activeWorktreePath: null,
71 activeThreadBranch: "feature/base",
72 currentGitBranch: "main",
73 }),
74 ).toBe("main");
75 });
76});
77​
78describe("deriveLocalBranchNameFromRemoteRef", () => {
79 it("strips the remote prefix from a remote ref", () => {
80 expect(deriveLocalBranchNameFromRemoteRef("origin/feature/demo")).toBe("feature/demo");
81 });
82​
83 it("supports remote names that contain slashes", () => {
84 expect(deriveLocalBranchNameFromRemoteRef("my-org/upstream/feature/demo")).toBe(
85 "upstream/feature/demo",
86 );
87 });
88​
89 it("returns the original name when ref is malformed", () => {
90 expect(deriveLocalBranchNameFromRemoteRef("origin/")).toBe("origin/");
91 expect(deriveLocalBranchNameFromRemoteRef("/feature/demo")).toBe("/feature/demo");
92 });
93});
94​
95describe("dedupeRemoteBranchesWithLocalMatches", () => {
96 it("hides remote refs when the matching local branch exists", () => {
97 const input: GitBranch[] = [
98 {
99 name: "feature/demo",
100 current: false,
101 isDefault: false,
102 worktreePath: null,
103 },
104 {
105 name: "origin/feature/demo",
106 isRemote: true,
107 remoteName: "origin",
108 current: false,
109 isDefault: false,
110 worktreePath: null,
111 },
112 {
113 name: "origin/feature/remote-only",
114 isRemote: true,
115 remoteName: "origin",
116 current: false,
117 isDefault: false,
118 worktreePath: null,
119 },
120 ];
121​
122 expect(dedupeRemoteBranchesWithLocalMatches(input).map((branch) => branch.name)).toEqual([
123 "feature/demo",
124 "origin/feature/remote-only",
125 ]);
126 });
127​
128 it("keeps all entries when no local match exists for a remote ref", () => {
129 const input: GitBranch[] = [
130 {
131 name: "feature/local",
132 current: false,
133 isDefault: false,
134 worktreePath: null,
135 },
136 {
137 name: "origin/feature/remote-only",
138 isRemote: true,
139 remoteName: "origin",
140 current: false,
141 isDefault: false,
142 worktreePath: null,
143 },
144 ];
145​
146 expect(dedupeRemoteBranchesWithLocalMatches(input).map((branch) => branch.name)).toEqual([
147 "feature/local",
148 "origin/feature/remote-only",
149 ]);
150 });
151​
152 it("keeps non-origin remote refs visible even when a matching local branch exists", () => {
153 const input: GitBranch[] = [
154 {
155 name: "feature/demo",
156 current: false,
157 isDefault: false,
158 worktreePath: null,
159 },
160 {
161 name: "my-org/upstream/feature/demo",
162 isRemote: true,
163 remoteName: "my-org/upstream",
164 current: false,
165 isDefault: false,
166 worktreePath: null,
167 },
168 ];
169​
170 expect(dedupeRemoteBranchesWithLocalMatches(input).map((branch) => branch.name)).toEqual([
171 "feature/demo",
172 "my-org/upstream/feature/demo",
173 ]);
174 });
175​
176 it("keeps non-origin remote refs visible when git tracks with first-slash local naming", () => {
177 const input: GitBranch[] = [
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((branch) => branch.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 branch", () => {
203 expect(
204 resolveBranchSelectionTarget({
205 activeProjectCwd: "/repo",
206 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
207 branch: {
208 isDefault: false,
209 worktreePath: "/repo/.t3/worktrees/feature-b",
210 },
211 }),
212 ).toEqual({
213 checkoutCwd: "/repo/.t3/worktrees/feature-b",
214 nextWorktreePath: "/repo/.t3/worktrees/feature-b",
215 reuseExistingWorktree: true,
216 });
217 });
218​
219 it("switches back to the main repo when the branch already lives there", () => {
220 expect(
221 resolveBranchSelectionTarget({
222 activeProjectCwd: "/repo",
223 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
224 branch: {
225 isDefault: true,
226 worktreePath: "/repo",
227 },
228 }),
229 ).toEqual({
230 checkoutCwd: "/repo",
231 nextWorktreePath: null,
232 reuseExistingWorktree: true,
233 });
234 });
235​
236 it("checks out the default branch in the main repo when leaving a secondary worktree", () => {
237 expect(
238 resolveBranchSelectionTarget({
239 activeProjectCwd: "/repo",
240 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
241 branch: {
242 isDefault: true,
243 worktreePath: null,
244 },
245 }),
246 ).toEqual({
247 checkoutCwd: "/repo",
248 nextWorktreePath: null,
249 reuseExistingWorktree: false,
250 });
251 });
252​
253 it("keeps checkout in the current worktree for non-default branches", () => {
254 expect(
255 resolveBranchSelectionTarget({
256 activeProjectCwd: "/repo",
257 activeWorktreePath: "/repo/.t3/worktrees/feature-a",
258 branch: {
259 isDefault: false,
260 worktreePath: null,
261 },
262 }),
263 ).toEqual({
264 checkoutCwd: "/repo/.t3/worktrees/feature-a",
265 nextWorktreePath: "/repo/.t3/worktrees/feature-a",
266 reuseExistingWorktree: false,
267 });
268 });
269});