upstream.test.ts
This file
Path
packages/git/test/upstream.test.ts
275 lines · 7.9 KiB · TypeScript
Last changed by
Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276import { describe, expect, it } from "@effect/vitest";import { Fetch, FetchFailed } from "@repo/artifacts/fetch";import { RepoHost, RepoName, RepoNotFound } from "@repo/artifacts/repo-host";import type { TokenScope } from "@repo/artifacts/repo-host";import { RepoHostMemory } from "@repo/artifacts/repo-host/memory";import { DateTime, Duration, Effect, Layer, Redacted, Ref, Stream,} from "effect";import { TestClock } from "effect/testing";
import { Exchange, Upstream, UpstreamFailure } from "../src/upstream.ts";import { UpstreamRepoHost } from "../src/upstream/repo-host.ts";
const repo = RepoName.make("demo");
interface Minted { readonly scope: TokenScope; readonly secret: string; readonly expiresAt: DateTime.Utc;}
interface Seen { readonly requests: Ref.Ref<readonly Request[]>; readonly minted: Ref.Ref<readonly Minted[]>;}
const store = (minted: Seen["minted"]) => Layer.effect( RepoHost, Effect.gen(function* () { const host = yield* RepoHost;
return RepoHost.of({ ...host, mintToken: (name, scope, ttl) => Effect.gen(function* () { const token = yield* host.mintToken(name, scope, ttl); const secret = Redacted.value(token.secret);
yield* Ref.update(minted, (all) => [ ...all, { expiresAt: token.expiresAt, scope, secret }, ]);
return { ...token, secret: Redacted.make( `${secret}?expires=${DateTime.toEpochMillis(token.expiresAt) / 1000}` ), }; }), }); }) ).pipe(Layer.provide(RepoHostMemory));
const recording = (requests: Seen["requests"]) => Layer.succeed( Fetch, Fetch.of({ fetch: (request) => Ref.update(requests, (all) => [...all, request]).pipe( Effect.as(new Response("upstream")) ), }) );
const world = (seen: Seen) => UpstreamRepoHost.pipe( Layer.provideMerge( Layer.mergeAll(store(seen.minted), recording(seen.requests)) ) );
const advertise = Exchange.Advertise({ headers: new Headers({ "git-protocol": "version=2" }), service: "git-upload-pack",});
const withUpstream = <A, E>( body: (seen: Seen) => Effect.Effect<A, E, Upstream | RepoHost>) => Effect.gen(function* () { const seen: Seen = { minted: yield* Ref.make<readonly Minted[]>([]), requests: yield* Ref.make<readonly Request[]>([]), };
return yield* Effect.gen(function* () { yield* RepoHost.use((host) => host.create(repo)).pipe(Effect.orDie);
return yield* body(seen); }).pipe(Effect.provide(world(seen))); });
const basic = (secret: string) => `Basic ${btoa(`x:${secret}`)}`;
const sent = (request: Request) => ({ authorization: request.headers.get("authorization"), method: request.method, protocol: request.headers.get("git-protocol"), url: request.url,});
describe("the upstream over RepoHost", () => { it.effect( "advertises refs at the repo's remote with a minted read token", () => withUpstream((seen) => Effect.gen(function* () { const upstream = yield* Upstream; const response = yield* upstream.exchange(repo, advertise); const remote = yield* RepoHost.use((host) => host.remote(repo)); const minted = yield* Ref.get(seen.minted);
expect(yield* Effect.promise(() => response.text())).toBe("upstream"); expect({ requests: (yield* Ref.get(seen.requests)).map(sent), scopes: minted.map(({ scope }) => scope), }).toStrictEqual({ requests: [ { authorization: basic(minted[0]?.secret ?? ""), method: "GET", protocol: "version=2", url: `${remote}/info/refs?service=git-upload-pack`, }, ], scopes: ["read"], }); }) ) );
it.effect("posts a receive-pack call with a write token", () => withUpstream((seen) => Effect.gen(function* () { const upstream = yield* Upstream; const remote = yield* RepoHost.use((host) => host.remote(repo));
yield* upstream.exchange( repo, Exchange.Call({ body: Stream.toReadableStream( Stream.make(new TextEncoder().encode("0000")) ), headers: new Headers({ "content-type": "application/x-git-receive-pack-request", }), service: "git-receive-pack", }) );
const [request] = yield* Ref.get(seen.requests); const minted = yield* Ref.get(seen.minted);
expect({ request: request === undefined ? undefined : sent(request), scopes: minted.map(({ scope }) => scope), }).toStrictEqual({ request: { authorization: basic(minted[0]?.secret ?? ""), method: "POST", protocol: null, url: `${remote}/git-receive-pack`, }, scopes: ["write"], }); expect( yield* Effect.promise(() => request?.text() ?? Promise.resolve("")) ).toBe("0000"); }) ) );
it.effect( "mints once while the token lasts, and again a minute before it expires", () => withUpstream((seen) => Effect.gen(function* () { const upstream = yield* Upstream;
yield* upstream.exchange(repo, advertise); yield* upstream.exchange(repo, advertise);
const first = yield* Ref.get(seen.minted);
expect(first).toHaveLength(1);
const lasts = Duration.subtract( DateTime.distance( yield* DateTime.now, first[0]?.expiresAt ?? DateTime.makeUnsafe(0) ), Duration.seconds(60) );
yield* TestClock.adjust( Duration.subtract(lasts, Duration.seconds(1)) ); yield* upstream.exchange(repo, advertise);
expect(yield* Ref.get(seen.minted)).toHaveLength(1);
yield* TestClock.adjust(Duration.seconds(1)); yield* upstream.exchange(repo, advertise);
const minted = yield* Ref.get(seen.minted); const requests = yield* Ref.get(seen.requests);
expect(minted).toHaveLength(2); expect(requests.at(-1)?.headers.get("authorization")).toBe( basic(minted[1]?.secret ?? "") ); }) ) );
it.effect("calls a request that got no response an UpstreamFailure", () => Effect.gen(function* () { const unreachable = Layer.succeed( Fetch, Fetch.of({ fetch: (request) => Effect.fail( new FetchFailed({ reason: "connection reset", url: request.url }) ), }) );
const minted = yield* Ref.make<readonly Minted[]>([]);
const failure = yield* Effect.gen(function* () { yield* RepoHost.use((host) => host.create(repo)).pipe(Effect.orDie);
return yield* Upstream.use((upstream) => upstream.exchange(repo, advertise) ).pipe(Effect.flip); }).pipe( Effect.provide( UpstreamRepoHost.pipe( Layer.provideMerge(Layer.mergeAll(store(minted), unreachable)) ) ) );
expect(failure).toStrictEqual( new UpstreamFailure({ reason: "connection reset", repo }) ); }) );
it.effect("passes RepoNotFound through for a repo the store lacks", () => withUpstream(() => Effect.gen(function* () { const upstream = yield* Upstream; const missing = RepoName.make("missing");
expect( yield* upstream.exchange(missing, advertise).pipe( Effect.match({ onFailure: (error) => error, onSuccess: () => "forwarded", }) ) ).toStrictEqual(new RepoNotFound({ name: missing })); }) ) );});