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

git.test.ts

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