返回 CodeWhale
docs-ia.test.ts
根目录 / web / lib / docs-ia.test.ts
1 /**
2 * Information-architecture contracts: docs-map registration, sitemap and
3 * hreflang preservation, navigation parity across breakpoints and locales,
4 * and the accessibility hooks (skip link, labelled nav, aria-current).
5 *
6 * These are deterministic source/unit contracts in the same style as
7 * public-copy.test.ts: they read the real sources and assert structure, so a
8 * future IA change fails here first instead of drifting silently.
9 */
10 import { existsSync, readFileSync } from "node:fs";
11 import { describe, expect, it } from "vitest";
12 import buildSitemap from "../app/sitemap";
13 import { DOC_TOPICS, docTopicHref, getTopic } from "./docs-map";
14 import { docsTopicIsCurrent } from "./docs-navigation";
15 import { locales } from "./i18n/config";
16 import { contentLocalesForPath } from "./i18n/content-locales";
17 import { getChrome, getHome } from "./i18n/dictionaries";
18 import {
19 currentNavHref,
20 footerLegalLinks,
21 footerProductLinks,
22 footerProjectLinks,
23 navLinks as buildNavLinks,
24 secondaryNavLinks as buildSecondaryNavLinks,
25 } from "./i18n/links";
26 import { SITE_URL } from "./page-meta";
27 import { siteCss } from "./site-css";
28 import { readCatalogue } from "./ratatui/catalogue";
29
30 const webRoot = new URL("../", import.meta.url);
31 const repoRoot = new URL("../../", import.meta.url);
32
33 function webText(path: string): string {
34 return readFileSync(new URL(path, webRoot), "utf8");
35 }
36
37 const sitemapEntries = buildSitemap();
38 const nav = webText("components/nav.tsx");
39 const navLinks = webText("components/nav-links.tsx");
40 const mobileMenu = webText("components/mobile-menu.tsx");
41 const footer = webText("components/footer.tsx");
42 const localeLayout = webText("app/[locale]/layout.tsx");
43 const css = siteCss();
44
45 describe("docs-map registration", () => {
46 it("registers the guide and vocabulary topics as first-party pages", () => {
47 const guide = getTopic("guide");
48 const vocabulary = getTopic("vocabulary");
49 expect(guide?.hasPage).toBe(true);
50 expect(vocabulary?.hasPage).toBe(true);
51 expect(vocabulary?.category).toBe("reference");
52 expect(docTopicHref(guide!, "en")).toBe("/en/docs/guide");
53 expect(docTopicHref(vocabulary!, "zh")).toBe("/zh/docs/vocabulary");
54 expect(docsTopicIsCurrent(vocabulary!, "en", "/en/docs/vocabulary")).toBe(true);
55 });
56
57 it("keeps every docs topic repo source on disk", () => {
58 for (const topic of DOC_TOPICS) {
59 const sources = Array.isArray(topic.repoSource) ? topic.repoSource : [topic.repoSource];
60 for (const source of sources) {
61 expect(existsSync(new URL(source, repoRoot)), `${topic.id}: ${source}`).toBe(true);
62 }
63 }
64 });
65
66 it("keeps topic labels and descriptions bilingual", () => {
67 for (const topic of DOC_TOPICS) {
68 for (const pair of [topic.label, topic.description]) {
69 expect(pair.en.trim().length, `${topic.id} en`).toBeGreaterThan(0);
70 expect(pair.zh.trim().length, `${topic.id} zh`).toBeGreaterThan(0);
71 }
72 }
73 });
74 });
75
76 describe("sitemap and hreflang preservation", () => {
77 it("indexes every first-party docs page", () => {
78 for (const topic of DOC_TOPICS) {
79 if (!topic.hasPage) continue;
80 const path = topic.sitePath ? `/${topic.sitePath}` : `/docs/${topic.slug}`;
81 expect(
82 sitemapEntries.some((entry) => entry.url === `${SITE_URL}/en${path}`),
83 path,
84 ).toBe(true);
85 }
86 expect(
87 sitemapEntries.some((entry) => entry.url === `${SITE_URL}/en/docs/guide`),
88 ).toBe(true);
89 expect(
90 sitemapEntries.some((entry) => entry.url === `${SITE_URL}/en/docs/vocabulary`),
91 ).toBe(true);
92 });
93
94 it("keeps sitemap and hreflang output aligned with real translation coverage", () => {
95 // 18 home locales + 18 /computer-use locales + English-only install + (en, zh) for every
96 // other route (including /docs/guide, /product, /plugins, and /changelog, whose bodies
97 // ship en/zh only).
98 const existingEntries = sitemapEntries.filter(
99 (entry) => new URL(entry.url).pathname.split("/")[2] !== "ratatui",
100 );
101 expect(existingEntries).toHaveLength(107);
102 expect(sitemapEntries.filter(entry => entry.url.endsWith("/install")).map(entry => entry.url))
103 .toEqual([`${SITE_URL}/en/install`]);
104 expect(sitemapEntries.some(entry => entry.url.endsWith("/pricing"))).toBe(false);
105 for (const path of ["/product", "/plugins", "/computer-use", "/signin", "/signup", "/legal/terms", "/legal/privacy"]) {
106 expect(
107 sitemapEntries.some((entry) => entry.url === `${SITE_URL}/en${path}`),
108 path,
109 ).toBe(true);
110 }
111 for (const [path, expectedLocales] of [
112 ["/", locales],
113 ["/docs/guide", ["en", "zh"]],
114 ["/computer-use", locales],
115 ["/install", ["en"]],
116 ["/docs", ["en", "zh"]],
117 ] as const) {
118 const suffix = path === "/" ? "" : path;
119 const entry = sitemapEntries.find(
120 (candidate) => candidate.url === `${SITE_URL}/en${suffix}`,
121 );
122 expect(entry, path).toBeDefined();
123 expect(Object.keys(entry?.alternates?.languages ?? {}), path).toEqual([
124 ...expectedLocales,
125 ]);
126 }
127 expect(sitemapEntries.every((entry) => !("lastModified" in entry))).toBe(true);
128 });
129
130 it("indexes every Ratatui preview with genuine translation alternates", () => {
131 const catalogue = readCatalogue();
132 const paths = ["/ratatui", ...catalogue.entries.map((entry) => `/ratatui/${encodeURIComponent(entry.name)}`)];
133 const expectedUrls = paths.flatMap((path) =>
134 contentLocalesForPath(path).map((locale) => `${SITE_URL}/${locale}${path}`),
135 );
136 const entries = sitemapEntries.filter(
137 (entry) => new URL(entry.url).pathname.split("/")[2] === "ratatui",
138 );
139 expect(entries.map((entry) => entry.url)).toEqual(expectedUrls);
140 expect(new Set(sitemapEntries.map((entry) => entry.url)).size).toBe(sitemapEntries.length);
141 for (const entry of entries) {
142 expect(Object.keys(entry.alternates?.languages ?? {})).toEqual(["en", "zh"]);
143 }
144 });
145
146 it("keeps the new docs pages on the shared metadata helper", () => {
147 for (const route of ["guide", "vocabulary"]) {
148 const page = webText(`app/[locale]/docs/${route}/page.tsx`);
149 expect(page, route).toContain('import { buildPageMetadata } from "@/lib/page-meta"');
150 expect(page, route).toContain(`path: "/docs/${route}"`);
151 }
152 });
153
154 it("indexes public sign-in and create-account routes", () => {
155 for (const path of ["/signin", "/signup"]) {
156 expect(
157 sitemapEntries.some((entry) => entry.url === `${SITE_URL}/en${path}`),
158 path,
159 ).toBe(true);
160 expect(existsSync(new URL(`app/[locale]${path}/page.tsx`, webRoot)), path).toBe(true);
161 }
162 const entry = webText("components/public-account-entry.tsx");
163 expect(entry).toContain("<WhalePose");
164 expect(entry).toContain("installLocally");
165 expect(webText("app/[locale]/signin/page.tsx")).toContain('kind="sign-in"');
166 expect(webText("app/[locale]/signup/page.tsx")).toContain('kind="sign-up"');
167 // One identity door in the header; the sign-in page links onward to
168 // account creation.
169 expect(nav).toContain("APP_LOGIN_URL");
170 expect(nav).not.toContain("APP_SIGNUP_URL");
171 });
172 });
173
174 describe("navigation parity and accessibility", () => {
175 it("keeps desktop and mobile navigation on one shared link set", () => {
176 // Both surfaces consume the same `links` prop from nav.tsx — assert the
177 // wiring rather than duplicating the arrays.
178 expect(nav).toContain("<NavLinks links={links} primaryAria={chrome.navPrimaryAria} />");
179 expect(nav).toContain("links={links}");
180 expect(mobileMenu).toContain("[...links, ...moreLinks].map");
181 expect(navLinks).toContain("links.map");
182 // One generator feeds both surfaces — no per-locale hardcoded arrays.
183 expect(nav).toContain("navLinks(locale, chrome)");
184 expect(nav).not.toMatch(/const (EN|ZH)_LINKS/);
185 // The primary strip replaces the compact menu at lg, with icon-only
186 // controls leaving room for translated labels (see nav-hit-target.test).
187 // Wrapping is the escape valve for a translated strip that outgrows the
188 // row; the row gap stays tight so a second row does not double the
189 // sticky header's height.
190 expect(navLinks).toContain(
191 'className="hidden lg:flex min-w-0 shrink items-center gap-x-5 gap-y-1 flex-wrap"',
192 );
193 // One label per destination: no companion labels on either surface.
194 expect(navLinks).not.toContain("secondary");
195 expect(mobileMenu).not.toContain("secondary");
196 expect(mobileMenu).toContain('className="nav-icon-button lg:hidden"');
197 expect(nav).toContain("paper-install-cta hidden lg:inline-flex");
198 // A fixed descendant of the blurred sticky header uses the header as its
199 // containing block and collapses. The open sheet must live at body scope.
200 expect(mobileMenu).toContain('import { createPortal } from "react-dom"');
201 expect(mobileMenu).toContain("createPortal(<div");
202 expect(mobileMenu).toContain("document.body");
203 expect(mobileMenu).toContain("element.inert = true");
204 expect(mobileMenu).toContain('if (e.key !== "Tab") return');
205 expect(mobileMenu).toContain('window.matchMedia("(min-width: 1024px)")');
206 expect(mobileMenu).toContain("if (event.matches) closeImmediately()");
207 // Locale handlers are shared so a regional tag cannot nest
208 // (`/ja/pt-BR/...`); the theme control is site-wide, never route-gated.
209 expect(webText("components/locale-switcher.tsx")).toContain("replacePathLocale(pathname, code)");
210 expect(webText("components/theme-toggle.tsx")).not.toContain("isDocsPath");
211 expect(webText("middleware.ts")).toContain("pathLocale(pathname)");
212 });
213
214 it("keeps nav link paths in exact locale-swap parity for every routed locale", () => {
215 // The hardcoded /en/ and /zh/ arrays are gone; assert the generated set
216 // directly, across every routed locale rather than only two of them.
217 const reference = buildNavLinks("en", getChrome("en")).map((l) =>
218 l.href.replace(/^\/en\//, ""),
219 );
220 expect(reference).toEqual(["product", "models", "plugins", "docs"]);
221 const moreReference = buildSecondaryNavLinks("en", getChrome("en")).map((l) =>
222 l.href.replace(/^\/en\//, ""),
223 );
224 expect(moreReference).toEqual(["docs/guide", "install", "ratatui", "faq", "community", "contribute"]);
225 for (const locale of locales) {
226 const links = buildNavLinks(locale, getChrome(locale));
227 expect(
228 links.map((l) => l.href.replace(new RegExp(`^/${locale}/`), "")),
229 `${locale} nav routes`,
230 ).toEqual(reference);
231 const more = buildSecondaryNavLinks(locale, getChrome(locale));
232 expect(
233 more.map((l) => l.href.replace(new RegExp(`^/${locale}/`), "")),
234 `${locale} secondary nav routes`,
235 ).toEqual(moreReference);
236 for (const link of [...links, ...more]) {
237 expect(link.href.startsWith(`/${locale}/`), `${locale} ${link.href}`).toBe(true);
238 expect(link.label.trim().length, `${locale} empty nav label`).toBeGreaterThan(0);
239 }
240 }
241 });
242
243 it("keeps footer link paths in exact locale-swap parity for every routed locale", () => {
244 const reference = footerProductLinks("en", getChrome("en")).map((l) =>
245 l.href.replace(/^\/en\//, ""),
246 );
247 expect(reference).toContain("product");
248 expect(reference).toContain("docs/guide");
249 expect(reference).toContain("faq");
250 for (const locale of locales) {
251 const product = footerProductLinks(locale, getChrome(locale));
252 expect(
253 product.map((l) => l.href.replace(new RegExp(`^/${locale}/`), "")),
254 `${locale} footer product routes`,
255 ).toEqual(reference);
256 const project = footerProjectLinks(locale, getChrome(locale));
257 expect(project.map((l) => l.href), `${locale} footer project routes`).toEqual([
258 "https://github.com/codewhale-hq/CodeWhale",
259 "https://github.com/codewhale-hq/CodeWhale/issues",
260 "https://discord.gg/37gfS3ksug",
261 `/${locale}/contribute`,
262 "https://github.com/codewhale-hq/CodeWhale/blob/main/LICENSE",
263 ]);
264 const legal = footerLegalLinks(locale, getChrome(locale));
265 expect(legal.map((l) => l.href), `${locale} footer legal`).toEqual([
266 `/${locale}/legal/terms`,
267 `/${locale}/legal/privacy`,
268 ]);
269 // Labels come from the dictionary, not hardcoded English, so every
270 // routed locale renders the footer legal links in its own language.
271 expect(legal.map((l) => l.label), `${locale} footer legal labels`).toEqual([
272 getChrome(locale).footerTerms,
273 getChrome(locale).footerPrivacy,
274 ]);
275 }
276 });
277
278 it("labels the primary nav and marks the current page accessibly", () => {
279 expect(navLinks).toContain("aria-label={primaryAria}");
280 expect(getChrome("en").navPrimaryAria).toBe("Primary");
281 expect(getChrome("zh").navPrimaryAria).toBe("主导航");
282 expect(navLinks).toContain('aria-current={isActive ? "page" : undefined}');
283 expect(mobileMenu).toContain('aria-current={isActive ? "page" : undefined}');
284 expect(mobileMenu).toContain('aria-expanded={open}');
285 expect(mobileMenu).toContain('aria-controls="mobile-menu"');
286 expect(mobileMenu).toContain('role="dialog"');
287 });
288
289 it("marks exactly one nav link as the current page on a nested route", () => {
290 // `/xx/docs` and `/xx/docs/guide` are both nav links, so the plain
291 // prefix test both surfaces used marked two links `aria-current="page"`
292 // on the guide route — and drew the nav underline under both.
293 for (const locale of locales) {
294 const links = [
295 ...buildNavLinks(locale, getChrome(locale)),
296 ...buildSecondaryNavLinks(locale, getChrome(locale)),
297 ];
298 const guide = `/${locale}/docs/guide`;
299 const naive = links.filter(
300 (l) => guide === l.href || guide.startsWith(`${l.href}/`),
301 );
302 expect(naive.length, `${locale} ancestor+page collision`).toBeGreaterThan(1);
303 expect(currentNavHref(links, guide), `${locale} current nav link`).toBe(guide);
304 // A route that is not itself a nav link still resolves to its section.
305 expect(
306 currentNavHref(links, `/${locale}/docs/configuration`),
307 `${locale} section fallback`,
308 ).toBe(`/${locale}/docs`);
309 expect(currentNavHref(links, `/${locale}`), `${locale} home`).toBeNull();
310 }
311 // Both surfaces resolve the current page through the shared helper
312 // rather than repeating the prefix test that collided.
313 expect(navLinks).toContain("currentNavHref(links, pathname)");
314 expect(mobileMenu).toContain("currentNavHref([...links, ...moreLinks], pathname)");
315 expect(navLinks).not.toContain("pathname.startsWith(");
316 expect(mobileMenu).not.toContain("pathname.startsWith(");
317 });
318
319 it("ships a keyboard-reachable skip link to the main landmark", () => {
320 expect(localeLayout).toContain('href="#main-content"');
321 expect(localeLayout).toContain('className="skip-link"');
322 expect(localeLayout).toContain('<main id="main-content">');
323 expect(css).toContain(".skip-link:focus-visible");
324 });
325
326 it("keeps the docs trail on the docs-map registry", () => {
327 const docsLayout = webText("app/[locale]/docs/layout.tsx");
328 const docsMap = webText("lib/docs-map.ts");
329 expect(docsMap).toContain("sidebar, breadcrumbs, and drift/parity checks");
330 expect(docsMap).toContain("export const DOC_CATEGORY_LABELS");
331 expect(docsLayout).toContain("<DocsBreadcrumb locale={locale} />");
332 expect(css).toContain(".docs-breadcrumb");
333 });
334
335 it("keeps responsive breakpoints for the getting-started steps", () => {
336 // 4-up grid by default, 2-up at the tablet breakpoint, 1-up on phones —
337 // the same responsive ladder as the existing workflow steps.
338 expect(css).toMatch(/\.gs-steps\s*\{[^}]*repeat\(4, minmax\(0, 1fr\)\)/);
339 expect(css).toMatch(
340 /@media \(max-width: 760px\)[\s\S]*?\.gs-steps\s*\{[^}]*repeat\(2, minmax\(0, 1fr\)\)/,
341 );
342 expect(css).toMatch(
343 /@media \(max-width: 520px\)[\s\S]*?\.gs-steps\s*\{\s*grid-template-columns: 1fr/,
344 );
345 });
346
347 it("keeps the footer discovery links alongside the pinned legal links", () => {
348 // The link sets moved to lib/i18n/links.ts, so assert the rendered
349 // contract for en AND zh rather than scraping literals out of the TSX.
350 for (const locale of ["en", "zh"]) {
351 const product = footerProductLinks(locale, getChrome(locale)).map((l) => l.href);
352 expect(product, `${locale} footer product`).toContain(`/${locale}/docs/guide`);
353 expect(product, `${locale} footer product`).toContain(`/${locale}/faq`);
354 }
355 const license = footerProjectLinks("en", getChrome("en")).at(-1);
356 expect(license).toEqual({
357 label: "MIT license",
358 href: "https://github.com/codewhale-hq/CodeWhale/blob/main/LICENSE",
359 });
360 // zh gets the footer legal labels from its dictionary, not English.
361 expect(footerLegalLinks("zh", getChrome("zh")).map((l) => l.label)).toEqual([
362 "服务条款",
363 "隐私政策",
364 ]);
365 expect(footer).toContain("footerProductLinks(locale, chrome)");
366 expect(footer).toContain("footerProjectLinks(locale, chrome)");
367 expect(footer).toContain("footerLegalLinks(locale, chrome)");
368 });
369 });
370
371 describe("homepage integration", () => {
372 const homepage = webText("app/[locale]/page.tsx");
373
374 it("renders the shared getting-started path on the homepage", () => {
375 expect(homepage).toContain('import { GettingStartedSteps } from "@/components/getting-started-steps"');
376 expect(homepage).toContain("<GettingStartedSteps locale={locale} />");
377 expect(homepage).toContain("product-start");
378 expect(homepage).toContain("/docs/guide");
379 expect(homepage).toContain("/docs/vocabulary");
380 });
381
382 it("keeps the previously pinned homepage facts intact", () => {
383 // Guard against the new band accidentally displacing the public-copy
384 // gate's required surface (the full contract lives in public-copy.test.ts).
385 expect(homepage).toContain("facts.latestPublishedRelease");
386 // The unreleased-source label is the EN dictionary value the page renders
387 // (plain "Unreleased", per docs/design/WEB_VOICE.md).
388 expect(homepage).toContain("d.sourceCandidate");
389 expect(getHome("en").sourceCandidate).toBe("Unreleased");
390 // The terminal is the live capture, labelled with the captured build.
391 expect(homepage).toContain("<NativeTerminalGallery");
392 expect(homepage).toContain("TERMINAL_SCREENSHOT.version");
393 for (const label of ["Plan", "Work", "Operate", "Ask", "Auto-Review", "Full Access"]) {
394 expect(homepage).toContain(label);
395 }
396 });
397 });
398
398 lines TYPESCRIPT