import { createProjectHealthSnapshot, filterLiveProjects, isCurrentLiveCheck, type ProjectHealthSnapshot, } from "./project-health"; describe("project health snapshot", () => { const now = new Date("2026-07-10T12:00:00.000Z"); const snapshot: ProjectHealthSnapshot = { checkedAt: "2026-07-10T11:55:00.000Z", projects: { online: { status: 200, contentType: "text/html; charset=utf-8", checkedAt: "2026-07-10T11:55:00.000Z", }, redirect: { status: 301, contentType: "text/html", checkedAt: "2026-07-10T11:55:00.000Z", }, stale: { status: 200, contentType: "text/html", checkedAt: "2026-07-10T10:30:00.000Z", }, }, }; it("accepts only fresh final HTTP 200 HTML projects", () => { expect(isCurrentLiveCheck(snapshot.projects.online, now)).toBe(true); expect(isCurrentLiveCheck(snapshot.projects.redirect, now)).toBe(false); expect(isCurrentLiveCheck(snapshot.projects.stale, now)).toBe(false); expect( filterLiveProjects( [ { id: "online", featured: true }, { id: "redirect", featured: true }, { id: "stale", featured: true }, { id: "hidden", featured: false }, ], snapshot, now, ).map((project) => project.id), ).toEqual(["online"]); }); it("fails closed when the snapshot is absent or expired", () => { expect( filterLiveProjects([{ id: "online", featured: true }], null, now), ).toEqual([]); expect( filterLiveProjects( [{ id: "online", featured: true }], { ...snapshot, checkedAt: "2026-07-10T10:00:00.000Z" }, now, ), ).toEqual([]); }); it("creates a fresh health snapshot when the stored result is unavailable", async () => { const fetcher = jest.fn(async (url: string) => ({ status: url.includes("online") ? 200 : 503, url, headers: new Headers({ "content-type": url.includes("online") ? "text/html" : "text/plain", }), body: { cancel: jest.fn() }, })); const result = await createProjectHealthSnapshot( [ { id: "online", demoUrl: "https://online.example" }, { id: "offline", demoUrl: "https://offline.example" }, ], fetcher, new Date("2026-07-10T12:00:00.000Z"), ); expect(result.projects.online.status).toBe(200); expect(result.projects.online.contentType).toBe("text/html"); expect(result.projects.offline.status).toBe(503); expect(fetcher).toHaveBeenCalledTimes(2); }); });