The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | import type { PullRequestDiffResult } from "@t3tools/contracts"; |
| 3 | import type { ReviewParsedDiff } from "../review/reviewModel"; |
| 4 | import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; |
| 5 | import * as reviewModel from "../review/reviewModel"; |
| 6 | import { |
| 7 | combinePullRequestDiffPages, |
| 8 | getCachedPullRequestDiffPages, |
| 9 | retainPullRequestDiffPage, |
| 10 | withWithheldFiles, |
| 11 | type WithheldFileLoad, |
| 12 | } from "./pullRequestDiffPages"; |
| 13 | |
| 14 | const GITHUB = sourceControlClients.get("github").changeRequest; |
| 15 | |
| 16 | function diff(path: string): PullRequestDiffResult { |
| 17 | return { |
| 18 | patch: `diff --git a/${path} b/${path}\n--- a/${path}\n+++ b/${path}\n@@ -1 +1 @@\n-old\n+new\n`, |
| 19 | truncated: false, |
| 20 | nextCursor: null, |
| 21 | }; |
| 22 | } |
| 23 | |
| 24 | function withheldDiff(): PullRequestDiffResult { |
| 25 | return { |
| 26 | patch: "diff --git a/large.ts b/large.ts\n--- a/large.ts\n+++ b/large.ts\n", |
| 27 | truncated: true, |
| 28 | nextCursor: null, |
| 29 | omittedFileStats: [{ path: "large.ts", additions: 3000, deletions: 1800 }], |
| 30 | }; |
| 31 | } |
| 32 | |
| 33 | describe("paged mobile pull request diffs", () => { |
| 34 | it("isolates scopes, cursors, and provider terminology for the same result", () => { |
| 35 | const result = withheldDiff(); |
| 36 | const pages = [{ cursor: null, result }]; |
| 37 | const first = getCachedPullRequestDiffPages(pages, "first", GITHUB); |
| 38 | const otherScope = getCachedPullRequestDiffPages(pages, "second", GITHUB); |
| 39 | const otherCursor = getCachedPullRequestDiffPages( |
| 40 | [{ cursor: "next", result }], |
| 41 | "first", |
| 42 | GITHUB, |
| 43 | ); |
| 44 | const otherProvider = getCachedPullRequestDiffPages( |
| 45 | pages, |
| 46 | "first", |
| 47 | sourceControlClients.get("gitlab").changeRequest, |
| 48 | ); |
| 49 | expect(otherScope).not.toBe(first); |
| 50 | expect(otherCursor).not.toBe(first); |
| 51 | expect(otherProvider).not.toBe(first); |
| 52 | if (first.kind !== "files" || otherScope.kind !== "files" || otherProvider.kind !== "files") |
| 53 | throw new Error("Expected structured files"); |
| 54 | expect(otherScope.files[0]?.id).not.toBe(first.files[0]?.id); |
| 55 | expect(first.notice).toContain("pull request"); |
| 56 | expect(otherProvider.notice).toContain("merge request"); |
| 57 | expect(getCachedPullRequestDiffPages(pages, "first", GITHUB)).toBe(first); |
| 58 | }); |
| 59 | |
| 60 | it("reuses earlier pages on append and invalidates changed continuation results", () => { |
| 61 | using parse = vi.spyOn(reviewModel, "buildReviewParsedDiff"); |
| 62 | const firstPage = { cursor: null, result: diff("first.ts") }; |
| 63 | const first = getCachedPullRequestDiffPages([firstPage], "append", GITHUB); |
| 64 | const nextPage = { cursor: "next", result: withheldDiff() }; |
| 65 | const combined = getCachedPullRequestDiffPages([firstPage, nextPage], "append", GITHUB); |
| 66 | expect(parse).toHaveBeenCalledTimes(2); |
| 67 | if (first.kind !== "files" || combined.kind !== "files") throw new Error("Expected files"); |
| 68 | expect(combined.files[0]).toBe(first.files[0]); |
| 69 | expect(combined.files.map((file) => file.path)).toEqual(["first.ts", "large.ts"]); |
| 70 | expect(getCachedPullRequestDiffPages([firstPage, nextPage], "append", GITHUB)).toBe(combined); |
| 71 | const refreshed = getCachedPullRequestDiffPages( |
| 72 | [ |
| 73 | firstPage, |
| 74 | { |
| 75 | ...nextPage, |
| 76 | result: { |
| 77 | ...nextPage.result, |
| 78 | omittedFileStats: [{ path: "large.ts", additions: 4000, deletions: 2000 }], |
| 79 | }, |
| 80 | }, |
| 81 | ], |
| 82 | "append", |
| 83 | GITHUB, |
| 84 | ); |
| 85 | expect(parse).toHaveBeenCalledTimes(3); |
| 86 | expect(refreshed).not.toBe(combined); |
| 87 | if (refreshed.kind !== "files") throw new Error("Expected refreshed files"); |
| 88 | expect(refreshed.files[0]).toBe(first.files[0]); |
| 89 | expect(refreshed.files[1]).toMatchObject({ additions: 4000, deletions: 2000 }); |
| 90 | expect(combined.files[1]?.additions).toBe(3000); |
| 91 | }); |
| 92 | |
| 93 | it("withheld PR hunks must retain provider line counts", () => { |
| 94 | const result = withheldDiff(); |
| 95 | const cache = new WeakMap<PullRequestDiffResult, ReviewParsedDiff>(); |
| 96 | const pages = [{ cursor: null, result }]; |
| 97 | const parsed = combinePullRequestDiffPages(pages, cache, "pr", GITHUB); |
| 98 | if (parsed.kind !== "files") throw new Error("Expected structured diff"); |
| 99 | expect(parsed.files[0]).toMatchObject({ |
| 100 | path: "large.ts", |
| 101 | additions: 3000, |
| 102 | deletions: 1800, |
| 103 | rows: [], |
| 104 | }); |
| 105 | expect(parsed.additions).toBe(3000); |
| 106 | expect(parsed.deletions).toBe(1800); |
| 107 | expect(parsed.notice).toBe( |
| 108 | "Some file contents are unavailable. Open the pull request on its provider to see the full changes.", |
| 109 | ); |
| 110 | const cached = combinePullRequestDiffPages(pages, cache, "pr", GITHUB); |
| 111 | if (cached.kind !== "files") throw new Error("Expected structured diff"); |
| 112 | expect(cached.files[0]).toBe(parsed.files[0]); |
| 113 | }); |
| 114 | |
| 115 | it("combines normal and withheld pages and refreshes counts with unchanged patch text", () => { |
| 116 | const first = { ...diff("first.ts"), nextCursor: "second" }; |
| 117 | const withheld = withheldDiff(); |
| 118 | const cache = new WeakMap<PullRequestDiffResult, ReviewParsedDiff>(); |
| 119 | const pages = retainPullRequestDiffPage( |
| 120 | retainPullRequestDiffPage([], null, first), |
| 121 | "second", |
| 122 | withheld, |
| 123 | ); |
| 124 | const before = combinePullRequestDiffPages(pages, cache, "pr", GITHUB); |
| 125 | if (before.kind !== "files") throw new Error("Expected structured diff"); |
| 126 | expect(before.files.map((file) => [file.path, file.additions, file.deletions])).toEqual([ |
| 127 | ["first.ts", 1, 1], |
| 128 | ["large.ts", 3000, 1800], |
| 129 | ]); |
| 130 | expect(before.additions).toBe(3001); |
| 131 | expect(before.deletions).toBe(1801); |
| 132 | |
| 133 | const refreshed = retainPullRequestDiffPage(pages, "second", { |
| 134 | ...withheld, |
| 135 | omittedFileStats: [{ path: "large.ts", additions: 4000, deletions: 2000 }], |
| 136 | }); |
| 137 | const after = combinePullRequestDiffPages(refreshed, cache, "pr", GITHUB); |
| 138 | if (after.kind !== "files") throw new Error("Expected structured diff"); |
| 139 | expect(after.files[0]).toBe(before.files[0]); |
| 140 | expect(after.files[1]?.id).toBe(before.files[1]?.id); |
| 141 | expect(after.files[1]).toMatchObject({ additions: 4000, deletions: 2000, rows: [] }); |
| 142 | expect(after.additions).toBe(4001); |
| 143 | expect(after.deletions).toBe(2001); |
| 144 | expect(after.notice).toBe(before.notice); |
| 145 | expect(before.additions).toBe(3001); |
| 146 | }); |
| 147 | |
| 148 | it("discards later pages when a refreshed page changes", () => { |
| 149 | const first = diff("first.ts"); |
| 150 | const pages = retainPullRequestDiffPage( |
| 151 | retainPullRequestDiffPage([], null, first), |
| 152 | "second", |
| 153 | diff("second.ts"), |
| 154 | ); |
| 155 | expect(retainPullRequestDiffPage(pages, null, first)).toBe(pages); |
| 156 | expect(retainPullRequestDiffPage(pages, null, { ...first })).toBe(pages); |
| 157 | const refreshed = retainPullRequestDiffPage(pages, null, diff("changed.ts")); |
| 158 | expect(refreshed).toHaveLength(1); |
| 159 | expect(refreshed[0]?.result.patch).toContain("changed.ts"); |
| 160 | }); |
| 161 | |
| 162 | it("swaps a loaded withheld file in under its id and says what a tap does for the rest", () => { |
| 163 | const pages = [{ cursor: null, result: withheldDiff() }]; |
| 164 | const parsed = combinePullRequestDiffPages(pages, new WeakMap(), "pr", GITHUB); |
| 165 | if (parsed.kind !== "files") throw new Error("Expected structured diff"); |
| 166 | const placeholder = parsed.files[0]; |
| 167 | let merged: ReviewParsedDiff = parsed; |
| 168 | const fileWith = (load: WithheldFileLoad | undefined, canLoad = true) => { |
| 169 | merged = withWithheldFiles( |
| 170 | parsed, |
| 171 | new Map(load === undefined ? [] : [["large.ts", load]]), |
| 172 | canLoad, |
| 173 | "pr", |
| 174 | ); |
| 175 | return merged.kind === "files" ? merged.files[0] : undefined; |
| 176 | }; |
| 177 | expect(fileWith(undefined)).toMatchObject({ |
| 178 | notice: "Diff too large to display. Tap to load it.", |
| 179 | noticeTone: "warning", |
| 180 | noticeActionable: true, |
| 181 | }); |
| 182 | expect(fileWith(undefined, false)).toMatchObject({ |
| 183 | notice: "Diff too large to display here.", |
| 184 | noticeActionable: false, |
| 185 | }); |
| 186 | expect(fileWith({ status: "loading" })).toMatchObject({ noticeActionable: false }); |
| 187 | expect(fileWith({ status: "error" })).toMatchObject({ |
| 188 | notice: "Couldn't load this diff. Tap to try again.", |
| 189 | noticeActionable: true, |
| 190 | }); |
| 191 | const loaded = fileWith({ status: "loaded", result: diff("large.ts") }); |
| 192 | expect(loaded).toMatchObject({ id: placeholder?.id, path: "large.ts" }); |
| 193 | expect(loaded?.notice).toBeUndefined(); |
| 194 | expect(loaded?.rows.length).toBeGreaterThan(0); |
| 195 | // The totals follow the loaded file, which may have changed since the page was read. |
| 196 | expect(merged).toMatchObject({ additions: 1, deletions: 1 }); |
| 197 | }); |
| 198 | }); |