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

GitHubCli.test.ts

128 lines3.7 KB

1import { assert, it } from "@effect/vitest";
2import { Effect } from "effect";
3import { afterEach, expect, vi } from "vitest";
4​
5vi.mock("../../processRunner", () => ({
6 runProcess: vi.fn(),
7}));
8​
9import { runProcess } from "../../processRunner";
10import { GitHubCli } from "../Services/GitHubCli.ts";
11import { GitHubCliLive } from "./GitHubCli.ts";
12​
13const mockedRunProcess = vi.mocked(runProcess);
14const layer = it.layer(GitHubCliLive);
15​
16afterEach(() => {
17 mockedRunProcess.mockReset();
18});
19​
20layer("GitHubCliLive", (it) => {
21 it.effect("parses pull request view output", () =>
22 Effect.gen(function* () {
23 mockedRunProcess.mockResolvedValueOnce({
24 stdout: JSON.stringify({
25 number: 42,
26 title: "Add PR thread creation",
27 url: "https://github.com/pingdotgg/codething-mvp/pull/42",
28 baseRefName: "main",
29 headRefName: "feature/pr-threads",
30 state: "OPEN",
31 mergedAt: null,
32 isCrossRepository: true,
33 headRepository: {
34 nameWithOwner: "octocat/codething-mvp",
35 },
36 headRepositoryOwner: {
37 login: "octocat",
38 },
39 }),
40 stderr: "",
41 code: 0,
42 signal: null,
43 timedOut: false,
44 });
45​
46 const result = yield* Effect.gen(function* () {
47 const gh = yield* GitHubCli;
48 return yield* gh.getPullRequest({
49 cwd: "/repo",
50 reference: "#42",
51 });
52 });
53​
54 assert.deepStrictEqual(result, {
55 number: 42,
56 title: "Add PR thread creation",
57 url: "https://github.com/pingdotgg/codething-mvp/pull/42",
58 baseRefName: "main",
59 headRefName: "feature/pr-threads",
60 state: "open",
61 isCrossRepository: true,
62 headRepositoryNameWithOwner: "octocat/codething-mvp",
63 headRepositoryOwnerLogin: "octocat",
64 });
65 expect(mockedRunProcess).toHaveBeenCalledWith(
66 "gh",
67 [
68 "pr",
69 "view",
70 "#42",
71 "--json",
72 "number,title,url,baseRefName,headRefName,state,mergedAt,isCrossRepository,headRepository,headRepositoryOwner",
73 ],
74 expect.objectContaining({ cwd: "/repo" }),
75 );
76 }),
77 );
78​
79 it.effect("reads repository clone URLs", () =>
80 Effect.gen(function* () {
81 mockedRunProcess.mockResolvedValueOnce({
82 stdout: JSON.stringify({
83 nameWithOwner: "octocat/codething-mvp",
84 url: "https://github.com/octocat/codething-mvp",
85 sshUrl: "git@github.com:octocat/codething-mvp.git",
86 }),
87 stderr: "",
88 code: 0,
89 signal: null,
90 timedOut: false,
91 });
92​
93 const result = yield* Effect.gen(function* () {
94 const gh = yield* GitHubCli;
95 return yield* gh.getRepositoryCloneUrls({
96 cwd: "/repo",
97 repository: "octocat/codething-mvp",
98 });
99 });
100​
101 assert.deepStrictEqual(result, {
102 nameWithOwner: "octocat/codething-mvp",
103 url: "https://github.com/octocat/codething-mvp",
104 sshUrl: "git@github.com:octocat/codething-mvp.git",
105 });
106 }),
107 );
108​
109 it.effect("surfaces a friendly error when the pull request is not found", () =>
110 Effect.gen(function* () {
111 mockedRunProcess.mockRejectedValueOnce(
112 new Error(
113 "GraphQL: Could not resolve to a PullRequest with the number of 4888. (repository.pullRequest)",
114 ),
115 );
116​
117 const error = yield* Effect.gen(function* () {
118 const gh = yield* GitHubCli;
119 return yield* gh.getPullRequest({
120 cwd: "/repo",
121 reference: "4888",
122 });
123 }).pipe(Effect.flip);
124​
125 assert.equal(error.message.includes("Pull request not found"), true);
126 }),
127 );
128});