raw.test.ts
This file
Path
apps/platform/test/raw.test.ts
227 lines · 7.0 KiB · TypeScript
Last changed by
Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228import { describe, expect, it } from "@effect/vitest";import { RefName, RepoHost } from "@repo/artifacts/repo-host";import { memoryFs } from "@repo/artifacts/repo-host/memory-fs";import { CodeCacheLive } from "@repo/project/code/cache";import { Projects } from "@repo/project/projects";import { keyOf, RepoName } from "@repo/protocol/ids";import { Person } from "@repo/protocol/principal";import { advanceTo } from "@repo/testkit/clock";import { gitClient } from "@repo/testkit/git-client";import { aProject } from "@repo/testkit/scenarios";import { Context, Effect, Layer, Option, Ref } from "effect";
import { Handlers } from "../src/handlers.ts";import { MemorySessions } from "../src/sessions/memory.ts";import { worldOf } from "./handlers.fixtures.ts";import { origin, served } from "./harness.ts";
const datekit = aProject({ people: [] });
const { project } = datekit;
const readme = "# datekit\n\nDates, parsed.\n";
const platformWith = (reads: Option.Option<Ref.Ref<number>>) => Effect.gen(function* () { yield* advanceTo("2026-10-07T12:00:00Z");
const world = yield* Layer.build(worldOf([datekit]));
const first = yield* Effect.gen(function* () { const host = yield* RepoHost; const api = yield* (yield* Projects).create(project); const initializer = Person.make({ handle: datekit.initializer }); const client = yield* gitClient(memoryFs());
yield* api.initProject(initializer, {});
const sha = yield* client.commit("Start", [ ["README.md", readme], ["package.json", '{ "name": "alice/datekit" }\n'], ]);
yield* host.push(RepoName.make(keyOf(project)), client.source, [ { from: Option.none(), ref: RefName.make("refs/heads/main"), to: sha }, { from: Option.none(), ref: RefName.make("refs/tags/@datekit/core@1.0.0"), to: sha, }, ]); yield* api.recordFirstPush(initializer, sha);
return sha; }).pipe(Effect.orDie, Effect.provide(world));
const cached = yield* Option.match(reads, { onNone: () => Effect.succeed(world), onSome: (count) => Effect.map(Layer.build(CodeCacheLive(100)), (cache) => { const host = Context.get(world, RepoHost);
return Context.merge(world, cache).pipe( Context.add( RepoHost, RepoHost.of({ ...host, readTree: (name, tree) => Effect.andThen( Ref.update(count, (n) => n + 1), host.readTree(name, tree) ), }) ) ); }), });
const ports = Layer.succeedContext(cached);
const { fetch } = yield* served( Layer.mergeAll( Handlers.pipe(Layer.provide(ports)), ports, MemorySessions.layer({}) ) );
return { fetch, first }; });
const platform = platformWith(Option.none());
const get = ( fetch: (request: Request) => Effect.Effect<Response>, path: string, headers: Record<string, string> = {}) => Effect.flatMap( fetch(new Request(`${origin}${path}`, { headers })), (response) => Effect.map( Effect.promise(() => response.text()), (text) => ({ headers: response.headers, status: response.status, text }) ) );
describe("a project's raw files", () => { it.effect( "answer anyone with the bytes and their headers, a stale bearer or not", () => Effect.gen(function* () { const { fetch, first } = yield* platform; const path = `/${project}/raw/${first}/README.md`;
const answers = yield* Effect.forEach( [{}, { authorization: "Bearer not-a-token-the-platform-knows" }], (headers) => Effect.map(get(fetch, path, headers), (answer) => ({ cache: answer.headers.get("cache-control"), etag: /^"[0-9a-f]{40}"$/u.test(answer.headers.get("etag") ?? ""), nosniff: answer.headers.get("x-content-type-options"), sandboxed: ( answer.headers.get("content-security-policy") ?? "" ).includes("sandbox"), status: answer.status, text: answer.text, type: answer.headers.get("content-type"), })) );
expect(answers).toStrictEqual( answers.map(() => ({ cache: "public, max-age=31536000, immutable", etag: true, nosniff: "nosniff", sandboxed: true, status: 200, text: readme, type: "text/plain; charset=utf-8", })) ); expect(answers).toHaveLength(2);
const main = yield* get(fetch, `/${project}/raw/main/README.md`);
expect([main.status, main.headers.get("cache-control")]).toStrictEqual([ 200, "public, max-age=60", ]); }) );
it.effect( "find a tag by its name, URL-encoded in one segment, slash and all", () => Effect.gen(function* () { const { fetch } = yield* platform;
const answer = yield* get( fetch, `/${project}/raw/${encodeURIComponent("@datekit/core@1.0.0")}/README.md` );
expect([ answer.status, answer.text, answer.headers.get("cache-control"), ]).toStrictEqual([200, readme, "public, max-age=60"]); }) );
it.effect( "is a 404 with no body for a path that climbs out, a missing file or project", () => Effect.gen(function* () { const { fetch, first } = yield* platform;
const answers = yield* Effect.forEach( [ `/${project}/raw/${first}/../../x`, `/${project}/raw/${first}/..%2F..%2Fx`, `/${project}/raw/${first}/nope.md`, `/${project}/raw/v9/README.md`, `/nobody/raw/main/README.md`, ], (path) => Effect.map(get(fetch, path), ({ status, text }) => [status, text]) );
expect(answers).toStrictEqual(answers.map(() => [404, ""])); }) );
it.effect("read a tree once per isolate, by its id", () => Effect.gen(function* () { const reads = yield* Ref.make(0); const { fetch, first } = yield* platformWith(Option.some(reads)); const path = `/${project}/raw/${first}/README.md`;
const once = yield* get(fetch, path); const counted = yield* Ref.get(reads); const again = yield* get(fetch, path);
expect([once.status, again.status, again.text]).toStrictEqual([ 200, 200, readme, ]); expect(counted).toBeGreaterThan(0); expect(yield* Ref.get(reads)).toBe(counted); }) );
it.effect("leave git's own paths to git", () => Effect.gen(function* () { const { fetch } = yield* platform;
const answer = yield* get( fetch, `/${project}/info/refs?service=git-upload-pack` );
expect(answer.status).not.toBe(404); expect(answer.headers.get("content-security-policy")).toBeNull(); }) );});