diff --git a/src/render/page.test.ts b/src/render/page.test.ts new file mode 100644 index 0000000..9dd0ce3 --- /dev/null +++ b/src/render/page.test.ts @@ -0,0 +1,78 @@ +import { describe, it, expect } from "vitest"; +import { renderPage } from "./page"; +import type { Preferences } from "../types"; + +const defaultPrefs: Preferences = { + tz: "eastern", + theme: "system", + mode: "vcr", + week: "2026-W45", + setCookies: [], +}; + +describe("renderPage", () => { + it("returns valid HTML with doctype", () => { + const html = renderPage("Test", "

hello

", defaultPrefs); + expect(html).toContain(""); + expect(html).toContain(""); + }); + + it("sets data-theme attribute from preferences", () => { + const html = renderPage("Test", "", { ...defaultPrefs, theme: "dark" }); + expect(html).toContain('data-theme="dark"'); + }); + + it("includes inlined CSS in a style tag", () => { + const html = renderPage("Test", "", defaultPrefs); + expect(html).toContain(""); + }); + + it("includes timezone picker with all 5 zones", () => { + const html = renderPage("Test", "", defaultPrefs); + expect(html).toContain("Eastern"); + expect(html).toContain("Central"); + expect(html).toContain("Pacific"); + expect(html).toContain("UTC"); + expect(html).toContain("Amsterdam"); + }); + + it("highlights the current timezone", () => { + const html = renderPage("Test", "", { ...defaultPrefs, tz: "pacific" }); + expect(html).toContain('aria-current="true"'); + }); + + it("includes mode toggle links", () => { + const html = renderPage("Test", "", defaultPrefs); + expect(html).toContain("VCR"); + expect(html).toContain("All Shows"); + }); + + it("includes theme toggle links", () => { + const html = renderPage("Test", "", defaultPrefs); + expect(html).toContain("Light"); + expect(html).toContain("Dark"); + expect(html).toContain("System"); + }); + + it("includes week navigation", () => { + const html = renderPage("Test", "", defaultPrefs); + expect(html).toContain("W44"); // prev + expect(html).toContain("W46"); // next + }); + + it("renders body content inside main", () => { + const html = renderPage("Test", "

calendar here

", defaultPrefs); + expect(html).toContain("

calendar here

"); + }); + + it("includes reset link", () => { + const html = renderPage("Test", "", defaultPrefs); + expect(html).toContain("reset"); + }); + + it("contains no script tags", () => { + const html = renderPage("Test", "", defaultPrefs); + expect(html).not.toContain(" { + const isCurrent = key === prefs.tz; + const href = buildLink("/week", { ...prefs, tz: key }); + return `${config.label}`; + }) + .join(" · "); + + const modeLinks = [ + { key: "vcr", label: "VCR" }, + { key: "all", label: "All Shows" }, + ] + .map(({ key, label }) => { + const isCurrent = key === prefs.mode; + const href = buildLink("/week", { ...prefs, mode: key }); + return `${label}`; + }) + .join(" | "); + + const themeLinks = [ + { key: "light", label: "☀️ Light" }, + { key: "dark", label: "🌙 Dark" }, + { key: "system", label: "⚙️ System" }, + ] + .map(({ key, label }) => { + const isCurrent = key === prefs.theme; + const href = buildLink("/week", { ...prefs, theme: key }); + return `${label}`; + }) + .join(" · "); + + const prevLink = buildLink("/week", { ...prefs, week: prev }); + const nextLink = buildLink("/week", { ...prefs, week: next }); + + return ` + + + + + ${escapeHtml(title)} — VCR Calendar + + + + +
${bodyContent}
+ + +`; +} + +function buildLink(path: string, prefs: Record): string { + const params = new URLSearchParams(); + if (prefs.tz && prefs.tz !== "eastern") params.set("tz", prefs.tz); + if (prefs.theme && prefs.theme !== "system") params.set("theme", prefs.theme); + if (prefs.mode && prefs.mode !== "vcr") params.set("mode", prefs.mode); + if (prefs.week) params.set("week", prefs.week); + const qs = params.toString(); + return qs ? `${path}?${qs}` : path; +} + +function escapeHtml(s: string): string { + return s.replace(/&/g, "&").replace(//g, ">").replace(/"/g, """); +}