docs.test.tsx
This file
Path
apps/web/test/docs.test.tsx
389 lines · 11.9 KiB · TypeScript
Last changed by
Merge redesign into mainoscabrielbefore adoptioncaedd21
History of this fileTake it
Merge build/1313-docs into mainoscabrielbefore adoption9000f4e
line 1why, one step back1import { describe, expect, it } from "@effect/vitest";Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 2–3why, one step back2import { agentEndpoints, agentLimits } from "@repo/agent-kit/agent-setup";3import { commandDocs } from "@repo/agent-kit/command-doc";Merge build/1313-docs into mainoscabrielbefore adoption9000f4e
line 4why, one step back4import { docPage, docPages } from "@repo/agent-kit/docs/index";Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 5–6why, one step back5import { Brand } from "@repo/brand";6import { Contracts } from "@repo/protocol/contracts";Merge build/1313-docs into mainoscabrielbefore adoption9000f4e
lines 7–14why, one step back7import { Effect, Option } from "effect";89import { commands } from "../src/features/agents/catalog.ts";10import {11 DocsIndexView,12 DocsPageView,13} from "../src/features/docs/docs-view.tsx";14import { docsHead } from "../src/features/docs/head.ts";Merge redesign into mainoscabrielbefore adoptioncaedd21
line 15why, one step back15import { AppShell } from "../src/features/shared/shell/app-shell.tsx";Merge build/1313-docs into mainoscabrielbefore adoption9000f4e
line 16why, one step back16import { docsMarkdownHandler } from "../src/routes/docs/{$page}[.]md.ts";Merge redesign into mainoscabrielbefore adoptioncaedd21
line 17why, one step back17import { attributeOf, tagsWith, textIn } from "./markup.ts";Merge build/1313-docs into mainoscabrielbefore adoption9000f4e
lines 18–31why, one step back18import { renderAt } from "./render.tsx";1920const input = {21 commands,22 platformUrl: "https://platform-test.example.workers.dev",23 webUrl: "https://web-test.example.workers.dev",24};2526const pages = docPages(input);2728const textOf = (markup: string) =>29 markup30 .replaceAll(/<[^>]+>/gu, " ")31 .replaceAll("'", "'")Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 32–34why, one step back32 .replaceAll("<", "<")33 .replaceAll(">", ">")34 .replaceAll(""", '"')Merge build/1313-docs into mainoscabrielbefore adoption9000f4e
lines 35–40why, one step back35 .replaceAll("&", "&")36 .replaceAll(/\s+/gu, " ")37 .trim();3839const pageNamed = (slug: string) => Option.getOrThrow(docPage(input, slug));40Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 41–81why, one step back41/** The first element of `tag` carrying the attribute, whole; none of these nest in themselves. */42const elementOf = (markup: string, tag: string, attribute: string) =>43 new RegExp(`<${tag}\\b[^>]*${attribute}[^>]*>[\\s\\S]*?</${tag}>`, "u").exec(44 markup45 )?.[0] ?? "";4647const linksOf = (markup: string) =>48 [...markup.matchAll(/<a\b(?<attributes>[^>]*)>(?<text>[\s\S]*?)<\/a>/gu)].map(49 ({ groups }) => {50 const attributes = groups?.attributes ?? "";5152 return {53 current: attributes.includes('aria-current="page"'),54 href: /href="(?<href>[^"]*)"/u.exec(attributes)?.groups?.href ?? "",55 text: textOf(groups?.text ?? ""),56 };57 }58 );5960/** The article's own `h2`s, as `[id, text]`. */61const headingsOf = (markup: string) =>62 [63 ...elementOf(markup, "article", "data-doc").matchAll(64 /<h2\b[^>]*id="(?<id>[^"]*)"[^>]*>(?<text>[\s\S]*?)<\/h2>/gu65 ),66 ].map(({ groups }) => [groups?.id ?? "", textOf(groups?.text ?? "")]);6768/** "On this page": its in-page links, as `[id, text]`. */69const contentsOf = (markup: string) =>70 linksOf(elementOf(markup, "aside", 'aria-label="On this page"')).flatMap(71 ({ href, text }) => (href.startsWith("#") ? [[href.slice(1), text]] : [])72 );7374const inDocsShell = (slug: string) =>75 renderAt(76 `/docs/${slug}`,77 <AppShell>78 <DocsPageView page={pageNamed(slug)} />79 </AppShell>80 );81Merge build/1313-docs into mainoscabrielbefore adoption9000f4e
lines 82–105why, one step back82describe("the docs' index", () => {83 it.effect(84 "lists every page under Guides then Reference, each linking its page with its summary",85 () =>86 Effect.gen(function* () {87 const markup = yield* renderAt(88 "/docs",89 <DocsIndexView pages={pages} />90 );9192 const text = textOf(markup);9394 expect(text.indexOf("Guides")).toBeLessThan(text.indexOf("Reference"));9596 for (const { slug, summary, title } of pages) {97 expect(markup).toMatch(98 new RegExp(`<a\\b[^>]*href="/docs/${slug}"[^>]*>${title}</a>`, "u")99 );100 expect(text).toContain(summary);101 }102 })103 );104});105Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 106–194why, one step back106describe("a page of the docs in the docs shell", () => {107 it.effect(108 "marks itself current in the tree, and offers its Markdown and its headings beside the article",109 () =>110 Effect.gen(function* () {111 const markup = yield* inDocsShell("landing");112 const aside = elementOf(markup, "aside", 'aria-label="On this page"');113114 expect(115 linksOf(elementOf(markup, "nav", 'aria-label="Docs"'))116 .filter(({ current }) => current)117 .map(({ text }) => text)118 ).toStrictEqual(["How a landing happens"]);119 expect(aside).toMatch(120 /<button\b[^>]*>[\s\S]*?Copy as Markdown[\s\S]*?<\/button>/u121 );122 expect(linksOf(aside)).toContainEqual({123 current: false,124 href: "/docs/landing.md",125 text: "View as .md",126 });127 expect(contentsOf(markup)).toStrictEqual(headingsOf(markup));128 })129 );130});131132/** The classes on the docs tree's wrapper. */133const treeClasses = (tag: string) =>134 (attributeOf(tag, "class") ?? "").split(" ");135136describe("the docs below `columns`", () => {137 it.effect(138 "fold a page's headings into a disclosure ahead of its body, linking what On this page links",139 () =>140 Effect.gen(function* () {141 const markup = yield* inDocsShell("start-here");142 const ahead = elementOf(markup, "details", 'class="');143 const article = elementOf(markup, "article", "data-doc");144145 expect(ahead).toContain("columns:hidden");146 expect(147 linksOf(ahead).map(({ href, text }) => [href.slice(1), text])148 ).toStrictEqual(contentsOf(markup));149 expect(article.indexOf("<details")).toBeLessThan(150 article.indexOf("<h2")151 );152 })153 );154155 // The index already lists every page, so the tree under it would say it twice.156 it.effect("show no tree under the index, and the tree under a page", () =>157 Effect.gen(function* () {158 const index = tagsWith(159 yield* renderAt(160 "/docs",161 <AppShell>162 <DocsIndexView pages={pages} />163 </AppShell>164 ),165 "data-docs-tree"166 );167168 const page = tagsWith(yield* inDocsShell("landing"), "data-docs-tree");169170 expect(171 ["hidden", "columns:block"].filter(172 (name) => !index.flatMap(treeClasses).includes(name)173 )174 ).toStrictEqual([]);175 expect(page.flatMap(treeClasses)).not.toContain("hidden");176 })177 );178});179180describe("the docs' links to setup", () => {181 it("send a reader to /install, never the old /agents", () => {182 expect(183 pages.flatMap(({ markdown, slug }) =>184 /\]\(\/agents[)#]/u.test(markdown) ? [slug] : []185 )186 ).toStrictEqual([]);187 expect(188 ["connect-an-agent", "mcp"].filter(189 (slug) => !pageNamed(slug).markdown.includes("](/install)")190 )191 ).toStrictEqual([]);192 });193});194Merge build/1313-docs into mainoscabrielbefore adoption9000f4e
lines 195–196why, one step back195describe("a page of the docs", () => {196 it.effect(Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 197–231why, one step back197 "lists its article's h2s under On this page, in order, each linking its heading's id",198 () =>199 Effect.gen(function* () {200 const mismatched: string[] = [];201202 for (const page of pages) {203 const markup = yield* renderAt(204 `/docs/${page.slug}`,205 <DocsPageView page={page} />206 );207208 if (209 JSON.stringify(contentsOf(markup)) !==210 JSON.stringify(headingsOf(markup))211 ) {212 mismatched.push(page.slug);213 }214 }215216 expect(mismatched).toStrictEqual([]);217 expect(218 contentsOf(219 yield* renderAt(220 "/docs/start-here",221 <DocsPageView page={pageNamed("start-here")} />222 )223 )224 ).toContainEqual([225 "how-a-change-reaches-main",226 "How a change reaches main",227 ]);228 })229 );230231 it.effect(Merge build/1313-docs into mainoscabrielbefore adoption9000f4e
lines 232–238why, one step back232 "draws its Markdown under one title, with the other pages beside it",233 () =>234 Effect.gen(function* () {235 const page = pageNamed("answer-an-ask");236237 const markup = yield* renderAt(238 "/docs/answer-an-ask",Merge redesign into mainoscabrielbefore adoptioncaedd21
line 239why, one step back239 <DocsPageView page={page} />Merge build/1313-docs into mainoscabrielbefore adoption9000f4e
lines 240–259why, one step back240 );241242 expect(markup.match(/<h1\b[^>]*>[^<]*<\/h1>/gu)).toStrictEqual([243 expect.stringMatching(/>Answer an ask<\/h1>$/u),244 ]);245 expect(markup).toMatch(/<h2\b[^>]*id="claim-it"[^>]*>Claim it<\/h2>/u);246 expect(247 [248 'href="/asks"',249 'href="/docs/refusals"',250 'href="/docs/answer-an-ask.md"',251 ].filter((href) => !markup.includes(href))252 ).toStrictEqual([]);253 expect(markup).toMatch(/aria-current="page"[^>]*>Answer an ask</u);254 expect(markup).toMatch(255 /aria-label="Breadcrumb"[\s\S]*?href="\/docs"[^>]*>Docs<[\s\S]*?>Answer an ask</u256 );257 })258 );259Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 260–300why, one step back260 it.effect(261 "lists on Commands every command the CLI has, in its order, each as its synopsis with what it does",262 () =>263 Effect.gen(function* () {264 const markup = yield* renderAt(265 "/docs/commands",266 <DocsPageView page={pageNamed("commands")} />267 );268269 const rows = [270 ...elementOf(markup, "article", "data-doc").matchAll(271 /<tr>\s*<td\b[^>]*>(?<usage>[\s\S]*?)<\/td>\s*<td\b[^>]*>(?<summary>[\s\S]*?)<\/td>/gu272 ),273 ].map(({ groups }) => ({274 summary: textIn(groups?.summary ?? ""),275 usage: textOf(groups?.usage ?? ""),276 }));277278 const expected = commandDocs(Contracts).map(279 ({ flags, path, positional, summary }) => ({280 summary,281 words: [282 Brand.cli,283 ...path,284 ...positional.map((name) => `<${name}>`),285 ...flags286 .filter((flag) => !positional.includes(flag.slice(2)))287 .map((flag) => `[${flag}]`),288 ],289 })290 );291292 expect(rows.map(({ usage }) => usage.split(" "))).toStrictEqual(293 expected.map(({ words }) => words)294 );295 expect(rows.map(({ summary }) => summary)).toStrictEqual(296 expected.map(({ summary }) => summary.replaceAll("`", ""))297 );298 })299 );300Merge build/1313-docs into mainoscabrielbefore adoption9000f4e
lines 301–304why, one step back301 it.effect("draws a reference page's tables", () =>302 Effect.gen(function* () {303 const markup = yield* renderAt(304 "/docs/refusals",Merge redesign into mainoscabrielbefore adoptioncaedd21
line 305why, one step back305 <DocsPageView page={pageNamed("refusals")} />Merge build/1313-docs into mainoscabrielbefore adoption9000f4e
lines 306–310why, one step back306 );307308 expect(markup).toContain("<table");309 expect(markup).toContain("<code>OutOfScope</code>");310 })Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 311–339why, one step back311 );312313 it.effect(314 "draws every limit and endpoint agent-kit gives on the Limits and endpoints page, each endpoint a reader can open as a link",315 () =>316 Effect.gen(function* () {317 const markup = yield* renderAt(318 "/docs/limits",319 <DocsPageView page={pageNamed("limits")} />320 );321322 const text = textOf(markup);323324 expect(markup.match(/<table/gu)).toHaveLength(2);325 expect(326 [327 ...agentLimits().flatMap(({ limit, value }) => [limit, value]),328 ...agentEndpoints(input).flatMap(({ url, what }) => [url, what]),329 ].filter((words) => !text.includes(words))330 ).toStrictEqual([]);331 expect(332 agentEndpoints(input)333 .filter(334 ({ readable, url }) =>335 markup.includes(`href="${url}"`) !== readable336 )337 .map(({ url }) => url)338 ).toStrictEqual([]);339 })Merge build/1313-docs into mainoscabrielbefore adoption9000f4e
lines 340–389why, one step back340 );341342 it("names its Markdown in its head, with its summary as its description", () => {343 const head = docsHead(pageNamed("events"));344345 expect(head.links).toContainEqual({346 href: "/docs/events.md",347 rel: "alternate",348 type: "text/markdown",349 });350 expect(head.meta).toContainEqual({351 content: pageNamed("events").summary,352 name: "description",353 });354 });355});356357const served = (page: string) =>358 Effect.gen(function* () {359 const response = yield* Effect.promise(() =>360 docsMarkdownHandler(input.platformUrl)({361 params: { page },362 request: new Request(`${input.webUrl}/docs/${page}.md`),363 })364 );365366 return {367 body: yield* Effect.promise(() => response.text()),368 contentType: response.headers.get("content-type"),369 status: response.status,370 };371 });372373describe("a page of the docs as Markdown", () => {374 it.effect("is the page's text", () =>375 Effect.gen(function* () {376 expect(yield* served("commands")).toStrictEqual({377 body: pageNamed("commands").markdown,378 contentType: "text/markdown; charset=utf-8",379 status: 200,380 });381 })382 );383384 it.effect("is a 404 for a page that does not exist", () =>385 Effect.gen(function* () {386 expect((yield* served("nope")).status).toBe(404);387 })388 );389});