Skip to content

Commit dfe1fe5

Browse files
saphidcodex
andcommitted
fix(mobile): preserve line counts for withheld pull request hunks
Co-Authored-By: GPT-6.1 Sol <[email protected]>
1 parent 511d20c commit dfe1fe5

2 files changed

Lines changed: 79 additions & 1 deletion

File tree

‎apps/mobile/src/features/pull-requests/pullRequestDiffPages.test.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,70 @@ function diff(path: string): PullRequestDiffResult {
1111
};
1212
}
1313

14+
function 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+
};
21+
}
22+
1423
describe("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+
});
1578
it("retains file order and IDs when another page arrives", () => {
1679
const first = { ...diff("first.ts"), nextCursor: "second" };
1780
const pages = retainPullRequestDiffPage([], null, first);

‎apps/mobile/src/features/pull-requests/pullRequestDiffPages.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,22 @@ export function combinePullRequestDiffPages(
3535
const parsed = pages.map((page) => {
3636
const cached = cache.get(page.result);
3737
if (cached) return cached;
38-
const value = buildReviewParsedDiff(page.result.patch, `${scope}:${page.cursor ?? "first"}`);
38+
let value = buildReviewParsedDiff(page.result.patch, `${scope}:${page.cursor ?? "first"}`);
39+
if (value.kind === "files" && page.result.omittedFileStats?.length) {
40+
const omittedFileStats = new Map(
41+
page.result.omittedFileStats.map((stat) => [stat.path, stat]),
42+
);
43+
const files = value.files.map((file) => {
44+
const stat = omittedFileStats.get(file.path);
45+
return stat ? { ...file, additions: stat.additions, deletions: stat.deletions } : file;
46+
});
47+
value = {
48+
...value,
49+
files,
50+
additions: files.reduce((total, file) => total + file.additions, 0),
51+
deletions: files.reduce((total, file) => total + file.deletions, 0),
52+
};
53+
}
3954
cache.set(page.result, value);
4055
return value;
4156
});

0 commit comments

Comments
 (0)