| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | import type { PullRequestDiffResult } from "@t3tools/contracts"; |
| 3 | import type { ReviewParsedDiff } from "../review/reviewModel"; |
| 4 | import { combinePullRequestDiffPages, retainPullRequestDiffPage } from "./pullRequestDiffPages"; |
| 5 | |
| 6 | function 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 | }; |
| 12 | } |
| 13 | |
| 14 | describe("paged mobile pull request diffs", () => { |
| 15 | it("retains file order and IDs when another page arrives", () => { |
| 16 | const first = { ...diff("first.ts"), nextCursor: "second" }; |
| 17 | const pages = retainPullRequestDiffPage([], null, first); |
| 18 | const cache = new WeakMap<PullRequestDiffResult, ReviewParsedDiff>(); |
| 19 | const before = combinePullRequestDiffPages(pages, cache, "pr"); |
| 20 | const after = combinePullRequestDiffPages( |
| 21 | retainPullRequestDiffPage(pages, "second", diff("second.ts")), |
| 22 | cache, |
| 23 | "pr", |
| 24 | ); |
| 25 | expect(before.kind).toBe("files"); |
| 26 | expect(after.kind).toBe("files"); |
| 27 | if (before.kind !== "files" || after.kind !== "files") |
| 28 | throw new Error("Expected structured diff"); |
| 29 | expect(after.files.map((file) => file.path)).toEqual(["first.ts", "second.ts"]); |
| 30 | expect(after.files[0]).toBe(before.files[0]); |
| 31 | expect(after.additions).toBe(2); |
| 32 | }); |
| 33 | |
| 34 | it("discards later pages when a refreshed page changes", () => { |
| 35 | const first = diff("first.ts"); |
| 36 | const pages = retainPullRequestDiffPage( |
| 37 | retainPullRequestDiffPage([], null, first), |
| 38 | "second", |
| 39 | diff("second.ts"), |
| 40 | ); |
| 41 | expect(retainPullRequestDiffPage(pages, null, first)).toBe(pages); |
| 42 | expect(retainPullRequestDiffPage(pages, null, { ...first })).toBe(pages); |
| 43 | const refreshed = retainPullRequestDiffPage(pages, null, diff("changed.ts")); |
| 44 | expect(refreshed).toHaveLength(1); |
| 45 | expect(refreshed[0]?.result.patch).toContain("changed.ts"); |
| 46 | }); |
| 47 | }); |