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

definition.test.ts

180 lines6.9 KB

1import { recognizeRemoteWith } from "@t3tools/source-control-core/shared/definition";
2import { describe, expect, it } from "vite-plus/test";
3​
4import { definition } from "./definition.ts";
5​
6const parse = (url: string) => definition.parseChangeRequestUrl(new URL(url));
7​
8describe("GitHub change request URLs", () => {
9 it("reads a pull request on any host, lower-casing host and repository", () => {
10 expect(parse("https://github.com/T3Tools/T3Code/pull/123/files?w=1#diff")).toEqual({
11 host: "github.com",
12 repository: "t3tools/t3code",
13 number: 123,
14 repositoryUrl: "https://github.com/T3Tools/T3Code",
15 });
16 expect(parse("https://GHE.Example.com:8443/acme/web/pull/42/")).toEqual({
17 host: "ghe.example.com",
18 repository: "acme/web",
19 number: 42,
20 repositoryUrl: "https://ghe.example.com:8443/acme/web",
21 });
22 });
23​
24 it("claims only the two-segment pull route", () => {
25 for (const url of [
26 "https://github.com/t3tools/t3code/issues/123",
27 "https://github.com/t3tools/t3code/pulls/123",
28 "https://github.com/t3tools/t3code/pull/abc",
29 "https://github.com/t3tools/t3code/pull/0",
30 "https://github.com/group/sub/repo/pull/1",
31 "ftp://github.com/t3tools/t3code/pull/1",
32 ]) {
33 expect(parse(url), url).toBeNull();
34 }
35 });
36​
37 it("builds pull request URLs below the repository, keeping an HTTP remote's origin", () => {
38 expect(
39 definition.changeRequestUrl({
40 host: "github.acme.test",
41 repository: "platform/api",
42 number: 7,
43 remoteUrl: "http://token@github.acme.test:8443/team/default.git",
44 }),
45 ).toBe("http://github.acme.test:8443/platform/api/pull/7");
46 for (const remoteUrl of [
47 "git@github.acme.test:team/default.git",
48 "git://github.acme.test/team/default.git",
49 "http://other.test/team/default.git",
50 ]) {
51 expect(
52 definition.changeRequestUrl({
53 host: "github.acme.test",
54 repository: "platform/api",
55 number: 9,
56 remoteUrl,
57 }),
58 remoteUrl,
59 ).toBe("https://github.acme.test/platform/api/pull/9");
60 }
61 expect(
62 definition.changeRequestUrl({
63 host: "github.com",
64 repository: "pull/1",
65 number: 43,
66 webUrl: "https://github.com/pull/1",
67 }),
68 ).toBe("https://github.com/pull/1/pull/43");
69 });
70​
71 // regression: #8507 the browser fallback must not open a bogus URL
72 it("builds nothing from an invalid repository, number, or host", () => {
73 for (const [host, repository, number] of [
74 ["github.com", "acme", 1],
75 ["github.com", "../repository", 1],
76 ["github.com", "acme/%2e%2e", 1],
77 ["github.com", "acme/repository?tab=1", 1],
78 ["github.com", "acme/repository", 0],
79 ["bad host", "acme/repository", 1],
80 ] as const) {
81 expect(definition.changeRequestUrl({ host, repository, number })).toBeNull();
82 }
83 });
84​
85 it("turns GitHub's shared issue route into a pull request candidate", () => {
86 const candidate = (url: string) =>
87 definition.changeRequestUrlFromReferenceAutolink(new URL(url));
88 expect(candidate("https://github.com/pingdotgg/t3code/issues/8600#issuecomment-1")).toBe(
89 "https://github.com/pingdotgg/t3code/pull/8600#issuecomment-1",
90 );
91 expect(candidate("https://code.github.example.com/acme/web/issues/42")).toBe(
92 "https://code.github.example.com/acme/web/pull/42",
93 );
94 expect(candidate("https://github.com/pingdotgg/t3code/issues/not-a-number")).toBeNull();
95 expect(candidate("https://code.example.test/acme/web/issues/42")).toBeNull();
96 });
97});
98​
99describe("GitHub remotes", () => {
100 const recognize = (url: string) => recognizeRemoteWith(definition, url);
101​
102 it("recognises github.com, ghe.com subdomains and hosts with a github label", () => {
103 expect(recognize("deploy@github.com:owner/repo.git")).toEqual({
104 kind: "github",
105 name: "GitHub",
106 baseUrl: "https://github.com",
107 });
108 expect(recognize("https://acme.ghe.com/owner/repo.git")).toEqual({
109 kind: "github",
110 name: "GitHub Self-Hosted",
111 baseUrl: "https://acme.ghe.com",
112 });
113 expect(recognize("git@acme.ghe.com:owner/repo.git")?.kind).toBe("github");
114 expect(recognize("https://github.example.com:8443/owner/repo.git")?.baseUrl).toBe(
115 "https://github.example.com:8443",
116 );
117 });
118​
119 it("does not claim ghe.com itself or a label that only contains github", () => {
120 expect(recognize("https://ghe.com/owner/repo.git")).toBeNull();
121 expect(recognize("https://notgithub.example.com/owner/repo.git")).toBeNull();
122 });
123});
124​
125describe("GitHub media URLs", () => {
126 const media = (url: string) => definition.mediaUrl(new URL(url));
127​
128 it("fetches every spelling of an upload or committed file from one canonical URL", () => {
129 expect(
130 media("https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c"),
131 ).toEqual({
132 fetchUrl: "https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c",
133 fileName: "1a1842fb-6383-492f-873c-57aa0033fa6c",
134 kind: "video",
135 immutable: true,
136 });
137 expect(media("https://github.com/owner/repo/blob/main/docs/shot.png")).toEqual({
138 fetchUrl: "https://raw.githubusercontent.com/owner/repo/main/docs/shot.png",
139 fileName: "shot.png",
140 });
141 expect(media("https://github.com/owner/repo/assets/45952064/1a1842fb")?.fetchUrl).toBe(
142 "https://github.com/owner/repo/assets/45952064/1a1842fb",
143 );
144 expect(media("https://media.githubusercontent.com/media/owner/repo/main/a.mp4")).toEqual({
145 fetchUrl: "https://media.githubusercontent.com/media/owner/repo/main/a.mp4",
146 fileName: "a.mp4",
147 });
148 expect(media("https://raw.githubusercontent.com/o/r/main/100%.png")?.fileName).toBe("100%.png");
149 });
150​
151 it("marks only uploads and files at a full commit SHA as immutable", () => {
152 const sha = "0123456789abcdef0123456789abcdef01234567";
153 for (const url of [
154 "https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c",
155 "https://github.com/owner/repo/assets/45952064/1a1842fb",
156 `https://github.com/owner/repo/blob/${sha}/docs/shot.png`,
157 `https://raw.githubusercontent.com/owner/repo/${sha}/docs/shot.png`,
158 ]) {
159 expect(media(url)?.immutable, url).toBe(true);
160 }
161 for (const url of [
162 "https://raw.githubusercontent.com/owner/repo/0123456789ab/docs/shot.png",
163 `https://media.githubusercontent.com/media/owner/repo/${sha}/a.mp4`,
164 ]) {
165 expect(media(url)?.immutable, url).toBeUndefined();
166 }
167 });
168​
169 it("claims nothing a request without a credential loads just as well", () => {
170 for (const url of [
171 "https://example.com/shot.png",
172 "http://github.com/user-attachments/assets/1a1842fb",
173 "https://github.com/owner/repo/pull/1",
174 "https://github.com/owner/repo/blob/main/",
175 "https://objects.githubusercontent.com/github-production-user-asset/shot.png",
176 ]) {
177 expect(media(url), url).toBeNull();
178 }
179 });
180});