Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 fillNewTaskWorkspaceBranch,
5 resolveNewTaskBranchLabel,
6 resolveNewTaskBranchWorktreePath,
7 resolveNewTaskLocalWorkspaceSelection,
8 resolveNewTaskWorkspaceSelection,
9} from "./new-task-context-presentation";
10
11describe("resolveNewTaskWorkspaceSelection", () => {
12 const fallback = { mode: "worktree", branch: null, worktreePath: null } as const;
13
14 it("falls back to the current checkout when a non-git project defaults to worktree", () => {
15 expect(
16 resolveNewTaskWorkspaceSelection({ selection: undefined, fallback, isGitRepo: false }),
17 ).toEqual({ mode: "local", branch: null, worktreePath: null });
18 });
19
20 it.each(["local", "worktree"] as const)(
21 "clears stale %s selections for non-git projects",
22 (mode) => {
23 expect(
24 resolveNewTaskWorkspaceSelection({
25 selection: {
26 mode,
27 branch: "main",
28 worktreePath: "/old-worktree",
29 startFromOrigin: true,
30 pullRequestReference: "https://github.com/acme/repo/pull/123",
31 },
32 fallback,
33 isGitRepo: false,
34 }),
35 ).toEqual({ mode: "local", branch: null, worktreePath: null });
36 },
37 );
38
39 it("keeps a worktree of tracked repositories for a non-git project folder", () => {
40 expect(
41 resolveNewTaskWorkspaceSelection({
42 selection: {
43 mode: "worktree",
44 branch: "main",
45 worktreePath: null,
46 startFromOrigin: true,
47 repositories: ["/workspace/api"],
48 },
49 fallback,
50 isGitRepo: false,
51 tracksGitRepositories: true,
52 }),
53 ).toEqual({
54 mode: "worktree",
55 branch: null,
56 worktreePath: null,
57 repositories: ["/workspace/api"],
58 });
59 expect(
60 resolveNewTaskWorkspaceSelection({
61 selection: undefined,
62 fallback,
63 isGitRepo: false,
64 tracksGitRepositories: true,
65 }),
66 ).toEqual(fallback);
67 });
68
69 // regression: #21404 use current checkout for non-git projects
70 it("preserves the selection and defaults for git projects", () => {
71 const selection = { mode: "local", branch: "feature", worktreePath: "/worktree" } as const;
72 expect(resolveNewTaskWorkspaceSelection({ selection, fallback, isGitRepo: true })).toBe(
73 selection,
74 );
75 expect(
76 resolveNewTaskWorkspaceSelection({ selection: undefined, fallback, isGitRepo: true }),
77 ).toBe(fallback);
78 });
79});
80
81describe("resolveNewTaskBranchLabel", () => {
82 it.each([
83 ["https://github.com/acme/repo/pull/123", "From PR #123"],
84 ["https://gitlab.com/acme/repo/-/merge_requests/123", "From MR #123"],
85 ])("names the change request a worktree starts from (%s)", (pullRequestReference, label) => {
86 expect(
87 resolveNewTaskBranchLabel({
88 branchName: null,
89 startFromOrigin: false,
90 workspaceMode: "worktree",
91 pullRequestReference,
92 }),
93 ).toBe(label);
94 });
95});
96
97describe("resolveNewTaskLocalWorkspaceSelection", () => {
98 it("waits for refs instead of carrying a worktree base into Current checkout", () => {
99 expect(
100 resolveNewTaskLocalWorkspaceSelection({
101 branches: [],
102 projectCwd: "/repo",
103 }),
104 ).toEqual({
105 branch: null,
106 worktreePath: null,
107 awaitsCurrentBranch: true,
108 });
109 });
110
111 it("carries the worktree path when the current branch lives in another worktree", () => {
112 expect(
113 resolveNewTaskLocalWorkspaceSelection({
114 branches: [
115 { name: "feature/split", current: true, worktreePath: "/repo/.t3/worktrees/split" },
116 { name: "main", current: false, worktreePath: "/repo" },
117 ],
118 projectCwd: "/repo",
119 }),
120 ).toEqual({
121 branch: "feature/split",
122 worktreePath: "/repo/.t3/worktrees/split",
123 awaitsCurrentBranch: false,
124 });
125 });
126});
127
128describe("resolveNewTaskBranchWorktreePath", () => {
129 it("does not reuse an existing worktree while creating a new one", () => {
130 expect(
131 resolveNewTaskBranchWorktreePath({
132 workspaceMode: "worktree",
133 projectCwd: "/repo",
134 branchWorktreePath: "/repo/.t3/worktrees/feature",
135 }),
136 ).toBeNull();
137 });
138});
139
140describe("fillNewTaskWorkspaceBranch", () => {
141 const branches = [
142 { name: "origin/main", current: false, isDefault: true, isRemote: true },
143 { name: "feature", current: true, isDefault: false, worktreePath: "/repo-feature" },
144 ];
145 const base = {
146 branches,
147 projectCwd: "/repo",
148 worktreeDefaultSettled: true,
149 };
150
151 it("bases a new worktree on the default branch, even when only the remote has it", () => {
152 expect(
153 fillNewTaskWorkspaceBranch({
154 ...base,
155 selection: { mode: "worktree", branch: null, worktreePath: null },
156 }),
157 ).toEqual({ mode: "worktree", branch: "origin/main", worktreePath: null });
158 });
159
160 it("waits for the workspace default to settle before picking a base", () => {
161 const selection = { mode: "worktree", branch: null, worktreePath: null } as const;
162 expect(fillNewTaskWorkspaceBranch({ ...base, selection, worktreeDefaultSettled: false })).toBe(
163 selection,
164 );
165 });
166
167 it("follows the current checkout only for a local selection the user just chose", () => {
168 const selection = { mode: "local", branch: null, worktreePath: null } as const;
169 expect(fillNewTaskWorkspaceBranch({ ...base, selection })).toBe(selection);
170 expect(
171 fillNewTaskWorkspaceBranch({
172 ...base,
173 selection: { ...selection, followsCurrentCheckout: true },
174 }),
175 ).toEqual({
176 mode: "local",
177 branch: "feature",
178 worktreePath: "/repo-feature",
179 followsCurrentCheckout: true,
180 });
181 });
182
183 it("fills the current branch once refs arrive after local mode was chosen", () => {
184 const selection = {
185 mode: "local",
186 branch: null,
187 worktreePath: null,
188 followsCurrentCheckout: true,
189 } as const;
190 expect(fillNewTaskWorkspaceBranch({ ...base, branches: [], selection })).toBe(selection);
191 expect(fillNewTaskWorkspaceBranch({ ...base, selection })).toMatchObject({
192 branch: "feature",
193 worktreePath: "/repo-feature",
194 });
195 });
196
197 it("keeps an explicit branch", () => {
198 const selection = { mode: "worktree", branch: "release", worktreePath: null } as const;
199 expect(fillNewTaskWorkspaceBranch({ ...base, selection })).toBe(selection);
200 });
201});