rpc-route.test.ts
This file
Path
apps/web/test/rpc-route.test.ts
206 lines · 5.9 KiB · TypeScript
Last changed by
Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207import { describe, expect, it } from "@effect/vitest";import { Client } from "@repo/protocol/contracts";import { ProjectName } from "@repo/protocol/ids";import { untouchable } from "@repo/testkit/untouchable";import { Effect, Exit, Schema } from "effect";
import { backend } from "../src/server/backend.ts";import { serverClientOver } from "../src/server/client.ts";import { rpcHandler } from "../src/server/rpc.ts";import type { Backend } from "../src/server/rpc.ts";
const PLATFORM_URL = "https://platform-test.example.workers.dev";
const WEB_URL = "https://web-test.example.workers.dev";
const LIST_ASKS = '[{"_tag":"Request","id":"0","tag":"listAsks","payload":{"project":"datekit"},"headers":[]}]';
const seenOf = (request: Request) => Effect.map( Effect.promise(() => request.text()), (body) => ({ authorization: request.headers.get("authorization"), body, contentType: request.headers.get("content-type"), cookie: request.headers.get("cookie"), method: request.method, url: request.url, }) );
const recorder = (answer: () => Response) => { const handed: Request[] = [];
const fetch = (request: Request) => { handed.push(request);
return Promise.resolve(answer()); };
return { fetch, seen: Effect.suspend(() => Effect.forEach(handed, seenOf)) };};
const envWith = (fetch: (request: Request) => Promise<Response>): Env => ({ PLATFORM: untouchable<Fetcher>("PLATFORM", { fetch: (input, init) => fetch(new Request(input, init)), }), PUBLIC_PLATFORM_URL: PLATFORM_URL,});
const unused = envWith(() => Promise.resolve(new Response("unused")));
const listAsksPost = (headers: Readonly<Record<string, string>> = {}) => new Request(`${WEB_URL}/rpc`, { body: LIST_ASKS, headers: { "content-type": "application/json", ...headers }, method: "POST", });
describe("the /rpc route's handler", () => { it.effect("hands the request to the backend it was given, with the env", () => Effect.gen(function* () { const envs: Env[] = []; const stub = recorder(() => new Response("answered"));
const given: Backend = (request, env) => { envs.push(env);
return stub.fetch(request); };
const response = yield* Effect.promise(() => rpcHandler(given, unused)({ request: listAsksPost() }) );
expect(envs).toStrictEqual([unused]); expect(yield* stub.seen).toStrictEqual([ { authorization: null, body: LIST_ASKS, contentType: "application/json", cookie: null, method: "POST", url: `${WEB_URL}/rpc`, }, ]); expect(yield* Effect.promise(() => response.text())).toBe("answered"); }) );});
describe("the production backend", () => { it.effect( "forwards to PLATFORM at the platform's URL, keeping method, headers and body, but not the browser's cookies", () => Effect.gen(function* () { const platform = recorder(() => new Response("from the platform"));
const response = yield* Effect.promise(() => backend( listAsksPost({ cookie: "theme=dark" }), envWith(platform.fetch) ) );
expect(yield* platform.seen).toStrictEqual([ { authorization: null, body: LIST_ASKS, contentType: "application/json", cookie: null, method: "POST", url: `${PLATFORM_URL}/rpc`, }, ]); expect(yield* Effect.promise(() => response.text())).toBe( "from the platform" ); }) );
it.effect("keeps the path and the query on the platform's origin", () => Effect.gen(function* () { const platform = recorder(() => new Response("ok"));
yield* Effect.promise(() => backend( new Request(`${WEB_URL}/releases/x?y=1`), envWith(platform.fetch) ) );
const seen = yield* platform.seen;
expect(seen.map(({ method, url }) => [method, url])).toStrictEqual([ ["GET", `${PLATFORM_URL}/releases/x?y=1`], ]); }) );});
describe("a server route's client", () => { it.effect( "posts each call to /rpc through the backend, in process, with the caller's cookie", () => Effect.gen(function* () { const stub = recorder( () => new Response("not an rpc answer", { status: 500 }) );
const given: Backend = (request) => stub.fetch(request);
const exit = yield* Effect.service(Client).pipe( Effect.flatMap((client) => client.listAsks({ project: Schema.decodeSync(ProjectName)("datekit"), }) ), Effect.provide( serverClientOver(given)( unused, listAsksPost({ cookie: "session=abc" }) ) ), Effect.exit );
const seen = yield* stub.seen;
expect(Exit.isFailure(exit)).toBeTruthy(); expect( seen.map(({ body, contentType, cookie, method, url }) => ({ body: body.includes('"tag":"listAsks"'), contentType, cookie, method, // Effect's HTTP protocol posts to its URL with a trailing slash. path: new URL(url).pathname.replace(/\/$/u, ""), })) ).toStrictEqual([ { body: true, contentType: "application/json", cookie: "session=abc", method: "POST", path: "/rpc", }, ]); expect(new URL(seen[0]?.url ?? "").origin).toBe(WEB_URL); }) );});
describe("the /rpc route", () => { it.effect("answers POST with the backend's handler, and nothing else", () => Effect.gen(function* () { const { Route } = yield* Effect.promise( () => import("../src/routes/rpc.ts") );
expect(Object.keys(Route.options.server?.handlers ?? {})).toStrictEqual([ "POST", ]); }) );});