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

git.test.ts

108 lines3.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} from "./git.ts";
9import { PullRequestDiffFileContentsInput } from "./pullRequest.ts";
10import { ReviewDiffFileContentsInput } from "./review.ts";
11​
12const decodeGitCommandError = Schema.decodeUnknownSync(Schema.toCodecJson(GitCommandError));
13const decodePreparePullRequestThreadResult = Schema.decodeUnknownSync(
14 GitPreparePullRequestThreadResult,
15);
16const decodeRunStackedActionInput = Schema.decodeUnknownSync(GitRunStackedActionInput);
17const encodeRunStackedActionInput = Schema.encodeUnknownSync(GitRunStackedActionInput);
18const decodeReviewDiffFileContentsInput = Schema.decodeUnknownSync(ReviewDiffFileContentsInput);
19const encodeReviewDiffFileContentsInput = Schema.encodeUnknownSync(ReviewDiffFileContentsInput);
20const decodePullRequestDiffFileContentsInput = Schema.decodeUnknownSync(
21 PullRequestDiffFileContentsInput,
22);
23const encodePullRequestDiffFileContentsInput = Schema.encodeUnknownSync(
24 PullRequestDiffFileContentsInput,
25);
26​
27describe("GitCommandError", () => {
28 it("decodes an unknown failure reason as absent", () => {
29 const error = decodeGitCommandError({
30 _tag: "GitCommandError",
31 operation: "GitVcsDriver.createWorktree",
32 command: "git",
33 cwd: "/repo",
34 detail: "git worktree add failed",
35 reason: "added_by_a_newer_server",
36 });
37​
38 expect(error.reason).toBeUndefined();
39 expect(error.message).toBe(
40 "Git command failed in GitVcsDriver.createWorktree (/repo): git worktree add failed",
41 );
42 });
43});
44​
45describe("GitPreparePullRequestThreadResult", () => {
46 it("defaults legacy responses to the pull request head", () => {
47 const parsed = decodePreparePullRequestThreadResult({
48 pullRequest: {
49 number: 42,
50 title: "PR threads",
51 url: "https://github.com/pingdotgg/codething-mvp/pull/42",
52 baseBranch: "main",
53 headBranch: "feature/pr-threads",
54 state: "open",
55 },
56 branch: "feature/pr-threads",
57 worktreePath: "/tmp/pr-threads",
58 });
59​
60 expect(parsed.isOnPullRequestHead).toBe(true);
61 });
62});
63​
64describe("Git file paths", () => {
65 it.each([
66 {
67 operation: "commit",
68 decode: decodeRunStackedActionInput,
69 encode: encodeRunStackedActionInput,
70 input: {
71 actionId: "action-1",
72 cwd: "/repo",
73 action: "commit",
74 filePaths: [" leading.txt", "trailing.txt "],
75 },
76 },
77 {
78 operation: "local diff expansion",
79 decode: decodeReviewDiffFileContentsInput,
80 encode: encodeReviewDiffFileContentsInput,
81 input: {
82 cwd: "/repo",
83 sourceKind: "working-tree",
84 changeType: "rename-changed",
85 baseRef: "HEAD",
86 headRef: null,
87 oldPath: " leading.txt",
88 newPath: "trailing.txt ",
89 },
90 },
91 {
92 operation: "pull request diff expansion",
93 decode: decodePullRequestDiffFileContentsInput,
94 encode: encodePullRequestDiffFileContentsInput,
95 input: {
96 projectId: "project-1",
97 repository: "owner/repo",
98 number: 1,
99 changeType: "rename-changed",
100 oldPath: " leading.txt",
101 newPath: "trailing.txt ",
102 },
103 },
104 ])("preserves leading and trailing whitespace for $operation", ({ decode, encode, input }) => {
105 expect(decode(input)).toEqual(input);
106 expect(encode(input)).toEqual(input);
107 });
108});