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 { describe, expect, it } from "@effect/vitest"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as Redacted from "effect/Redacted"; |
| 5 | import * as TestClock from "effect/testing/TestClock"; |
| 6 | import { HttpClient, HttpClientResponse } from "effect/http"; |
| 7 | |
| 8 | import * as GitHubCredentials from "./GitHubCredentials.ts"; |
| 9 | import * as GitHubMediaFetch from "./gitHubMediaFetch.ts"; |
| 10 | import * as GitHubRest from "./GitHubRest.ts"; |
| 11 | |
| 12 | const PR_REPOSITORY = "enterprise/private"; |
| 13 | |
| 14 | /** An SSO organization: the upload URL answers with a sign-in page, a rendered signed URL with bytes. */ |
| 15 | const makeSsoFixture = (uploadUrl: string) => { |
| 16 | const state = { account: "account-a", expired: new Set<string>() }; |
| 17 | const rendered: Array<GitHubRest.GitHubRestInput> = []; |
| 18 | const signedUrls: Array<string> = []; |
| 19 | const requests: Array<{ url: string; authorization?: string; range?: string }> = []; |
| 20 | const credential = (host: string) => |
| 21 | Effect.sync(() => ({ |
| 22 | host, |
| 23 | token: Redacted.make(state.account), |
| 24 | fingerprint: state.account, |
| 25 | source: "gh" as const, |
| 26 | })); |
| 27 | const layer = Layer.mergeAll( |
| 28 | Layer.mock(GitHubCredentials.GitHubCredentials)({ get: credential }), |
| 29 | Layer.mock(GitHubRest.GitHubRest)({ |
| 30 | rest: (input) => |
| 31 | Effect.sync(() => { |
| 32 | rendered.push(input); |
| 33 | const signed = `https://private-user-images.githubusercontent.com/1/upload.mp4?jwt=${state.account}-${rendered.length}&x=1`; |
| 34 | const inRepository = |
| 35 | (input.body as { readonly context?: string } | undefined)?.context === PR_REPOSITORY; |
| 36 | if (inRepository) signedUrls.push(signed); |
| 37 | return { |
| 38 | status: 200, |
| 39 | headers: {}, |
| 40 | body: `<p><img src="${inRepository ? signed.replaceAll("&", "&") : uploadUrl}" alt=""></p>`, |
| 41 | truncated: false, |
| 42 | invalidUtf8: false, |
| 43 | }; |
| 44 | }), |
| 45 | }), |
| 46 | Layer.succeed( |
| 47 | HttpClient.HttpClient, |
| 48 | HttpClient.make((request) => |
| 49 | Effect.sync(() => { |
| 50 | requests.push({ |
| 51 | url: request.url, |
| 52 | ...(request.headers.authorization |
| 53 | ? { authorization: request.headers.authorization } |
| 54 | : {}), |
| 55 | ...(request.headers.range ? { range: request.headers.range } : {}), |
| 56 | }); |
| 57 | if (request.url === uploadUrl) { |
| 58 | return HttpClientResponse.fromWeb( |
| 59 | request, |
| 60 | new Response("<html>Sign in with SSO</html>", { |
| 61 | headers: { "content-type": "text/html; charset=utf-8" }, |
| 62 | }), |
| 63 | ); |
| 64 | } |
| 65 | expect(signedUrls).toContain(request.url); |
| 66 | const status = state.expired.has(request.url) ? 403 : request.headers.range ? 206 : 200; |
| 67 | return HttpClientResponse.fromWeb( |
| 68 | request, |
| 69 | new Response(null, { status, headers: { "content-type": "video/mp4" } }), |
| 70 | ); |
| 71 | }), |
| 72 | ), |
| 73 | ), |
| 74 | ); |
| 75 | return { layer, state, rendered, signedUrls, requests }; |
| 76 | }; |
| 77 | |
| 78 | describe("GitHubMediaFetch", () => { |
| 79 | it.effect("loads private media immediately after login with the GitHub credential", () => { |
| 80 | let lookups = 0; |
| 81 | const authorizations: Array<string | undefined> = []; |
| 82 | return Effect.gen(function* () { |
| 83 | const fetchMedia = yield* GitHubMediaFetch.make; |
| 84 | const request = { |
| 85 | url: "https://raw.githubusercontent.com/owner/repo/main/shot.png", |
| 86 | headers: {}, |
| 87 | }; |
| 88 | expect((yield* fetchMedia(request))?.status).toBe(404); |
| 89 | expect((yield* fetchMedia(request))?.status).toBe(200); |
| 90 | expect((yield* fetchMedia(request))?.status).toBe(200); |
| 91 | expect(lookups).toBe(3); |
| 92 | expect(authorizations).toEqual([undefined, "Bearer signed-in", "Bearer signed-in"]); |
| 93 | }).pipe( |
| 94 | Effect.provide( |
| 95 | Layer.merge( |
| 96 | Layer.mock(GitHubRest.GitHubRest)({}), |
| 97 | Layer.mock(GitHubCredentials.GitHubCredentials)({ |
| 98 | get: (host) => |
| 99 | ++lookups === 1 |
| 100 | ? Effect.fail(new GitHubCredentials.GitHubNotSignedInError({ host })) |
| 101 | : Effect.succeed({ |
| 102 | host, |
| 103 | token: Redacted.make("signed-in"), |
| 104 | source: "gh" as const, |
| 105 | fingerprint: "fingerprint", |
| 106 | }), |
| 107 | }), |
| 108 | ), |
| 109 | ), |
| 110 | Effect.provideService( |
| 111 | HttpClient.HttpClient, |
| 112 | HttpClient.make((request) => { |
| 113 | authorizations.push(request.headers.authorization); |
| 114 | return Effect.succeed( |
| 115 | HttpClientResponse.fromWeb( |
| 116 | request, |
| 117 | new Response(null, { |
| 118 | status: request.headers.authorization ? 200 : 404, |
| 119 | headers: { "content-type": "image/png" }, |
| 120 | }), |
| 121 | ), |
| 122 | ); |
| 123 | }), |
| 124 | ), |
| 125 | ); |
| 126 | }); |
| 127 | |
| 128 | it.effect("serves a Git LFS file's object, not the pointer a raw read returns", () => { |
| 129 | const object = "\x89PNG real object bytes"; |
| 130 | const oid = "5a8f16132efe3eb67a8dea335571beac2745147010dabcedde78dbe708e6bccd"; |
| 131 | const pointer = `version https://git-lfs.github.com/spec/v1\noid sha256:${oid}\nsize ${object.length}\n`; |
| 132 | return Effect.gen(function* () { |
| 133 | const fetchMedia = yield* GitHubMediaFetch.make; |
| 134 | const response = yield* fetchMedia({ |
| 135 | url: "https://raw.githubusercontent.com/owner/repo/HEAD/art/background.png", |
| 136 | headers: {}, |
| 137 | }); |
| 138 | expect(response?.status).toBe(200); |
| 139 | expect(yield* response!.text).toBe(object); |
| 140 | }).pipe( |
| 141 | Effect.provide( |
| 142 | Layer.merge( |
| 143 | Layer.mock(GitHubRest.GitHubRest)({}), |
| 144 | Layer.mock(GitHubCredentials.GitHubCredentials)({ |
| 145 | get: (host) => |
| 146 | Effect.succeed({ |
| 147 | host, |
| 148 | token: Redacted.make("signed-in"), |
| 149 | source: "gh" as const, |
| 150 | fingerprint: "fingerprint", |
| 151 | }), |
| 152 | }), |
| 153 | ), |
| 154 | ), |
| 155 | // GitHub's two routes to the object: the LFS media host and the Git LFS batch API. |
| 156 | Effect.provideService( |
| 157 | HttpClient.HttpClient, |
| 158 | HttpClient.make((request) => { |
| 159 | const url = new URL(request.url); |
| 160 | const respond = (body: string | null, init: ResponseInit) => |
| 161 | Effect.succeed(HttpClientResponse.fromWeb(request, new Response(body, init))); |
| 162 | if (url.hostname === "raw.githubusercontent.com") { |
| 163 | return respond(pointer, { headers: { "content-type": "text/plain; charset=utf-8" } }); |
| 164 | } |
| 165 | if ( |
| 166 | url.hostname === "media.githubusercontent.com" || |
| 167 | url.hostname === "lfs.example.com" |
| 168 | ) { |
| 169 | return respond(object, { headers: { "content-type": "image/png" } }); |
| 170 | } |
| 171 | if (url.pathname.endsWith(".git/info/lfs/objects/batch")) { |
| 172 | return respond( |
| 173 | JSON.stringify({ |
| 174 | objects: [ |
| 175 | { |
| 176 | oid, |
| 177 | size: object.length, |
| 178 | actions: { download: { href: `https://lfs.example.com/${oid}` } }, |
| 179 | }, |
| 180 | ], |
| 181 | }), |
| 182 | { headers: { "content-type": "application/vnd.git-lfs+json" } }, |
| 183 | ); |
| 184 | } |
| 185 | return respond(null, { status: 404 }); |
| 186 | }), |
| 187 | ), |
| 188 | Effect.scoped, |
| 189 | ); |
| 190 | }); |
| 191 | |
| 192 | it.effect("keeps the credential off the object store a redirect hands the media to", () => { |
| 193 | const authorizations: Array<[string, string | undefined]> = []; |
| 194 | return Effect.gen(function* () { |
| 195 | const fetchMedia = yield* GitHubMediaFetch.make; |
| 196 | const response = yield* fetchMedia({ |
| 197 | url: "https://github.com/user-attachments/assets/1a1842fb", |
| 198 | headers: { range: "bytes=0-1" }, |
| 199 | }); |
| 200 | expect(response?.status).toBe(200); |
| 201 | expect(authorizations).toEqual([ |
| 202 | ["github.com", "Bearer signed-in"], |
| 203 | ["objects.example.com", undefined], |
| 204 | ]); |
| 205 | }).pipe( |
| 206 | Effect.provide( |
| 207 | Layer.merge( |
| 208 | Layer.mock(GitHubRest.GitHubRest)({}), |
| 209 | Layer.mock(GitHubCredentials.GitHubCredentials)({ |
| 210 | get: (host) => |
| 211 | Effect.succeed({ |
| 212 | host, |
| 213 | token: Redacted.make("signed-in"), |
| 214 | source: "gh" as const, |
| 215 | fingerprint: "fingerprint", |
| 216 | }), |
| 217 | }), |
| 218 | ), |
| 219 | ), |
| 220 | Effect.provideService( |
| 221 | HttpClient.HttpClient, |
| 222 | HttpClient.make((request) => { |
| 223 | const url = new URL(request.url); |
| 224 | authorizations.push([url.hostname, request.headers.authorization]); |
| 225 | return Effect.succeed( |
| 226 | HttpClientResponse.fromWeb( |
| 227 | request, |
| 228 | url.hostname === "github.com" |
| 229 | ? new Response(null, { |
| 230 | status: 302, |
| 231 | headers: { location: "https://objects.example.com/signed" }, |
| 232 | }) |
| 233 | : new Response(null, { status: 200 }), |
| 234 | ), |
| 235 | ); |
| 236 | }), |
| 237 | ), |
| 238 | ); |
| 239 | }); |
| 240 | |
| 241 | it.effect( |
| 242 | "renders SSO uploads in the PR repository and reuses the signed URL for a range", |
| 243 | () => { |
| 244 | const url = "https://github.com/user-attachments/assets/explicit-pr-context"; |
| 245 | const fixture = makeSsoFixture(url); |
| 246 | return Effect.gen(function* () { |
| 247 | const fetchMedia = yield* GitHubMediaFetch.make; |
| 248 | const request = { url, headers: {}, repository: PR_REPOSITORY }; |
| 249 | expect((yield* fetchMedia(request))?.status).toBe(200); |
| 250 | expect((yield* fetchMedia({ ...request, headers: { range: "bytes=2-5" } }))?.status).toBe( |
| 251 | 206, |
| 252 | ); |
| 253 | expect(fixture.rendered).toHaveLength(1); |
| 254 | expect(fixture.rendered[0]).toMatchObject({ |
| 255 | host: "github.com", |
| 256 | method: "POST", |
| 257 | path: "markdown", |
| 258 | accept: "text/html", |
| 259 | body: { text: ``, mode: "gfm", context: PR_REPOSITORY }, |
| 260 | }); |
| 261 | expect(fixture.requests).toEqual([ |
| 262 | { url, authorization: "Bearer account-a" }, |
| 263 | { url: fixture.signedUrls[0] }, |
| 264 | { url: fixture.signedUrls[0], range: "bytes=2-5" }, |
| 265 | ]); |
| 266 | }).pipe(Effect.provide(fixture.layer)); |
| 267 | }, |
| 268 | ); |
| 269 | |
| 270 | it.effect("renders a legacy upload in the repository its path names", () => { |
| 271 | const url = `https://github.com/${PR_REPOSITORY}/assets/123/legacy`; |
| 272 | const fixture = makeSsoFixture(url); |
| 273 | return Effect.gen(function* () { |
| 274 | const fetchMedia = yield* GitHubMediaFetch.make; |
| 275 | const response = yield* fetchMedia({ url, headers: {}, repository: "personal/unrelated" }); |
| 276 | expect(response?.status).toBe(200); |
| 277 | expect(fixture.rendered.map((input) => input.body)).toEqual([ |
| 278 | { text: ``, mode: "gfm", context: PR_REPOSITORY }, |
| 279 | ]); |
| 280 | }).pipe(Effect.provide(fixture.layer)); |
| 281 | }); |
| 282 | |
| 283 | it.effect("renders a fresh signed URL after an account switch, early expiry, or its TTL", () => { |
| 284 | const url = "https://github.com/user-attachments/assets/cache-lifecycle"; |
| 285 | const fixture = makeSsoFixture(url); |
| 286 | const request = { url, headers: {}, repository: PR_REPOSITORY }; |
| 287 | return Effect.gen(function* () { |
| 288 | const fetchMedia = yield* GitHubMediaFetch.make; |
| 289 | yield* fetchMedia(request); |
| 290 | fixture.state.account = "account-b"; |
| 291 | yield* fetchMedia(request); |
| 292 | expect(fixture.rendered).toHaveLength(2); |
| 293 | |
| 294 | fixture.state.expired.add(fixture.signedUrls.at(-1)!); |
| 295 | expect((yield* fetchMedia(request))?.status).toBe(200); |
| 296 | expect(fixture.rendered).toHaveLength(3); |
| 297 | |
| 298 | yield* TestClock.adjust("6 minutes"); |
| 299 | yield* fetchMedia(request); |
| 300 | expect(fixture.rendered).toHaveLength(4); |
| 301 | expect(fixture.requests.at(-1)?.url).toBe(fixture.signedUrls.at(-1)); |
| 302 | }).pipe(Effect.provide(fixture.layer)); |
| 303 | }); |
| 304 | }); |