TypeScript
fix(mobile): preserve line counts for withheld pull request hunks (#18836)
Alex Southwell5432ce0df966
diff --git a/apps/mobile/src/features/pull-requests/pullRequestDiffPages.test.ts b/apps/mobile/src/features/pull-requests/pullRequestDiffPages.test.tsindex 38b9393d420..9e4933ba5fb 100644--- a/apps/mobile/src/features/pull-requests/pullRequestDiffPages.test.ts+++ b/apps/mobile/src/features/pull-requests/pullRequestDiffPages.test.ts@@ -11,7 +11,70 @@ function diff(path: string): PullRequestDiffResult {};}+function withheldDiff(): PullRequestDiffResult {+ return {+ patch: "diff --git a/large.ts b/large.ts\n--- a/large.ts\n+++ b/large.ts\n",+ truncated: true,+ nextCursor: null,+ omittedFileStats: [{ path: "large.ts", additions: 3000, deletions: 1800 }],+ };+}+describe("paged mobile pull request diffs", () => {+ it("withheld PR hunks must retain provider line counts", () => {+ const result = withheldDiff();+ const cache = new WeakMap<PullRequestDiffResult, ReviewParsedDiff>();+ const pages = [{ cursor: null, result }];+ const parsed = combinePullRequestDiffPages(pages, cache, "pr");+ if (parsed.kind !== "files") throw new Error("Expected structured diff");+ expect(parsed.files[0]).toMatchObject({+ path: "large.ts",+ additions: 3000,+ deletions: 1800,+ rows: [],+ });+ expect(parsed.additions).toBe(3000);+ expect(parsed.deletions).toBe(1800);+ expect(parsed.notice).toBe(+ "Some file contents are unavailable. Open the pull request on its provider to see the full changes.",+ );+ const cached = combinePullRequestDiffPages(pages, cache, "pr");+ if (cached.kind !== "files") throw new Error("Expected structured diff");+ expect(cached.files[0]).toBe(parsed.files[0]);+ });++ it("combines normal and withheld pages and refreshes counts with unchanged patch text", () => {+ const first = { ...diff("first.ts"), nextCursor: "second" };+ const withheld = withheldDiff();+ const cache = new WeakMap<PullRequestDiffResult, ReviewParsedDiff>();+ const pages = retainPullRequestDiffPage(+ retainPullRequestDiffPage([], null, first),+ "second",+ withheld,+ );+ const before = combinePullRequestDiffPages(pages, cache, "pr");+ if (before.kind !== "files") throw new Error("Expected structured diff");+ expect(before.files.map((file) => [file.path, file.additions, file.deletions])).toEqual([+ ["first.ts", 1, 1],+ ["large.ts", 3000, 1800],+ ]);+ expect(before.additions).toBe(3001);+ expect(before.deletions).toBe(1801);++ const refreshed = retainPullRequestDiffPage(pages, "second", {+ ...withheld,+ omittedFileStats: [{ path: "large.ts", additions: 4000, deletions: 2000 }],+ });+ const after = combinePullRequestDiffPages(refreshed, cache, "pr");+ if (after.kind !== "files") throw new Error("Expected structured diff");+ expect(after.files[0]).toBe(before.files[0]);+ expect(after.files[1]?.id).toBe(before.files[1]?.id);+ expect(after.files[1]).toMatchObject({ additions: 4000, deletions: 2000, rows: [] });+ expect(after.additions).toBe(4001);+ expect(after.deletions).toBe(2001);+ expect(after.notice).toBe(before.notice);+ expect(before.additions).toBe(3001);+ });it("retains file order and IDs when another page arrives", () => {const first = { ...diff("first.ts"), nextCursor: "second" };const pages = retainPullRequestDiffPage([], null, first);diff --git a/apps/mobile/src/features/pull-requests/pullRequestDiffPages.ts b/apps/mobile/src/features/pull-requests/pullRequestDiffPages.tsindex fda053e7a0a..2392245b447 100644--- a/apps/mobile/src/features/pull-requests/pullRequestDiffPages.ts+++ b/apps/mobile/src/features/pull-requests/pullRequestDiffPages.ts@@ -35,7 +35,22 @@ export function combinePullRequestDiffPages(const parsed = pages.map((page) => {const cached = cache.get(page.result);if (cached) return cached;- const value = buildReviewParsedDiff(page.result.patch, `${scope}:${page.cursor ?? "first"}`);+ let value = buildReviewParsedDiff(page.result.patch, `${scope}:${page.cursor ?? "first"}`);+ if (value.kind === "files" && page.result.omittedFileStats?.length) {+ const omittedFileStats = new Map(+ page.result.omittedFileStats.map((stat) => [stat.path, stat]),+ );+ const files = value.files.map((file) => {+ const stat = omittedFileStats.get(file.path);+ return stat ? { ...file, additions: stat.additions, deletions: stat.deletions } : file;+ });+ value = {+ ...value,+ files,+ additions: files.reduce((total, file) => total + file.additions, 0),+ deletions: files.reduce((total, file) => total + file.deletions, 0),+ };+ }cache.set(page.result, value);return value;});