Open History to see the last commit
1import {
2 parseGitRemoteHost,
3 recognizeRemoteWith,
4} from "@t3tools/source-control-core/shared/definition";
5import { describe, expect, it } from "vite-plus/test";
6
7import { definition } from "./definition.ts";
8
9const parse = (url: string) => definition.parseChangeRequestUrl(new URL(url));
10
11describe("GitHub change request URLs", () => {
12 it("reads a pull request on any host, lower-casing host and repository", () => {
13 expect(parse("https://github.com/T3Tools/T3Code/pull/123/files?w=1#diff")).toEqual({
14 host: "github.com",
15 repository: "t3tools/t3code",
16 number: 123,
17 repositoryUrl: "https://github.com/T3Tools/T3Code",
18 });
19 expect(parse("https://GHE.Example.com:8443/acme/web/pull/42/")).toEqual({
20 host: "ghe.example.com",
21 repository: "acme/web",
22 number: 42,
23 repositoryUrl: "https://ghe.example.com:8443/acme/web",
24 });
25 });
26
27 it("claims only the two-segment pull route", () => {
28 for (const url of [
29 "https://github.com/t3tools/t3code/issues/123",
30 "https://github.com/t3tools/t3code/pulls/123",
31 "https://github.com/t3tools/t3code/pull/abc",
32 "https://github.com/t3tools/t3code/pull/0",
33 "https://github.com/group/sub/repo/pull/1",
34 "ftp://github.com/t3tools/t3code/pull/1",
35 ]) {
36 expect(parse(url), url).toBeNull();
37 }
38 });
39
40 it("builds pull request URLs below the repository, keeping an HTTP remote's origin", () => {
41 expect(
42 definition.changeRequestUrl({
43 host: "github.acme.test",
44 repository: "platform/api",
45 number: 7,
46 remoteUrl: "http://token@github.acme.test:8443/team/default.git",
47 }),
48 ).toBe("http://github.acme.test:8443/platform/api/pull/7");
49 for (const remoteUrl of [
50 "git@github.acme.test:team/default.git",
51 "git://github.acme.test/team/default.git",
52 "http://other.test/team/default.git",
53 ]) {
54 expect(
55 definition.changeRequestUrl({
56 host: "github.acme.test",
57 repository: "platform/api",
58 number: 9,
59 remoteUrl,
60 }),
61 remoteUrl,
62 ).toBe("https://github.acme.test/platform/api/pull/9");
63 }
64 expect(
65 definition.changeRequestUrl({
66 host: "github.com",
67 repository: "pull/1",
68 number: 43,
69 webUrl: "https://github.com/pull/1",
70 }),
71 ).toBe("https://github.com/pull/1/pull/43");
72 });
73
74 // regression: #8507 the browser fallback must not open a bogus URL
75 it("builds nothing from an invalid repository, number, or host", () => {
76 for (const [host, repository, number] of [
77 ["github.com", "acme", 1],
78 ["github.com", "../repository", 1],
79 ["github.com", "acme/%2e%2e", 1],
80 ["github.com", "acme/repository?tab=1", 1],
81 ["github.com", "acme/repository", 0],
82 ["bad host", "acme/repository", 1],
83 ] as const) {
84 expect(definition.changeRequestUrl({ host, repository, number })).toBeNull();
85 }
86 });
87
88 it("turns GitHub's shared issue route into a pull request candidate", () => {
89 const candidate = (url: string) =>
90 definition.changeRequestUrlFromReferenceAutolink(new URL(url));
91 expect(candidate("https://github.com/pingdotgg/t3code/issues/8600#issuecomment-1")).toBe(
92 "https://github.com/pingdotgg/t3code/pull/8600#issuecomment-1",
93 );
94 expect(candidate("https://code.github.example.com/acme/web/issues/42")).toBe(
95 "https://code.github.example.com/acme/web/pull/42",
96 );
97 expect(candidate("https://github.com/pingdotgg/t3code/issues/not-a-number")).toBeNull();
98 expect(candidate("https://code.example.test/acme/web/issues/42")).toBeNull();
99 });
100});
101
102describe("GitHub author profiles", () => {
103 it("links people but not bots, whose GraphQL logins have no `[bot]` suffix", () => {
104 const repositoryUrl = "https://github.com/owner/repo";
105 expect(definition.authorProfileUrl({ login: "octocat" }, repositoryUrl)).toBe(
106 "https://github.com/octocat",
107 );
108 expect(
109 definition.authorProfileUrl({ login: "macroscopeapp", isBot: true }, repositoryUrl),
110 ).toBe(null);
111 expect(definition.authorProfileUrl({ login: "dependabot[bot]" }, repositoryUrl)).toBe(null);
112 });
113});
114
115describe("GitHub CLI commands", () => {
116 it("recognizes shell pull request creates, including gh's `new` alias", () => {
117 for (const command of ["gh pr create --fill", "gh pr new --fill"]) {
118 expect(definition.changeRequestCreateCommand.test(command), command).toBe(true);
119 }
120 expect(definition.changeRequestCreateCommand.test("gh pr view 7")).toBe(false);
121 });
122});
123
124describe("GitHub repository pages", () => {
125 it("leaves the tree route on other hosts to them", () => {
126 expect(
127 definition.repositoryUrlFromPage?.(new URL("https://scm.example.test/o/r/tree/main")),
128 ).toBeNull();
129 });
130});
131
132describe("GitHub remotes", () => {
133 const recognize = (url: string) => recognizeRemoteWith(definition, url);
134
135 it("recognises github.com, ghe.com subdomains and hosts with a github label", () => {
136 expect(recognize("deploy@github.com:owner/repo.git")).toEqual({
137 kind: "github",
138 name: "GitHub",
139 baseUrl: "https://github.com",
140 });
141 expect(recognize("https://acme.ghe.com/owner/repo.git")).toEqual({
142 kind: "github",
143 name: "GitHub Self-Hosted",
144 baseUrl: "https://acme.ghe.com",
145 });
146 expect(recognize("git@acme.ghe.com:owner/repo.git")?.kind).toBe("github");
147 expect(recognize("https://github.example.com:8443/owner/repo.git")?.baseUrl).toBe(
148 "https://github.example.com:8443",
149 );
150 });
151
152 it("does not claim ghe.com itself or a label that only contains github", () => {
153 expect(recognize("https://ghe.com/owner/repo.git")).toBeNull();
154 expect(recognize("https://notgithub.example.com/owner/repo.git")).toBeNull();
155 });
156
157 it("reads the host ssh connects to for SCP logins, but not Git's bracketed forms", () => {
158 for (const url of [
159 "alice+ci@github.com:owner/repo.git",
160 "alice@corp.example@github.com:owner/repo.git",
161 ]) {
162 expect(recognize(url)?.baseUrl, url).toBe("https://github.com");
163 }
164 for (const url of [
165 "@github.com:owner/repo.git",
166 "alice@[work]@github.com:owner/repo.git",
167 "[git@git.internal]@github.com:repo@[x",
168 "-oProxyCommand=x@github.com:owner/repo.git",
169 "git@github.com:-x",
170 ]) {
171 expect(recognize(url), url).toBeNull();
172 }
173 expect(parseGitRemoteHost("alice+ci@[2001:db8::1]:team/repo.git")).toBeNull();
174 });
175});
176
177describe("GitHub media URLs", () => {
178 const media = (url: string) => definition.mediaUrl(new URL(url));
179
180 it("fetches every spelling of an upload or committed file from one canonical URL", () => {
181 expect(
182 media("https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c"),
183 ).toEqual({
184 fetchUrl: "https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c",
185 fileName: "1a1842fb-6383-492f-873c-57aa0033fa6c",
186 kind: "unknown",
187 immutable: true,
188 });
189 expect(media("https://github.com/owner/repo/blob/main/docs/shot.png")).toEqual({
190 fetchUrl: "https://raw.githubusercontent.com/owner/repo/main/docs/shot.png",
191 fileName: "shot.png",
192 });
193 expect(media("https://github.com/owner/repo/assets/45952064/1a1842fb")?.fetchUrl).toBe(
194 "https://github.com/owner/repo/assets/45952064/1a1842fb",
195 );
196 expect(media("https://media.githubusercontent.com/media/owner/repo/main/a.mp4")).toEqual({
197 fetchUrl: "https://media.githubusercontent.com/media/owner/repo/main/a.mp4",
198 fileName: "a.mp4",
199 });
200 expect(media("https://raw.githubusercontent.com/o/r/main/100%.png")?.fileName).toBe("100%.png");
201 });
202
203 it("marks only uploads and files at a full commit SHA as immutable", () => {
204 const sha = "0123456789abcdef0123456789abcdef01234567";
205 for (const url of [
206 "https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c",
207 "https://github.com/owner/repo/assets/45952064/1a1842fb",
208 `https://github.com/owner/repo/blob/${sha}/docs/shot.png`,
209 `https://raw.githubusercontent.com/owner/repo/${sha}/docs/shot.png`,
210 ]) {
211 expect(media(url)?.immutable, url).toBe(true);
212 }
213 for (const url of [
214 "https://raw.githubusercontent.com/owner/repo/0123456789ab/docs/shot.png",
215 `https://media.githubusercontent.com/media/owner/repo/${sha}/a.mp4`,
216 ]) {
217 expect(media(url)?.immutable, url).toBeUndefined();
218 }
219 });
220
221 it("claims nothing a request without a credential loads just as well", () => {
222 for (const url of [
223 "https://example.com/shot.png",
224 "http://github.com/user-attachments/assets/1a1842fb",
225 "https://github.com/owner/repo/pull/1",
226 "https://github.com/owner/repo/blob/main/",
227 "https://objects.githubusercontent.com/github-production-user-asset/shot.png",
228 ]) {
229 expect(media(url), url).toBeNull();
230 }
231 });
232
233 it("addresses a committed file at a full SHA as immutable credentialed media", () => {
234 const sha = "0123456789abcdef0123456789abcdef01234567";
235 const rawFileUrl = (repositoryUrl: string, path: string, revision = sha) =>
236 definition.rawFileUrl?.({ repositoryUrl, sha: revision, path }) ?? null;
237 const url = rawFileUrl("https://github.com/owner/repo", "docs/a #1?.png");
238 expect(url).toBe(`https://github.com/owner/repo/raw/${sha}/docs/a%20%231%3F.png`);
239 expect(media(url ?? "")).toEqual({
240 fetchUrl: `https://raw.githubusercontent.com/owner/repo/${sha}/docs/a%20%231%3F.png`,
241 fileName: "a #1?.png",
242 immutable: true,
243 });
244 expect(rawFileUrl("https://github.com/owner/repo", "a.png", "main")).toBeNull();
245 expect(rawFileUrl("https://github.example.com/owner/repo", "a.png")).toBeNull();
246 });
247});