| 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 | ["ja", "/", "Codewhale", "ja_JP", "ja"], |
| 30 | ])( |
| 31 | "builds canonical, hreflang, Open Graph, and Twitter fields for %s%s", |
| 32 | (locale, path, title, ogLocale, canonicalLocale) => { |
| 33 | const description = `${locale} metadata contract`; |
| 34 | const metadata = buildPageMetadata({ path, locale, title, description }); |
| 35 | const suffix = path === "/" ? "" : path; |
| 36 | const canonical = `${SITE_URL}/${canonicalLocale}${suffix}`; |
| 37 | |
| 38 | expect(metadata.alternates).toEqual({ |
| 39 | canonical, |
| 40 | languages: expectedLanguages(path), |
| 41 | }); |
| 42 | expect(metadata.openGraph).toEqual({ |
| 43 | title, |
| 44 | description, |
| 45 | url: canonical, |
| 46 | siteName: SITE_NAME, |
| 47 | type: "website", |
| 48 | locale: ogLocale, |
| 49 | images: [ |
| 50 | { |
| 51 | url: `${SITE_URL}/opengraph-image`, |
| 52 | width: 1200, |
| 53 | height: 630, |
| 54 | alt: OG_ALT, |
| 55 | }, |
| 56 | ], |
| 57 | }); |
| 58 | expect(metadata.twitter).toEqual({ |
| 59 | card: "summary_large_image", |
| 60 | title, |
| 61 | description, |
| 62 | // The card must carry its own alt text: Twitter reads |
| 63 | // `twitter:image:alt` and does not inherit `og:image:alt` once a |
| 64 | // `twitter:image` tag is present. |
| 65 | images: [{ url: `${SITE_URL}/opengraph-image`, alt: OG_ALT }], |
| 66 | }); |
| 67 | }, |
| 68 | ); |
| 69 | |
| 70 | it("advertises only genuine page-body translations", () => { |
| 71 | for (const [path, locale, expectedLocales, expectedCanonical] of [ |
| 72 | ["/", "ja", locales, `${SITE_URL}/ja`], |
| 73 | [ |
| 74 | "/docs/guide", |
| 75 | "fr", |
| 76 | ["en", "zh", "fr", "de", "ca", "hi", "tr", "it", "pl", "ar"], |
| 77 | `${SITE_URL}/fr/docs/guide`, |
| 78 | ], |
| 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 | ] as const) { |
| 87 | const metadata = buildPageMetadata({ |
| 88 | path, |
| 89 | locale, |
| 90 | title: "Metadata contract", |
| 91 | description: "Truthful locale coverage", |
| 92 | }); |
| 93 | const languages = metadata.alternates?.languages as Record<string, string>; |
| 94 | expect(Object.keys(languages)).toEqual([...expectedLocales, "x-default"]); |
| 95 | expect(metadata.alternates?.canonical).toBe(expectedCanonical); |
| 96 | } |
| 97 | }); |
| 98 | |
| 99 | it("keeps the canonical brand-first identity without duplicating an OG heading", () => { |
| 100 | const brand = new RegExp(`\\b${SITE_NAME}\\b`, "gi"); |
| 101 | const ogImage = readFileSync(new URL("../app/opengraph-image.tsx", import.meta.url), "utf8"); |
| 102 | |
| 103 | expect(IDENTITY_PHRASE.startsWith(`${SITE_NAME} — `)).toBe(true); |
| 104 | expect(OG_ALT).toBe(IDENTITY_PHRASE); |
| 105 | expect(OG_ALT.match(brand)).toHaveLength(1); |
| 106 | expect(ogImage).toContain("{IDENTITY_PHRASE}"); |
| 107 | expect(ogImage).not.toContain("{SITE_NAME}"); |
| 108 | }); |
| 109 | |
| 110 | it("keeps the previously incomplete indexable routes on the shared helper", () => { |
| 111 | for (const [route, path] of [ |
| 112 | ["faq", "/faq"], |
| 113 | ["feed", "/feed"], |
| 114 | ["roadmap", "/roadmap"], |
| 115 | ["legal/terms", "/legal/terms"], |
| 116 | ["legal/privacy", "/legal/privacy"], |
| 117 | ]) { |
| 118 | const source = readFileSync( |
| 119 | new URL(`../app/[locale]/${route}/page.tsx`, import.meta.url), |
| 120 | "utf8", |
| 121 | ); |
| 122 | expect(source, route).toContain('import { buildPageMetadata } from "@/lib/page-meta"'); |
| 123 | expect(source, route).toContain("return buildPageMetadata({"); |
| 124 | expect(source, route).toContain(`path: "${path}"`); |
| 125 | } |
| 126 | }); |
| 127 | }); |
| 128 |