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

pullRequestDiffPages.test.ts

92 lines3.8 KB

1import { describe, expect, it } from "vite-plus/test";
2import type { PullRequestDiffResult } from "@t3tools/contracts";
3import type { ReviewParsedDiff } from "../review/reviewModel";
4import { combinePullRequestDiffPages, retainPullRequestDiffPage } from "./pullRequestDiffPages";
5​
6function diff(path: string): PullRequestDiffResult {
7 return {
8 patch: `diff --git a/${path} b/${path}\n--- a/${path}\n+++ b/${path}\n@@ -1 +1 @@\n-old\n+new\n`,
9 truncated: false,
10 nextCursor: null,
11 };
13​
14function withheldDiff(): PullRequestDiffResult {
15 return {
16 patch: "diff --git a/large.ts b/large.ts\n--- a/large.ts\n+++ b/large.ts\n",
17 truncated: true,
18 nextCursor: null,
19 omittedFileStats: [{ path: "large.ts", additions: 3000, deletions: 1800 }],
20 };
22​
23describe("paged mobile pull request diffs", () => {
24 it("withheld PR hunks must retain provider line counts", () => {
25 const result = withheldDiff();
26 const cache = new WeakMap<PullRequestDiffResult, ReviewParsedDiff>();
27 const pages = [{ cursor: null, result }];
28 const parsed = combinePullRequestDiffPages(pages, cache, "pr");
29 if (parsed.kind !== "files") throw new Error("Expected structured diff");
30 expect(parsed.files[0]).toMatchObject({
31 path: "large.ts",
32 additions: 3000,
33 deletions: 1800,
34 rows: [],
35 });
36 expect(parsed.additions).toBe(3000);
37 expect(parsed.deletions).toBe(1800);
38 expect(parsed.notice).toBe(
39 "Some file contents are unavailable. Open the pull request on its provider to see the full changes.",
40 );
41 const cached = combinePullRequestDiffPages(pages, cache, "pr");
42 if (cached.kind !== "files") throw new Error("Expected structured diff");
43 expect(cached.files[0]).toBe(parsed.files[0]);
44 });
45​
46 it("combines normal and withheld pages and refreshes counts with unchanged patch text", () => {
47 const first = { ...diff("first.ts"), nextCursor: "second" };
48 const withheld = withheldDiff();
49 const cache = new WeakMap<PullRequestDiffResult, ReviewParsedDiff>();
50 const pages = retainPullRequestDiffPage(
51 retainPullRequestDiffPage([], null, first),
52 "second",
53 withheld,
54 );
55 const before = combinePullRequestDiffPages(pages, cache, "pr");
56 if (before.kind !== "files") throw new Error("Expected structured diff");
57 expect(before.files.map((file) => [file.path, file.additions, file.deletions])).toEqual([
58 ["first.ts", 1, 1],
59 ["large.ts", 3000, 1800],
60 ]);
61 expect(before.additions).toBe(3001);
62 expect(before.deletions).toBe(1801);
63​
64 const refreshed = retainPullRequestDiffPage(pages, "second", {
65 ...withheld,
66 omittedFileStats: [{ path: "large.ts", additions: 4000, deletions: 2000 }],
67 });
68 const after = combinePullRequestDiffPages(refreshed, cache, "pr");
69 if (after.kind !== "files") throw new Error("Expected structured diff");
70 expect(after.files[0]).toBe(before.files[0]);
71 expect(after.files[1]?.id).toBe(before.files[1]?.id);
72 expect(after.files[1]).toMatchObject({ additions: 4000, deletions: 2000, rows: [] });
73 expect(after.additions).toBe(4001);
74 expect(after.deletions).toBe(2001);
75 expect(after.notice).toBe(before.notice);
76 expect(before.additions).toBe(3001);
77 });
78​
79 it("discards later pages when a refreshed page changes", () => {
80 const first = diff("first.ts");
81 const pages = retainPullRequestDiffPage(
82 retainPullRequestDiffPage([], null, first),
83 "second",
84 diff("second.ts"),
85 );
86 expect(retainPullRequestDiffPage(pages, null, first)).toBe(pages);
87 expect(retainPullRequestDiffPage(pages, null, { ...first })).toBe(pages);
88 const refreshed = retainPullRequestDiffPage(pages, null, diff("changed.ts"));
89 expect(refreshed).toHaveLength(1);
90 expect(refreshed[0]?.result.patch).toContain("changed.ts");
91 });
92});