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

git.test.ts

255 lines7.3 KB

1import { describe, expect, it } from "vite-plus/test";
2import * as Schema from "effect/Schema";
3​
4import {
5 GitCommandError,
6 VcsCreateWorktreeInput,
7 GitPreparePullRequestThreadInput,
8 GitPreparePullRequestThreadResult,
9 GitRunStackedActionResult,
10 GitRunStackedActionInput,
11 GitResolvePullRequestResult,
12 VcsStatusLocalResult,
13} from "./git.ts";
14​
15const decodeGitCommandError = Schema.decodeUnknownSync(Schema.toCodecJson(GitCommandError));
16const decodeCreateWorktreeInput = Schema.decodeUnknownSync(VcsCreateWorktreeInput);
17const decodePreparePullRequestThreadInput = Schema.decodeUnknownSync(
18 GitPreparePullRequestThreadInput,
19);
20const decodePreparePullRequestThreadResult = Schema.decodeUnknownSync(
21 GitPreparePullRequestThreadResult,
22);
23const decodeRunStackedActionInput = Schema.decodeUnknownSync(GitRunStackedActionInput);
24const decodeRunStackedActionResult = Schema.decodeUnknownSync(GitRunStackedActionResult);
25const decodeResolvePullRequestResult = Schema.decodeUnknownSync(GitResolvePullRequestResult);
26const decodeVcsStatusLocalResult = Schema.decodeUnknownSync(VcsStatusLocalResult);
27​
28describe("GitCommandError", () => {
29 it("decodes an unknown failure reason as absent", () => {
30 const error = decodeGitCommandError({
31 _tag: "GitCommandError",
32 operation: "GitVcsDriver.createWorktree",
33 command: "git",
34 cwd: "/repo",
35 detail: "git worktree add failed",
36 reason: "added_by_a_newer_server",
37 });
38​
39 expect(error.reason).toBeUndefined();
40 expect(error.message).toBe(
41 "Git command failed in GitVcsDriver.createWorktree (/repo): git worktree add failed",
42 );
43 });
44});
45​
46describe("VcsCreateWorktreeInput", () => {
47 it("accepts omitted newRefName for existing-refName worktrees", () => {
48 const parsed = decodeCreateWorktreeInput({
49 cwd: "/repo",
50 refName: "feature/existing",
51 path: "/tmp/worktree",
52 });
53​
54 expect(parsed.newRefName).toBeUndefined();
55 expect(parsed.refName).toBe("feature/existing");
56 });
57​
58 it("accepts baseRefName metadata for a new worktree ref", () => {
59 const parsed = decodeCreateWorktreeInput({
60 cwd: "/repo",
61 refName: "0123456789abcdef",
62 newRefName: "feature/new",
63 baseRefName: "origin/main",
64 path: "/tmp/worktree",
65 });
66​
67 expect(parsed.baseRefName).toBe("origin/main");
68 });
69});
70​
71describe("GitPreparePullRequestThreadInput", () => {
72 it("accepts pull request references and mode", () => {
73 const parsed = decodePreparePullRequestThreadInput({
74 cwd: "/repo",
75 reference: "#42",
76 mode: "worktree",
77 });
78​
79 expect(parsed.reference).toBe("#42");
80 expect(parsed.mode).toBe("worktree");
81 });
82});
83​
84describe("GitPreparePullRequestThreadResult", () => {
85 it("defaults legacy responses to the pull request head", () => {
86 const parsed = decodePreparePullRequestThreadResult({
87 pullRequest: {
88 number: 42,
89 title: "PR threads",
90 url: "https://github.com/pingdotgg/codething-mvp/pull/42",
91 baseBranch: "main",
92 headBranch: "feature/pr-threads",
93 state: "open",
94 },
95 branch: "feature/pr-threads",
96 worktreePath: "/tmp/pr-threads",
97 });
98​
99 expect(parsed.isOnPullRequestHead).toBe(true);
100 });
101​
102 it("preserves an explicit stale pull request checkout result", () => {
103 const parsed = decodePreparePullRequestThreadResult({
104 pullRequest: {
105 number: 42,
106 title: "PR threads",
107 url: "https://github.com/pingdotgg/codething-mvp/pull/42",
108 baseBranch: "main",
109 headBranch: "feature/pr-threads",
110 state: "open",
111 },
112 branch: "feature/pr-threads",
113 worktreePath: "/tmp/pr-threads",
114 isOnPullRequestHead: false,
115 });
116​
117 expect(parsed.isOnPullRequestHead).toBe(false);
118 });
119});
120​
121describe("GitResolvePullRequestResult", () => {
122 it("decodes resolved pull request metadata", () => {
123 const parsed = decodeResolvePullRequestResult({
124 pullRequest: {
125 number: 42,
126 title: "PR threads",
127 url: "https://github.com/pingdotgg/codething-mvp/pull/42",
128 baseBranch: "main",
129 headBranch: "feature/pr-threads",
130 state: "open",
131 },
132 });
133​
134 expect(parsed.pullRequest.number).toBe(42);
135 expect(parsed.pullRequest.headBranch).toBe("feature/pr-threads");
136 });
137});
138​
139describe("GitRunStackedActionInput", () => {
140 it("accepts explicit stacked actions and requires a client-provided actionId", () => {
141 const parsed = decodeRunStackedActionInput({
142 actionId: "action-1",
143 cwd: "/repo",
144 action: "create_pr",
145 });
146​
147 expect(parsed.actionId).toBe("action-1");
148 expect(parsed.action).toBe("create_pr");
149 });
150​
151 it("preserves leading and trailing whitespace in file paths", () => {
152 const parsed = decodeRunStackedActionInput({
153 actionId: "action-1",
154 cwd: "/repo",
155 action: "commit",
156 filePaths: [" leading.txt", "trailing.txt "],
157 });
158​
159 expect(parsed.filePaths).toEqual([" leading.txt", "trailing.txt "]);
160 });
161​
162 it("rejects an empty file path", () => {
163 expect(() =>
164 decodeRunStackedActionInput({
165 actionId: "action-1",
166 cwd: "/repo",
167 action: "commit",
168 filePaths: [""],
169 }),
170 ).toThrow();
171 });
172});
173​
174describe("VcsStatusLocalResult", () => {
175 it("preserves leading and trailing whitespace in working-tree paths", () => {
176 const parsed = decodeVcsStatusLocalResult({
177 isRepo: true,
178 hasPrimaryRemote: true,
179 isDefaultRef: false,
180 refName: "main",
181 hasWorkingTreeChanges: true,
182 workingTree: {
183 files: [
184 { path: " leading.txt", insertions: 1, deletions: 0 },
185 { path: "trailing.txt ", insertions: 0, deletions: 1 },
186 ],
187 insertions: 1,
188 deletions: 1,
189 },
190 });
191​
192 expect(parsed.workingTree.files.map((file) => file.path)).toEqual([
193 " leading.txt",
194 "trailing.txt ",
195 ]);
196 });
197​
198 it("rejects an empty working-tree path", () => {
199 expect(() =>
200 decodeVcsStatusLocalResult({
201 isRepo: true,
202 hasPrimaryRemote: true,
203 isDefaultRef: false,
204 refName: "main",
205 hasWorkingTreeChanges: true,
206 workingTree: {
207 files: [{ path: "", insertions: 0, deletions: 0 }],
208 insertions: 0,
209 deletions: 0,
210 },
211 }),
212 ).toThrow();
213 });
214});
215​
216describe("GitRunStackedActionResult", () => {
217 it("decodes a server-authored completion toast", () => {
218 const parsed = decodeRunStackedActionResult({
219 action: "commit_push",
220 branch: {
221 status: "created",
222 name: "feature/server-owned-toast",
223 },
224 commit: {
225 status: "created",
226 commitSha: "89abcdef01234567",
227 subject: "feat: move toast state into git manager",
228 },
229 push: {
230 status: "pushed",
231 branch: "feature/server-owned-toast",
232 upstreamBranch: "origin/feature/server-owned-toast",
233 },
234 pr: {
235 status: "skipped_not_requested",
236 },
237 toast: {
238 title: "Pushed 89abcde to origin/feature/server-owned-toast",
239 description: "feat: move toast state into git manager",
240 cta: {
241 kind: "run_action",
242 label: "Create PR",
243 action: {
244 kind: "create_pr",
245 },
246 },
247 },
248 });
249​
250 expect(parsed.toast.cta.kind).toBe("run_action");
251 if (parsed.toast.cta.kind === "run_action") {
252 expect(parsed.toast.cta.action.kind).toBe("create_pr");
253 }
254 });
255});