| 1 | import { readFileSync } from "node:fs"; |
| 2 | import { describe, expect, it } from "vitest"; |
| 3 | import { locales } from "./i18n/config"; |
| 4 | import { contentLocalesForPath } from "./i18n/content-locales"; |
| 5 | import { buildPageMetadata, IDENTITY_PHRASE, OG_ALT, SITE_NAME, SITE_URL } from "./page-meta"; |
| 6 | |
| 7 | /** hreflang alternates derive from genuine page-body translation coverage. */ |
| 8 | function expectedLanguages(path: string): Record<string, string> { |
| 9 | const suffix = path === "/" ? "" : path; |
| 10 | const languages: Record<string, string> = {}; |
| 11 | for (const l of contentLocalesForPath(path)) { |
| 12 | languages[l] = `${SITE_URL}/${l}${suffix}`; |
| 13 | } |
| 14 | languages["x-default"] = `${SITE_URL}/en${suffix}`; |
| 15 | return languages; |
| 16 | } |
| 17 | |
| 18 | describe("page metadata", () => { |
| 19 | it.each([ |
| 20 | ["en", "/faq", "FAQ · Codewhale", "en_US", "en"], |
| 21 | ["zh", "/faq", "常见问题 · Codewhale", "zh_CN", "zh"], |
| 22 | ["en", "/feed", "Activity · Codewhale", "en_US", "en"], |
| 23 | ["zh", "/feed", "动态 · Codewhale", "zh_CN", "zh"], |
| 24 | ["en", "/roadmap", "Roadmap · Codewhale", "en_US", "en"], |
| 25 | ["zh", "/roadmap", "路线图 · Codewhale", "zh_CN", "zh"], |
| 26 | ["ru", "/faq", "FAQ · Codewhale", "en_US", "en"], |
| 27 | ["uk", "/faq", "FAQ · Codewhale", "en_US", "en"], |
| 28 | ["pt-BR", "/install", "Install · Codewhale", "en_US", "en"], |
| 29 | ["zh", "/install", "安装 · Codewhale", "en_US", "en"], |
| 30 | ["ja", "/", "Codewhale", "ja_JP", "ja"], |
| 31 | ])( |
| 32 | "builds canonical, hreflang, Open Graph, and Twitter fields for %s%s", |
| 33 | (locale, path, title, ogLocale, canonicalLocale) => { |
| 34 | const description = `${locale} metadata contract`; |
| 35 | const metadata = buildPageMetadata({ path, locale, title, description }); |
| 36 | const suffix = path === "/" ? "" : path; |
| 37 | const canonical = `${SITE_URL}/${canonicalLocale}${suffix}`; |
| 38 | |
| 39 | expect(metadata.alternates).toEqual({ |
| 40 | canonical, |
| 41 | languages: expectedLanguages(path), |
| 42 | }); |
| 43 | expect(metadata.openGraph).toEqual({ |
| 44 | title, |
| 45 | description, |
| 46 | url: canonical, |
| 47 | siteName: SITE_NAME, |
| 48 | type: "website", |
| 49 | locale: ogLocale, |
| 50 | images: [ |
| 51 | { |
| 52 | url: `${SITE_URL}/opengraph-image`, |
| 53 | width: 1200, |
| 54 | height: 630, |
| 55 | alt: OG_ALT, |
| 56 | }, |
| 57 | ], |
| 58 | }); |
| 59 | expect(metadata.twitter).toEqual({ |
| 60 | card: "summary_large_image", |
| 61 | title, |
| 62 | description, |
| 63 | // The card must carry its own alt text: Twitter reads |
| 64 | // `twitter:image:alt` and does not inherit `og:image:alt` once a |
| 65 | // `twitter:image` tag is present. |
| 66 | images: [{ url: `${SITE_URL}/opengraph-image`, alt: OG_ALT }], |
| 67 | }); |
| 68 | }, |
| 69 | ); |
| 70 | |
| 71 | it("advertises only genuine page-body translations", () => { |
| 72 | for (const [path, locale, expectedLocales, expectedCanonical] of [ |
| 73 | ["/", "ja", locales, `${SITE_URL}/ja`], |
| 74 | // The guide's steps (its body) ship en/zh only; a French overview |
| 75 | // dictionary does not make the page a French translation. |
| 76 | ["/docs/guide", "fr", ["en", "zh"], `${SITE_URL}/en/docs/guide`], |
| 77 | // /computer-use renders only from its per-locale dictionary. |
| 78 | ["/computer-use", "ja", locales, `${SITE_URL}/ja/computer-use`], |
| 79 | [ |
| 80 | "/docs/guide", |
| 81 | "ja", |
| 82 | contentLocalesForPath("/docs/guide"), |
| 83 | `${SITE_URL}/en/docs/guide`, |
| 84 | ], |
| 85 | ["/docs", "ja", ["en", "zh"], `${SITE_URL}/en/docs`], |
| 86 | ["/install", "zh", ["en"], `${SITE_URL}/en/install`], |
| 87 | ] as const) { |
| 88 | const metadata = buildPageMetadata({ |
| 89 | path, |
| 90 | locale, |
| 91 | title: "Metadata contract", |
| 92 | description: "Truthful locale coverage", |
| 93 | }); |
| 94 | const languages = metadata.alternates?.languages as Record<string, string>; |
| 95 | expect(Object.keys(languages)).toEqual([...expectedLocales, "x-default"]); |
| 96 | expect(metadata.alternates?.canonical).toBe(expectedCanonical); |
| 97 | } |
| 98 | }); |
| 99 | |
| 100 | it("keeps the canonical brand-first identity without duplicating an OG heading", () => { |
| 101 | const brand = new RegExp(`\\b${SITE_NAME}\\b`, "gi"); |
| 102 | const ogImage = readFileSync(new URL("../app/opengraph-image.tsx", import.meta.url), "utf8"); |
| 103 | |
| 104 | expect(IDENTITY_PHRASE.startsWith(`${SITE_NAME} — `)).toBe(true); |
| 105 | expect(OG_ALT).toBe(IDENTITY_PHRASE); |
| 106 | expect(OG_ALT.match(brand)).toHaveLength(1); |
| 107 | expect(ogImage).toContain("{IDENTITY_PHRASE}"); |
| 108 | expect(ogImage).not.toContain("{SITE_NAME}"); |
| 109 | }); |
| 110 | |
| 111 | it("keeps the previously incomplete indexable routes on the shared helper", () => { |
| 112 | for (const [route, path] of [ |
| 113 | ["faq", "/faq"], |
| 114 | ["feed", "/feed"], |
| 115 | ["roadmap", "/roadmap"], |
| 116 | ["legal/terms", "/legal/terms"], |
| 117 | ["legal/privacy", "/legal/privacy"], |
| 118 | ]) { |
| 119 | const source = readFileSync( |
| 120 | new URL(`../app/[locale]/${route}/page.tsx`, import.meta.url), |
| 121 | "utf8", |
| 122 | ); |
| 123 | expect(source, route).toContain('import { buildPageMetadata } from "@/lib/page-meta"'); |
| 124 | expect(source, route).toContain("return buildPageMetadata({"); |
| 125 | expect(source, route).toContain(`path: "${path}"`); |
| 126 | } |
| 127 | }); |
| 128 | |
| 129 | it("omits robots by default and passes an explicit noindex through", () => { |
| 130 | const base = { path: "/digest", locale: "en", title: "Digest · Codewhale", description: "d" }; |
| 131 | expect(buildPageMetadata(base)).not.toHaveProperty("robots"); |
| 132 | |
| 133 | const noindex = buildPageMetadata({ ...base, robots: { index: false, follow: true } }); |
| 134 | expect(noindex.robots).toEqual({ index: false, follow: true }); |
| 135 | // Canonical and social fields are unchanged by the robots option. |
| 136 | expect(noindex.alternates).toEqual(buildPageMetadata(base).alternates); |
| 137 | expect(noindex.openGraph).toEqual(buildPageMetadata(base).openGraph); |
| 138 | }); |
| 139 | }); |
| 140 |