ask-markdown.test.ts
This file
Path
apps/web/test/ask-markdown.test.ts
378 lines · 10.8 KiB · TypeScript
Last changed by
Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379import { describe, expect, it } from "@effect/vitest";import { Brand } from "@repo/brand";import { Client, Contracts } from "@repo/protocol/contracts";import { AskId, ProjectName } from "@repo/protocol/ids";import { UnknownAsk, UnknownProject } from "@repo/protocol/refusal";import { explain } from "@repo/protocol/render";import { asTester, unanswered } from "@repo/protocol/testing";import { Effect, Exit, Layer, Ref } from "effect";import { RpcTest } from "effect/rpc";
import { indexBanner } from "../src/features/agents/llms.ts";import { askMarkdownResponse, refusalsMarkdown, wantsMarkdown,} from "../src/features/ask/markdown.ts";import { askPageHandler, askPageHeaders,} from "../src/routes/$owner/$project/asks/$ask.tsx";import { askMarkdownHandler } from "../src/routes/$owner/$project/asks/{$ask}[.]md.ts";import { ask, askId, brief, project } from "./ask-fixtures.ts";
const WEB = "https://web-test.example.workers.dev";
const index = { project, webUrl: WEB };
const [title = "", ...rest] = brief.split("\n");
const served = [title, "", indexBanner(index), ...rest].join("\n");
const MARKDOWN = "text/markdown; charset=utf-8";
const BROWSER = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
const platformOf = (calls: Ref.Ref<number>) => () => Layer.effect(Client, RpcTest.makeClient(Contracts)).pipe( Layer.provide([ Layer.mergeAll( unanswered, Contracts.toLayerHandler("showAsk", (payload) => Effect.gen(function* () { yield* Ref.update(calls, (count) => count + 1);
if (payload.project !== project) { return yield* new UnknownProject({ project: payload.project }); }
if (payload.ask !== askId) { return yield* new UnknownAsk({ ask: payload.ask }); }
return { ask, brief }; }) ) ), asTester, ]) );
const unreachable = () => Layer.effect(Client, RpcTest.makeClient(Contracts)).pipe( Layer.provide([unanswered, asTester]) );
const get = (path: string, accept?: string) => new Request(`${WEB}${path}`, { headers: accept === undefined ? {} : { accept }, });
const params = (askName: string, projectName: string = project) => ({ ask: askName, project: projectName,});
const read = (response: Response) => Effect.map( Effect.promise(() => response.text()), (body) => ({ body, cacheControl: response.headers.get("cache-control"), contentType: response.headers.get("content-type"), status: response.status, vary: response.headers.get("vary"), }) );
const PAGE = new Response("<!doctype html><title>the page</title>");
describe("an ask's URL with Accept: text/markdown", () => { it.effect( "answers with the brief showAsk gives, as Markdown that public caches may keep a minute", () => Effect.gen(function* () { const calls = yield* Ref.make(0);
const response = yield* Effect.promise(() => Promise.resolve( askPageHandler(platformOf(calls))({ next: () => PAGE, params: params(askId), request: get(`/oscabriel/datekit/asks/${askId}`, "text/markdown"), }) ) );
expect(yield* read(response)).toStrictEqual({ body: served, cacheControl: "public, max-age=60", contentType: MARKDOWN, status: 200, vary: "Accept", }); expect(yield* Ref.get(calls)).toBe(1); }) );
it.effect( "falls through to the page when the reader asks for HTML, and reads no ask", () => Effect.gen(function* () { const calls = yield* Ref.make(0);
const answered = askPageHandler(platformOf(calls))({ next: () => PAGE, params: params(askId), request: get(`/oscabriel/datekit/asks/${askId}`, BROWSER), });
expect(answered).toBe(PAGE); expect(yield* Ref.get(calls)).toBe(0); }) );});
describe("an ask's .md URL", () => { it.effect( "answers with the same Markdown, whatever the request accepts", () => Effect.gen(function* () { const calls = yield* Ref.make(0);
const response = yield* Effect.promise(() => askMarkdownHandler(platformOf(calls))({ params: params(askId), request: get(`/oscabriel/datekit/asks/${askId}.md`, BROWSER), }) );
expect(yield* read(response)).toStrictEqual({ body: served, cacheControl: "public, max-age=60", contentType: MARKDOWN, status: 200, vary: "Accept", }); }) );
it.effect( "answers an unknown ask with 404 and the UnknownAsk refusal, as git prints it, then its next command", () => Effect.gen(function* () { const calls = yield* Ref.make(0); const refusal = new UnknownAsk({ ask: AskId.make("no-such-ask") });
const response = yield* Effect.promise(() => askMarkdownHandler(platformOf(calls))({ params: params("no-such-ask"), request: get("/oscabriel/datekit/asks/no-such-ask.md"), }) );
const seen = yield* read(response);
expect(seen.status).toBe(404); expect(seen.contentType).toBe(MARKDOWN); expect(seen.body).toBe( [ "```text", ...explain([refusal]), "```", "", `Next: \`${Brand.cli} asks\``, "", ].join("\n") ); expect(seen.cacheControl).toBe("no-store"); }) );
it.effect( "answers an unknown project with 404 and the UnknownProject refusal", () => Effect.gen(function* () { const calls = yield* Ref.make(0);
const response = yield* Effect.promise(() => askMarkdownHandler(platformOf(calls))({ params: params(askId, "oscabriel/nowhere"), request: get(`/oscabriel/nowhere/asks/${askId}.md`), }) );
const seen = yield* read(response);
expect(seen.status).toBe(404); expect(seen.body).toBe( refusalsMarkdown([ new UnknownProject({ project: ProjectName.make("oscabriel/nowhere"), }), ]) ); expect(seen.body).toContain( "UnknownProject: no project is named oscabriel/nowhere" ); }) );
it.effect( "answers a path that can name no ask with 404, and asks the platform nothing", () => Effect.gen(function* () { const calls = yield* Ref.make(0);
const response = yield* Effect.promise(() => askMarkdownHandler(platformOf(calls))({ params: params("Not_An-Ask"), request: get("/oscabriel/datekit/asks/Not_An-Ask.md"), }) );
const seen = yield* read(response);
expect(seen.status).toBe(404); expect(seen.contentType).toBe(MARKDOWN); expect(seen.body).toContain("Not_An-Ask"); expect(yield* Ref.get(calls)).toBe(0); }) );
it.effect( "answers 502 when the platform does not answer, and keeps it out of caches", () => Effect.gen(function* () { const response = yield* Effect.promise(() => askMarkdownHandler(unreachable)({ params: params(askId), request: get(`/oscabriel/datekit/asks/${askId}.md`), }) );
const seen = yield* read(response);
expect(seen.status).toBe(502); expect(seen.contentType).toBe(MARKDOWN); expect(seen.cacheControl).toBe("no-store"); }) );});
describe("the ask page", () => { it("names its Markdown as an alternate, and says it varies by Accept", () => { expect(askPageHeaders({ params: params(askId) })).toStrictEqual({ link: `</oscabriel/datekit/asks/${askId}.md>; rel="alternate"; type="text/markdown"`, vary: "Accept", }); });
it("names no alternate for a path that can name no ask, so no raw path text reaches a header", () => { expect( askPageHeaders({ params: params('x>; rel="next"\nset-cookie: a=b') }) ).toStrictEqual({ vary: "Accept" }); });});
describe(askMarkdownResponse, () => { it.effect( "never lets a cache keep the answer of a contract only maintainers may see", () => Effect.gen(function* () { const seen = yield* read( askMarkdownResponse(Exit.succeed({ brief }), "maintainers", index) );
expect([seen.status, seen.cacheControl, seen.body]).toStrictEqual([ 200, "private, no-store", served, ]); }) );});
describe("an ask's Markdown with the index named", () => { it.effect( "opens with the brief's title, then the banner naming its project's llms.txt", () => Effect.gen(function* () { const seen = yield* read( askMarkdownResponse(Exit.succeed({ brief }), "public", index) );
expect(seen.status).toBe(200); expect(seen.body).toBe(served); expect(seen.body.startsWith("# ")).toBeTruthy(); expect(seen.body.split("\n")[2]).toContain( `${WEB}/${project}/llms.txt` ); }) );
it.effect("keeps a refusal's Markdown as it is, with no banner", () => Effect.gen(function* () { const refusal = new UnknownAsk({ ask: AskId.make("no-such-ask") });
const seen = yield* read( askMarkdownResponse(Exit.fail(refusal), "public", index) );
expect([seen.status, seen.body]).toStrictEqual([ 404, refusalsMarkdown([refusal]), ]); }) );});
describe(refusalsMarkdown, () => { it("sets every refusal's explain lines in one text block, then each distinct next command once", () => { const refusals = [ new UnknownProject({ project: ProjectName.make("oscabriel/nowhere") }), new UnknownAsk({ ask: askId }), ] as const;
expect(refusalsMarkdown(refusals)).toBe( [ "```text", ...explain(refusals), "```", "", `Next: \`${Brand.cli} asks\``, "", ].join("\n") ); });});
const accepting = (accept: string | undefined) => wantsMarkdown(get("/oscabriel/datekit/asks/parse-iso-week", accept));
describe(wantsMarkdown, () => { it.each([ ["text/markdown"], ["text/markdown, text/html"], ["text/html;q=0.9, text/markdown"], ["text/markdown;q=0.8, */*;q=0.1"], ["Text/Markdown; charset=utf-8"], ])("serves Markdown to %o", (accept) => { expect(accepting(accept)).toBeTruthy(); });
it.each([ [undefined], ["*/*"], ["text/*"], ["text/html"], [BROWSER], ["text/markdown;q=0.5, text/html"], ["text/markdown;q=0, text/html;q=0.1"], ["text/markdown;q=0"], ["application/json"], ])("serves the page to %o", (accept) => { expect(accepting(accept)).toBeFalsy(); });});