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

GitHubPullRequestProvider.test.ts

222 lines7.4 KB

1import { describe, expect, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4​
5import * as GitHubPullRequestCli from "./GitHubPullRequestCli.ts";
6import { gitHubViewerPermissions, loginAvatarUrl, make } from "./GitHubPullRequestProvider.ts";
7import type { GitHubReviewThreadComments } from "./gitHubPullRequestJson.ts";
8​
9describe("gitHubViewerPermissions", () => {
10 it("offers everything to a viewer who can write to the repository", () => {
11 expect(gitHubViewerPermissions({ canWrite: true, canUpdate: true, didAuthor: false })).toEqual({
12 actions: ["merge", "ready", "draft", "close", "reopen"],
13 comment: true,
14 resolve: true,
15 verdicts: ["comment", "approve", "request-changes"],
16 requestReviewers: true,
17 });
18 });
19​
20 it("leaves a passer-by on a repository they can only read nothing but the review", () => {
21 // Every open-source pull request somebody else opened: GitHub says no to all five actions
22 // and to resolving, and yes to commenting and to every verdict.
23 expect(
24 gitHubViewerPermissions({ canWrite: false, canUpdate: false, didAuthor: false }),
25 ).toEqual({
26 actions: [],
27 comment: true,
28 resolve: false,
29 verdicts: ["comment", "approve", "request-changes"],
30 // Asking somebody else to review is the one thing read access never stretches to.
31 requestReviewers: false,
32 });
33 });
34​
35 it("keeps an author's own pull request theirs to close, with read access and no more", () => {
36 expect(gitHubViewerPermissions({ canWrite: false, canUpdate: true, didAuthor: true })).toEqual({
37 // Merging is the one thing writing is needed for; the rest an author may do.
38 actions: ["ready", "draft", "close", "reopen"],
39 comment: true,
40 resolve: true,
41 // GitHub refuses an author's approval of their own change, so the page does not offer one.
42 verdicts: ["comment"],
43 requestReviewers: false,
44 });
45 });
46​
47 it.effect("uses the small viewer-access read for core permissions", () =>
48 Effect.gen(function* () {
49 const provider = yield* make;
50 const detail = yield* provider.getChangeRequest({
51 cwd: "/w",
52 repository: "acme/web",
53 host: "github.com",
54 number: 7,
55 });
56​
57 expect(detail.viewerPermissions).toEqual({
58 actions: ["ready", "draft", "close", "reopen"],
59 comment: true,
60 resolve: false,
61 verdicts: ["comment", "approve", "request-changes"],
62 requestReviewers: false,
63 });
64 }).pipe(
65 Effect.provide(
66 Layer.mock(GitHubPullRequestCli.GitHubPullRequestCli)({
67 getPullRequestDetail: () =>
68 Effect.succeed({
69 authorId: null,
70 number: 7,
71 title: "Pull request 7",
72 url: "https://github.com/acme/web/pull/7",
73 author: null,
74 headBranch: "feat/page",
75 baseBranch: "main",
76 state: "open",
77 isDraft: false,
78 mergeability: "mergeable",
79 additions: 1,
80 deletions: 1,
81 createdAt: "2026-07-01T00:00:00Z",
82 updatedAt: "2026-07-02T00:00:00Z",
83 reviewRequestLogins: [],
84 hasTeamReviewRequest: false,
85 labels: [],
86 body: "",
87 changedFiles: 1,
88 mergedAt: null,
89 closedAt: null,
90 checks: [],
91 comments: [],
92 commits: [],
93 }),
94 getRepositoryAccess: () =>
95 Effect.succeed({
96 canWrite: false,
97 mergeCapabilities: { merge: true, squash: true, rebase: true },
98 }),
99 getViewerAccess: () =>
100 Effect.succeed({ canWrite: false, canUpdate: true, didAuthor: false }),
101 }),
102 ),
103 ),
104 );
105});
106​
107describe("getChangeRequest commits", () => {
108 const baseDetail = {
109 authorId: null,
110 number: 7,
111 title: "Pull request 7",
112 url: "https://github.com/acme/web/pull/7",
113 author: null,
114 headBranch: "feat/page",
115 baseBranch: "main",
116 state: "open" as const,
117 isDraft: false,
118 mergeability: "mergeable" as const,
119 additions: 1,
120 deletions: 1,
121 createdAt: "2026-07-01T00:00:00Z",
122 updatedAt: "2026-07-02T00:00:00Z",
123 reviewRequestLogins: [],
124 hasTeamReviewRequest: false,
125 labels: [],
126 body: "",
127 changedFiles: 1,
128 mergedAt: null,
129 closedAt: null,
130 checks: [],
131 comments: [],
132 };
133​
134 const baseThreadComments = {
135 comments: [],
136 reviewThreads: [],
137 commentCount: 0,
138 truncated: false,
139 reviewers: [],
140 avatarsByLogin: new Map<string, string>(),
141 commitStats: new Map<string, { readonly additions: number; readonly deletions: number }>(),
142 viewer: { canUpdate: true, didAuthor: false },
143 };
144​
145 const layerWith = (commits: GitHubReviewThreadComments["commits"]) =>
146 Layer.mock(GitHubPullRequestCli.GitHubPullRequestCli)({
147 getPullRequestActivity: () =>
148 Effect.succeed({
149 author: baseDetail.author,
150 comments: baseDetail.comments,
151 commits: [
152 {
153 oid: "view-oldest",
154 messageHeadline: "gh pr view's oldest commit",
155 committedDate: "2026-01-01T00:00:00Z",
156 authors: [],
157 },
158 ],
159 }),
160 listReviewThreadComments: () => Effect.succeed({ ...baseThreadComments, commits }),
161 });
162​
163 it.effect("prefers the GraphQL commits, which are the newest, over the gh view list", () =>
164 Effect.gen(function* () {
165 const provider = yield* make;
166 const detail = yield* provider.getChangeRequestActivity({
167 cwd: "/w",
168 repository: "acme/web",
169 host: "github.com",
170 number: 7,
171 });
172​
173 expect(detail.commits.map((commit) => commit.oid)).toEqual(["graphql-newest"]);
174 }).pipe(
175 Effect.provide(
176 layerWith([
177 {
178 oid: "graphql-newest",
179 messageHeadline: "the newest commit gh pr view drops",
180 committedDate: "2026-07-06T00:00:00Z",
181 authors: [],
182 },
183 ]),
184 ),
185 ),
186 );
187​
188 it.effect("falls back to the gh view list when the GraphQL read has no commits", () =>
189 Effect.gen(function* () {
190 const provider = yield* make;
191 const detail = yield* provider.getChangeRequestActivity({
192 cwd: "/w",
193 repository: "acme/web",
194 host: "github.com",
195 number: 7,
196 });
197​
198 expect(detail.commits.map((commit) => commit.oid)).toEqual(["view-oldest"]);
199 }).pipe(Effect.provide(layerWith([]))),
200 );
201});
202​
203describe("loginAvatarUrl", () => {
204 it("serves a user's picture from the host they belong to", () => {
205 expect(loginAvatarUrl("octocat", "github.com")).toBe("https://github.com/octocat.png?size=80");
206 expect(loginAvatarUrl("octocat", "ghe.example.com")).toBe(
207 "https://ghe.example.com/octocat.png?size=80",
208 );
209 });
210​
211 it("has nothing for an app, which names no page", () => {
212 // `dependabot[bot]` has a picture, but not at `/dependabot[bot].png` — a guess that 404s is
213 // worse than the initials it would replace.
214 expect(loginAvatarUrl("dependabot[bot]", "github.com")).toBeNull();
215 });
216​
217 it("refuses anything that is not a login, rather than building a URL out of it", () => {
218 for (const login of ["../../etc", "a b", "-leading", "x".repeat(40), ""]) {
219 expect(loginAvatarUrl(login, "github.com")).toBeNull();
220 }
221 });
222});