The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { recognizeRemoteWith } from "@t3tools/source-control-core/shared/definition"; |
| 2 | import { describe, expect, it } from "vite-plus/test"; |
| 3 | |
| 4 | import { definition } from "./definition.ts"; |
| 5 | |
| 6 | const parse = (url: string) => definition.parseChangeRequestUrl(new URL(url)); |
| 7 | |
| 8 | describe("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 | |
| 99 | describe("GitHub CLI commands", () => { |
| 100 | it("recognizes shell pull request creates, including gh's `new` alias", () => { |
| 101 | for (const command of ["gh pr create --fill", "gh pr new --fill"]) { |
| 102 | expect(definition.changeRequestCreateCommand.test(command), command).toBe(true); |
| 103 | } |
| 104 | expect(definition.changeRequestCreateCommand.test("gh pr view 7")).toBe(false); |
| 105 | }); |
| 106 | }); |
| 107 | |
| 108 | describe("GitHub repository pages", () => { |
| 109 | it("leaves the tree route on other hosts to them", () => { |
| 110 | expect( |
| 111 | definition.repositoryUrlFromPage?.(new URL("https://scm.example.test/o/r/tree/main")), |
| 112 | ).toBeNull(); |
| 113 | }); |
| 114 | }); |
| 115 | |
| 116 | describe("GitHub remotes", () => { |
| 117 | const recognize = (url: string) => recognizeRemoteWith(definition, url); |
| 118 | |
| 119 | it("recognises github.com, ghe.com subdomains and hosts with a github label", () => { |
| 120 | expect(recognize("deploy@github.com:owner/repo.git")).toEqual({ |
| 121 | kind: "github", |
| 122 | name: "GitHub", |
| 123 | baseUrl: "https://github.com", |
| 124 | }); |
| 125 | expect(recognize("https://acme.ghe.com/owner/repo.git")).toEqual({ |
| 126 | kind: "github", |
| 127 | name: "GitHub Self-Hosted", |
| 128 | baseUrl: "https://acme.ghe.com", |
| 129 | }); |
| 130 | expect(recognize("git@acme.ghe.com:owner/repo.git")?.kind).toBe("github"); |
| 131 | expect(recognize("https://github.example.com:8443/owner/repo.git")?.baseUrl).toBe( |
| 132 | "https://github.example.com:8443", |
| 133 | ); |
| 134 | }); |
| 135 | |
| 136 | it("does not claim ghe.com itself or a label that only contains github", () => { |
| 137 | expect(recognize("https://ghe.com/owner/repo.git")).toBeNull(); |
| 138 | expect(recognize("https://notgithub.example.com/owner/repo.git")).toBeNull(); |
| 139 | }); |
| 140 | }); |
| 141 | |
| 142 | describe("GitHub media URLs", () => { |
| 143 | const media = (url: string) => definition.mediaUrl(new URL(url)); |
| 144 | |
| 145 | it("fetches every spelling of an upload or committed file from one canonical URL", () => { |
| 146 | expect( |
| 147 | media("https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c"), |
| 148 | ).toEqual({ |
| 149 | fetchUrl: "https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c", |
| 150 | fileName: "1a1842fb-6383-492f-873c-57aa0033fa6c", |
| 151 | kind: "video", |
| 152 | immutable: true, |
| 153 | }); |
| 154 | expect(media("https://github.com/owner/repo/blob/main/docs/shot.png")).toEqual({ |
| 155 | fetchUrl: "https://raw.githubusercontent.com/owner/repo/main/docs/shot.png", |
| 156 | fileName: "shot.png", |
| 157 | }); |
| 158 | expect(media("https://github.com/owner/repo/assets/45952064/1a1842fb")?.fetchUrl).toBe( |
| 159 | "https://github.com/owner/repo/assets/45952064/1a1842fb", |
| 160 | ); |
| 161 | expect(media("https://media.githubusercontent.com/media/owner/repo/main/a.mp4")).toEqual({ |
| 162 | fetchUrl: "https://media.githubusercontent.com/media/owner/repo/main/a.mp4", |
| 163 | fileName: "a.mp4", |
| 164 | }); |
| 165 | expect(media("https://raw.githubusercontent.com/o/r/main/100%.png")?.fileName).toBe("100%.png"); |
| 166 | }); |
| 167 | |
| 168 | it("marks only uploads and files at a full commit SHA as immutable", () => { |
| 169 | const sha = "0123456789abcdef0123456789abcdef01234567"; |
| 170 | for (const url of [ |
| 171 | "https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c", |
| 172 | "https://github.com/owner/repo/assets/45952064/1a1842fb", |
| 173 | `https://github.com/owner/repo/blob/${sha}/docs/shot.png`, |
| 174 | `https://raw.githubusercontent.com/owner/repo/${sha}/docs/shot.png`, |
| 175 | ]) { |
| 176 | expect(media(url)?.immutable, url).toBe(true); |
| 177 | } |
| 178 | for (const url of [ |
| 179 | "https://raw.githubusercontent.com/owner/repo/0123456789ab/docs/shot.png", |
| 180 | `https://media.githubusercontent.com/media/owner/repo/${sha}/a.mp4`, |
| 181 | ]) { |
| 182 | expect(media(url)?.immutable, url).toBeUndefined(); |
| 183 | } |
| 184 | }); |
| 185 | |
| 186 | it("claims nothing a request without a credential loads just as well", () => { |
| 187 | for (const url of [ |
| 188 | "https://example.com/shot.png", |
| 189 | "http://github.com/user-attachments/assets/1a1842fb", |
| 190 | "https://github.com/owner/repo/pull/1", |
| 191 | "https://github.com/owner/repo/blob/main/", |
| 192 | "https://objects.githubusercontent.com/github-production-user-asset/shot.png", |
| 193 | ]) { |
| 194 | expect(media(url), url).toBeNull(); |
| 195 | } |
| 196 | }); |
| 197 | |
| 198 | it("addresses a committed file at a full SHA as immutable credentialed media", () => { |
| 199 | const sha = "0123456789abcdef0123456789abcdef01234567"; |
| 200 | const rawFileUrl = (repositoryUrl: string, path: string, revision = sha) => |
| 201 | definition.rawFileUrl?.({ repositoryUrl, sha: revision, path }) ?? null; |
| 202 | const url = rawFileUrl("https://github.com/owner/repo", "docs/a #1?.png"); |
| 203 | expect(url).toBe(`https://github.com/owner/repo/raw/${sha}/docs/a%20%231%3F.png`); |
| 204 | expect(media(url ?? "")).toEqual({ |
| 205 | fetchUrl: `https://raw.githubusercontent.com/owner/repo/${sha}/docs/a%20%231%3F.png`, |
| 206 | fileName: "a #1?.png", |
| 207 | immutable: true, |
| 208 | }); |
| 209 | expect(rawFileUrl("https://github.com/owner/repo", "a.png", "main")).toBeNull(); |
| 210 | expect(rawFileUrl("https://github.example.com/owner/repo", "a.png")).toBeNull(); |
| 211 | }); |
| 212 | }); |