| 1 | import { readFileSync } from "node:fs"; |
| 2 | import { afterEach, describe, expect, it } from "vitest"; |
| 3 | import { GET as getTopicMarkdown, generateStaticParams } from "../app/docs-markdown/[topic]/route"; |
| 4 | import { GET as getFullText } from "../app/llms-full.txt/route"; |
| 5 | import { checkReleaseDocs, refreshReleaseDocs, releaseDocsIndex } from "../scripts/release-docs-lib.mjs"; |
| 6 | import { AGENT_DOCS } from "./agent-docs"; |
| 7 | import { RELEASE_DOCS, buildTopicMarkdown } from "./release-docs"; |
| 8 | import { DOC_TOPICS } from "./docs-map"; |
| 9 | import { FACTS } from "./facts.generated"; |
| 10 | import { REPO_URL } from "./i18n/links"; |
| 11 | |
| 12 | const release = FACTS.latestPublishedRelease!; |
| 13 | const SNAPSHOT: typeof RELEASE_DOCS = JSON.parse(readFileSync(new URL("../data/agent-docs.release.json", import.meta.url), "utf8")); |
| 14 | const SERVED = SNAPSHOT.topics.filter((topic) => topic.slug !== "install"); |
| 15 | const releasedText = SNAPSHOT.topics.flatMap((topic) => topic.sources.map((source) => source.content)).join("\n"); |
| 16 | const mainOnly = [...new Set(AGENT_DOCS.filter((topic) => topic.slug !== "install").flatMap((topic) => topic.sources.flatMap((source) => source.content.split("\n"))))] |
| 17 | .filter((line) => line.trim().length >= 40 && !releasedText.includes(line)); |
| 18 | |
| 19 | async function topicBody(slug: string): Promise<string> { |
| 20 | const topic = `${slug}.md`; |
| 21 | const response = await getTopicMarkdown(new Request(`https://example.test/docs-markdown/${topic}`), { params: Promise.resolve({ topic }) }); |
| 22 | expect(response.status, slug).toBe(200); |
| 23 | return response.text(); |
| 24 | } |
| 25 | |
| 26 | describe("documents at the released tag", () => { |
| 27 | it("is a snapshot of the released version and nothing else", () => { |
| 28 | expect(SNAPSHOT.tag).toBe(release.tag); |
| 29 | expect(SNAPSHOT.version).toBe(release.version); |
| 30 | expect(RELEASE_DOCS).toEqual(SNAPSHOT); |
| 31 | }); |
| 32 | |
| 33 | it("serves the snapshot's documents in the full text and every topic", async () => { |
| 34 | const full = await getFullText().text(); |
| 35 | expect(generateStaticParams()).toEqual(SNAPSHOT.topics.map((topic) => ({ topic: `${topic.slug}.md` }))); |
| 36 | expect(SERVED.length).toBeGreaterThan(0); |
| 37 | for (const topic of SERVED) { |
| 38 | const body = await topicBody(topic.slug); |
| 39 | expect(topic.sources.length, topic.slug).toBeGreaterThan(0); |
| 40 | for (const source of topic.sources) { |
| 41 | expect(body.includes(source.content), `${topic.slug} ${source.path}`).toBe(true); |
| 42 | expect(full.includes(source.content), `full ${source.path}`).toBe(true); |
| 43 | } |
| 44 | } |
| 45 | }); |
| 46 | |
| 47 | it("serves no line that exists only in the documents on main", async () => { |
| 48 | const full = await getFullText().text(); |
| 49 | for (const line of mainOnly) expect(full.includes(line), line).toBe(false); |
| 50 | for (const topic of SERVED) { |
| 51 | const body = await topicBody(topic.slug); |
| 52 | for (const line of mainOnly) expect(body.includes(line), `${topic.slug} ${line}`).toBe(false); |
| 53 | } |
| 54 | }); |
| 55 | |
| 56 | it("links each source at the tag and never at main", () => { |
| 57 | const body = buildTopicMarkdown("mcp")!; |
| 58 | expect(body).toContain(`Source: ${REPO_URL}/blob/${release.tag}/docs/MCP.md`); |
| 59 | expect(body).not.toContain("Source: " + REPO_URL + "/blob/main/"); |
| 60 | }); |
| 61 | |
| 62 | describe("with a registered document the tag does not have", () => { |
| 63 | const topic = RELEASE_DOCS.topics.find((entry) => entry.slug === "mcp")!; |
| 64 | const saved = [...topic.missing]; |
| 65 | afterEach(() => { |
| 66 | topic.missing.splice(0, topic.missing.length, ...saved); |
| 67 | }); |
| 68 | |
| 69 | it("says plainly that it is not part of the release", () => { |
| 70 | const path = "docs/ADDED_AFTER_THE_TAG.md"; |
| 71 | expect(buildTopicMarkdown("mcp")).not.toContain(path); |
| 72 | (topic.missing as string[]).push(path); |
| 73 | expect(buildTopicMarkdown("mcp")).toContain(`${path} was added after ${release.tag} and is not part of this release`); |
| 74 | }); |
| 75 | }); |
| 76 | |
| 77 | it("answers 404 for a topic that is not in the snapshot", async () => { |
| 78 | const response = await getTopicMarkdown(new Request("https://example.test/docs-markdown/nope.md"), { params: Promise.resolve({ topic: "nope.md" }) }); |
| 79 | expect(response.status).toBe(404); |
| 80 | }); |
| 81 | |
| 82 | it("fails its check on a tag mismatch and passes on the released tag", () => { |
| 83 | const snapshot = JSON.parse(JSON.stringify(RELEASE_DOCS)); |
| 84 | expect(checkReleaseDocs(snapshot, release, DOC_TOPICS, snapshot)).toEqual([]); |
| 85 | const next = { tag: "v99.0.0", version: "99.0.0" }; |
| 86 | const problems = checkReleaseDocs(snapshot, next, DOC_TOPICS, null); |
| 87 | expect(problems.some((line) => line.includes(`is for ${release.tag}`) && line.includes("v99.0.0"))).toBe(true); |
| 88 | const mismatched = checkReleaseDocs({ ...snapshot, tag: "v99.0.0" }, release, DOC_TOPICS, null, undefined, { allowShallow: true }); |
| 89 | expect(mismatched).toEqual([expect.stringContaining(`is for v99.0.0, the published release is ${release.tag}`)]); |
| 90 | expect(checkReleaseDocs(null, release, DOC_TOPICS, null).length).toBeGreaterThan(0); |
| 91 | }); |
| 92 | |
| 93 | it("fails closed when the release tag is unavailable unless allowShallow is explicitly opted in", () => { |
| 94 | const snapshot = JSON.parse(JSON.stringify(RELEASE_DOCS)); |
| 95 | expect(checkReleaseDocs(snapshot, release, DOC_TOPICS, null).join("\n")).toContain("could not be verified"); |
| 96 | expect(checkReleaseDocs(snapshot, release, DOC_TOPICS, null, undefined, { allowShallow: true })).toEqual([]); |
| 97 | }); |
| 98 | |
| 99 | it("fails its check when a document is altered or a topic is dropped", () => { |
| 100 | const altered = JSON.parse(JSON.stringify(RELEASE_DOCS)); |
| 101 | altered.topics[0].sources[0].content += "\nedited"; |
| 102 | expect(checkReleaseDocs(altered, release, DOC_TOPICS, null).join("\n")).toContain("hash"); |
| 103 | const dropped = JSON.parse(JSON.stringify(RELEASE_DOCS)); |
| 104 | dropped.topics.pop(); |
| 105 | expect(checkReleaseDocs(dropped, release, DOC_TOPICS, null).join("\n")).toContain("neither in the snapshot"); |
| 106 | }); |
| 107 | |
| 108 | it("fails its check when the topic index disagrees with the snapshot", () => { |
| 109 | expect(checkReleaseDocs(RELEASE_DOCS, release, DOC_TOPICS, RELEASE_DOCS, releaseDocsIndex(RELEASE_DOCS))).toEqual([]); |
| 110 | const stale = { ...releaseDocsIndex(RELEASE_DOCS), slugs: [] }; |
| 111 | expect(checkReleaseDocs(RELEASE_DOCS, release, DOC_TOPICS, RELEASE_DOCS, stale).join("\n")).toContain("release-index"); |
| 112 | }); |
| 113 | |
| 114 | it("refuses to generate, and writes nothing, when the tag is missing and the snapshot is for another release", () => { |
| 115 | expect(() => refreshReleaseDocs(new URL("..", import.meta.url).pathname, { tag: "v9.9.9", version: "9.9.9" })).toThrow(/v9\.9\.9/); |
| 116 | expect(() => refreshReleaseDocs(new URL("..", import.meta.url).pathname, null)).not.toThrow(); |
| 117 | }); |
| 118 | }); |
| 119 |