返回 CodeWhale
llms-txt.test.ts
根目录 / web / lib / llms-txt.test.ts
1 import { readFileSync } from "node:fs";
2 import { describe, expect, it } from "vitest";
3 import { GET } from "../app/llms.txt/route";
4 import { DOC_TOPICS, docTopicHref } from "./docs-map";
5 import { DISCORD_URL, REPO_URL } from "./i18n/links";
6 import { IDENTITY_PHRASE, SITE_NAME, SITE_URL } from "./page-meta";
7 import { buildLlmsTxt } from "./llms-txt";
8 import robots from "../app/robots";
9
10 describe("llms.txt", () => {
11 it("indexes first-party docs topics plus the remaining sitemap pages", () => {
12 const body = buildLlmsTxt();
13
14 expect(body.startsWith(`# ${SITE_NAME}`)).toBe(true);
15 expect(body).toContain(`> ${IDENTITY_PHRASE}`);
16 expect(body).toContain(REPO_URL);
17 expect(body).toContain(DISCORD_URL);
18
19 for (const topic of DOC_TOPICS) {
20 if (topic.hasPage) {
21 expect(body, topic.id).toContain(`${SITE_URL}${docTopicHref(topic, "en")}`);
22 expect(body, topic.id).toContain(topic.description.en);
23 } else {
24 expect(body, topic.id).toContain(topic.label.en);
25 }
26 }
27
28 for (const path of [
29 "/en/faq",
30 "/en/runtime",
31 "/en/constitution",
32 "/en/roadmap",
33 "/en/feed",
34 "/en/digest",
35 "/en/community",
36 "/en/contribute",
37 "/en/docs",
38 "/en/legal/terms",
39 "/en/legal/privacy",
40 ]) {
41 expect(body, path).toContain(`${SITE_URL}${path}`);
42 }
43 expect(body).not.toContain("/pricing");
44 });
45
46 it("serves the generated body as text/plain from the well-known route", async () => {
47 const response = GET();
48 expect(response.headers.get("content-type")).toBe("text/plain; charset=utf-8");
49 expect(await response.text()).toBe(buildLlmsTxt());
50 });
51
52 it("advertises /llms.txt from robots without inventing a robots field", () => {
53 const spec = robots();
54 expect(spec.rules).toEqual([
55 {
56 userAgent: "*",
57 allow: ["/", "/llms.txt"],
58 disallow: ["/api/", "/*/admin"],
59 },
60 ]);
61 expect(spec.sitemap).toBe(`${SITE_URL}/sitemap.xml`);
62
63 const route = readFileSync(new URL("../app/llms.txt/route.ts", import.meta.url), "utf8");
64 expect(route).toContain("buildLlmsTxt()");
65 });
66 });
67
67 lines TYPESCRIPT