| 1 | import { readFileSync } from "node:fs"; |
| 2 | import { buildInstallGuide } from "../scripts/install-guide-lib.mjs"; |
| 3 | import { FACTS } from "./facts.generated"; |
| 4 | import { CONSTITUTION_SCENES } from "./content/constitution"; |
| 5 | import { describe, expect, it } from "vitest"; |
| 6 | import { getDocSearchSections } from "./docs-article-search"; |
| 7 | import { rankDocSections, searchDocSections } from "./doc-search-match"; |
| 8 | |
| 9 | describe("article section search", () => { |
| 10 | it("finds commands and body-only text at the rendered section in English and Chinese", () => { |
| 11 | expect(searchDocSections(getDocSearchSections("en"), "codewhale auth clear")) |
| 12 | .toEqual(expect.arrayContaining([expect.objectContaining({ href: "/docs/auth#which-key", excerpt: expect.stringContaining("codewhale auth clear") })])); |
| 13 | expect(searchDocSections(getDocSearchSections("zh"), "--api-key-stdin")) |
| 14 | .toEqual(expect.arrayContaining([expect.objectContaining({ href: "/docs/auth#save-key" })])); |
| 15 | expect(searchDocSections(getDocSearchSections("en"), "Read one source file")) |
| 16 | .toEqual(expect.arrayContaining([expect.objectContaining({ href: "/docs/guide#first-session" })])); |
| 17 | expect(searchDocSections(getDocSearchSections("en"), " ")).toEqual([]); |
| 18 | expect(searchDocSections(getDocSearchSections("en"), "no-such-command-12345")).toEqual([]); |
| 19 | expect(getDocSearchSections("en").map((section) => section.text).join(" ")).not.toContain("Source documents: docs/INSTALL.md §8, docs/PROVIDERS.md, docs/CODEWHALE_AGENT.md"); |
| 20 | }); |
| 21 | |
| 22 | it("matches a phrase across repeated whitespace in the query and the section", () => { |
| 23 | const query = "Read one source file"; |
| 24 | const href = "/docs/guide#first-session"; |
| 25 | const sections = getDocSearchSections("en"); |
| 26 | expect(searchDocSections(sections, query)).toEqual(expect.arrayContaining([expect.objectContaining({ href })])); |
| 27 | |
| 28 | const fixture = { href, pageTitle: "Guide", title: "First session", text: "Read one\n source file." }; |
| 29 | expect(searchDocSections([fixture], "Read one source file")) |
| 30 | .toEqual([{ ...fixture, excerpt: "Read one source file." }]); |
| 31 | }); |
| 32 | |
| 33 | it("aligns section excerpts after Unicode case-expanding characters", () => { |
| 34 | const prefix = "İ".repeat(260); |
| 35 | const fixture = { |
| 36 | href: "/docs/guide#tr", |
| 37 | pageTitle: "Guide", |
| 38 | title: "Kurulum", |
| 39 | text: `${prefix} hedef-kelime sonu`, |
| 40 | }; |
| 41 | const [matched] = searchDocSections([fixture], "hedef-kelime"); |
| 42 | expect(matched.excerpt).toContain("hedef-kelime"); |
| 43 | |
| 44 | const [ranked] = rankDocSections([fixture], "kurulum hedef-kelime"); |
| 45 | expect(ranked.excerpt).toContain("hedef-kelime"); |
| 46 | }); |
| 47 | |
| 48 | it("finds body-only content on every custom documentation destination at existing anchors", () => { |
| 49 | for (const locale of ["en", "zh"] as const) { |
| 50 | const sections = getDocSearchSections(locale); |
| 51 | const searches = [ |
| 52 | ["winget install --id", "/install#install-overview"], |
| 53 | ["codewhale --version", "/install#install-verify"], |
| 54 | ["DEEPSEEK_API_KEY", "/models#models-setup"], |
| 55 | ["/plugin trust <name>", "/plugins#safe-title"], |
| 56 | ["extension_host", "/plugins#reference-title"], |
| 57 | [".codewhale/constitution.json", "/constitution#constitution-rank"], |
| 58 | [CONSTITUTION_SCENES[2].trace, "/constitution#constitution-trace"], |
| 59 | ]; |
| 60 | for (const [query, href] of searches) { |
| 61 | expect(searchDocSections(sections, query), `${locale}: ${query}`) |
| 62 | .toEqual(expect.arrayContaining([expect.objectContaining({ href })])); |
| 63 | } |
| 64 | for (const model of FACTS.models) { |
| 65 | expect(searchDocSections(sections, model.id)).toEqual(expect.arrayContaining([expect.objectContaining({ href: "/models#models-title" })])); |
| 66 | } |
| 67 | for (const provider of FACTS.providers) { |
| 68 | expect(searchDocSections(sections, provider.env)).toEqual(expect.arrayContaining([expect.objectContaining({ href: "/models#providers-title" })])); |
| 69 | } |
| 70 | for (const path of ["install", "models", "plugins", "constitution"]) { |
| 71 | const source = readFileSync(new URL(`../app/[locale]/${path === "constitution" ? "docs/constitution" : path}/page.tsx`, import.meta.url), "utf8"); |
| 72 | const ids = new Set([...source.matchAll(/(?:id|titleId)="([^"]+)"/g)].map(match => match[1])); |
| 73 | const pageSections = sections.filter(section => section.href.startsWith(`/${path}#`)); |
| 74 | expect(pageSections.length).toBeGreaterThan(0); |
| 75 | for (const section of pageSections) expect(ids.has(section.href.split("#")[1]), section.href).toBe(true); |
| 76 | } |
| 77 | } |
| 78 | }); |
| 79 | |
| 80 | it("derives install search sections from the same visible Markdown and heading ids", () => { |
| 81 | const guide = buildInstallGuide('# Setup\n\n## Notes\n\n1. Run this:\n\n ```sh\n echo nested\n ```\n\n| Need | Reason |\n| --- | --- |\n| curl | Download |\n\n[Visible label](https://example.com/hidden-url)\n\n## Notes\n\nSecond body.\n'); |
| 82 | expect(guide.searchSections.map((section: { id: string }) => section.id)).toEqual(["setup", "notes", "notes-1"]); |
| 83 | expect(guide.searchSections[1].text).toContain("echo nested"); |
| 84 | expect(guide.searchSections[1].text).toContain("curl Download"); |
| 85 | expect(guide.searchSections[1].text).toContain("Visible label"); |
| 86 | expect(guide.searchSections[1].text).not.toContain("hidden-url"); |
| 87 | expect(guide.searchSections[2].text).toContain("Second body."); |
| 88 | expect(buildInstallGuide("Visible introduction.\n\n## Section\n\nBody.").preamble).toBe("Visible introduction."); |
| 89 | }); |
| 90 | |
| 91 | }); |
| 92 |