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
Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 1–5why, one step back1import { describe, expect, it } from "@effect/vitest";2import { RefName, RepoHost } from "@repo/artifacts/repo-host";3import { memoryFs } from "@repo/artifacts/repo-host/memory-fs";4import { CodeCacheLive } from "@repo/project/code/cache";5import { Projects } from "@repo/project/projects";Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 6why, one step back6import { keyOf, RepoName } from "@repo/protocol/ids";Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 7–39why, one step back7import { Person } from "@repo/protocol/principal";8import { advanceTo } from "@repo/testkit/clock";9import { gitClient } from "@repo/testkit/git-client";10import { aProject } from "@repo/testkit/scenarios";11import { Context, Effect, Layer, Option, Ref } from "effect";1213import { Handlers } from "../src/handlers.ts";14import { MemorySessions } from "../src/sessions/memory.ts";15import { worldOf } from "./handlers.fixtures.ts";16import { origin, served } from "./harness.ts";1718const datekit = aProject({ people: [] });1920const { project } = datekit;2122const readme = "# datekit\n\nDates, parsed.\n";2324const platformWith = (reads: Option.Option<Ref.Ref<number>>) =>25 Effect.gen(function* () {26 yield* advanceTo("2026-10-07T12:00:00Z");2728 const world = yield* Layer.build(worldOf([datekit]));2930 const first = yield* Effect.gen(function* () {31 const host = yield* RepoHost;32 const api = yield* (yield* Projects).create(project);33 const initializer = Person.make({ handle: datekit.initializer });34 const client = yield* gitClient(memoryFs());3536 yield* api.initProject(initializer, {});3738 const sha = yield* client.commit("Start", [39 ["README.md", readme],Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 40why, one step back40 ["package.json", '{ "name": "alice/datekit" }\n'],Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 41–42why, one step back41 ]);42Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 43why, one step back43 yield* host.push(RepoName.make(keyOf(project)), client.source, [Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 44–227why, one step back44 { from: Option.none(), ref: RefName.make("refs/heads/main"), to: sha },45 {46 from: Option.none(),47 ref: RefName.make("refs/tags/@datekit/core@1.0.0"),48 to: sha,49 },50 ]);51 yield* api.recordFirstPush(initializer, sha);5253 return sha;54 }).pipe(Effect.orDie, Effect.provide(world));5556 const cached = yield* Option.match(reads, {57 onNone: () => Effect.succeed(world),58 onSome: (count) =>59 Effect.map(Layer.build(CodeCacheLive(100)), (cache) => {60 const host = Context.get(world, RepoHost);6162 return Context.merge(world, cache).pipe(63 Context.add(64 RepoHost,65 RepoHost.of({66 ...host,67 readTree: (name, tree) =>68 Effect.andThen(69 Ref.update(count, (n) => n + 1),70 host.readTree(name, tree)71 ),72 })73 )74 );75 }),76 });7778 const ports = Layer.succeedContext(cached);7980 const { fetch } = yield* served(81 Layer.mergeAll(82 Handlers.pipe(Layer.provide(ports)),83 ports,84 MemorySessions.layer({})85 )86 );8788 return { fetch, first };89 });9091const platform = platformWith(Option.none());9293const get = (94 fetch: (request: Request) => Effect.Effect<Response>,95 path: string,96 headers: Record<string, string> = {}97) =>98 Effect.flatMap(99 fetch(new Request(`${origin}${path}`, { headers })),100 (response) =>101 Effect.map(102 Effect.promise(() => response.text()),103 (text) => ({ headers: response.headers, status: response.status, text })104 )105 );106107describe("a project's raw files", () => {108 it.effect(109 "answer anyone with the bytes and their headers, a stale bearer or not",110 () =>111 Effect.gen(function* () {112 const { fetch, first } = yield* platform;113 const path = `/${project}/raw/${first}/README.md`;114115 const answers = yield* Effect.forEach(116 [{}, { authorization: "Bearer not-a-token-the-platform-knows" }],117 (headers) =>118 Effect.map(get(fetch, path, headers), (answer) => ({119 cache: answer.headers.get("cache-control"),120 etag: /^"[0-9a-f]{40}"$/u.test(answer.headers.get("etag") ?? ""),121 nosniff: answer.headers.get("x-content-type-options"),122 sandboxed: (123 answer.headers.get("content-security-policy") ?? ""124 ).includes("sandbox"),125 status: answer.status,126 text: answer.text,127 type: answer.headers.get("content-type"),128 }))129 );130131 expect(answers).toStrictEqual(132 answers.map(() => ({133 cache: "public, max-age=31536000, immutable",134 etag: true,135 nosniff: "nosniff",136 sandboxed: true,137 status: 200,138 text: readme,139 type: "text/plain; charset=utf-8",140 }))141 );142 expect(answers).toHaveLength(2);143144 const main = yield* get(fetch, `/${project}/raw/main/README.md`);145146 expect([main.status, main.headers.get("cache-control")]).toStrictEqual([147 200,148 "public, max-age=60",149 ]);150 })151 );152153 it.effect(154 "find a tag by its name, URL-encoded in one segment, slash and all",155 () =>156 Effect.gen(function* () {157 const { fetch } = yield* platform;158159 const answer = yield* get(160 fetch,161 `/${project}/raw/${encodeURIComponent("@datekit/core@1.0.0")}/README.md`162 );163164 expect([165 answer.status,166 answer.text,167 answer.headers.get("cache-control"),168 ]).toStrictEqual([200, readme, "public, max-age=60"]);169 })170 );171172 it.effect(173 "is a 404 with no body for a path that climbs out, a missing file or project",174 () =>175 Effect.gen(function* () {176 const { fetch, first } = yield* platform;177178 const answers = yield* Effect.forEach(179 [180 `/${project}/raw/${first}/../../x`,181 `/${project}/raw/${first}/..%2F..%2Fx`,182 `/${project}/raw/${first}/nope.md`,183 `/${project}/raw/v9/README.md`,184 `/nobody/raw/main/README.md`,185 ],186 (path) =>187 Effect.map(get(fetch, path), ({ status, text }) => [status, text])188 );189190 expect(answers).toStrictEqual(answers.map(() => [404, ""]));191 })192 );193194 it.effect("read a tree once per isolate, by its id", () =>195 Effect.gen(function* () {196 const reads = yield* Ref.make(0);197 const { fetch, first } = yield* platformWith(Option.some(reads));198 const path = `/${project}/raw/${first}/README.md`;199200 const once = yield* get(fetch, path);201 const counted = yield* Ref.get(reads);202 const again = yield* get(fetch, path);203204 expect([once.status, again.status, again.text]).toStrictEqual([205 200,206 200,207 readme,208 ]);209 expect(counted).toBeGreaterThan(0);210 expect(yield* Ref.get(reads)).toBe(counted);211 })212 );213214 it.effect("leave git's own paths to git", () =>215 Effect.gen(function* () {216 const { fetch } = yield* platform;217218 const answer = yield* get(219 fetch,220 `/${project}/info/refs?service=git-upload-pack`221 );222223 expect(answer.status).not.toBe(404);224 expect(answer.headers.get("content-security-policy")).toBeNull();225 })226 );227});