docs.test.ts
This file
Path
packages/agent-kit/test/docs.test.ts
258 lines · 7.7 KiB · TypeScript
Last changed by
Merge redesign into mainoscabrielbefore adoptioncaedd21
History of this fileTake it
Merge build/1313-docs into mainoscabrielbefore adoption9000f4e
lines 1–7why, one step back1import { describe, expect, it } from "@effect/vitest";2import { Brand } from "@repo/brand";3import { Contracts, Surfaces } from "@repo/protocol/contracts";4import type { ProjectEvent } from "@repo/protocol/domain/event";5import { groups } from "@repo/protocol/refusal";6import { Context, Option } from "effect";7Merge redesign into mainoscabrielbefore adoptioncaedd21
line 8why, one step back8import { agentEndpoints, agentLimits } from "../src/agent-setup.ts";Merge build/1313-docs into mainoscabrielbefore adoption9000f4e
lines 9–36why, one step back9import { commandDocs } from "../src/command-doc.ts";10import { importedProjects } from "../src/docs/imported.ts";11import { docEntries, docPage, docPages } from "../src/docs/index.ts";1213const commands = commandDocs(Contracts);1415const input = {16 commands,17 platformUrl: "https://platform-test.example.workers.dev",18 webUrl: "https://web-test.example.workers.dev",19};2021const pages = docPages(input);2223const markdownOf = (slug: string) =>24 Option.match(docPage(input, slug), {25 onNone: () => "",26 onSome: ({ markdown }) => markdown,27 });2829const cliSpans = (markdown: string) =>30 [31 ...[...markdown.matchAll(/`(?<span>[^`\n]+)`/gu)].map(32 (found) => found.groups?.span ?? ""33 ),34 ...markdown.split("\n"),35 ].flatMap((span) =>36 span.startsWith(`${Brand.cli} `)Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 37–43why, one step back37 ? [38 span39 .slice(Brand.cli.length + 1)40 .split(/\s+/u)41 // A synopsis writes an optional flag as `[--flag]`.42 .map((word) => word.replaceAll(/^\[|\]$/gu, "")),43 ]Merge build/1313-docs into mainoscabrielbefore adoption9000f4e
lines 44–67why, one step back44 : []45 );4647const commandOf = (words: readonly string[]) =>48 commands49 .filter(({ path }) => path.every((word, at) => words[at] === word))50 .toSorted((a, b) => b.path.length - a.path.length)[0];5152describe("the docs", () => {53 it("are five guides, then the reference, each with a title and a one-line summary", () => {54 expect(55 pages.map(({ part, slug, title }) => [part, slug, title])56 ).toStrictEqual([57 ["guide", "start-here", "Start here"],58 ["guide", "answer-an-ask", "Answer an ask"],59 ["guide", "run-a-project", "Run a project"],60 ["guide", "held-back-checks", "Held-back checks and layers"],61 ["guide", "connect-an-agent", "Connect an agent"],62 ["reference", "commands", "Commands"],63 ["reference", "refusals", "Refusals"],64 ["reference", "git", "Git here"],65 ["reference", "landing", "How a landing happens"],66 ["reference", "events", "Events"],67 ["reference", "mcp", "MCP tools"],Merge redesign into mainoscabrielbefore adoptioncaedd21
line 68why, one step back68 ["reference", "limits", "Limits and endpoints"],Merge build/1313-docs into mainoscabrielbefore adoption9000f4e
lines 69–129why, one step back69 ["reference", "imported", "Imported projects"],70 ]);7172 for (const { markdown, summary, title } of pages) {73 expect(summary).toMatch(/^[A-Z].+\.$/u);74 expect(summary).not.toContain("\n");75 expect(markdown.startsWith(`# ${title}\n\n${summary}\n`)).toBeTruthy();76 }77 });7879 it("name only commands the CLI has, with flags each one takes", () => {80 const named = pages.flatMap(({ markdown, slug }) =>81 cliSpans(markdown).map((words) => ({ slug, words }))82 );8384 expect(named.length).toBeGreaterThan(20);8586 const wrong = named.flatMap(({ slug, words }) => {87 const command = commandOf(words);8889 if (command === undefined) {90 return [`${slug}: ${words.join(" ")} is no command`];91 }9293 return words94 .filter((word) => word.startsWith("--"))95 .map((flag) => flag.split("=")[0] ?? flag)96 .filter((flag) => !command.flags.includes(flag))97 .map((flag) => `${slug}: ${command.path.join(" ")} takes no ${flag}`);98 });99100 expect(wrong).toStrictEqual([]);101 });102103 it("link only to pages that exist", () => {104 const slugs = new Set(pages.map(({ slug }) => slug));105106 const broken = pages.flatMap(({ markdown, slug }) =>107 [...markdown.matchAll(/\]\(\/docs\/(?<target>[^)#]+)/gu)].flatMap(108 (found) => {109 const target = found.groups?.target ?? "";110111 return slugs.has(target) ? [] : [`${slug} → ${target}`];112 }113 )114 );115116 expect(broken).toStrictEqual([]);117 });118119 it("are written for every entry the index and the heads name, in its order", () => {120 expect(121 pages.map(({ part, slug, summary, title }) => ({122 part,123 slug,124 summary,125 title,126 }))127 ).toStrictEqual(docEntries);128 });129Merge build/1413-docs-and-texts into mainoscabrielbefore adoptionf10338e
lines 130–145why, one step back130 it("spell a project as <owner>/<name>, never <project>", () => {131 expect(132 pages.flatMap(({ markdown, slug }) =>133 markdown.includes("<project>") ? [slug] : []134 )135 ).toStrictEqual([]);136 });137138 it("say how a new project is named, and that /import brings a repo over", () => {139 const markdown = markdownOf("run-a-project");140141 expect(markdown).toContain(`${Brand.cli} init --project <owner>/<name>`);142 expect(markdown).toContain("](/import)");143 expect(markdown).toContain("github.com/pallets/click");144 });145Merge build/1313-docs into mainoscabrielbefore adoption9000f4e
lines 146–215why, one step back146 it("find a page by its slug, and none by another", () => {147 expect(148 Option.map(docPage(input, "events"), ({ title }) => title)149 ).toStrictEqual(Option.some("Events"));150 expect(docPage(input, "nope")).toStrictEqual(Option.none());151 });152});153154describe("the reference", () => {155 it("lists every command the CLI has, with what it does", () => {156 const markdown = markdownOf("commands");157158 expect(159 commands.flatMap(({ path, summary }) =>160 markdown.includes(`${Brand.cli} ${path.join(" ")}`) &&161 markdown.includes(summary)162 ? []163 : [path.join(" ")]164 )165 ).toStrictEqual([]);166 });167168 it("lists every refusal of the closed union", () => {169 const markdown = markdownOf("refusals");170171 const tags = groups.flatMap((group) =>172 group.members.map((member) => member.identifier)173 );174175 expect(tags.length).toBeGreaterThan(20);176 expect(177 tags.filter((tag) => !markdown.includes(`\`${tag}\``))178 ).toStrictEqual([]);179 });180181 it("lists every event a watcher hears", () => {182 const markdown = markdownOf("events");183184 const tags = [185 "AskClosed",186 "AskOpened",187 "Claimed",188 "Decided",189 "Judged",190 "Landed",191 "Lapsed",192 "LedgerAppended",193 "ProposalDrafted",194 "ProposalOpened",195 "Pushed",196 "Released",197 "Warned",198 ] as const satisfies readonly ProjectEvent["_tag"][];199200 expect(201 tags.filter((tag) => !markdown.includes(`\`${tag}\``))202 ).toStrictEqual([]);203 });204205 it("lists every contract served over MCP, by its tool name", () => {206 const markdown = markdownOf("mcp");207208 const served = [...Contracts.requests.values()].flatMap(209 ({ _tag, annotations }) =>210 Context.get(annotations, Surfaces).includes("mcp") ? [_tag] : []211 );212213 expect(served).toContain("readFile");214 expect(215 served.filter((tag) => !markdown.includes(`\`${tag}\``))Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 216–240why, one step back216 ).toStrictEqual([]);217 });218219 it("names every limit and endpoint agent-kit gives, each with its value or what it is, and links each endpoint a reader can open", () => {220 const markdown = markdownOf("limits");221222 expect(agentLimits().length).toBeGreaterThan(4);223 expect(224 agentLimits()225 .filter(226 ({ limit, value }) => !markdown.includes(`| ${limit} | ${value} |`)227 )228 .map(({ limit }) => limit)229 ).toStrictEqual([]);230 expect(231 agentEndpoints(input)232 .filter(233 ({ readable, url, what }) =>234 !markdown.includes(235 readable236 ? `| [\`${url}\`](${url}) | ${what} |`237 : `| \`${url}\` | ${what} |`238 )239 )240 .map(({ url }) => url)Merge build/1313-docs into mainoscabrielbefore adoption9000f4e
lines 241–249why, one step back241 ).toStrictEqual([]);242 });243244 it("names each imported project's source and licence", () => {245 const markdown = markdownOf("imported");246247 expect(248 importedProjects249 .filter(Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
lines 250–251why, one step back250 ({ licence, name, url }) =>251 !markdown.includes(`[${name}](/${name})`) ||Merge build/1313-docs into mainoscabrielbefore adoption9000f4e
lines 252–254why, one step back252 !markdown.includes(url) ||253 !markdown.includes(licence)254 )Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 255why, one step back255 .map(({ name }) => name)Merge build/1313-docs into mainoscabrielbefore adoption9000f4e
lines 256–258why, one step back256 ).toStrictEqual([]);257 });258});