Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pullRequestAnchor.test.ts

257 lines10.5 KB

1import { expect, it } from "vite-plus/test";
2import {
3 parsePullRequestAnchor,
4 pullRequestAnchorMatchesUrl,
5 pullRequestDiffPathMatches,
6 pullRequestLinkCommit,
7 resolvePullRequestAnchorRange,
8 resolvePullRequestReviewAnchor,
9} from "./pullRequestAnchor.ts";
10​
11const gitlab = "https://gitlab.com/group/repo/-/merge_requests/7";
12const forgejo = "https://codeberg.org/owner/repo/pulls/7";
13const bitbucket = "https://bitbucket.org/workspace/repo/pull-requests/7";
14const azure = "https://dev.azure.com/org/project/_git/repo/pullrequest/7";
15​
16const url = "https://github.com/owner/repo/pull/7";
17it.each(["discussion_r123", "r123", "discussion-diff-123R5-L9", "discussion-diff-123"])(
18 "resolves review comment alias %s",
19 (hash) => {
20 expect(parsePullRequestAnchor(`${url}#${hash}`)).toEqual({
21 kind: "timeline",
22 anchor: "discussion_r123",
23 });
24 expect(pullRequestAnchorMatchesUrl("discussion_r123", `${url}#${hash}`)).toBe(true);
25 },
26);
27it.each([
28 "pullrequestreview-123",
29 "issuecomment-123",
30 "commitcomment-123",
31 "event-123",
32 "note_123",
33 "comment-123",
34])("preserves timeline anchor %s", (anchor) => {
35 expect(parsePullRequestAnchor(`${url}#${anchor}`)).toEqual({ kind: "timeline", anchor });
36});
37it("normalizes and matches mixed-case timeline anchors", () => {
38 for (const [raw, canonical] of [
39 ["ISSUECOMMENT-123", "issuecomment-123"],
40 ["PullRequestReview-9", "pullrequestreview-9"],
41 ["DISCUSSION_R123", "discussion_r123"],
42 ["NOTE_5", "note_5"],
43 ] as const) {
44 expect(parsePullRequestAnchor(`${url}#${raw}`)).toEqual({
45 kind: "timeline",
46 anchor: canonical,
47 });
48 expect(pullRequestAnchorMatchesUrl(raw, `${url}#${canonical}`)).toBe(true);
49 expect(pullRequestAnchorMatchesUrl(canonical, `${url}#${raw}`)).toBe(true);
50 }
51});
52it.each(["a".repeat(32), "b".repeat(64)])(
53 "reads file and cross-side line ranges for %s",
54 (digest) => {
55 expect(parsePullRequestAnchor(`${url}/files#diff-${digest}`)).toEqual({
56 kind: "diff",
57 digest,
58 range: null,
59 });
60 expect(parsePullRequestAnchor(`${url}/changes#diff-${digest}L12-R15`)).toEqual({
61 kind: "diff",
62 digest,
63 range: { start: 12, side: "left", end: 15, endSide: "right" },
64 });
65 expect(parsePullRequestAnchor(`${url}#diff-${digest}R42`)).toEqual({
66 kind: "diff",
67 digest,
68 range: { start: 42, side: "right", end: 42, endSide: "right" },
69 });
70 },
71);
72it.each([
73 [
74 "src/hello world.ts",
75 "bcea8deb9d67b305e28c4a0b0d021615",
76 "066802cf2cf056706e4e4951cb7dff2717a2ae32fd9797c1f1b41062d57db929",
77 "f3f78c2026bd34f4ad36bf36e9c557dd9df528fb",
78 ],
79 [
80 "src/日本語.ts",
81 "5c66d5d565666e46924cf80d7bbfdab3",
82 "191b8ebf2241225db999eaf8b6ff44b07934d577f347d0ff684591eecad34944",
83 "2ec8ce696c4c6df9419ad640fbc0d6ef4d79ebad",
84 ],
85 [
86 "old/path.ts",
87 "f5d455478d496859b654884654393a61",
88 "aa38d67014e2c59d51ea456a155e039f4e32f636a4c450714c3b517bb8bde7e7",
89 "ca0d69af01140f25bafd6ab7aec204ce52a9bbb3",
90 ],
91])("matches GitHub and GitLab path digests for %s", (path, legacy, current, gitLab) => {
92 for (const digest of [legacy, current, gitLab, gitLab.slice(0, 9)]) {
93 expect(pullRequestDiffPathMatches(digest, path)).toBe(true);
94 expect(pullRequestDiffPathMatches(digest, "other.ts")).toBe(false);
95 }
96});
97it("handles encoded hashes and rejects invalid line numbers and URLs", () => {
98 expect(parsePullRequestAnchor(`${url}#%64iscussion_r123`)).toEqual({
99 kind: "timeline",
100 anchor: "discussion_r123",
101 });
102 expect(parsePullRequestAnchor(`${url}#diff-abcR0`)).toBeNull();
103 expect(parsePullRequestAnchor(`${url}#diff-abcR9999999999999999999`)).toBeNull();
104 const sha = "c".repeat(40);
105 for (const anchor of [
106 `${sha}_0_0`,
107 `${sha}_1_9999999999999999999`,
108 `line_${sha.slice(0, 9)}_0`,
109 ]) {
110 expect(parsePullRequestAnchor(`${gitlab}/diffs#${anchor}`)).toMatchObject({ range: null });
111 }
112 expect(parsePullRequestAnchor(`${gitlab}/diffs#${sha}_0_5`)).toEqual({
113 kind: "diff",
114 digest: sha,
115 range: { start: 5, side: "right", end: 5, endSide: "right" },
116 });
117 expect(parsePullRequestAnchor("invalid")).toBeNull();
118 expect(parsePullRequestAnchor(url)).toBeNull();
119 expect(parsePullRequestAnchor(`${url}#unknown`)).toEqual({ kind: "summary", anchor: "unknown" });
120 expect(parsePullRequestAnchor(`${url}/files-not-a-tab`)).toBeNull();
121 expect(parsePullRequestAnchor(`${url}/commits/main`)).toBeNull();
122 expect(parsePullRequestAnchor(`${url}/checks/extra`)).toBeNull();
123 for (const query of ["_a=overview", "_a=commits", "_a=unknown", "discussionId=123"]) {
124 expect(parsePullRequestAnchor(`${azure}?${query}`)).toBeNull();
125 }
126 expect(parsePullRequestAnchor(`${url}?_a=files`)).toBeNull();
127});
128​
129it("keeps the commit scope of a PR diff permalink", () => {
130 const sha = "a".repeat(40);
131 const upperSha = sha.toUpperCase();
132 expect(pullRequestLinkCommit(`${url}/commits/${sha}#diff-abcR5`)).toBe(sha);
133 expect(pullRequestLinkCommit(`${url}/commits/${upperSha}#diff-abcR5`)).toBe(sha);
134 expect(pullRequestLinkCommit(`${forgejo}/commits/${sha}#diff-abcR5`)).toBe(sha);
135 expect(pullRequestLinkCommit(`${url}/files#diff-abcR5`)).toBeNull();
136 expect(pullRequestLinkCommit(`${gitlab}/diffs?commit_id=${sha}#${"b".repeat(40)}`)).toBe(sha);
137 expect(pullRequestLinkCommit(`${gitlab}/diffs?commit_id=${upperSha}#${"b".repeat(40)}`)).toBe(
138 sha,
139 );
140 expect(pullRequestLinkCommit(`${gitlab}/diffs?commit_id=main`)).toBeNull();
141 expect(pullRequestLinkCommit(`${gitlab}?commit_id=${sha}`)).toBeNull();
142});
143​
144it("opens a SHA-256 commit named by a PR link", () => {
145 const sha = "a".repeat(64);
146 const commit = `${forgejo}/commits/${sha}`;
147 const digest = "fe0ea98c799ff6bd74b212310873eb13adb520aa";
148 expect(parsePullRequestAnchor(commit)).toEqual({ kind: "code" });
149 expect(pullRequestLinkCommit(commit)).toBe(sha);
150 expect(parsePullRequestAnchor(`${commit}#diff-${digest}R5`)).toEqual({
151 kind: "diff",
152 digest,
153 range: { start: 5, side: "right", end: 5, endSide: "right" },
154 });
155 expect(pullRequestLinkCommit(`${commit}#diff-${digest}R5`)).toBe(sha);
156 expect(parsePullRequestAnchor(`${forgejo}/commits/${"a".repeat(50)}`)).toBeNull();
157 expect(pullRequestLinkCommit(`${forgejo}/commits/${"a".repeat(50)}`)).toBeNull();
158});
159​
160it("reads GitLab file and line anchors, letting a removed old line win", () => {
161 const digest = "c".repeat(40);
162 const diff = (range: object | null, at = digest) => ({ kind: "diff", digest: at, range });
163 const line = (start: number, side: "left" | "right") => ({
164 start,
165 side,
166 end: start,
167 endSide: side,
168 });
169 expect(parsePullRequestAnchor(`${gitlab}/diffs#${digest}`)).toEqual(diff(null));
170 expect(parsePullRequestAnchor(`${gitlab}/diffs#diff-content-${digest}`)).toEqual(diff(null));
171 expect(parsePullRequestAnchor(`${gitlab}/diffs?file=${digest}`)).toEqual(diff(null));
172 expect(parsePullRequestAnchor(`${gitlab}/diffs?file=${digest}#note_5`)).toEqual({
173 kind: "timeline",
174 anchor: "note_5",
175 });
176 const code = parsePullRequestAnchor(`${gitlab}/diffs#${digest}_248_253`);
177 expect(code).toEqual(diff({ ...line(253, "right"), oldLine: 248 }));
178 const range = code?.kind === "diff" ? code.range : null;
179 expect(resolvePullRequestAnchorRange(range!, () => false)).toBe(range);
180 expect(resolvePullRequestAnchorRange(range!, (old) => old === 248)).toEqual(line(248, "left"));
181 expect(parsePullRequestAnchor(`${gitlab}/diffs#${digest}_12_0`)).toEqual(diff(line(12, "left")));
182 expect(parsePullRequestAnchor(`${gitlab}/diffs#line_${digest.slice(0, 9)}_A12`)).toEqual(
183 diff(line(12, "right"), digest.slice(0, 9)),
184 );
185 expect(parsePullRequestAnchor(`${gitlab}/diffs#line_${digest.slice(0, 9)}_12`)).toEqual(
186 diff(line(12, "left"), digest.slice(0, 9)),
187 );
188 expect(parsePullRequestAnchor(`${gitlab}#${digest}_248_253`)).toEqual(code);
189 expect(parsePullRequestAnchor(`${gitlab}#${digest}`)).toEqual(diff(null));
190 expect(parsePullRequestAnchor(`${gitlab}?file=${digest}`)).toBeNull();
191});
192​
193it("reads a SHA-like heading on a non-GitLab PR as a summary anchor", () => {
194 const heading = "0123456789abcdef0123456789abcdef01234567";
195 expect(parsePullRequestAnchor(`${url}#${heading}`)).toEqual({ kind: "summary", anchor: heading });
196 expect(parsePullRequestAnchor(`${url}#line_${heading.slice(0, 9)}_12`)).toEqual({
197 kind: "summary",
198 anchor: `line_${heading.slice(0, 9)}_12`,
199 });
200 expect(parsePullRequestAnchor(`${gitlab}/diffs#${heading}`)).toEqual({
201 kind: "diff",
202 digest: heading,
203 range: null,
204 });
205 expect(
206 parsePullRequestAnchor(`https://git.example.com/a/b/-/merge_requests/3#${heading}`),
207 ).toMatchObject({ kind: "diff" });
208});
209​
210it.each([
211 [`${url}/files`, "code"],
212 [`${url}/files/`, "code"],
213 [`${url}/changes`, "code"],
214 [`${url}/commits/${"a".repeat(40)}`, "code"],
215 [`${url}/commits`, "commits"],
216 [`${url}/checks?check_run_id=1`, "checks"],
217 [`${forgejo}/files`, "code"],
218 [`${bitbucket}/diff`, "code"],
219 [`${bitbucket}/commits`, "commits"],
220 [`${azure}?_a=files`, "code"],
221 [`${azure}?path=%2Fsrc%2Fa.ts&discussionId=123&_a=files`, "code"],
222 [`${azure}?_a=Files`, "code"],
223 [`${gitlab}/diffs`, "code"],
224 [`${gitlab}/commits`, "commits"],
225 [`${gitlab}/pipelines`, "checks"],
226] as const)("keeps unknown and missing hashes in the tab %s names", (path, kind) => {
227 for (const hash of ["", "#unknown", "#diff-abcR0", "#diff-abcR9999999999999999999", "#%ZZ"]) {
228 expect(parsePullRequestAnchor(`${path}${hash}`)).toEqual({ kind });
229 }
230 expect(parsePullRequestAnchor(`${path}#issuecomment-123`)).toEqual({
231 kind: "timeline",
232 anchor: "issuecomment-123",
233 });
234});
235​
236it("finds a review with no text of its own through the earliest comment it posted", () => {
237 const review = `${url}#pullrequestreview-9`;
238 const threads = [
239 {
240 comments: [
241 { url: `${url}#discussion_r3`, createdAt: "2026-07-01T00:02:00Z", reviewUrl: review },
242 { url: `${url}#discussion_r2`, createdAt: "2026-07-01T00:01:00Z", reviewUrl: review },
243 { url: `${url}#discussion_r1`, createdAt: "2026-07-01T00:00:00Z" },
244 ],
245 },
246 ];
247 expect(resolvePullRequestReviewAnchor("pullrequestreview-9", [], threads)).toBe("discussion_r2");
248 expect(resolvePullRequestReviewAnchor("PullRequestReview-9", [], threads)).toBe("discussion_r2");
249 // A review shown in its own right, an unknown review and other anchors are kept.
250 expect(resolvePullRequestReviewAnchor("pullrequestreview-9", [{ url: review }], threads)).toBe(
251 "pullrequestreview-9",
252 );
253 expect(resolvePullRequestReviewAnchor("pullrequestreview-8", [], threads)).toBe(
254 "pullrequestreview-8",
255 );
256 expect(resolvePullRequestReviewAnchor("issuecomment-9", [], threads)).toBe("issuecomment-9");
257});