| 1 | import { AUTO_ROUTING_COPY } from "./content/faq-routing"; |
| 2 | import { createElement } from "react"; |
| 3 | import { readFileSync } from "node:fs"; |
| 4 | import { describe, expect, it } from "vitest"; |
| 5 | import { buildFaqPageJsonLd } from "./faq-schema"; |
| 6 | import { serializeJsonLd } from "./json-ld"; |
| 7 | import { extractText, flattenExtractedText } from "./react-text"; |
| 8 | import { SITE_URL } from "./page-meta"; |
| 9 | |
| 10 | const faqContent = readFileSync(new URL("./content/faq.tsx", import.meta.url), "utf8"); |
| 11 | const faqPage = readFileSync(new URL("../app/[locale]/faq/page.tsx", import.meta.url), "utf8"); |
| 12 | |
| 13 | describe("FAQPage structured data", () => { |
| 14 | it("maps every question to an acceptedAnswer with flattened text", () => { |
| 15 | const items = [ |
| 16 | { |
| 17 | q: "What is Codewhale?", |
| 18 | a: createElement( |
| 19 | "p", |
| 20 | null, |
| 21 | "A terminal-native agent. Run ", |
| 22 | createElement("code", null, "codewhale"), |
| 23 | ".", |
| 24 | ), |
| 25 | }, |
| 26 | { |
| 27 | q: "How do I install?", |
| 28 | a: ["npm install -g codewhale", createElement("span", null, " or Cargo.")], |
| 29 | }, |
| 30 | ]; |
| 31 | const schema = buildFaqPageJsonLd({ |
| 32 | items, |
| 33 | url: `${SITE_URL}/en/faq`, |
| 34 | inLanguage: "en", |
| 35 | }); |
| 36 | |
| 37 | expect(schema).toEqual({ |
| 38 | "@context": "https://schema.org", |
| 39 | "@type": "FAQPage", |
| 40 | url: "https://codewhale.net/en/faq", |
| 41 | inLanguage: "en", |
| 42 | mainEntity: [ |
| 43 | { |
| 44 | "@type": "Question", |
| 45 | name: "What is Codewhale?", |
| 46 | acceptedAnswer: { |
| 47 | "@type": "Answer", |
| 48 | text: "A terminal-native agent. Run codewhale .", |
| 49 | }, |
| 50 | }, |
| 51 | { |
| 52 | "@type": "Question", |
| 53 | name: "How do I install?", |
| 54 | acceptedAnswer: { |
| 55 | "@type": "Answer", |
| 56 | text: "npm install -g codewhale or Cargo.", |
| 57 | }, |
| 58 | }, |
| 59 | ], |
| 60 | }); |
| 61 | }); |
| 62 | |
| 63 | it("covers all 48 curated pairs from the shared FAQ content", () => { |
| 64 | const questions = [...faqContent.matchAll(/^\s+q: "(.+)"/gm)].map((match) => match[1]); |
| 65 | questions.push(AUTO_ROUTING_COPY.question.en, AUTO_ROUTING_COPY.question.zh); |
| 66 | expect(questions).toHaveLength(48); |
| 67 | expect(new Set(questions).size).toBe(48); |
| 68 | expect(faqContent).toContain("const faqEn"); |
| 69 | expect(faqContent).toContain("const faqZh"); |
| 70 | expect(faqPage).toContain("buildFaqPageJsonLd({"); |
| 71 | expect(faqPage).toContain("items,"); |
| 72 | expect(faqPage).toContain('canonicalLocaleForPath("/faq", locale)'); |
| 73 | expect(faqPage).toContain('type="application/ld+json"'); |
| 74 | expect(faqPage).toContain("serializeJsonLd(jsonLd)"); |
| 75 | }); |
| 76 | |
| 77 | it("keeps every internal answer link on the reader's locale", () => { |
| 78 | // Every locale without its own FAQ reads the English answers, so a |
| 79 | // literal `/en/...` link sent a /ja reader to the English site (W01-02). |
| 80 | expect(faqContent).not.toMatch(/href=\{?["'`]\/[a-z]{2}(?:-[A-Za-z]+)?\//); |
| 81 | }); |
| 82 | }); |
| 83 | |
| 84 | describe("extractText", () => { |
| 85 | it("flattens nested elements and ignores booleans", () => { |
| 86 | const node = createElement( |
| 87 | "div", |
| 88 | null, |
| 89 | "Hello ", |
| 90 | createElement("code", null, "codewhale"), |
| 91 | false, |
| 92 | createElement("span", null, [" ", 2]), |
| 93 | ); |
| 94 | |
| 95 | expect(extractText(node)).toContain("codewhale"); |
| 96 | expect(flattenExtractedText(node)).toBe("Hello codewhale 2"); |
| 97 | }); |
| 98 | |
| 99 | it("escapes angle brackets so JSON-LD cannot close the script tag", () => { |
| 100 | expect(serializeJsonLd({ text: "</script>" })).toBe('{"text":"\\u003c/script>"}'); |
| 101 | }); |
| 102 | }); |
| 103 |