/** * RFC-0045 step 8b on the renderer: `entryStructuredData` fills Event.location from the venue * entry the view shows — its member in the entry's language (`pickMember`), its non-localized * address read from its original (`resolveFields` with the member's original), and nothing (so no * document, the address being required) when the view shows no member. */ import type { ContentEntry, Site } from "@cloudhal/core-contracts"; import { describe, expect, it } from "vitest"; import { entryStructuredData } from "../src/structured-data.ts"; const L = { en: { label: "x" } }; const HALL = "entry_01HZX8K3QHALL01"; const HALL_TR = "entry_01HZX8K3QHALLTR"; const site = { id: "site_01HZX8K3Q9AB", name: "Halls", defaultLocale: "en", locales: ["en", "tr"], timeZone: "Europe/Istanbul", collections: { places: { fields: [ { key: "city", type: "text", labels: L, localized: false }, { key: "street", type: "text", labels: L, localized: true }, ], }, events: { fields: [ { key: "startsAt", type: "datetime", labels: L, localized: false }, { key: "venue", type: "reference", labels: L, localized: false, collections: ["places"] }, ], structuredData: { type: "Event", properties: { startDate: { kind: "field", field: "startsAt" }, "location.name": { kind: "reference", field: "venue", property: "title" }, "location.address.streetAddress": { kind: "reference", field: "venue", targetField: "street", }, "location.address.addressLocality": { kind: "reference", field: "venue", targetField: "city", }, }, }, }, }, } as unknown as Site; const entry = (id: string, over: Partial): ContentEntry => ({ id, siteId: site.id, collection: "events", slug: id, locale: "en", status: "published", title: id, body: { format: "markdown", value: "" }, fields: {}, createdAt: "2026-11-01T00:00:00.000Z", ...over, }) as ContentEntry; const hall = entry(HALL, { collection: "places", slug: "grand-hall", title: "Grand Hall", fields: { city: "Istanbul", street: "Main Street 1" }, }); const hallTr = entry(HALL_TR, { collection: "places", slug: "grand-hall", locale: "tr", title: "Büyük Salon", fields: { street: "Ana Cadde 1" }, translation: { group: HALL, method: "human" }, }); const startsAt = { at: "2026-11-05T16:30:00Z", timeZone: "Europe/Istanbul" }; const launch = entry("entry_01HZX8K3QLAUNCH", { fields: { startsAt, venue: HALL } }); const launchTr = entry("entry_01HZX8K3QLAUNTR", { locale: "tr", translation: { group: launch.id, method: "human" }, }); const sourceOf = (shown: readonly ContentEntry[]) => ({ site: async () => site, entryById: async (id: string) => [launch, launchTr, ...shown].find((e) => e.id === id), assets: async () => [], groupMembers: async (groups: readonly string[]) => new Map(groups.map((g) => [g, shown.filter((m) => (m.translation?.group ?? m.id) === g)])), }); const origin = "https://halls.test"; describe("entryStructuredData by reference (RFC-0045 step 8b)", () => { it("reads the venue in the entry's language, its non-localized city from the original", async () => { const en = await entryStructuredData(sourceOf([hall, hallTr]), launch, { origin }); expect(en.doc?.location).toEqual({ "@type": "Place", name: "Grand Hall", address: { "@type": "PostalAddress", streetAddress: "Main Street 1", addressLocality: "Istanbul", }, }); // the Turkish event reads the Turkish venue: its own street, the original's city const tr = await entryStructuredData(sourceOf([hall, hallTr]), launchTr, { origin }); expect(tr.doc?.location).toEqual({ "@type": "Place", name: "Büyük Salon", address: { "@type": "PostalAddress", streetAddress: "Ana Cadde 1", addressLocality: "Istanbul", }, }); }); it("drops the document when the view shows no member (location.name is required)", async () => { const r = await entryStructuredData(sourceOf([]), launch, { origin }); expect(r.doc).toBeUndefined(); expect(r.issues.map((i) => i.path)).toEqual(["location.name", "location.address"]); // no Turkish venue under `fallback: none` (the default): nothing for the Turkish event const tr = await entryStructuredData(sourceOf([hall]), launchTr, { origin }); expect(tr.doc).toBeUndefined(); // under default-with-notice, the default language's venue const fallback = { ...site, localization: { fallback: "default-with-notice" } } as Site; const withFallback = { ...sourceOf([hall]), site: async () => fallback }; const r2 = await entryStructuredData(withFallback, launchTr, { origin }); expect((r2.doc?.location as { name?: string } | undefined)?.name).toBe("Grand Hall"); }); });