jest.mock("node:fs/promises", () => ({ readFile: jest.fn(), })); jest.mock("./project-health", () => ({ ...jest.requireActual("./project-health"), createProjectHealthSnapshot: jest.fn(), })); import { readFile } from "node:fs/promises"; import { portfolio } from "./portfolio-data"; import { loadFeaturedProjects } from "./portfolio-data.server"; import { createProjectHealthSnapshot } from "./project-health"; const featuredIds = portfolio.projects .filter(({ featured }) => featured !== false) .map(({ id }) => id); describe("loadFeaturedProjects", () => { beforeEach(() => { jest.clearAllMocks(); jest.mocked(readFile).mockResolvedValue( JSON.stringify({ checkedAt: "2026-07-11T05:31:16.931Z", projects: {}, }) as never, ); jest .mocked(createProjectHealthSnapshot) .mockRejectedValue( new Error("remote refresh should not run during render"), ); }); it("returns every featured project without network in production", async () => { const nodeEnv = jest.replaceProperty(process.env, "NODE_ENV", "production"); jest.mocked(readFile).mockRejectedValueOnce(new Error("missing snapshot")); try { const result = await loadFeaturedProjects(); expect(result.map(({ id }) => id)).toEqual(featuredIds); expect( result.find(({ id }) => id === "sev-modaintima")?.demoAvailability, ).toBe("unknown"); expect(createProjectHealthSnapshot).not.toHaveBeenCalled(); } finally { nodeEnv.restore(); } }); it("does not refresh an old snapshot in production", async () => { const nodeEnv = jest.replaceProperty(process.env, "NODE_ENV", "production"); try { const result = await loadFeaturedProjects(); expect(result.map(({ id }) => id)).toEqual(featuredIds); expect(createProjectHealthSnapshot).not.toHaveBeenCalled(); } finally { nodeEnv.restore(); } }); it("refreshes an old snapshot in development", async () => { const nodeEnv = jest.replaceProperty( process.env, "NODE_ENV", "development", ); const checkedAt = new Date().toISOString(); jest.mocked(createProjectHealthSnapshot).mockResolvedValue({ checkedAt, projects: { "sev-modaintima": { status: 200, contentType: "text/html; charset=utf-8", checkedAt, }, }, }); try { const result = await loadFeaturedProjects(); expect(result.map(({ id }) => id)).toEqual(featuredIds); expect( result.find(({ id }) => id === "sev-modaintima")?.demoAvailability, ).toBe("available"); expect(createProjectHealthSnapshot).toHaveBeenCalledTimes(1); expect(createProjectHealthSnapshot).toHaveBeenCalledWith( expect.arrayContaining([ expect.objectContaining({ id: "sev-modaintima", demoUrl: "https://sevmodaintima.com.br", }), ]), ); } finally { nodeEnv.restore(); } }); it("does not refresh a fresh snapshot in development", async () => { const nodeEnv = jest.replaceProperty( process.env, "NODE_ENV", "development", ); const checkedAt = new Date().toISOString(); jest.mocked(readFile).mockResolvedValueOnce( JSON.stringify({ checkedAt, projects: { "sev-modaintima": { status: 200, contentType: "text/html", checkedAt, }, }, }) as never, ); try { const result = await loadFeaturedProjects(); expect( result.find(({ id }) => id === "sev-modaintima")?.demoAvailability, ).toBe("available"); expect(createProjectHealthSnapshot).not.toHaveBeenCalled(); } finally { nodeEnv.restore(); } }); it("refreshes a missing snapshot in development", async () => { const nodeEnv = jest.replaceProperty( process.env, "NODE_ENV", "development", ); const checkedAt = new Date().toISOString(); jest.mocked(readFile).mockRejectedValueOnce(new Error("missing snapshot")); jest.mocked(createProjectHealthSnapshot).mockResolvedValue({ checkedAt, projects: { "sev-modaintima": { status: 200, contentType: "text/html", checkedAt, }, }, }); try { const result = await loadFeaturedProjects(); expect(result.map(({ id }) => id)).toEqual(featuredIds); expect(createProjectHealthSnapshot).toHaveBeenCalledTimes(1); } finally { nodeEnv.restore(); } }); it("preserves prior observations when refreshed checks fail", async () => { const nodeEnv = jest.replaceProperty( process.env, "NODE_ENV", "development", ); const refreshedAt = new Date().toISOString(); jest.mocked(readFile).mockResolvedValueOnce( JSON.stringify({ checkedAt: "2020-01-01T00:00:00.000Z", projects: { "sev-modaintima": { status: 200, contentType: "text/html", checkedAt: "2020-01-01T00:00:00.000Z", }, }, }) as never, ); jest.mocked(createProjectHealthSnapshot).mockResolvedValue({ checkedAt: refreshedAt, projects: { "sev-modaintima": { status: null, contentType: null, checkedAt: refreshedAt, error: "network unavailable", }, "twins-lab": { status: null, contentType: null, checkedAt: refreshedAt, error: "network unavailable", }, }, }); try { const result = await loadFeaturedProjects(); const previous = result.find(({ id }) => id === "sev-modaintima"); const unknown = result.find(({ id }) => id === "twins-lab"); expect(result.map(({ id }) => id)).toEqual(featuredIds); expect(previous?.demoAvailability).toBe("available"); expect(previous?.demoCheckedAt).toBe("2020-01-01T00:00:00.000Z"); expect(unknown?.demoAvailability).toBe("none"); expect(unknown?.demoCheckedAt).toBeNull(); expect(createProjectHealthSnapshot).toHaveBeenCalledTimes(1); } finally { nodeEnv.restore(); } }); });