import { readFileSync } from "node:fs"; import { join } from "node:path"; import { churchSchema, eventSchema, type Church, type CrosscodeEvent } from "@crosscode/contracts"; import { canonicalUrl, churchJsonLd, eventJsonLd, jsonLdString, pageMeta, truncate } from "../src"; const SAMPLES = join(__dirname, "..", "..", "contracts", "samples"); const sample = (file: string): T => JSON.parse(readFileSync(join(SAMPLES, file), "utf8")) as T; const event = () => eventSchema.parse(sample("event.json")) as CrosscodeEvent; const simple = () => eventSchema.parse(sample("event-simple.json")) as CrosscodeEvent; const church = () => churchSchema.parse(sample("church.json")) as Church; describe("eventJsonLd", () => { it("builds an Event from crosscode.event/1", () => { const ld = eventJsonLd(event(), { organizer: { name: "Hope Family Church", url: "https://events.hopefamily.co.za" } }); expect(ld).toMatchObject({ "@context": "https://schema.org", "@type": "Event", name: "Youth Camp 2027", startDate: "2027-03-14T06:00:00.000Z", endDate: "2027-03-16T12:00:00.000Z", eventStatus: "https://schema.org/EventScheduled", eventAttendanceMode: "https://schema.org/OfflineEventAttendanceMode", location: { "@type": "Place", name: "Camp Wartburg", address: "R103, Wartburg, KwaZulu-Natal" }, url: "https://events.hopefamily.co.za/events/youth-camp", organizer: { "@type": "Organization", name: "Hope Family Church" }, offers: { "@type": "Offer", price: "450.00", priceCurrency: "ZAR", availability: "https://schema.org/InStock" }, }); expect((ld.subEvent as unknown[]).length).toBe(3); }); it("gives a price range when the origin knows it", () => { const ld = eventJsonLd(event(), { priceRange: { lowCents: 45000, highCents: 90000 } }); expect(ld.offers).toMatchObject({ "@type": "AggregateOffer", lowPrice: "450.00", highPrice: "900.00" }); }); it("marks full and waitlisted events sold out, and cancelled ones cancelled", () => { const e = event(); e.registration.status = "full"; e.status = "cancelled"; const ld = eventJsonLd(e); expect((ld.offers as Record).availability).toBe("https://schema.org/SoldOut"); expect(ld.eventStatus).toBe("https://schema.org/EventCancelled"); }); it("has no offer for contact-only events, or for an external link without a price", () => { const e = event(); e.registration = { mode: "contact", url: null, status: "open", opensAt: null, closesAt: null, priceFrom: null }; expect(eventJsonLd(e).offers).toBeUndefined(); e.registration = { mode: "external", url: "https://example.org/tickets", status: "open", opensAt: null, closesAt: null, priceFrom: null }; expect(eventJsonLd(e).offers).toBeUndefined(); }); it("offers a free origin registration at 0.00", () => { const e = event(); e.registration.priceFrom = null; expect(eventJsonLd(e).offers).toMatchObject({ "@type": "Offer", price: "0.00" }); }); it("describes online and mixed events", () => { const e = event(); e.location = { name: null, address: null, onlineUrl: "https://youtube.com/live/x" }; expect(eventJsonLd(e)).toMatchObject({ eventAttendanceMode: "https://schema.org/OnlineEventAttendanceMode", location: { "@type": "VirtualLocation" } }); e.location = { name: "Main hall", address: null, onlineUrl: "https://youtube.com/live/x" }; const ld = eventJsonLd(e); expect(ld.eventAttendanceMode).toBe("https://schema.org/MixedEventAttendanceMode"); expect(ld.location).toEqual([ { "@type": "Place", name: "Main hall", address: "Main hall" }, { "@type": "VirtualLocation", url: "https://youtube.com/live/x" }, ]); }); it("works for a simple event from another product", () => { const ld = eventJsonLd(simple()); expect(ld["@type"]).toBe("Event"); expect(ld.subEvent).toBeUndefined(); }); it("leaves cancelled sessions out of the dates", () => { const e = event(); e.sessions[1]!.cancelled = true; expect((eventJsonLd(e).subEvent as unknown[]).length).toBe(2); }); }); describe("churchJsonLd", () => { it("builds a Church with address, geo, service times and social links", () => { const ld = churchJsonLd(church()); expect(ld).toMatchObject({ "@type": "Church", name: "Hope Family Church", alternateName: "Hope Family", url: "https://www.hopefamily.co.za", logo: "https://www.hopefamily.co.za/uploads/logo.png", email: "office@hopefamily.co.za", telephone: "+27161234567", address: { "@type": "PostalAddress", streetAddress: "12 Church Street", addressLocality: "Meyerton Central, Meyerton", postalCode: "1961", addressCountry: "ZA" }, geo: { latitude: -26.5559, longitude: 28.0141 }, sameAs: ["https://www.facebook.com/hopefamilychurch", "https://www.youtube.com/@hopefamilychurch"], }); expect((ld.openingHoursSpecification as unknown[]).length).toBe(2); expect(ld.department).toBeUndefined(); }); it("lists each campus when there are several", () => { const c = church(); c.campuses.push({ slug: "vereeniging", name: "Vereeniging", address: null }); const ld = churchJsonLd(c); expect(ld.department).toEqual([ expect.objectContaining({ name: "Hope Family Church Meyerton" }), { "@type": "Church", name: "Hope Family Church Vereeniging" }, ]); }); }); describe("meta", () => { it.each([ ["https://e.co.za", "/events/1?src=whatsapp&utm_source=fb", "https://e.co.za/events/1"], ["https://e.co.za/", "/events/1/", "https://e.co.za/events/1"], ["https://e.co.za", "/", "https://e.co.za/"], ["https://e.co.za", "/events?campus=b&page=2&gclid=x#top", "https://e.co.za/events?campus=b&page=2"], ])("canonicalUrl(%s, %s)", (base, path, expected) => { expect(canonicalUrl(base, path)).toBe(expected); }); it("truncates at a word boundary", () => { expect(truncate("Three days of worship, teaching and adventure", 30)).toBe("Three days of worship…"); expect(truncate("short", 30)).toBe("short"); }); it("warns about missing and long titles and descriptions", () => { expect(pageMeta({ title: "", description: "" }).warnings).toEqual(["title_missing", "description_missing"]); const long = pageMeta({ title: "x".repeat(61), description: "word ".repeat(40) }); expect(long.warnings).toEqual(["title_too_long", "description_too_long"]); expect(long.description.length).toBeLessThanOrEqual(155); expect(pageMeta({ title: "Home", description: null, fallbackDescription: "Hope Family Church" }).description).toBe("Hope Family Church"); }); it("makes JSON-LD safe inside a script tag", () => { expect(jsonLdString({ name: "" })).not.toContain(""); }); });