import { describe, it, expect } from "vitest"; import { contrastText, emptyPage, isSafeLink, pageBody, slugify, withTitle } from "./cmsPages"; describe("cmsPages helpers", () => { it("slug follows the title until edited", () => { const d = withTitle(emptyPage(), "Hotel Technology Day 2027!"); expect(d.slug).toBe("hotel-technology-day-2027"); expect(withTitle({ ...d, slug: "mine", slugTouched: true }, "Other").slug).toBe("mine"); expect(slugify(" ")).toBe(""); }); it("save body trims and drops blank optional fields", () => { const body = pageBody({ ...emptyPage(), title: " T ", slug: "t", metaDescription: " ", blocks: [{ type: "HERO", heading: " Hi ", text: "", imageId: null }, { type: "FAQ", items: [{ question: " Q ", answer: " A " }] }], }); expect(body.title).toBe("T"); expect(body.metaDescription).toBeNull(); expect(body.blocks).toEqual([{ type: "HERO", heading: "Hi" }, { type: "FAQ", items: [{ question: "Q", answer: "A" }] }]); }); it("picks the more readable text colour", () => { expect(contrastText("#FFEE00")).toBe("#000000"); expect(contrastText("#1A237E")).toBe("#FFFFFF"); expect(contrastText("bad")).toBe("#FFFFFF"); }); it("allows only safe button links", () => { for (const ok of ["/p/INS/a", "https://x.com", "mailto:a@b.c", "tel:+60123"]) expect(isSafeLink(ok), ok).toBe(true); for (const bad of ["//evil.com", "javascript:alert(1)", "http://x.com", "https://a b", "/\\evil", undefined]) expect(isSafeLink(bad), String(bad)).toBe(false); }); });