返回 CodeWhale
llms-txt.test.ts
根目录 / web / lib / llms-txt.test.ts
1 import { readFileSync } from "node:fs";
2 import { renderToStaticMarkup } from "react-dom/server";
3 import { describe, expect, it } from "vitest";
4 import InstallPage from "../app/[locale]/install/page";
5 import { GET as getTopicMarkdown } from "../app/docs-markdown/[topic]/route";
6 import { GET as getFullText } from "../app/llms-full.txt/route";
7 import { GET } from "../app/llms.txt/route";
8 import robots from "../app/robots";
9 import { RELEASE_DOCS } from "./release-docs";
10 import { claimAvailability, runtimeCapabilityContext } from "./capabilities";
11 import { INSTALL_COMMANDS, INSTALL_COPY, INSTALL_PAGE_COMMANDS } from "./content/install";
12 import { PRODUCT_MAP_STATE_KEY, PRODUCT_MAP_TEXT_KEYS, productMap } from "./content/product-map";
13 import { REFERENCE_GUIDE_COPY } from "./content/reference-guides";
14 import type { DesktopReleaseFact } from "./desktop-release";
15 import { DOC_TOPICS, docTopicHref } from "./docs-map";
16 import type { RepoFacts } from "./facts";
17 import { FACTS } from "./facts.generated";
18 import { EN_HOME, fill } from "./i18n/dictionaries";
19 import { DISCORD_URL, REPO_URL } from "./i18n/links";
20 import { buildInstallMarkdown, buildLlmsTxt } from "./llms-txt";
21 import { SITE_NAME, SITE_URL } from "./page-meta";
22 import { REDIRECTED_ROUTES } from "./route-redirects";
23 import { extractHtmlText } from "./test-html-text";
24
25 const release = FACTS.latestPublishedRelease!;
26
27 const desktopRelease: DesktopReleaseFact = {
28 tag: "v1.2.3",
29 version: "1.2.3",
30 publishedAt: "2026-12-01T10:00:00Z",
31 readAt: "2026-12-01T11:00:00Z",
32 url: "https://github.com/codewhale-hq/codewhale-app/releases/tag/v1.2.3",
33 assets: [
34 {
35 name: "Codewhale-1.2.3-arm64.dmg",
36 platform: "macos",
37 size: 123_456_789,
38 sha256: "a".repeat(64),
39 signed: true,
40 notarized: true,
41 url: "https://github.com/codewhale-hq/codewhale-app/releases/download/v1.2.3/Codewhale-1.2.3-arm64.dmg",
42 },
43 ],
44 };
45
46 const PRICED = /[$€£¥¥]\s?\d|\b\d[\d,.]*\s?(?:USD|CNY|RMB|EUR|GBP)\b|\bper (?:month|seat|user|year)\b|\b(?:Free|Pro|Plus|Team|Teams|Max|Starter|Premium|Business|Enterprise|Individual)\s+(?:plan|tier|subscription)s?\b|\bpricing\b|\bbilled\b|\bfree trial\b|\ballowances?\b/i;
47 const DATED = /\b\d{4}-\d{2}-\d{2}\b|\b(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*\.? \d{1,2}\b|\b(?:19|20)\d{2}\b/;
48
49 const mapLine = (id: keyof typeof PRODUCT_MAP_TEXT_KEYS, state: keyof typeof PRODUCT_MAP_STATE_KEY) =>
50 `- ${EN_HOME[PRODUCT_MAP_TEXT_KEYS[id].name]}: ${EN_HOME[PRODUCT_MAP_STATE_KEY[state]]}.`;
51
52 const withRelease = (version: string): RepoFacts => ({
53 ...FACTS,
54 latestPublishedRelease: { ...release, tag: `v${version}`, version, url: release.url.replace(release.tag, `v${version}`) },
55 });
56
57 describe("llms.txt", () => {
58 it("opens with the locked headline and subhead and indexes the docs topics and the pages the sitemap keeps", () => {
59 const body = buildLlmsTxt();
60
61 expect(body.startsWith(`# ${SITE_NAME}\n\n> ${EN_HOME.heroTitle}\n\n${fill(EN_HOME.heroIntro, { brand: SITE_NAME })}\n`)).toBe(true);
62 expect(body).toContain(REPO_URL);
63 expect(body).toContain(DISCORD_URL);
64
65 for (const topic of DOC_TOPICS) {
66 if (topic.hasPage) {
67 expect(body, topic.id).toContain(`${SITE_URL}${docTopicHref(topic, "en")}`);
68 expect(body, topic.id).toContain(topic.id === "install" ? INSTALL_COPY.metaDescription.en : topic.description.en);
69 } else {
70 expect(body, topic.id).toContain(topic.label.en);
71 }
72 }
73
74 for (const path of ["/en/install", "/en/docs/guide", "/en/models", "/en/changelog", "/en/app", "/en/faq", "/en/computer-use", "/en/contribute", "/en/docs", "/en/ratatui", "/en/legal/terms", "/en/legal/privacy"]) {
75 expect(body, path).toContain(`${SITE_URL}${path}`);
76 }
77 const linked = [...body.matchAll(/https:\/\/codewhale\.net\/en\/([a-z-]+)/g)].map((match) => match[1]);
78 expect(linked.length).toBeGreaterThan(DOC_TOPICS.length);
79 for (const segment of linked) expect(REDIRECTED_ROUTES, segment).not.toContain(segment);
80 });
81
82 it("names the published release and never the source version", () => {
83 const body = buildLlmsTxt();
84 expect(body).toContain(`The latest release is v${release.version}.`);
85 expect(body).toContain(`codewhale ${release.version}`);
86 if (FACTS.version && FACTS.version !== release.version) expect(body).not.toContain(FACTS.version);
87
88 const other = buildLlmsTxt({ facts: withRelease("9.8.7") });
89 expect(other).toContain("The latest release is v9.8.7.");
90 expect(other).toContain("codewhale 9.8.7");
91 expect(other).not.toContain(release.version);
92 });
93
94 it("gives every install command and the same steps as the install page", async () => {
95 const html = renderToStaticMarkup(await InstallPage({ params: Promise.resolve({ locale: "en" }) }));
96 const page = extractHtmlText(html, { tagReplacement: "", decodeEntities: true });
97 const stated = [
98 ...Object.values(INSTALL_COPY).map((copy) => fill(copy.en, { version: release.version }).replaceAll("`", "")),
99 ...Object.values(INSTALL_COMMANDS),
100 ...Object.values(INSTALL_PAGE_COMMANDS),
101 ].filter((text) => page.includes(text));
102 expect(stated.length).toBeGreaterThan(Object.keys(INSTALL_COPY).length / 2);
103
104 for (const text of [buildLlmsTxt(), buildInstallMarkdown()]) {
105 const plain = text.replaceAll("`", "");
106 for (const statement of stated) expect(plain, statement).toContain(statement);
107 for (const command of Object.values(INSTALL_COMMANDS)) expect(text, command).toContain(`\`${command}\``);
108 const uninstallReleased = claimAvailability({ feature: "unix-uninstall" }, runtimeCapabilityContext(FACTS)) === "released";
109 expect(text.includes(INSTALL_PAGE_COMMANDS.uninstall)).toBe(uninstallReleased);
110 }
111 });
112
113 it("states each part of the product with its computed state, and a desktop release flips only the desktop line", () => {
114 const body = buildLlmsTxt({ surface: { desktopRelease: null } });
115 const rows = productMap({ surface: { desktopRelease: null }, capabilities: runtimeCapabilityContext(FACTS) });
116 expect(rows).toHaveLength(6);
117 for (const row of rows) {
118 expect(body, row.id).toContain(mapLine(row.id, row.state));
119 expect(body.includes(EN_HOME[PRODUCT_MAP_TEXT_KEYS[row.id].sentence]), row.id).toBe(row.sentenceHolds);
120 }
121 expect(body).toContain(`${mapLine("desktop", "developing")} ${EN_HOME.mapDesktopSentence}`);
122
123 const flipped = buildLlmsTxt({ surface: { desktopRelease } });
124 expect(flipped).toContain(mapLine("desktop", "released"));
125 expect(flipped).not.toContain(mapLine("desktop", "developing"));
126 expect(flipped).not.toContain(EN_HOME.mapDesktopSentence);
127 for (const row of rows.filter((entry) => entry.id !== "desktop")) expect(flipped, row.id).toContain(mapLine(row.id, row.state));
128 });
129
130 it("carries no price, plan, date outside the provenance stamp, or retired headline", () => {
131 for (const text of [buildLlmsTxt(), buildInstallMarkdown()]) {
132 const lines = text.split("\n");
133 const stamp = lines.filter((line) => /^(?:- )?Facts generated /.test(line));
134 expect(stamp).toHaveLength(1);
135 expect(stamp[0]).toContain(FACTS.generatedAt.slice(0, 10));
136
137 const rest = lines.filter((line) => line !== stamp[0]).join("\n");
138 expect(rest.match(PRICED)?.[0]).toBeUndefined();
139 expect(rest.match(DATED)?.[0]).toBeUndefined();
140 expect(rest).not.toContain("agent you can read");
141 }
142 });
143
144 it("says unknown, or leaves the line out, when the release or a count is unknown", () => {
145 const unreadable: RepoFacts = { ...FACTS, latestPublishedRelease: null };
146 const body = buildLlmsTxt({ facts: unreadable });
147 expect(body).toContain("The latest release is unknown");
148 expect(body).not.toContain("Built-in provider routes");
149 expect(body).not.toContain(mapLine("manyAgents", "released"));
150
151 for (const text of [body, buildInstallMarkdown({ facts: unreadable })]) {
152 expect(text).toContain(INSTALL_COPY.verifyBodyUnknown.en);
153 expect(text).toContain(INSTALL_COPY.removeFilesOnlyUnknown.en);
154 expect(text).not.toContain(INSTALL_PAGE_COMMANDS.uninstall);
155 expect(text.match(/\d+\.\d+\.\d+|\{\w+\}|undefined|\bnull\b|NaN/)?.[0]).toBeUndefined();
156 }
157
158 const untagged = buildLlmsTxt({ facts: withRelease("9.8.7") });
159 expect(untagged).not.toContain("Built-in provider routes");
160 expect(untagged).not.toMatch(/\(0\)/);
161 });
162
163 it("serves the generated body as text/plain from the well-known route", async () => {
164 const response = GET();
165 expect(response.headers.get("content-type")).toBe("text/plain; charset=utf-8");
166 expect(await response.text()).toBe(buildLlmsTxt());
167 });
168
169 it("serves the install steps as markdown, alone and inside the full text", async () => {
170 const install = await getTopicMarkdown(new Request(`${SITE_URL}/docs-markdown/install.md`), { params: Promise.resolve({ topic: "install.md" }) });
171 expect(install.headers.get("content-type")).toBe("text/markdown; charset=utf-8");
172 expect(await install.text()).toBe(buildInstallMarkdown());
173
174 const full = await getFullText().text();
175 expect(full.startsWith(buildLlmsTxt())).toBe(true);
176 expect(full).toContain(buildInstallMarkdown());
177 });
178
179 it("serves the documents as they were at the published release tag", async () => {
180 const full = await getFullText().text();
181 const label = ` at the published release ${release.tag}.`;
182 expect(RELEASE_DOCS.tag).toBe(release.tag);
183 expect(full.split(label).length - 1).toBe(RELEASE_DOCS.topics.length - 1);
184 expect(full).not.toContain(REFERENCE_GUIDE_COPY.scope.en.slice(0, 40));
185 expect(full).not.toContain(`Source: ${REPO_URL}/blob/main/`);
186 });
187
188 it("advertises /llms.txt from robots without inventing a robots field", () => {
189 const spec = robots();
190 expect(spec.rules).toEqual([
191 {
192 userAgent: "*",
193 allow: ["/", "/llms.txt", "/llms-full.txt", "/features.json", "/docs-markdown/"],
194 disallow: ["/api/", "/*/admin"],
195 },
196 {
197 userAgent: ["GPTBot", "OAI-SearchBot", "ChatGPT-User", "ClaudeBot", "Claude-SearchBot", "Claude-User", "PerplexityBot", "Perplexity-User", "Google-Extended"],
198 allow: "/",
199 disallow: ["/api/", "/*/admin"],
200 },
201 ]);
202 expect(spec.sitemap).toBe(`${SITE_URL}/sitemap.xml`);
203
204 const route = readFileSync(new URL("../app/llms.txt/route.ts", import.meta.url), "utf8");
205 expect(route).toContain("buildLlmsTxt()");
206 });
207 });
208
208 lines TYPESCRIPT