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

gitCafePullRequestJson.test.ts

521 lines16.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 it("requires capabilities on a detail response", () => {
123 assert.throws(() => decodePullDetail(pull));
124 });
125});
126​
127describe("toViewerPermissions", () => {
128 const permissions = (overrides: Record<string, unknown>) =>
129 GitCafePullRequestJson.toViewerPermissions(decodePullDetail({ ...detail, ...overrides }));
130​
131 it("lets an editor rewrite the title and description, and nobody else", () => {
132 assert.isTrue(permissions({}).editChangeRequest);
133 assert.isFalse(
134 permissions({
135 capabilities: { comment: true, review: true, merge: true, edit: false, moderate: true },
136 }).editChangeRequest,
137 );
138 });
139​
140 it("offers lifecycle actions by state and never a standalone branch update", () => {
141 assert.deepStrictEqual(permissions({}).actions, ["draft", "close", "merge"]);
142 assert.deepStrictEqual(permissions({ state: "closed" }).actions, ["reopen"]);
143 assert.deepStrictEqual(permissions({ state: "merged" }).actions, []);
144 assert.isTrue(permissions({}).stackRebase);
145 });
146​
147 it("keeps a locked conversation's composer for moderators only", () => {
148 const locked = (moderate: boolean) =>
149 permissions({
150 lockedAt: timestamp,
151 capabilities: { comment: true, review: false, merge: false, edit: false, moderate },
152 });
153 assert.isFalse(locked(false).comment);
154 assert.isFalse(locked(false).resolve);
155 assert.isTrue(locked(true).comment);
156 assert.deepStrictEqual(locked(true).verdicts, []);
157 });
158​
159 it("offers no composer to a viewer GitCafe won't let comment", () => {
160 const silent = permissions({
161 capabilities: { comment: false, review: true, merge: true, edit: true, moderate: true },
162 });
163 assert.isFalse(silent.comment);
164 assert.isFalse(silent.resolve);
165 });
166});
167​
168describe("toActivity", () => {
169 it("groups line comments by their persisted thread id and marks a paged list truncated", () => {
170 const result = GitCafePullRequestJson.toActivity({
171 comments: decodeComments({
172 items: [
173 { ...lineComment, id: "reply", threadId: "thread-one" },
174 { ...lineComment, id: "other", threadId: "thread-two" },
175 ],
176 next: "next",
177 }),
178 reviews: noReviews,
179 commits: noCommits,
180 headOid: "abcdef",
181 host: "git.cafe",
182 });
183 assert.deepStrictEqual(result.reviewThreads.map((thread) => thread.id).toSorted(), [
184 "thread-one",
185 "thread-two",
186 ]);
187 assert.isTrue(result.commentsTruncated);
188 });
189​
190 it.each([timestamp, null])("takes the thread's resolution from its root: %s", (resolvedAt) => {
191 const result = activity([
192 { ...lineComment, id: "root", threadId: "root", resolvedAt },
193 { ...lineComment, id: "reply", threadId: "root", resolvedAt: null },
194 ]);
195 assert.strictEqual(result.reviewThreads.length, 1);
196 assert.strictEqual(result.reviewThreads[0]?.isResolved, resolvedAt !== null);
197 assert.strictEqual(result.reviewThreads[0]?.comments.length, 2);
198 });
199​
200 it("carries per-remark edit permission onto both copies of a remark", () => {
201 const result = activity([
202 {
203 ...lineComment,
204 id: "root",
205 threadId: "root",
206 capabilities: capabilities({ edit: false, resolve: true }),
207 },
208 {
209 ...lineComment,
210 id: "reply",
211 threadId: "root",
212 capabilities: capabilities({ edit: true }),
213 },
214 { ...lineComment, id: "silent", threadId: "silent", path: null, line: null, side: null },
215 ]);
216 const canEdit = (id: string) => result.comments.find((comment) => comment.id === id)?.canEdit;
217 assert.strictEqual(canEdit("root"), false);
218 assert.strictEqual(canEdit("reply"), true);
219 assert.isUndefined(canEdit("silent"));
220 assert.deepStrictEqual(
221 result.reviewThreads[0]?.comments.map((comment) => [comment.id, comment.canEdit]),
222 [
223 ["root", false],
224 ["reply", true],
225 ],
226 );
227 });
228​
229 it.each([
230 [null, { resolve: true, unresolve: false }, true],
231 [null, { resolve: false, unresolve: true }, false],
232 [timestamp, { resolve: true, unresolve: false }, false],
233 [timestamp, { resolve: false, unresolve: true }, true],
234 ] as const)(
235 "reads canResolve for a thread resolved at %s with %o as %s",
236 (resolvedAt, allowed, canResolve) => {
237 const result = activity([
238 {
239 ...lineComment,
240 id: "root",
241 threadId: "root",
242 resolvedAt,
243 capabilities: capabilities(allowed),
244 },
245 ]);
246 assert.strictEqual(result.reviewThreads[0]?.canResolve, canResolve);
247 },
248 );
249​
250 it("leaves canResolve absent when the root says nothing", () => {
251 const result = activity([{ ...lineComment, id: "root", threadId: "root" }]);
252 assert.isUndefined(result.reviewThreads[0]?.canResolve);
253 });
254​
255 it("maps review verdicts, dismissals and commits", () => {
256 const result = GitCafePullRequestJson.toActivity({
257 comments: decodeComments({ items: [], next: null }),
258 reviews: decodeReviews({
259 items: [
260 {
261 id: "r1",
262 author: actor,
263 body: "LGTM",
264 verdict: "approve",
265 dismissedAt: null,
266 createdAt: timestamp,
267 },
268 {
269 id: "r2",
270 author: actor,
271 body: null,
272 verdict: "request_changes",
273 dismissedAt: timestamp,
274 createdAt: "2026-09-12T13:00:00Z",
275 },
276 ],
277 next: null,
278 }),
279 commits: decodeCommits({
280 items: [{ oid: "abcdef", summary: "Change", time: 1_789_000_000 }],
281 headOid: "abcdef",
282 truncated: false,
283 next: null,
284 }),
285 host: "git.cafe",
286 });
287 assert.deepStrictEqual(
288 result.comments.map((comment) => [comment.id, comment.kind, comment.reviewState]),
289 [
290 ["r1", "review", "approved"],
291 ["r2", "review", "dismissed"],
292 ],
293 );
294 assert.deepStrictEqual(
295 result.commits.map((commit) => commit.oid),
296 ["abcdef"],
297 );
298 });
299​
300 it("maps supported reactions to the pull request and to matching comments in both views", () => {
301 const result = activity(
302 [
303 { ...lineComment, id: "comment-a", threadId: "comment-a" },
304 { ...lineComment, id: "comment-b", threadId: "comment-a" },
305 ],
306 {
307 reactions: {
308 next: null,
309 items: [
310 {
311 subject: { kind: "pull_request_comment", id: "comment-b" },
312 emoji: { kind: "unicode", value: "👍" },
313 count: 3,
314 viewerReactionId: "reaction-viewer",
315 reactors: [actor, { kind: "unavailable", actorId: "deleted" }],
316 },
317 {
318 subject: { kind: "pull_request", id: "pr_one" },
319 emoji: { kind: "unicode", value: "🚀" },
320 count: 1,
321 viewerReactionId: null,
322 reactors: [actor],
323 },
324 {
325 subject: { kind: "pull_request", id: "pr_one" },
326 emoji: { kind: "custom", id: "party-parrot" },
327 count: 2,
328 viewerReactionId: null,
329 reactors: [actor],
330 },
331 {
332 subject: { kind: "pull_request", id: "pr_one" },
333 emoji: { kind: "unicode", value: "🔥" },
334 count: 1,
335 viewerReactionId: null,
336 reactors: [actor],
337 },
338 ],
339 },
340 },
341 );
342 const thumbsUp = {
343 content: "thumbs-up",
344 count: 3,
345 actors: ["alice"],
346 viewerHasReacted: true,
347 } as const;
348 assert.deepStrictEqual(result.reactions, [
349 { content: "rocket", count: 1, actors: ["alice"], viewerHasReacted: false },
350 ]);
351 assert.deepStrictEqual(
352 result.comments.map((comment) => [comment.id, comment.reactions]),
353 [
354 ["comment-a", []],
355 ["comment-b", [thumbsUp]],
356 ],
357 );
358 assert.deepStrictEqual(
359 result.reviewThreads[0]?.comments.map((comment) => [comment.id, comment.reactions]),
360 [
361 ["comment-a", []],
362 ["comment-b", [thumbsUp]],
363 ],
364 );
365 });
366});
367​
368describe("toStack", () => {
369 it("orders members by position and reports heads only where GitCafe gives them", () => {
370 const { stack } = decodeStackEnvelope({
371 stack: {
372 id: "stack-one",
373 number: 3,
374 revision: 1,
375 landingBase: "main",
376 members: [
377 {
378 pullRequestNumber: 7,
379 title: "Top",
380 state: "open",
381 draft: true,
382 sourceBranch: "feature",
383 position: 2,
384 },
385 {
386 pullRequestNumber: 6,
387 title: "Base",
388 state: "merged",
389 draft: false,
390 sourceBranch: "base",
391 headOid: "0123456789abcdef0123456789abcdef01234567",
392 position: 1,
393 },
394 ],
395 },
396 });
397 assert.isNotNull(stack);
398 const result = GitCafePullRequestJson.toStack(stack!, {
399 repository: "owner/repo",
400 host: "staging.git.cafe",
401 });
402 assert.strictEqual(result.url, "https://staging.git.cafe/owner/repo/stacks/3");
403 assert.strictEqual(result.base, "main");
404 assert.deepStrictEqual(result.layers, [
405 {
406 number: 6,
407 title: "Base",
408 headBranch: "base",
409 state: "merged",
410 isDraft: false,
411 headSha: "0123456789abcdef0123456789abcdef01234567",
412 },
413 { number: 7, title: "Top", headBranch: "feature", state: "open", isDraft: true },
414 ]);
415 });
416​
417 it("reads an unstacked pull request as no stack", () => {
418 assert.isNull(decodeStackEnvelope({ stack: null }).stack);
419 });
420});
421​
422describe("toDiff", () => {
423 it("converts numeric hunk coordinates into a unified patch", () => {
424 const diff = decodeDiff({
425 items: [
426 {
427 path: "file.ts",
428 oldPath: null,
429 status: "modified",
430 additions: 1,
431 deletions: 1,
432 binary: false,
433 hunks: [
434 {
435 oldStart: 1,
436 oldLines: 1,
437 newStart: 1,
438 newLines: 1,
439 lines: [
440 { origin: "-", content: "old\n" },
441 { origin: "+", content: "new\n" },
442 ],
443 },
444 ],
445 },
446 ],
447 truncated: false,
448 });
449 assert.include(GitCafePullRequestJson.toDiff(diff).patch, "@@ -1,1 +1,1 @@\n-old\n+new\n");
450 });
451​
452 it("marks an omitted patch incomplete even when GitCafe's own flag is false", () => {
453 const diff = decodeDiff({
454 items: [{ path: "large.ts", oldPath: null, status: "modified", tooLarge: true, hunks: [] }],
455 truncated: false,
456 });
457 const result = GitCafePullRequestJson.toDiff(diff);
458 assert.isTrue(result.truncated);
459 assert.deepStrictEqual(result.omittedFileStats, [
460 { path: "large.ts", additions: 0, deletions: 0 },
461 ]);
462 });
463​
464 it("quotes renamed and binary paths", () => {
465 const { patch } = GitCafePullRequestJson.toDiff(
466 decodeDiff({
467 items: [
468 {
469 path: "new image.png",
470 oldPath: "old image.png",
471 status: "renamed",
472 binary: true,
473 hunks: [],
474 },
475 ],
476 truncated: false,
477 }),
478 );
479 assert.include(patch, "rename from old image.png");
480 assert.include(patch, "Binary files a/old image.png and b/new image.png differ");
481 });
482​
483 it("C-escapes a name whose newline or tab would break the patch headers", () => {
484 const { patch } = GitCafePullRequestJson.toDiff(
485 decodeDiff({
486 items: [{ path: "two\nlines\tname.ts", status: "added", hunks: [] }],
487 truncated: false,
488 }),
489 );
490 assert.include(patch, 'diff --git "a/two\\nlines\\tname.ts" "b/two\\nlines\\tname.ts"');
491 assert.notInclude(patch, "two\nlines");
492 });
493});
494​
495describe("toChecks", () => {
496 it("maps GitCafe's status and conclusion onto the shared check states", () => {
497 const check = (status: string, conclusion: string | null) => ({
498 name: `${status}-${conclusion}`,
499 status,
500 conclusion,
501 summary: null,
502 detailsUrl: null,
503 });
504 const checks = GitCafePullRequestJson.toChecks(
505 decodeChecks({
506 items: [
507 check("queued", null),
508 check("in_progress", null),
509 check("completed", "success"),
510 check("completed", "action_required"),
511 check("completed", "timed_out"),
512 ],
513 next: null,
514 }),
515 );
516 assert.deepStrictEqual(
517 checks.map((item) => item.status),
518 ["pending", "pending", "success", "action-required", "failure"],
519 );
520 });
521});