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
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 1–10why, one step back1import { describe, expect, it } from "@effect/vitest";2import { Brand } from "@repo/brand";3import { Client, Contracts } from "@repo/protocol/contracts";4import { AskId, ProjectName } from "@repo/protocol/ids";5import { UnknownAsk, UnknownProject } from "@repo/protocol/refusal";6import { explain } from "@repo/protocol/render";7import { asTester, unanswered } from "@repo/protocol/testing";8import { Effect, Exit, Layer, Ref } from "effect";9import { RpcTest } from "effect/rpc";10Merge build/1108 into mainoscabrielbefore adoption8d9ece9
line 11why, one step back11import { indexBanner } from "../src/features/agents/llms.ts";Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 12–19why, one step back12import {13 askMarkdownResponse,14 refusalsMarkdown,15 wantsMarkdown,16} from "../src/features/ask/markdown.ts";17import {18 askPageHandler,19 askPageHeaders,Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
lines 20–21why, one step back20} from "../src/routes/$owner/$project/asks/$ask.tsx";21import { askMarkdownHandler } from "../src/routes/$owner/$project/asks/{$ask}[.]md.ts";Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 22–25why, one step back22import { ask, askId, brief, project } from "./ask-fixtures.ts";2324const WEB = "https://web-test.example.workers.dev";25Merge build/1108 into mainoscabrielbefore adoption8d9ece9
lines 26–27why, one step back26const index = { project, webUrl: WEB };27Merge fix/banner-below-title into mainoscabrielbefore adoptionca95612
line 28why, one step back28const [title = "", ...rest] = brief.split("\n");Merge build/1108 into mainoscabrielbefore adoption8d9ece9
line 29why, one step back29Merge fix/banner-below-title into mainoscabrielbefore adoptionca95612
lines 30–31why, one step back30const served = [title, "", indexBanner(index), ...rest].join("\n");31Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 32–102why, one step back32const MARKDOWN = "text/markdown; charset=utf-8";3334const BROWSER =35 "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";3637const platformOf = (calls: Ref.Ref<number>) => () =>38 Layer.effect(Client, RpcTest.makeClient(Contracts)).pipe(39 Layer.provide([40 Layer.mergeAll(41 unanswered,42 Contracts.toLayerHandler("showAsk", (payload) =>43 Effect.gen(function* () {44 yield* Ref.update(calls, (count) => count + 1);4546 if (payload.project !== project) {47 return yield* new UnknownProject({ project: payload.project });48 }4950 if (payload.ask !== askId) {51 return yield* new UnknownAsk({ ask: payload.ask });52 }5354 return { ask, brief };55 })56 )57 ),58 asTester,59 ])60 );6162const unreachable = () =>63 Layer.effect(Client, RpcTest.makeClient(Contracts)).pipe(64 Layer.provide([unanswered, asTester])65 );6667const get = (path: string, accept?: string) =>68 new Request(`${WEB}${path}`, {69 headers: accept === undefined ? {} : { accept },70 });7172const params = (askName: string, projectName: string = project) => ({73 ask: askName,74 project: projectName,75});7677const read = (response: Response) =>78 Effect.map(79 Effect.promise(() => response.text()),80 (body) => ({81 body,82 cacheControl: response.headers.get("cache-control"),83 contentType: response.headers.get("content-type"),84 status: response.status,85 vary: response.headers.get("vary"),86 })87 );8889const PAGE = new Response("<!doctype html><title>the page</title>");9091describe("an ask's URL with Accept: text/markdown", () => {92 it.effect(93 "answers with the brief showAsk gives, as Markdown that public caches may keep a minute",94 () =>95 Effect.gen(function* () {96 const calls = yield* Ref.make(0);9798 const response = yield* Effect.promise(() =>99 Promise.resolve(100 askPageHandler(platformOf(calls))({101 next: () => PAGE,102 params: params(askId),Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 103why, one step back103 request: get(`/oscabriel/datekit/asks/${askId}`, "text/markdown"),Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 104–108why, one step back104 })105 )106 );107108 expect(yield* read(response)).toStrictEqual({Merge build/1108 into mainoscabrielbefore adoption8d9ece9
line 109why, one step back109 body: served,Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 110–127why, one step back110 cacheControl: "public, max-age=60",111 contentType: MARKDOWN,112 status: 200,113 vary: "Accept",114 });115 expect(yield* Ref.get(calls)).toBe(1);116 })117 );118119 it.effect(120 "falls through to the page when the reader asks for HTML, and reads no ask",121 () =>122 Effect.gen(function* () {123 const calls = yield* Ref.make(0);124125 const answered = askPageHandler(platformOf(calls))({126 next: () => PAGE,127 params: params(askId),Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 128why, one step back128 request: get(`/oscabriel/datekit/asks/${askId}`, BROWSER),Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 129–146why, one step back129 });130131 expect(answered).toBe(PAGE);132 expect(yield* Ref.get(calls)).toBe(0);133 })134 );135});136137describe("an ask's .md URL", () => {138 it.effect(139 "answers with the same Markdown, whatever the request accepts",140 () =>141 Effect.gen(function* () {142 const calls = yield* Ref.make(0);143144 const response = yield* Effect.promise(() =>145 askMarkdownHandler(platformOf(calls))({146 params: params(askId),Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 147why, one step back147 request: get(`/oscabriel/datekit/asks/${askId}.md`, BROWSER),Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 148–151why, one step back148 })149 );150151 expect(yield* read(response)).toStrictEqual({Merge build/1108 into mainoscabrielbefore adoption8d9ece9
line 152why, one step back152 body: served,Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 153–170why, one step back153 cacheControl: "public, max-age=60",154 contentType: MARKDOWN,155 status: 200,156 vary: "Accept",157 });158 })159 );160161 it.effect(162 "answers an unknown ask with 404 and the UnknownAsk refusal, as git prints it, then its next command",163 () =>164 Effect.gen(function* () {165 const calls = yield* Ref.make(0);166 const refusal = new UnknownAsk({ ask: AskId.make("no-such-ask") });167168 const response = yield* Effect.promise(() =>169 askMarkdownHandler(platformOf(calls))({170 params: params("no-such-ask"),Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 171why, one step back171 request: get("/oscabriel/datekit/asks/no-such-ask.md"),Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 172–200why, one step back172 })173 );174175 const seen = yield* read(response);176177 expect(seen.status).toBe(404);178 expect(seen.contentType).toBe(MARKDOWN);179 expect(seen.body).toBe(180 [181 "```text",182 ...explain([refusal]),183 "```",184 "",185 `Next: \`${Brand.cli} asks\``,186 "",187 ].join("\n")188 );189 expect(seen.cacheControl).toBe("no-store");190 })191 );192193 it.effect(194 "answers an unknown project with 404 and the UnknownProject refusal",195 () =>196 Effect.gen(function* () {197 const calls = yield* Ref.make(0);198199 const response = yield* Effect.promise(() =>200 askMarkdownHandler(platformOf(calls))({Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
lines 201–202why, one step back201 params: params(askId, "oscabriel/nowhere"),202 request: get(`/oscabriel/nowhere/asks/${askId}.md`),Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 203–210why, one step back203 })204 );205206 const seen = yield* read(response);207208 expect(seen.status).toBe(404);209 expect(seen.body).toBe(210 refusalsMarkdown([Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
lines 211–213why, one step back211 new UnknownProject({212 project: ProjectName.make("oscabriel/nowhere"),213 }),Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 214–216why, one step back214 ])215 );216 expect(seen.body).toContain(Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 217why, one step back217 "UnknownProject: no project is named oscabriel/nowhere"Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 218–230why, one step back218 );219 })220 );221222 it.effect(223 "answers a path that can name no ask with 404, and asks the platform nothing",224 () =>225 Effect.gen(function* () {226 const calls = yield* Ref.make(0);227228 const response = yield* Effect.promise(() =>229 askMarkdownHandler(platformOf(calls))({230 params: params("Not_An-Ask"),Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 231why, one step back231 request: get("/oscabriel/datekit/asks/Not_An-Ask.md"),Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 232–250why, one step back232 })233 );234235 const seen = yield* read(response);236237 expect(seen.status).toBe(404);238 expect(seen.contentType).toBe(MARKDOWN);239 expect(seen.body).toContain("Not_An-Ask");240 expect(yield* Ref.get(calls)).toBe(0);241 })242 );243244 it.effect(245 "answers 502 when the platform does not answer, and keeps it out of caches",246 () =>247 Effect.gen(function* () {248 const response = yield* Effect.promise(() =>249 askMarkdownHandler(unreachable)({250 params: params(askId),Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 251why, one step back251 request: get(`/oscabriel/datekit/asks/${askId}.md`),Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 252–266why, one step back252 })253 );254255 const seen = yield* read(response);256257 expect(seen.status).toBe(502);258 expect(seen.contentType).toBe(MARKDOWN);259 expect(seen.cacheControl).toBe("no-store");260 })261 );262});263264describe("the ask page", () => {265 it("names its Markdown as an alternate, and says it varies by Accept", () => {266 expect(askPageHeaders({ params: params(askId) })).toStrictEqual({Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 267why, one step back267 link: `</oscabriel/datekit/asks/${askId}.md>; rel="alternate"; type="text/markdown"`,Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 268–284why, one step back268 vary: "Accept",269 });270 });271272 it("names no alternate for a path that can name no ask, so no raw path text reaches a header", () => {273 expect(274 askPageHeaders({ params: params('x>; rel="next"\nset-cookie: a=b') })275 ).toStrictEqual({ vary: "Accept" });276 });277});278279describe(askMarkdownResponse, () => {280 it.effect(281 "never lets a cache keep the answer of a contract only maintainers may see",282 () =>283 Effect.gen(function* () {284 const seen = yield* read(Merge build/1108 into mainoscabrielbefore adoption8d9ece9
line 285why, one step back285 askMarkdownResponse(Exit.succeed({ brief }), "maintainers", index)Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 286–290why, one step back286 );287288 expect([seen.status, seen.cacheControl, seen.body]).toStrictEqual([289 200,290 "private, no-store",Merge build/1108 into mainoscabrielbefore adoption8d9ece9
line 291why, one step back291 served,Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 292–293why, one step back292 ]);293 })Merge build/1108 into mainoscabrielbefore adoption8d9ece9
lines 294–298why, one step back294 );295});296297describe("an ask's Markdown with the index named", () => {298 it.effect(Merge fix/banner-below-title into mainoscabrielbefore adoptionca95612
line 299why, one step back299 "opens with the brief's title, then the banner naming its project's llms.txt",Merge build/1108 into mainoscabrielbefore adoption8d9ece9
lines 300–307why, one step back300 () =>301 Effect.gen(function* () {302 const seen = yield* read(303 askMarkdownResponse(Exit.succeed({ brief }), "public", index)304 );305306 expect(seen.status).toBe(200);307 expect(seen.body).toBe(served);Merge fix/banner-below-title into mainoscabrielbefore adoptionca95612
lines 308–309why, one step back308 expect(seen.body.startsWith("# ")).toBeTruthy();309 expect(seen.body.split("\n")[2]).toContain(Merge build/1108 into mainoscabrielbefore adoption8d9ece9
lines 310–327why, one step back310 `${WEB}/${project}/llms.txt`311 );312 })313 );314315 it.effect("keeps a refusal's Markdown as it is, with no banner", () =>316 Effect.gen(function* () {317 const refusal = new UnknownAsk({ ask: AskId.make("no-such-ask") });318319 const seen = yield* read(320 askMarkdownResponse(Exit.fail(refusal), "public", index)321 );322323 expect([seen.status, seen.body]).toStrictEqual([324 404,325 refusalsMarkdown([refusal]),326 ]);327 })Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 328–333why, one step back328 );329});330331describe(refusalsMarkdown, () => {332 it("sets every refusal's explain lines in one text block, then each distinct next command once", () => {333 const refusals = [Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 334why, one step back334 new UnknownProject({ project: ProjectName.make("oscabriel/nowhere") }),Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 335–351why, one step back335 new UnknownAsk({ ask: askId }),336 ] as const;337338 expect(refusalsMarkdown(refusals)).toBe(339 [340 "```text",341 ...explain(refusals),342 "```",343 "",344 `Next: \`${Brand.cli} asks\``,345 "",346 ].join("\n")347 );348 });349});350351const accepting = (accept: string | undefined) =>Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 352why, one step back352 wantsMarkdown(get("/oscabriel/datekit/asks/parse-iso-week", accept));Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 353–378why, one step back353354describe(wantsMarkdown, () => {355 it.each([356 ["text/markdown"],357 ["text/markdown, text/html"],358 ["text/html;q=0.9, text/markdown"],359 ["text/markdown;q=0.8, */*;q=0.1"],360 ["Text/Markdown; charset=utf-8"],361 ])("serves Markdown to %o", (accept) => {362 expect(accepting(accept)).toBeTruthy();363 });364365 it.each([366 [undefined],367 ["*/*"],368 ["text/*"],369 ["text/html"],370 [BROWSER],371 ["text/markdown;q=0.5, text/html"],372 ["text/markdown;q=0, text/html;q=0.1"],373 ["text/markdown;q=0"],374 ["application/json"],375 ])("serves the page to %o", (accept) => {376 expect(accepting(accept)).toBeFalsy();377 });378});