Files

92 lines
2.7 KiB
TypeScript

import {
createProjectHealthSnapshot,
getDemoAvailability,
type ProjectHealthSnapshot,
} from "./project-health";
describe("project health snapshot", () => {
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("classifies demo availability from the latest observation", () => {
expect(getDemoAvailability(null, undefined)).toBe("none");
expect(getDemoAvailability("https://demo.example", undefined)).toBe(
"unknown",
);
expect(
getDemoAvailability("https://demo.example", {
status: null,
contentType: null,
checkedAt: "2026-07-10T11:55:00.000Z",
error: "network unavailable",
}),
).toBe("unknown");
expect(
getDemoAvailability("https://demo.example", snapshot.projects.online),
).toBe("available");
expect(
getDemoAvailability("https://demo.example", snapshot.projects.redirect),
).toBe("unavailable");
expect(
getDemoAvailability("https://demo.example", {
status: 200,
contentType: "application/json",
checkedAt: "2026-07-10T11:55:00.000Z",
}),
).toBe("unavailable");
expect(
getDemoAvailability("https://demo.example", {
status: 200,
contentType: "text/html+custom; charset=utf-8",
checkedAt: "2026-07-10T11:55:00.000Z",
}),
).toBe("unavailable");
expect(
getDemoAvailability("https://demo.example", snapshot.projects.stale),
).toBe("available");
});
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);
});
});