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

gitCafePullRequestJson.test.ts

430 lines13.3 KB

1import { assert, describe, it } from "@effect/vitest";
2import * as Schema from "effect/Schema";
3​
4import * as GitCafePullRequestJson from "./gitCafePullRequestJson.ts";
5​
6const decodePull = Schema.decodeUnknownSync(GitCafePullRequestJson.GitCafePull);
7const decodePullDetail = Schema.decodeUnknownSync(GitCafePullRequestJson.GitCafePullDetail);
8const decodeComments = Schema.decodeUnknownSync(GitCafePullRequestJson.GitCafeComments);
9const decodeReviews = Schema.decodeUnknownSync(GitCafePullRequestJson.GitCafeReviews);
10const decodeCommits = Schema.decodeUnknownSync(GitCafePullRequestJson.GitCafeCommits);
11const decodeReactions = Schema.decodeUnknownSync(GitCafePullRequestJson.GitCafeReactions);
12const decodeStackEnvelope = Schema.decodeUnknownSync(GitCafePullRequestJson.GitCafeStackEnvelope);
13const decodeDiff = Schema.decodeUnknownSync(GitCafePullRequestJson.GitCafeDiff);
14const decodeChecks = Schema.decodeUnknownSync(GitCafePullRequestJson.GitCafeChecks);
15​
16const timestamp = "2026-09-12T12:00:00Z";
17const actor = {
18 kind: "local",
19 actorId: "act_one",
20 handle: "alice",
21 displayName: null,
22 avatarUrl: "/avatars/one",
23};
24const pull = {
25 id: "pr_one",
26 number: 7,
27 title: "Change",
28 state: "open",
29 draft: true,
30 sourceBranch: "feature",
31 targetBranch: "main",
32 headOid: "abcdef",
33 author: actor,
34 createdAt: timestamp,
35 updatedAt: timestamp,
36 version: 1,
37};
38const detail = {
39 ...pull,
40 draft: false,
41 description: "Body",
42 closedAt: null,
43 mergedAt: null,
44 sourceRepo: null,
45 capabilities: { comment: true, review: true, merge: true, edit: true, moderate: true },
46};
47const lineComment = {
48 author: actor,
49 body: "Review",
50 path: "file.ts",
51 line: 3,
52 side: "right",
53 createdAt: timestamp,
54 commitOid: "abcdef",
55 resolvedAt: null,
56};
57const capabilities = (overrides: Record<string, boolean> = {}) => ({
58 edit: false,
59 hide: false,
60 unhide: false,
61 delete: false,
62 resolve: false,
63 unresolve: false,
64 ...overrides,
65});
66const noReviews = decodeReviews({ items: [], next: null });
67const noCommits = decodeCommits({ items: [], headOid: "abcdef", truncated: false, next: null });
68const activity = (
69 comments: ReadonlyArray<Record<string, unknown>>,
70 extra: { readonly reactions?: unknown; readonly host?: string } = {},
71) =>
72 GitCafePullRequestJson.toActivity({
73 comments: decodeComments({ items: comments, next: null }),
74 reviews: noReviews,
75 commits: noCommits,
76 headOid: "abcdef",
77 host: extra.host ?? "git.cafe",
78 ...(extra.reactions === undefined ? {} : { reactions: decodeReactions(extra.reactions) }),
79 });
80​
81describe("toChangeRequest", () => {
82 it("reads draft state, the repository host's URLs and absolute avatars", () => {
83 const row = GitCafePullRequestJson.toChangeRequest(decodePull(pull), {
84 repository: "owner/repo",
85 host: "staging.git.cafe",
86 });
87 assert.strictEqual(row.state, "open");
88 assert.isTrue(row.isDraft);
89 assert.strictEqual(row.url, "https://staging.git.cafe/owner/repo/pulls/7");
90 assert.deepStrictEqual(row.author, {
91 login: "alice",
92 name: null,
93 avatarUrl: "https://staging.git.cafe/avatars/one",
94 });
95 });
96​
97 it("names linked GitHub reviewers by both logins and drops unavailable ones", () => {
98 const row = GitCafePullRequestJson.toChangeRequest(
99 decodePull({
100 ...pull,
101 author: { kind: "github", actorId: "act_remote", login: "remote-user", avatarUrl: null },
102 reviewers: [
103 { actor },
104 {
105 actor: {
106 kind: "github",
107 actorId: "act_github",
108 login: "upstream",
109 avatarUrl: null,
110 linkedProfile: { handle: "linked-local" },
111 },
112 },
113 { actor: { kind: "unavailable", actorId: "act_deleted" } },
114 ],
115 }),
116 { repository: "owner/repo", host: "git.cafe" },
117 );
118 assert.strictEqual(row.author?.login, "remote-user");
119 assert.deepStrictEqual(row.reviewRequestLogins, ["alice", "upstream", "linked-local"]);
120 });
121});
122​
123describe("toViewerPermissions", () => {
124 const permissions = (overrides: Record<string, unknown>) =>
125 GitCafePullRequestJson.toViewerPermissions(decodePullDetail({ ...detail, ...overrides }));
126​
127 it("keeps a locked conversation's composer for moderators only", () => {
128 const locked = (moderate: boolean) =>
129 permissions({
130 lockedAt: timestamp,
131 capabilities: { comment: true, review: false, merge: false, edit: false, moderate },
132 });
133 assert.isFalse(locked(false).comment);
134 assert.isFalse(locked(false).resolve);
135 assert.isTrue(locked(true).comment);
136 assert.deepStrictEqual(locked(true).verdicts, []);
137 });
138});
139​
140describe("toActivity", () => {
141 it("groups line comments by their persisted thread id and marks a paged list truncated", () => {
142 const result = GitCafePullRequestJson.toActivity({
143 comments: decodeComments({
144 items: [
145 { ...lineComment, id: "reply", threadId: "thread-one" },
146 { ...lineComment, id: "other", threadId: "thread-two" },
147 ],
148 next: "next",
149 }),
150 reviews: noReviews,
151 commits: noCommits,
152 headOid: "abcdef",
153 host: "git.cafe",
154 });
155 assert.deepStrictEqual(result.reviewThreads.map((thread) => thread.id).toSorted(), [
156 "thread-one",
157 "thread-two",
158 ]);
159 assert.isTrue(result.commentsTruncated);
160 });
161​
162 it.each([timestamp, null])("takes the thread's resolution from its root: %s", (resolvedAt) => {
163 const result = activity([
164 { ...lineComment, id: "root", threadId: "root", resolvedAt },
165 { ...lineComment, id: "reply", threadId: "root", resolvedAt: null },
166 ]);
167 assert.strictEqual(result.reviewThreads.length, 1);
168 assert.strictEqual(result.reviewThreads[0]?.isResolved, resolvedAt !== null);
169 assert.strictEqual(result.reviewThreads[0]?.comments.length, 2);
170 });
171​
172 it("carries per-remark edit permission onto both copies of a remark", () => {
173 const result = activity([
174 {
175 ...lineComment,
176 id: "root",
177 threadId: "root",
178 capabilities: capabilities({ edit: false, resolve: true }),
179 },
180 {
181 ...lineComment,
182 id: "reply",
183 threadId: "root",
184 capabilities: capabilities({ edit: true }),
185 },
186 { ...lineComment, id: "silent", threadId: "silent", path: null, line: null, side: null },
187 ]);
188 const canEdit = (id: string) => result.comments.find((comment) => comment.id === id)?.canEdit;
189 assert.strictEqual(canEdit("root"), false);
190 assert.strictEqual(canEdit("reply"), true);
191 assert.isUndefined(canEdit("silent"));
192 assert.deepStrictEqual(
193 result.reviewThreads[0]?.comments.map((comment) => [comment.id, comment.canEdit]),
194 [
195 ["root", false],
196 ["reply", true],
197 ],
198 );
199 });
200​
201 it("maps supported reactions to the pull request and to matching comments in both views", () => {
202 const result = activity(
203 [
204 { ...lineComment, id: "comment-a", threadId: "comment-a" },
205 { ...lineComment, id: "comment-b", threadId: "comment-a" },
206 ],
207 {
208 reactions: {
209 next: null,
210 items: [
211 {
212 subject: { kind: "pull_request_comment", id: "comment-b" },
213 emoji: { kind: "unicode", value: "👍" },
214 count: 3,
215 viewerReactionId: "reaction-viewer",
216 reactors: [actor, { kind: "unavailable", actorId: "deleted" }],
217 },
218 {
219 subject: { kind: "pull_request", id: "pr_one" },
220 emoji: { kind: "unicode", value: "🚀" },
221 count: 1,
222 viewerReactionId: null,
223 reactors: [actor],
224 },
225 {
226 subject: { kind: "pull_request", id: "pr_one" },
227 emoji: { kind: "custom", id: "party-parrot" },
228 count: 2,
229 viewerReactionId: null,
230 reactors: [actor],
231 },
232 {
233 subject: { kind: "pull_request", id: "pr_one" },
234 emoji: { kind: "unicode", value: "🔥" },
235 count: 1,
236 viewerReactionId: null,
237 reactors: [actor],
238 },
239 ],
240 },
241 },
242 );
243 const thumbsUp = {
244 content: "thumbs-up",
245 count: 3,
246 actors: ["alice"],
247 viewerHasReacted: true,
248 } as const;
249 assert.deepStrictEqual(result.reactions, [
250 { content: "rocket", count: 1, actors: ["alice"], viewerHasReacted: false },
251 ]);
252 assert.deepStrictEqual(
253 result.comments.map((comment) => [comment.id, comment.reactions]),
254 [
255 ["comment-a", []],
256 ["comment-b", [thumbsUp]],
257 ],
258 );
259 assert.deepStrictEqual(
260 result.reviewThreads[0]?.comments.map((comment) => [comment.id, comment.reactions]),
261 [
262 ["comment-a", []],
263 ["comment-b", [thumbsUp]],
264 ],
265 );
266 });
267});
268​
269describe("toStack", () => {
270 it("orders members by position and reports heads only where GitCafe gives them", () => {
271 const { stack } = decodeStackEnvelope({
272 stack: {
273 id: "stack-one",
274 number: 3,
275 revision: 1,
276 landingBase: "main",
277 members: [
278 {
279 pullRequestNumber: 7,
280 title: "Top",
281 state: "open",
282 draft: true,
283 sourceBranch: "feature",
284 position: 2,
285 },
286 {
287 pullRequestNumber: 6,
288 title: "Base",
289 state: "merged",
290 draft: false,
291 sourceBranch: "base",
292 headOid: "0123456789abcdef0123456789abcdef01234567",
293 position: 1,
294 },
295 ],
296 },
297 });
298 assert.isNotNull(stack);
299 const result = GitCafePullRequestJson.toStack(stack!, {
300 repository: "owner/repo",
301 host: "staging.git.cafe",
302 });
303 assert.strictEqual(result.url, "https://staging.git.cafe/owner/repo/stacks/3");
304 assert.strictEqual(result.base, "main");
305 assert.deepStrictEqual(result.layers, [
306 {
307 number: 6,
308 title: "Base",
309 headBranch: "base",
310 state: "merged",
311 isDraft: false,
312 headSha: "0123456789abcdef0123456789abcdef01234567",
313 },
314 { number: 7, title: "Top", headBranch: "feature", state: "open", isDraft: true },
315 ]);
316 });
317});
318​
319describe("toDiff", () => {
320 it("converts numeric hunk coordinates into a unified patch", () => {
321 const diff = decodeDiff({
322 items: [
323 {
324 path: "file.ts",
325 oldPath: null,
326 status: "modified",
327 additions: 1,
328 deletions: 1,
329 binary: false,
330 hunks: [
331 {
332 oldStart: 1,
333 oldLines: 1,
334 newStart: 1,
335 newLines: 1,
336 lines: [
337 { origin: "-", content: "old\n" },
338 { origin: "+", content: "new\n" },
339 ],
340 },
341 ],
342 },
343 ],
344 truncated: false,
345 });
346 assert.include(GitCafePullRequestJson.toDiff(diff).patch, "@@ -1,1 +1,1 @@\n-old\n+new\n");
347 });
348​
349 it("marks an omitted patch incomplete even when GitCafe's own flag is false", () => {
350 const diff = decodeDiff({
351 items: [{ path: "large.ts", oldPath: null, status: "modified", tooLarge: true, hunks: [] }],
352 truncated: false,
353 });
354 const result = GitCafePullRequestJson.toDiff(diff);
355 assert.isTrue(result.truncated);
356 assert.deepStrictEqual(result.omittedFileStats, [
357 { path: "large.ts", additions: 0, deletions: 0 },
358 ]);
359 });
360​
361 it("quotes renamed and binary paths", () => {
362 const { patch } = GitCafePullRequestJson.toDiff(
363 decodeDiff({
364 items: [
365 {
366 path: "new image.png",
367 oldPath: "old image.png",
368 status: "renamed",
369 binary: true,
370 hunks: [],
371 },
372 ],
373 truncated: false,
374 }),
375 );
376 assert.include(patch, "rename from old image.png");
377 assert.include(patch, "Binary files a/old image.png and b/new image.png differ");
378 });
379​
380 it("C-escapes a name whose newline or tab would break the patch headers", () => {
381 const { patch } = GitCafePullRequestJson.toDiff(
382 decodeDiff({
383 items: [{ path: "two\nlines\tname.ts", status: "added", hunks: [] }],
384 truncated: false,
385 }),
386 );
387 assert.include(patch, 'diff --git "a/two\\nlines\\tname.ts" "b/two\\nlines\\tname.ts"');
388 assert.notInclude(patch, "two\nlines");
389 });
390});
391​
392describe("toChecks", () => {
393 it("maps GitCafe's status and conclusion onto the shared check states", () => {
394 const check = (
395 status: string,
396 conclusion: string | null,
397 name = `${status}-${conclusion}`,
398 ) => ({
399 name,
400 status,
401 conclusion,
402 summary: null,
403 detailsUrl: null,
404 });
405 const checks = GitCafePullRequestJson.toChecks(
406 decodeChecks({
407 items: [
408 check("queued", null),
409 check("in_progress", null),
410 check("completed", "success", "Build"),
411 check("completed", "cancelled"),
412 check("completed", "action_required"),
413 check("completed", "timed_out", "Build"),
414 ],
415 next: null,
416 }),
417 );
418 assert.deepStrictEqual(
419 checks.map((item) => [item.name, item.status]),
420 [
421 ["completed-cancelled", "cancelled"],
422 ["Build", "failure"],
423 ["queued-null", "pending"],
424 ["in_progress-null", "pending"],
425 ["Build", "success"],
426 ["completed-action_required", "action-required"],
427 ],
428 );
429 });
430});