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

git.test.ts

149 lines5.4 KB

1import { describe, expect, it } from "vite-plus/test";
2import * as Schema from "effect/Schema";
3​
4import {
5 GitCommandError,
6 GitPreparePullRequestThreadResult,
7 GitRunStackedActionInput,
8 VcsCreateWorktreeInput,
9 VcsListRefsInput,
10 VcsRef,
11} from "./git.ts";
12import { WorktreeMcpHandoffInput } from "./worktreeMcp.ts";
13import { PullRequestDiffFileContentsInput } from "./pullRequest.ts";
14import { ReviewDiffFileContentsInput } from "./review.ts";
15​
16const encodeVcsRef = Schema.encodeUnknownSync(VcsRef);
17const decodeVcsRef = Schema.decodeUnknownSync(VcsRef);
18const encodeCreateWorktreeInput = Schema.encodeUnknownSync(VcsCreateWorktreeInput);
19const decodeCreateWorktreeInput = Schema.decodeUnknownSync(VcsCreateWorktreeInput);
20const encodeListRefsInput = Schema.encodeUnknownSync(VcsListRefsInput);
21const decodeListRefsInput = Schema.decodeUnknownSync(VcsListRefsInput);
22const decodeWorktreeHandoffInput = Schema.decodeUnknownSync(WorktreeMcpHandoffInput);
23const decodeGitCommandError = Schema.decodeUnknownSync(Schema.toCodecJson(GitCommandError));
24const decodePreparePullRequestThreadResult = Schema.decodeUnknownSync(
25 GitPreparePullRequestThreadResult,
26);
27const decodeRunStackedActionInput = Schema.decodeUnknownSync(GitRunStackedActionInput);
28const encodeRunStackedActionInput = Schema.encodeUnknownSync(GitRunStackedActionInput);
29const decodeReviewDiffFileContentsInput = Schema.decodeUnknownSync(ReviewDiffFileContentsInput);
30const encodeReviewDiffFileContentsInput = Schema.encodeUnknownSync(ReviewDiffFileContentsInput);
31const decodePullRequestDiffFileContentsInput = Schema.decodeUnknownSync(
32 PullRequestDiffFileContentsInput,
33);
34const encodePullRequestDiffFileContentsInput = Schema.encodeUnknownSync(
35 PullRequestDiffFileContentsInput,
36);
37​
38describe("GitCommandError", () => {
39 it("decodes an unknown failure reason as absent", () => {
40 const error = decodeGitCommandError({
41 _tag: "GitCommandError",
42 operation: "GitVcsDriver.createWorktree",
43 command: "git",
44 cwd: "/repo",
45 detail: "git worktree add failed",
46 reason: "added_by_a_newer_server",
47 });
48​
49 expect(error.reason).toBeUndefined();
50 expect(error.message).toBe(
51 "Git command failed in GitVcsDriver.createWorktree (/repo): git worktree add failed",
52 );
53 });
54});
55​
56describe("GitPreparePullRequestThreadResult", () => {
57 it("defaults legacy responses to the pull request head", () => {
58 const parsed = decodePreparePullRequestThreadResult({
59 pullRequest: {
60 number: 42,
61 title: "PR threads",
62 url: "https://github.com/pingdotgg/codething-mvp/pull/42",
63 baseBranch: "main",
64 headBranch: "feature/pr-threads",
65 state: "open",
66 },
67 branch: "feature/pr-threads",
68 worktreePath: "/tmp/pr-threads",
69 });
70​
71 expect(parsed.isOnPullRequestHead).toBe(true);
72 });
73});
74​
75describe("Git file paths", () => {
76 it.each([
77 {
78 operation: "commit",
79 decode: decodeRunStackedActionInput,
80 encode: encodeRunStackedActionInput,
81 input: {
82 actionId: "action-1",
83 cwd: "/repo",
84 action: "commit",
85 filePaths: [" leading.txt", "trailing.txt "],
86 },
87 },
88 {
89 operation: "local diff expansion",
90 decode: decodeReviewDiffFileContentsInput,
91 encode: encodeReviewDiffFileContentsInput,
92 input: {
93 cwd: "/repo",
94 sourceKind: "working-tree",
95 changeType: "rename-changed",
96 baseRef: "HEAD",
97 headRef: null,
98 oldPath: " leading.txt",
99 newPath: "trailing.txt ",
100 },
101 },
102 {
103 operation: "pull request diff expansion",
104 decode: decodePullRequestDiffFileContentsInput,
105 encode: encodePullRequestDiffFileContentsInput,
106 input: {
107 projectId: "project-1",
108 repository: "owner/repo",
109 number: 1,
110 changeType: "rename-changed",
111 oldPath: " leading.txt",
112 newPath: "trailing.txt ",
113 },
114 },
115 ])("preserves leading and trailing whitespace for $operation", ({ decode, encode, input }) => {
116 expect(decode(input)).toEqual(input);
117 expect(encode(input)).toEqual(input);
118 });
119});
120​
121describe("Git ref names", () => {
122 it.each(["origin/release/v2\u3000", "\u00a0release", "\u3000"])(
123 "keeps Unicode whitespace at the edges of %j across the wire",
124 (name) => {
125 const ref = { name, upstreamRef: `refs/remotes/${name}`, current: false, isDefault: false };
126 const wireRef = encodeVcsRef({ ...ref, worktreePath: null });
127 expect(decodeVcsRef(wireRef)).toMatchObject(ref);
128​
129 const input = { cwd: "/repo", refName: name, newRefName: name, baseRefName: name };
130 const wireInput = encodeCreateWorktreeInput({ ...input, path: null });
131 expect(decodeCreateWorktreeInput(wireInput)).toMatchObject(input);
132​
133 const handoff = { branch: name, baseRef: name, startFromOrigin: true };
134 expect(decodeWorktreeHandoffInput(handoff)).toEqual(handoff);
135​
136 const listInput = { cwd: "/repo", query: name };
137 expect(decodeListRefsInput(encodeListRefsInput(listInput))).toEqual(listInput);
138 },
139 );
140​
141 it("still trims ASCII whitespace and rejects a blank name", () => {
142 expect(
143 decodeCreateWorktreeInput({ cwd: "/repo", refName: " \tmain\n", path: null }).refName,
144 ).toBe("main");
145 expect(() => decodeCreateWorktreeInput({ cwd: "/repo", refName: " \t", path: null })).toThrow();
146 expect(decodeListRefsInput({ cwd: "/repo", query: " \tmain\n" }).query).toBe("main");
147 expect(() => decodeListRefsInput({ cwd: "/repo", query: " \t" })).toThrow();
148 });
149});