forward.test.ts
This file
Path
apps/web/test/forward.test.ts
142 lines · 4.1 KiB · TypeScript
Last changed by
Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143import { describe, expect, it } from "@effect/vitest";import { Brand } from "@repo/brand";import { untouchable } from "@repo/testkit/untouchable";import { Effect } from "effect";
import { forwarded } from "../src/features/agents/forward.ts";import { Route as skills } from "../src/routes/[.]well-known/agent-skills/$.ts";import { Route as installScript } from "../src/routes/install[.]sh.ts";import { Route as releases } from "../src/routes/releases/$.ts";import { backend } from "../src/server/backend.ts";
const PLATFORM_URL = "https://platform-test.example.workers.dev";
const WEB_URL = "https://web-test.example.workers.dev";
const platform = (answer: (request: Request) => Response) => { const handed: Request[] = [];
const env: Env = { PLATFORM: untouchable<Fetcher>("PLATFORM", { fetch: (input, init) => { const request = new Request(input, init);
handed.push(request);
return Promise.resolve(answer(request)); }, }), PUBLIC_PLATFORM_URL: PLATFORM_URL, };
return { env, handed };};
const served = (answer: (request: Request) => Response) => { const binding = platform(answer);
return { ...binding, handlers: forwarded(backend, binding.env) };};
const PATHS = [ "/.well-known/agent-skills/index.json", "/.well-known/agent-skills/datekit-contribute/SKILL.md", "/install.sh", "/releases/manifest.json", `/releases/0.1.0/${Brand.slug}-linux-x64`, "/releases/0.1.0/SHA256SUMS",] as const;
describe("a path the platform serves, reached on the web host", () => { it.effect.each(PATHS)( "%s goes to PLATFORM at the platform's URL, by GET and by HEAD", (path) => Effect.gen(function* () { const { handed, handlers } = served(() => new Response("served"));
for (const method of ["GET", "HEAD"] as const) { yield* Effect.promise(() => handlers[method]({ request: new Request(`${WEB_URL}${path}?v=1`, { headers: { "if-none-match": '"abc"' }, method, }), }) ); }
expect( handed.map((request) => ({ ifNoneMatch: request.headers.get("if-none-match"), method: request.method, url: request.url, })) ).toStrictEqual( ["GET", "HEAD"].map((method) => ({ ifNoneMatch: '"abc"', method, url: `${PLATFORM_URL}${path}?v=1`, })) ); }) );
it.effect( "comes back as the platform answered, a redirect's Location kept", () => Effect.gen(function* () { const { handlers } = served( () => new Response(null, { headers: { "cache-control": "public, max-age=300", location: `/releases/0.1.0/${Brand.slug}-linux-x64`, }, status: 302, }) );
const response = yield* Effect.promise(() => handlers.GET({ request: new Request( `${WEB_URL}/releases/latest/${Brand.slug}-linux-x64` ), }) );
expect({ cacheControl: response.headers.get("cache-control"), location: response.headers.get("location"), status: response.status, }).toStrictEqual({ cacheControl: "public, max-age=300", location: `/releases/0.1.0/${Brand.slug}-linux-x64`, status: 302, }); }) );});
interface ServerRoute { readonly options: { readonly server?: { readonly handlers?: object } };}
// Imported with the file, not in the test: loading a route module pulls in// the router, which can take longer than a test's timeout under a full// verify's load.const forwardingRoutes: readonly (readonly [string, ServerRoute])[] = [ ["the skills", skills], ["the install script", installScript], ["the releases", releases],];
describe("the forwarding routes", () => { it.each(forwardingRoutes)( "%s answer GET and HEAD, and nothing else", (_name, route) => { expect( Object.keys(route.options.server?.handlers ?? {}).toSorted() ).toStrictEqual(["GET", "HEAD"]); } );});