/** * RFC-0033 §4 (phase B1): locale URLs, `hreflang`, link localization and locale-aware routing, over * a real store (SQLite + memory blobs) and its live source. */ import type { Page, Site, Term } from "@cloudhal/core-contracts"; import { createEdition, SiteGraphStore, seedStarterSite } from "@cloudhal/core-ts"; import { MemoryBlobStore } from "@cloudhal/storage"; import { BetterSqlite3Driver } from "@cloudhal/storage/sqlite"; import { beforeEach, describe, expect, it } from "vitest"; import { sitemapXml } from "../src/feeds.ts"; import { llmsTxt, localeFeedXml, localizedSitemap } from "../src/locale-feeds.ts"; import { alternatesFor, linkLocalizer, localeRequest, localeUrl, localizedPath, localizeHead, localizeLinks, textDirection, } from "../src/locales.ts"; import { resolveRoute } from "../src/routing.ts"; import { pageHead } from "../src/seo.ts"; import { LiveSource } from "../src/source.ts"; const ORIGIN = "https://example.com"; const bare = (over: Partial = {}): Site => ({ defaultLocale: "en", locales: ["en", "es", "ar"], ...over, }) as Site; describe("locale URLs", () => { it("splits the prefix, moves non-canonical URLs, and leaves the default locale unprefixed", () => { const site = bare(); expect(localeRequest(site, "example.com", "/")).toEqual({ locale: "en", path: "/" }); expect(localeRequest(site, "example.com", "/es")).toEqual({ locale: "es", path: "/" }); expect(localeRequest(site, "example.com", "/es/about")).toEqual({ locale: "es", path: "/about", }); expect(localeRequest(site, "example.com", "/en/about")).toEqual({ locale: "en", path: "/about", redirect: "/about", }); expect(localeRequest(site, "example.com", "/ES/about").redirect).toBe("/es/about"); // A first segment that is not a locale is a path. expect(localeRequest(site, "example.com", "/esports")).toEqual({ locale: "en", path: "/esports", }); }); it("prefixes every locale under prefix-all and serves a locale's own domain at its root", () => { const all = bare({ localization: { routing: "prefix-all" } }); expect(localeRequest(all, "example.com", "/about").redirect).toBe("/en/about"); expect(localizedPath(all, "en", "/")).toBe("/en"); const domains = bare({ localization: { domains: { ar: "ar.example.com" } } }); expect(localeRequest(domains, "ar.example.com:443", "/about")).toEqual({ locale: "ar", path: "/about", }); // On the main host, /ar is not a prefix: the locale lives on its domain. expect(localeRequest(domains, "example.com", "/ar/about").locale).toBe("en"); expect(localeUrl(domains, "ar", "/about", ORIGIN)).toBe("https://ar.example.com/about"); expect(localeUrl(domains, "es", "/", ORIGIN)).toBe("https://example.com/es"); }); it("points the search dialog at the locale's endpoint and page", () => { const html = ''; expect(localizeLinks(html, (p) => `/es${p}`)).toBe( '', ); }); it("knows which scripts run right to left", () => { expect(["ar", "fa-IR", "he", "ur"].map(textDirection)).toEqual(["rtl", "rtl", "rtl", "rtl"]); expect(["en", "tr", "ja", "ku-Latn"].map(textDirection)).toEqual(["ltr", "ltr", "ltr", "ltr"]); }); it("rewrites internal links and forms, never platform paths or other hosts", () => { const html = 'AH
' + 'MS' + 'CX'; const out = localizeLinks(html, (p) => `/es${p === "/" ? "" : p}`); expect(out).toBe( 'AH
' + 'MS' + 'CX', ); }); }); describe("locales over a store", () => { let store: SiteGraphStore; let site: Site; let about: Page; let acerca: Page; let source: LiveSource; beforeEach(async () => { store = new SiteGraphStore({ sql: await BetterSqlite3Driver.open(":memory:"), blobs: new MemoryBlobStore(), }); await store.init(); site = await seedStarterSite(store, { name: "Locales", slug: "locales" }); site = await store.updateSite({ ...site, locales: ["en", "es", "ar"] }); ({ page: about, site } = await store.createPage(site, { id: "page_01HZX8K3QABT", siteId: site.id, path: "/team", title: "Team", locale: "en", status: "published", sections: [], translation: { group: "page_01HZX8K3QABT", method: "original" }, })); const hash = (await store.translationGroup(site, about.id)).members[0]?.hash as string; ({ page: acerca, site } = await store.createPage(site, { siteId: site.id, path: "/equipo", title: "Equipo", locale: "es", status: "published", sections: [], translation: { group: about.id, method: "human", source: { id: about.id, locale: "en", rev: about.rev, hash }, }, })); source = new LiveSource(store, site.id); }); it("resolves pages in the request's locale", async () => { expect(await resolveRoute(source, "/equipo", { locale: "es" })).toMatchObject({ kind: "page", page: { id: acerca.id }, }); expect((await resolveRoute(source, "/equipo")).kind).toBe("not-found"); expect(await resolveRoute(source, "/team")).toMatchObject({ page: { id: about.id } }); }); it("lists every version with x-default, each self-canonical in its own locale", async () => { const alternates = await alternatesFor(source, acerca, ORIGIN); expect(alternates).toEqual([ { locale: "en", href: "https://example.com/team", current: false, original: true }, { locale: "es", href: "https://example.com/es/equipo", current: true }, { locale: "x-default", href: "https://example.com/team", current: false }, ]); const head = localizeHead(pageHead(site, acerca, ORIGIN), site, { locale: "es", origin: ORIGIN, alternates, }); expect(head.canonical).toBe("https://example.com/es/equipo"); expect(head.openGraph).toContainEqual(["og:url", "https://example.com/es/equipo"]); expect(head.openGraph).toContainEqual(["og:locale:alternate", "en"]); expect(head.alternates).toHaveLength(3); // A page without other versions has no hreflang set. const home = (await source.pageByPath("/")) as Page; expect(await alternatesFor(source, home, ORIGIN)).toEqual([]); const ar = localizeHead(pageHead(site, home, ORIGIN), site, { locale: "ar", origin: ORIGIN, alternates: [], }); expect([ar.lang, ar.dir, ar.alternates]).toEqual(["ar", "rtl", undefined]); }); it("adds the versions a language edition publishes, on its own host (RFC-0034 §6)", async () => { const edition = await createEdition(store, { fromSiteId: site.id, name: "Locales 日本語", slug: "locales-ja", locale: "ja", }); const team = (await store.listPages(edition.site.id)).find((p) => p.path === "/team") as Page; expect(team).toBeDefined(); const peer = { source: new LiveSource(store, edition.site.id), origin: "https://ja.example.com", }; // From the original: its own versions, then the edition's, x-default last. expect(await alternatesFor(source, about, ORIGIN, [peer])).toEqual([ { locale: "en", href: "https://example.com/team", current: true, original: true }, { locale: "es", href: "https://example.com/es/equipo", current: false }, { locale: "ja", href: "https://ja.example.com/team", current: false }, { locale: "x-default", href: "https://example.com/team", current: false }, ]); // From the edition: the same set, and the same x-default (the original). const ownJa = new LiveSource(store, edition.site.id); const back = { source, origin: ORIGIN }; const fromJa = await alternatesFor(ownJa, team, "https://ja.example.com", [back]); expect(fromJa.find((a) => a.locale === "x-default")?.href).toBe("https://example.com/team"); expect(fromJa.map((a) => a.locale)).toEqual(["ja", "en", "es", "x-default"]); // A page that is not yet in a group joins one through its edition. const home = (await source.pageByPath("/")) as Page; const peerHome = (await store.listPages(edition.site.id)).find((p) => p.path === "/"); expect(peerHome?.translation?.group).toBe(home.id); expect((await alternatesFor(source, home, ORIGIN, [peer])).map((a) => a.locale)).toEqual([ "en", "ja", "x-default", ]); }); it("sends a localized page's links to the same locale, or back to the default locale", async () => { const news = (await store.getTermBySlug(site.id, "category", "news")) as Term; await store.updateTerm({ ...news, labels: { ...news.labels, es: { name: "Noticias", slug: "noticias" } }, }); expect(await linkLocalizer(source, "en", ORIGIN)).toBeUndefined(); const es = (await linkLocalizer(source, "es", ORIGIN)) as (p: string) => string; expect(es("/team")).toBe("/es/equipo"); // translated expect(es("/")).toBe("/"); // the home page has no Spanish version: it stays English expect(es("/search")).toBe("/es/search"); // not a node: the same locale expect(es("/category/news/page/2")).toBe("/es/category/noticias/page/2"); }); it("links versions in JSON-LD and names the translator", async () => { const alternates = await alternatesFor(source, acerca, ORIGIN); const es = localizeHead(pageHead(site, acerca, ORIGIN), site, { locale: "es", origin: ORIGIN, alternates, translation: { group: about.id, method: "machine-reviewed", review: { by: { kind: "human", id: "user_01HZX8K3QREV", name: "Ana" }, at: "2026-10-18T10:00:00Z", }, }, }); expect(es.jsonLd[0]).toMatchObject({ translationOfWork: { "@id": "https://example.com/team", inLanguage: "en" }, translator: { "@type": "Person", name: "Ana" }, }); const en = localizeHead(pageHead(site, about, ORIGIN), site, { locale: "en", origin: ORIGIN, alternates: await alternatesFor(source, about, ORIGIN), }); expect(en.jsonLd[0]).toMatchObject({ workTranslation: [{ "@id": "https://example.com/es/equipo", inLanguage: "es" }], }); }); it("writes one sitemap with every locale and its alternates, and a feed and llms.txt per locale", async () => { const urls = await localizedSitemap(source, ORIGIN, "example.com"); const team = urls.find((u) => u.loc === "https://example.com/team"); expect(team?.alternates).toEqual([ { locale: "en", href: "https://example.com/team" }, { locale: "es", href: "https://example.com/es/equipo" }, { locale: "x-default", href: "https://example.com/team" }, ]); expect(urls.some((u) => u.loc === "https://example.com/es/equipo")).toBe(true); const xml = sitemapXml(urls); expect(xml).toContain('xmlns:xhtml="http://www.w3.org/1999/xhtml"'); expect(xml).toContain( '', ); await store.createContentEntry({ siteId: site.id, collection: "posts", slug: "hola", locale: "es", status: "published", title: "Hola", body: { format: "markdown", value: "Hola." }, fields: {}, tags: [], author: { kind: "human", id: "user_01HZX8K3QAUT", name: "Ana" }, terms: { category: { ids: [(await store.getTermBySlug(site.id, "category", "news"))?.id as string], primary: (await store.getTermBySlug(site.id, "category", "news"))?.id as string, }, }, }); const feed = await localeFeedXml(source, { origin: ORIGIN, locale: "es" }); expect(feed).toContain("es"); expect(feed).toContain("https://example.com/es/posts/hola"); expect(feed).toContain('