Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import * as Schema from "effect/Schema";
3
4import {
5 GitActionProgressEvent,
6 GitCommandError,
7 GitPreparePullRequestThreadInput,
8 GitPreparePullRequestThreadResult,
9 GitPullRequestMaterializationError,
10 GitPullRequestRefInput,
11 GitRunStackedActionInput,
12 VcsCreateRefInput,
13 VcsCreateWorktreeInput,
14 VcsCreateWorktreeResult,
15 VcsInitInput,
16 VcsListRefsInput,
17 VcsPullInput,
18 VcsRef,
19 VcsRemoveWorktreeInput,
20 VcsStatusInput,
21 VcsStatusSubscriptionInput,
22 VcsSwitchRefInput,
23} from "./git.ts";
24import { WorktreeMcpHandoffInput } from "./worktreeMcp.ts";
25import { PullRequestDiffFileContentsInput } from "./pullRequest.ts";
26import {
27 ReviewBranchCommitsInput,
28 ReviewDiffFileContentsInput,
29 ReviewDiffPreviewInput,
30 ReviewDiffPreviewResult,
31} from "./review.ts";
32
33const encodeVcsRef = Schema.encodeUnknownSync(VcsRef);
34const decodeVcsRef = Schema.decodeUnknownSync(VcsRef);
35const encodeCreateWorktreeInput = Schema.encodeUnknownSync(VcsCreateWorktreeInput);
36const decodeCreateWorktreeInput = Schema.decodeUnknownSync(VcsCreateWorktreeInput);
37const encodeListRefsInput = Schema.encodeUnknownSync(VcsListRefsInput);
38const decodeListRefsInput = Schema.decodeUnknownSync(VcsListRefsInput);
39const decodeWorktreeHandoffInput = Schema.decodeUnknownSync(WorktreeMcpHandoffInput);
40const decodeGitCommandError = Schema.decodeUnknownSync(Schema.toCodecJson(GitCommandError));
41const decodePreparePullRequestThreadResult = Schema.decodeUnknownSync(
42 GitPreparePullRequestThreadResult,
43);
44const decodeRunStackedActionInput = Schema.decodeUnknownSync(GitRunStackedActionInput);
45const encodeRunStackedActionInput = Schema.encodeUnknownSync(GitRunStackedActionInput);
46const decodeReviewDiffFileContentsInput = Schema.decodeUnknownSync(ReviewDiffFileContentsInput);
47const encodeReviewDiffFileContentsInput = Schema.encodeUnknownSync(ReviewDiffFileContentsInput);
48const decodePullRequestDiffFileContentsInput = Schema.decodeUnknownSync(
49 PullRequestDiffFileContentsInput,
50);
51const encodePullRequestDiffFileContentsInput = Schema.encodeUnknownSync(
52 PullRequestDiffFileContentsInput,
53);
54
55describe("GitCommandError", () => {
56 it("decodes an unknown failure reason as absent", () => {
57 const error = decodeGitCommandError({
58 _tag: "GitCommandError",
59 operation: "GitVcsDriver.createWorktree",
60 command: "git",
61 cwd: "/repo",
62 detail: "git worktree add failed",
63 reason: "added_by_a_newer_server",
64 });
65
66 expect(error.reason).toBeUndefined();
67 expect(error.message).toBe(
68 "Git command failed in GitVcsDriver.createWorktree (/repo): git worktree add failed",
69 );
70 });
71});
72
73describe("GitPreparePullRequestThreadResult", () => {
74 it("defaults legacy responses to the pull request head", () => {
75 const parsed = decodePreparePullRequestThreadResult({
76 pullRequest: {
77 number: 42,
78 title: "PR threads",
79 url: "https://github.com/pingdotgg/codething-mvp/pull/42",
80 baseBranch: "main",
81 headBranch: "feature/pr-threads",
82 state: "open",
83 },
84 branch: "feature/pr-threads",
85 worktreePath: "/tmp/pr-threads",
86 });
87
88 expect(parsed.isOnPullRequestHead).toBe(true);
89 });
90});
91
92describe("Git file paths", () => {
93 it.each([
94 {
95 operation: "commit",
96 decode: decodeRunStackedActionInput,
97 encode: encodeRunStackedActionInput,
98 input: {
99 actionId: "action-1",
100 cwd: "/repo",
101 action: "commit",
102 filePaths: [" leading.txt", "trailing.txt "],
103 },
104 },
105 {
106 operation: "local diff expansion",
107 decode: decodeReviewDiffFileContentsInput,
108 encode: encodeReviewDiffFileContentsInput,
109 input: {
110 cwd: "/repo",
111 sourceKind: "working-tree",
112 changeType: "rename-changed",
113 baseRef: "HEAD",
114 headRef: null,
115 oldPath: " leading.txt",
116 newPath: "trailing.txt ",
117 },
118 },
119 {
120 operation: "pull request diff expansion",
121 decode: decodePullRequestDiffFileContentsInput,
122 encode: encodePullRequestDiffFileContentsInput,
123 input: {
124 projectId: "project-1",
125 repository: "owner/repo",
126 number: 1,
127 changeType: "rename-changed",
128 oldPath: " leading.txt",
129 newPath: "trailing.txt ",
130 },
131 },
132 ])("preserves leading and trailing whitespace for $operation", ({ decode, encode, input }) => {
133 expect(decode(input)).toEqual(input);
134 expect(encode(input)).toEqual(input);
135 });
136});
137
138describe("Git ref names", () => {
139 it.each(["origin/release/v2\u3000", "\u00a0release", "\u3000"])(
140 "keeps Unicode whitespace at the edges of %j across the wire",
141 (name) => {
142 const ref = { name, upstreamRef: `refs/remotes/${name}`, current: false, isDefault: false };
143 const wireRef = encodeVcsRef({ ...ref, worktreePath: null });
144 expect(decodeVcsRef(wireRef)).toMatchObject(ref);
145
146 const input = { cwd: "/repo", refName: name, newRefName: name, baseRefName: name };
147 const wireInput = encodeCreateWorktreeInput({ ...input, path: null });
148 expect(decodeCreateWorktreeInput(wireInput)).toMatchObject(input);
149
150 const handoff = { branch: name, baseRef: name, startFromOrigin: true };
151 expect(decodeWorktreeHandoffInput(handoff)).toEqual(handoff);
152
153 const listInput = { cwd: "/repo", query: name };
154 expect(decodeListRefsInput(encodeListRefsInput(listInput))).toEqual(listInput);
155 },
156 );
157
158 it("still trims ASCII whitespace and rejects a blank name", () => {
159 expect(
160 decodeCreateWorktreeInput({ cwd: "/repo", refName: " \tmain\n", path: null }).refName,
161 ).toBe("main");
162 expect(() => decodeCreateWorktreeInput({ cwd: "/repo", refName: " \t", path: null })).toThrow();
163 expect(decodeListRefsInput({ cwd: "/repo", query: " \tmain\n" }).query).toBe("main");
164 expect(() => decodeListRefsInput({ cwd: "/repo", query: " \t" })).toThrow();
165 });
166
167 it("accepts a ref search for a valid branch name longer than 256 characters", () => {
168 const name = `${"a".repeat(140)}/${"b".repeat(140)}`;
169 expect(decodeListRefsInput({ cwd: "/repo", query: name }).query).toBe(name);
170 });
171});
172
173describe("Git repository and worktree paths", () => {
174 const pathSchemas: ReadonlyArray<{
175 readonly schema: Schema.Top;
176 readonly payload: (path: string) => Record<string, unknown>;
177 readonly field?: string;
178 }> = [
179 { schema: ReviewDiffPreviewInput, payload: (cwd) => ({ cwd, sourceKind: "working-tree" }) },
180 { schema: ReviewBranchCommitsInput, payload: (cwd) => ({ cwd }) },
181 {
182 schema: ReviewDiffFileContentsInput,
183 payload: (cwd) => ({
184 cwd,
185 sourceKind: "working-tree",
186 changeType: "change",
187 baseRef: "HEAD",
188 headRef: null,
189 oldPath: "file.txt",
190 newPath: "file.txt",
191 }),
192 },
193 {
194 schema: Schema.toCodecJson(ReviewDiffPreviewResult),
195 payload: (cwd) => ({ cwd, generatedAt: "2026-12-02T00:00:00.000Z", sources: [] }),
196 },
197 { schema: VcsInitInput, payload: (cwd) => ({ cwd }) },
198 { schema: VcsCreateWorktreeInput, payload: (cwd) => ({ cwd, refName: "main", path: null }) },
199 { schema: VcsRemoveWorktreeInput, payload: (cwd) => ({ cwd, path: "/worktree" }) },
200 {
201 schema: VcsCreateWorktreeInput,
202 field: "path",
203 payload: (path) => ({ cwd: "/repo", refName: "main", path }),
204 },
205 {
206 schema: VcsRemoveWorktreeInput,
207 field: "path",
208 payload: (path) => ({ cwd: "/repo", path }),
209 },
210 {
211 schema: VcsCreateWorktreeResult,
212 field: "worktree.path",
213 payload: (path) => ({ worktree: { path, refName: "main" } }),
214 },
215 {
216 schema: VcsRef,
217 field: "worktreePath",
218 payload: (worktreePath) => ({ name: "main", current: true, isDefault: true, worktreePath }),
219 },
220 {
221 schema: GitPreparePullRequestThreadResult,
222 field: "worktreePath",
223 payload: (worktreePath) => ({
224 pullRequest: {
225 number: 42,
226 title: "PR threads",
227 url: "https://github.com/owner/repo/pull/42",
228 baseBranch: "main",
229 headBranch: "feature/test",
230 state: "open",
231 },
232 branch: "feature/test",
233 worktreePath,
234 isOnPullRequestHead: true,
235 }),
236 },
237 { schema: GitPullRequestRefInput, payload: (cwd) => ({ cwd, reference: "42" }) },
238 {
239 schema: GitPreparePullRequestThreadInput,
240 payload: (cwd) => ({ cwd, reference: "42", mode: "local" }),
241 },
242 {
243 schema: Schema.toCodecJson(GitPullRequestMaterializationError),
244 payload: (cwd) => ({
245 _tag: "GitPullRequestMaterializationError",
246 cwd,
247 pullRequestNumber: 42,
248 headRepository: null,
249 headBranch: "feature/test",
250 localBranch: "feature/test",
251 cause: "fetch failed",
252 }),
253 },
254 { schema: VcsStatusInput, payload: (cwd) => ({ cwd }) },
255 { schema: VcsStatusSubscriptionInput, payload: (cwd) => ({ cwd, includeRemote: false }) },
256 { schema: VcsListRefsInput, payload: (cwd) => ({ cwd, refKind: "local" }) },
257 { schema: VcsPullInput, payload: (cwd) => ({ cwd }) },
258 {
259 schema: GitRunStackedActionInput,
260 payload: (cwd) => ({ actionId: "action-1", cwd, action: "commit" }),
261 },
262 { schema: VcsCreateRefInput, payload: (cwd) => ({ cwd, refName: "feature/test" }) },
263 { schema: VcsSwitchRefInput, payload: (cwd) => ({ cwd, refName: "main" }) },
264 {
265 schema: GitActionProgressEvent,
266 payload: (cwd) => ({
267 actionId: "action-1",
268 cwd,
269 action: "commit",
270 kind: "action_started",
271 phases: ["commit"],
272 }),
273 },
274 ];
275
276 it.each(["/workspace/repo ", " repo"])(
277 "preserves literal edge whitespace in path %j",
278 (literalPath) => {
279 for (const { schema, payload, field = "cwd" } of pathSchemas) {
280 const wire = payload(literalPath);
281 const codec = schema as Schema.Codec<unknown, unknown>;
282 const decoded = Schema.decodeSync(codec)(wire);
283 expect(decoded).toHaveProperty(field, literalPath);
284 expect(Schema.encodeSync(codec)(decoded)).toEqual(wire);
285 }
286 },
287 );
288
289 it("still rejects blank repository and worktree paths", () => {
290 for (const { schema, payload } of pathSchemas) {
291 expect(() =>
292 Schema.decodeSync(schema as Schema.Codec<unknown, unknown>)(payload(" ")),
293 ).toThrow();
294 }
295 });
296});