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 CLI commands", () => {
103 it("recognizes shell pull request creates, including gh's `new` alias", () => {
104 for (const command of ["gh pr create --fill", "gh pr new --fill"]) {
105 expect(definition.changeRequestCreateCommand.test(command), command).toBe(true);
106 }
107 expect(definition.changeRequestCreateCommand.test("gh pr view 7")).toBe(false);
108 });
109});
110
111describe("GitHub repository pages", () => {
112 it("leaves the tree route on other hosts to them", () => {
113 expect(
114 definition.repositoryUrlFromPage?.(new URL("https://scm.example.test/o/r/tree/main")),
115 ).toBeNull();
116 });
117});
118
119describe("GitHub remotes", () => {
120 const recognize = (url: string) => recognizeRemoteWith(definition, url);
121
122 it("recognises github.com, ghe.com subdomains and hosts with a github label", () => {
123 expect(recognize("deploy@github.com:owner/repo.git")).toEqual({
124 kind: "github",
125 name: "GitHub",
126 baseUrl: "https://github.com",
127 });
128 expect(recognize("https://acme.ghe.com/owner/repo.git")).toEqual({
129 kind: "github",
130 name: "GitHub Self-Hosted",
131 baseUrl: "https://acme.ghe.com",
132 });
133 expect(recognize("git@acme.ghe.com:owner/repo.git")?.kind).toBe("github");
134 expect(recognize("https://github.example.com:8443/owner/repo.git")?.baseUrl).toBe(
135 "https://github.example.com:8443",
136 );
137 });
138
139 it("does not claim ghe.com itself or a label that only contains github", () => {
140 expect(recognize("https://ghe.com/owner/repo.git")).toBeNull();
141 expect(recognize("https://notgithub.example.com/owner/repo.git")).toBeNull();
142 });
143
144 it("reads the host ssh connects to for SCP logins, but not Git's bracketed forms", () => {
145 for (const url of [
146 "alice+ci@github.com:owner/repo.git",
147 "alice@corp.example@github.com:owner/repo.git",
148 ]) {
149 expect(recognize(url)?.baseUrl, url).toBe("https://github.com");
150 }
151 for (const url of [
152 "@github.com:owner/repo.git",
153 "alice@[work]@github.com:owner/repo.git",
154 "[git@git.internal]@github.com:repo@[x",
155 "-oProxyCommand=x@github.com:owner/repo.git",
156 "git@github.com:-x",
157 ]) {
158 expect(recognize(url), url).toBeNull();
159 }
160 expect(parseGitRemoteHost("alice+ci@[2001:db8::1]:team/repo.git")).toBeNull();
161 });
162});
163
164describe("GitHub media URLs", () => {
165 const media = (url: string) => definition.mediaUrl(new URL(url));
166
167 it("fetches every spelling of an upload or committed file from one canonical URL", () => {
168 expect(
169 media("https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c"),
170 ).toEqual({
171 fetchUrl: "https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c",
172 fileName: "1a1842fb-6383-492f-873c-57aa0033fa6c",
173 kind: "unknown",
174 immutable: true,
175 });
176 expect(media("https://github.com/owner/repo/blob/main/docs/shot.png")).toEqual({
177 fetchUrl: "https://raw.githubusercontent.com/owner/repo/main/docs/shot.png",
178 fileName: "shot.png",
179 });
180 expect(media("https://github.com/owner/repo/assets/45952064/1a1842fb")?.fetchUrl).toBe(
181 "https://github.com/owner/repo/assets/45952064/1a1842fb",
182 );
183 expect(media("https://media.githubusercontent.com/media/owner/repo/main/a.mp4")).toEqual({
184 fetchUrl: "https://media.githubusercontent.com/media/owner/repo/main/a.mp4",
185 fileName: "a.mp4",
186 });
187 expect(media("https://raw.githubusercontent.com/o/r/main/100%.png")?.fileName).toBe("100%.png");
188 });
189
190 it("marks only uploads and files at a full commit SHA as immutable", () => {
191 const sha = "0123456789abcdef0123456789abcdef01234567";
192 for (const url of [
193 "https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c",
194 "https://github.com/owner/repo/assets/45952064/1a1842fb",
195 `https://github.com/owner/repo/blob/${sha}/docs/shot.png`,
196 `https://raw.githubusercontent.com/owner/repo/${sha}/docs/shot.png`,
197 ]) {
198 expect(media(url)?.immutable, url).toBe(true);
199 }
200 for (const url of [
201 "https://raw.githubusercontent.com/owner/repo/0123456789ab/docs/shot.png",
202 `https://media.githubusercontent.com/media/owner/repo/${sha}/a.mp4`,
203 ]) {
204 expect(media(url)?.immutable, url).toBeUndefined();
205 }
206 });
207
208 it("claims nothing a request without a credential loads just as well", () => {
209 for (const url of [
210 "https://example.com/shot.png",
211 "http://github.com/user-attachments/assets/1a1842fb",
212 "https://github.com/owner/repo/pull/1",
213 "https://github.com/owner/repo/blob/main/",
214 "https://objects.githubusercontent.com/github-production-user-asset/shot.png",
215 ]) {
216 expect(media(url), url).toBeNull();
217 }
218 });
219
220 it("addresses a committed file at a full SHA as immutable credentialed media", () => {
221 const sha = "0123456789abcdef0123456789abcdef01234567";
222 const rawFileUrl = (repositoryUrl: string, path: string, revision = sha) =>
223 definition.rawFileUrl?.({ repositoryUrl, sha: revision, path }) ?? null;
224 const url = rawFileUrl("https://github.com/owner/repo", "docs/a #1?.png");
225 expect(url).toBe(`https://github.com/owner/repo/raw/${sha}/docs/a%20%231%3F.png`);
226 expect(media(url ?? "")).toEqual({
227 fetchUrl: `https://raw.githubusercontent.com/owner/repo/${sha}/docs/a%20%231%3F.png`,
228 fileName: "a #1?.png",
229 immutable: true,
230 });
231 expect(rawFileUrl("https://github.com/owner/repo", "a.png", "main")).toBeNull();
232 expect(rawFileUrl("https://github.example.com/owner/repo", "a.png")).toBeNull();
233 });
234});