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, 03:37 UTC
| 1 | import { expect, it } from "vite-plus/test"; |
| 2 | import { |
| 3 | parsePullRequestAnchor, |
| 4 | pullRequestAnchorMatchesUrl, |
| 5 | pullRequestDiffPathMatches, |
| 6 | pullRequestLinkCommit, |
| 7 | resolvePullRequestAnchorRange, |
| 8 | resolvePullRequestReviewAnchor, |
| 9 | } from "./pullRequestAnchor.ts"; |
| 10 | |
| 11 | const gitlab = "https://gitlab.com/group/repo/-/merge_requests/7"; |
| 12 | const forgejo = "https://codeberg.org/owner/repo/pulls/7"; |
| 13 | |
| 14 | const url = "https://github.com/owner/repo/pull/7"; |
| 15 | it.each(["discussion_r123", "r123", "discussion-diff-123R5-L9", "discussion-diff-123"])( |
| 16 | "resolves review comment alias %s", |
| 17 | (hash) => { |
| 18 | expect(parsePullRequestAnchor(`${url}#${hash}`)).toEqual({ |
| 19 | kind: "timeline", |
| 20 | anchor: "discussion_r123", |
| 21 | }); |
| 22 | expect(pullRequestAnchorMatchesUrl("discussion_r123", `${url}#${hash}`)).toBe(true); |
| 23 | }, |
| 24 | ); |
| 25 | it.each([ |
| 26 | "pullrequestreview-123", |
| 27 | "issuecomment-123", |
| 28 | "commitcomment-123", |
| 29 | "event-123", |
| 30 | "note_123", |
| 31 | "comment-123", |
| 32 | ])("preserves timeline anchor %s", (anchor) => { |
| 33 | expect(parsePullRequestAnchor(`${url}#${anchor}`)).toEqual({ kind: "timeline", anchor }); |
| 34 | }); |
| 35 | it.each(["a".repeat(32), "b".repeat(64)])( |
| 36 | "reads file and cross-side line ranges for %s", |
| 37 | (digest) => { |
| 38 | expect(parsePullRequestAnchor(`${url}/files#diff-${digest}`)).toEqual({ |
| 39 | kind: "diff", |
| 40 | digest, |
| 41 | range: null, |
| 42 | }); |
| 43 | expect(parsePullRequestAnchor(`${url}/changes#diff-${digest}L12-R15`)).toEqual({ |
| 44 | kind: "diff", |
| 45 | digest, |
| 46 | range: { start: 12, side: "left", end: 15, endSide: "right" }, |
| 47 | }); |
| 48 | expect(parsePullRequestAnchor(`${url}#diff-${digest}R42`)).toEqual({ |
| 49 | kind: "diff", |
| 50 | digest, |
| 51 | range: { start: 42, side: "right", end: 42, endSide: "right" }, |
| 52 | }); |
| 53 | }, |
| 54 | ); |
| 55 | it.each([ |
| 56 | [ |
| 57 | "src/hello world.ts", |
| 58 | "bcea8deb9d67b305e28c4a0b0d021615", |
| 59 | "066802cf2cf056706e4e4951cb7dff2717a2ae32fd9797c1f1b41062d57db929", |
| 60 | "f3f78c2026bd34f4ad36bf36e9c557dd9df528fb", |
| 61 | ], |
| 62 | [ |
| 63 | "src/日本語.ts", |
| 64 | "5c66d5d565666e46924cf80d7bbfdab3", |
| 65 | "191b8ebf2241225db999eaf8b6ff44b07934d577f347d0ff684591eecad34944", |
| 66 | "2ec8ce696c4c6df9419ad640fbc0d6ef4d79ebad", |
| 67 | ], |
| 68 | [ |
| 69 | "old/path.ts", |
| 70 | "f5d455478d496859b654884654393a61", |
| 71 | "aa38d67014e2c59d51ea456a155e039f4e32f636a4c450714c3b517bb8bde7e7", |
| 72 | "ca0d69af01140f25bafd6ab7aec204ce52a9bbb3", |
| 73 | ], |
| 74 | ])("matches GitHub and GitLab path digests for %s", (path, legacy, current, gitLab) => { |
| 75 | for (const digest of [legacy, current, gitLab, gitLab.slice(0, 9)]) { |
| 76 | expect(pullRequestDiffPathMatches(digest, path)).toBe(true); |
| 77 | expect(pullRequestDiffPathMatches(digest, "other.ts")).toBe(false); |
| 78 | } |
| 79 | }); |
| 80 | it("handles encoded hashes and rejects invalid line numbers and URLs", () => { |
| 81 | expect(parsePullRequestAnchor(`${url}#%64iscussion_r123`)).toEqual({ |
| 82 | kind: "timeline", |
| 83 | anchor: "discussion_r123", |
| 84 | }); |
| 85 | expect(parsePullRequestAnchor(`${url}#diff-abcR0`)).toBeNull(); |
| 86 | expect(parsePullRequestAnchor(`${url}#diff-abcR9999999999999999999`)).toBeNull(); |
| 87 | const sha = "c".repeat(40); |
| 88 | for (const anchor of [ |
| 89 | `${sha}_0_0`, |
| 90 | `${sha}_1_9999999999999999999`, |
| 91 | `line_${sha.slice(0, 9)}_0`, |
| 92 | ]) { |
| 93 | expect(parsePullRequestAnchor(`${gitlab}/diffs#${anchor}`)).toMatchObject({ range: null }); |
| 94 | } |
| 95 | expect(parsePullRequestAnchor(`${gitlab}/diffs#${sha}_0_5`)).toEqual({ |
| 96 | kind: "diff", |
| 97 | digest: sha, |
| 98 | range: { start: 5, side: "right", end: 5, endSide: "right" }, |
| 99 | }); |
| 100 | expect(parsePullRequestAnchor("invalid")).toBeNull(); |
| 101 | expect(parsePullRequestAnchor(url)).toBeNull(); |
| 102 | expect(parsePullRequestAnchor(`${url}#unknown`)).toEqual({ kind: "summary", anchor: "unknown" }); |
| 103 | expect(parsePullRequestAnchor(`${url}/files-not-a-tab`)).toBeNull(); |
| 104 | expect(parsePullRequestAnchor(`${url}/commits/main`)).toBeNull(); |
| 105 | expect(parsePullRequestAnchor(`${url}/checks/extra`)).toBeNull(); |
| 106 | }); |
| 107 | |
| 108 | it("keeps the commit scope of a PR diff permalink", () => { |
| 109 | const sha = "a".repeat(40); |
| 110 | expect(pullRequestLinkCommit(`${url}/commits/${sha}#diff-abcR5`)).toBe(sha); |
| 111 | expect(pullRequestLinkCommit(`${forgejo}/commits/${sha}#diff-abcR5`)).toBe(sha); |
| 112 | expect(pullRequestLinkCommit(`${url}/files#diff-abcR5`)).toBeNull(); |
| 113 | expect(pullRequestLinkCommit(`${gitlab}/diffs?commit_id=${sha}#${"b".repeat(40)}`)).toBe(sha); |
| 114 | expect(pullRequestLinkCommit(`${gitlab}/diffs?commit_id=main`)).toBeNull(); |
| 115 | expect(pullRequestLinkCommit(`${gitlab}?commit_id=${sha}`)).toBeNull(); |
| 116 | }); |
| 117 | |
| 118 | it("reads GitLab file and line anchors, letting a removed old line win", () => { |
| 119 | const digest = "c".repeat(40); |
| 120 | const diff = (range: object | null, at = digest) => ({ kind: "diff", digest: at, range }); |
| 121 | const line = (start: number, side: "left" | "right") => ({ |
| 122 | start, |
| 123 | side, |
| 124 | end: start, |
| 125 | endSide: side, |
| 126 | }); |
| 127 | expect(parsePullRequestAnchor(`${gitlab}/diffs#${digest}`)).toEqual(diff(null)); |
| 128 | expect(parsePullRequestAnchor(`${gitlab}/diffs#diff-content-${digest}`)).toEqual(diff(null)); |
| 129 | expect(parsePullRequestAnchor(`${gitlab}/diffs?file=${digest}`)).toEqual(diff(null)); |
| 130 | expect(parsePullRequestAnchor(`${gitlab}/diffs?file=${digest}#note_5`)).toEqual({ |
| 131 | kind: "timeline", |
| 132 | anchor: "note_5", |
| 133 | }); |
| 134 | const code = parsePullRequestAnchor(`${gitlab}/diffs#${digest}_248_253`); |
| 135 | expect(code).toEqual(diff({ ...line(253, "right"), oldLine: 248 })); |
| 136 | const range = code?.kind === "diff" ? code.range : null; |
| 137 | expect(resolvePullRequestAnchorRange(range!, () => false)).toBe(range); |
| 138 | expect(resolvePullRequestAnchorRange(range!, (old) => old === 248)).toEqual(line(248, "left")); |
| 139 | expect(parsePullRequestAnchor(`${gitlab}/diffs#${digest}_12_0`)).toEqual(diff(line(12, "left"))); |
| 140 | expect(parsePullRequestAnchor(`${gitlab}/diffs#line_${digest.slice(0, 9)}_A12`)).toEqual( |
| 141 | diff(line(12, "right"), digest.slice(0, 9)), |
| 142 | ); |
| 143 | expect(parsePullRequestAnchor(`${gitlab}/diffs#line_${digest.slice(0, 9)}_12`)).toEqual( |
| 144 | diff(line(12, "left"), digest.slice(0, 9)), |
| 145 | ); |
| 146 | expect(parsePullRequestAnchor(`${gitlab}#${digest}_248_253`)).toEqual(code); |
| 147 | expect(parsePullRequestAnchor(`${gitlab}#${digest}`)).toEqual(diff(null)); |
| 148 | expect(parsePullRequestAnchor(`${gitlab}?file=${digest}`)).toBeNull(); |
| 149 | }); |
| 150 | |
| 151 | it.each([ |
| 152 | [`${url}/files`, "code"], |
| 153 | [`${url}/files/`, "code"], |
| 154 | [`${url}/changes`, "code"], |
| 155 | [`${url}/commits/${"a".repeat(40)}`, "code"], |
| 156 | [`${url}/commits`, "commits"], |
| 157 | [`${url}/checks?check_run_id=1`, "checks"], |
| 158 | [`${forgejo}/files`, "code"], |
| 159 | [`${gitlab}/diffs`, "code"], |
| 160 | [`${gitlab}/commits`, "commits"], |
| 161 | [`${gitlab}/pipelines`, "checks"], |
| 162 | ] as const)("keeps unknown and missing hashes in the tab %s names", (path, kind) => { |
| 163 | for (const hash of ["", "#unknown", "#diff-abcR0", "#diff-abcR9999999999999999999", "#%ZZ"]) { |
| 164 | expect(parsePullRequestAnchor(`${path}${hash}`)).toEqual({ kind }); |
| 165 | } |
| 166 | expect(parsePullRequestAnchor(`${path}#issuecomment-123`)).toEqual({ |
| 167 | kind: "timeline", |
| 168 | anchor: "issuecomment-123", |
| 169 | }); |
| 170 | }); |
| 171 | |
| 172 | it("finds a review with no text of its own through the earliest comment it posted", () => { |
| 173 | const review = `${url}#pullrequestreview-9`; |
| 174 | const threads = [ |
| 175 | { |
| 176 | comments: [ |
| 177 | { url: `${url}#discussion_r3`, createdAt: "2026-07-01T00:02:00Z", reviewUrl: review }, |
| 178 | { url: `${url}#discussion_r2`, createdAt: "2026-07-01T00:01:00Z", reviewUrl: review }, |
| 179 | { url: `${url}#discussion_r1`, createdAt: "2026-07-01T00:00:00Z" }, |
| 180 | ], |
| 181 | }, |
| 182 | ]; |
| 183 | expect(resolvePullRequestReviewAnchor("pullrequestreview-9", [], threads)).toBe("discussion_r2"); |
| 184 | // A review shown in its own right, an unknown review and other anchors are kept. |
| 185 | expect(resolvePullRequestReviewAnchor("pullrequestreview-9", [{ url: review }], threads)).toBe( |
| 186 | "pullrequestreview-9", |
| 187 | ); |
| 188 | expect(resolvePullRequestReviewAnchor("pullrequestreview-8", [], threads)).toBe( |
| 189 | "pullrequestreview-8", |
| 190 | ); |
| 191 | expect(resolvePullRequestReviewAnchor("issuecomment-9", [], threads)).toBe("issuecomment-9"); |
| 192 | }); |