|
| 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 HostProcess from "@t3tools/shared/HostProcess"; |
| 6 | +import { HttpClient, HttpClientResponse } from "effect/http"; |
| 7 | +import * as GitHubCredentials from "@t3tools/source-control-github/server/GitHubCredentials"; |
| 8 | +import { githubMediaResponse } from "./GitHubMediaFetch.ts"; |
| 9 | + |
| 10 | +const asset = (url: string) => ({ |
| 11 | + url, |
| 12 | + cwd: "/untrusted-checkout", |
| 13 | + expiresAt: Number.MAX_SAFE_INTEGER, |
| 14 | +}); |
| 15 | + |
| 16 | +describe("repository GitHub media credentials", () => { |
| 17 | + it.effect.each([true, false])( |
| 18 | + "keeps legacy fallback redirect behavior only with opt-in disabled: %s", |
| 19 | + (enabled) => { |
| 20 | + const authorizations: Array<string | undefined> = []; |
| 21 | + return Effect.gen(function* () { |
| 22 | + yield* githubMediaResponse( |
| 23 | + asset("https://raw.githubusercontent.com/OwnerA/Repo/main/image.png"), |
| 24 | + {}, |
| 25 | + ); |
| 26 | + expect(authorizations).toEqual([ |
| 27 | + "Bearer fake-fallback", |
| 28 | + enabled ? undefined : "Bearer fake-fallback", |
| 29 | + ]); |
| 30 | + }).pipe( |
| 31 | + Effect.provide( |
| 32 | + Layer.mock(GitHubCredentials.GitHubCredentials)({ |
| 33 | + get: (host, repository) => { |
| 34 | + expect(repository).toBe("OwnerA/Repo"); |
| 35 | + // A repository helper miss returns the legacy credential without helper provenance. |
| 36 | + return Effect.succeed({ |
| 37 | + host, |
| 38 | + source: "settings", |
| 39 | + fingerprint: "default", |
| 40 | + token: Redacted.make("fake-fallback"), |
| 41 | + }); |
| 42 | + }, |
| 43 | + }), |
| 44 | + ), |
| 45 | + Effect.provideService( |
| 46 | + HttpClient.HttpClient, |
| 47 | + HttpClient.make((request) => { |
| 48 | + authorizations.push(request.headers.authorization); |
| 49 | + return Effect.succeed( |
| 50 | + HttpClientResponse.fromWeb( |
| 51 | + request, |
| 52 | + new Response( |
| 53 | + null, |
| 54 | + authorizations.length === 1 |
| 55 | + ? { |
| 56 | + status: 302, |
| 57 | + headers: { |
| 58 | + location: "https://raw.githubusercontent.com/OwnerB/Repo/main/image.png", |
| 59 | + }, |
| 60 | + } |
| 61 | + : { status: 200, headers: { "content-type": "image/png" } }, |
| 62 | + ), |
| 63 | + ), |
| 64 | + ); |
| 65 | + }), |
| 66 | + ), |
| 67 | + Effect.provideService(HostProcess.Environment, { |
| 68 | + T3CODE_GITHUB_USE_GIT_CREDENTIALS: enabled ? "1" : "0", |
| 69 | + }), |
| 70 | + Effect.scoped, |
| 71 | + ); |
| 72 | + }, |
| 73 | + ); |
| 74 | + |
| 75 | + it.effect.each([401, 403])( |
| 76 | + "invalidates the host cache after opted-in fallback HTTP %s", |
| 77 | + (status) => { |
| 78 | + const invalidations: Array<readonly [string, string | undefined]> = []; |
| 79 | + let requests = 0; |
| 80 | + return Effect.gen(function* () { |
| 81 | + expect( |
| 82 | + (yield* githubMediaResponse( |
| 83 | + asset("https://raw.githubusercontent.com/OwnerA/Repo/main/image.png"), |
| 84 | + {}, |
| 85 | + )).status, |
| 86 | + ).toBe(status); |
| 87 | + expect(invalidations).toEqual([["github.com", undefined]]); |
| 88 | + expect(requests).toBe(1); |
| 89 | + }).pipe( |
| 90 | + Effect.provide( |
| 91 | + Layer.mock(GitHubCredentials.GitHubCredentials)({ |
| 92 | + get: (host) => |
| 93 | + Effect.succeed({ |
| 94 | + host, |
| 95 | + source: "gh", |
| 96 | + fingerprint: "default", |
| 97 | + token: Redacted.make("fake-fallback"), |
| 98 | + }), |
| 99 | + invalidate: (host, repository) => |
| 100 | + Effect.sync(() => { |
| 101 | + invalidations.push([host, repository]); |
| 102 | + }), |
| 103 | + }), |
| 104 | + ), |
| 105 | + Effect.provideService( |
| 106 | + HttpClient.HttpClient, |
| 107 | + HttpClient.make((request) => { |
| 108 | + requests++; |
| 109 | + return Effect.succeed( |
| 110 | + HttpClientResponse.fromWeb(request, new Response(null, { status })), |
| 111 | + ); |
| 112 | + }), |
| 113 | + ), |
| 114 | + Effect.provideService(HostProcess.Environment, { T3CODE_GITHUB_USE_GIT_CREDENTIALS: "1" }), |
| 115 | + Effect.scoped, |
| 116 | + ); |
| 117 | + }, |
| 118 | + ); |
| 119 | + |
| 120 | + it.effect( |
| 121 | + "uses raw URL repository context and strips a scoped token across repository redirects", |
| 122 | + () => { |
| 123 | + const scopes: Array<string | undefined> = []; |
| 124 | + const authorizations: Array<string | undefined> = []; |
| 125 | + return Effect.gen(function* () { |
| 126 | + const response = yield* githubMediaResponse( |
| 127 | + asset("https://raw.githubusercontent.com/OwnerA/Repo/main/image.png"), |
| 128 | + {}, |
| 129 | + ); |
| 130 | + expect(response.status).toBe(200); |
| 131 | + expect(scopes).toEqual(["OwnerA/Repo"]); |
| 132 | + expect(authorizations).toEqual(["Bearer fake-owner-a", undefined]); |
| 133 | + }).pipe( |
| 134 | + Effect.provide( |
| 135 | + Layer.mock(GitHubCredentials.GitHubCredentials)({ |
| 136 | + get: (host, repository) => { |
| 137 | + scopes.push(repository); |
| 138 | + return Effect.succeed({ |
| 139 | + host, |
| 140 | + repository: "ownera/repo", |
| 141 | + source: "git", |
| 142 | + fingerprint: "fake-a", |
| 143 | + token: Redacted.make("fake-owner-a"), |
| 144 | + }); |
| 145 | + }, |
| 146 | + }), |
| 147 | + ), |
| 148 | + Effect.provideService( |
| 149 | + HttpClient.HttpClient, |
| 150 | + HttpClient.make((request) => { |
| 151 | + authorizations.push(request.headers.authorization); |
| 152 | + return Effect.succeed( |
| 153 | + HttpClientResponse.fromWeb( |
| 154 | + request, |
| 155 | + new Response( |
| 156 | + null, |
| 157 | + authorizations.length === 1 |
| 158 | + ? { |
| 159 | + status: 302, |
| 160 | + headers: { |
| 161 | + location: "https://raw.githubusercontent.com/OwnerB/Repo/main/image.png", |
| 162 | + }, |
| 163 | + } |
| 164 | + : { status: 200, headers: { "content-type": "image/png" } }, |
| 165 | + ), |
| 166 | + ), |
| 167 | + ); |
| 168 | + }), |
| 169 | + ), |
| 170 | + Effect.scoped, |
| 171 | + ); |
| 172 | + }, |
| 173 | + ); |
| 174 | + |
| 175 | + it.effect("keeps attachments without repository context on the host default", () => { |
| 176 | + const scopes: Array<string | undefined> = []; |
| 177 | + return Effect.gen(function* () { |
| 178 | + yield* githubMediaResponse(asset("https://github.com/user-attachments/assets/fake-id"), {}); |
| 179 | + expect(scopes).toEqual([undefined]); |
| 180 | + }).pipe( |
| 181 | + Effect.provide( |
| 182 | + Layer.mock(GitHubCredentials.GitHubCredentials)({ |
| 183 | + get: (host, repository) => { |
| 184 | + scopes.push(repository); |
| 185 | + return Effect.succeed({ |
| 186 | + host, |
| 187 | + source: "settings", |
| 188 | + fingerprint: "default", |
| 189 | + token: Redacted.make("fake-default"), |
| 190 | + }); |
| 191 | + }, |
| 192 | + }), |
| 193 | + ), |
| 194 | + Effect.provideService( |
| 195 | + HttpClient.HttpClient, |
| 196 | + HttpClient.make((request) => |
| 197 | + Effect.succeed(HttpClientResponse.fromWeb(request, new Response(null, { status: 404 }))), |
| 198 | + ), |
| 199 | + ), |
| 200 | + Effect.scoped, |
| 201 | + ); |
| 202 | + }); |
| 203 | + |
| 204 | + it.effect("reports helper failures without a host fallback or HTTP request", () => |
| 205 | + Effect.gen(function* () { |
| 206 | + const error = yield* githubMediaResponse( |
| 207 | + asset("https://raw.githubusercontent.com/OwnerA/Repo/main/image.png"), |
| 208 | + {}, |
| 209 | + ).pipe(Effect.flip); |
| 210 | + expect(error._tag).toBe("GitHubGitCredentialError"); |
| 211 | + }).pipe( |
| 212 | + Effect.provide( |
| 213 | + Layer.mock(GitHubCredentials.GitHubCredentials)({ |
| 214 | + get: () => |
| 215 | + Effect.fail( |
| 216 | + new GitHubCredentials.GitHubGitCredentialError({ |
| 217 | + host: "github.com", |
| 218 | + reason: "process", |
| 219 | + }), |
| 220 | + ), |
| 221 | + }), |
| 222 | + ), |
| 223 | + Effect.provideService( |
| 224 | + HttpClient.HttpClient, |
| 225 | + HttpClient.make(() => Effect.die("Must not fetch after helper failure")), |
| 226 | + ), |
| 227 | + Effect.scoped, |
| 228 | + ), |
| 229 | + ); |
| 230 | +}); |
0 commit comments