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