Open-source status page, incident response and uptime monitoring
TypeScript alertingincident-managementmcpmcp-servermonitoringmonitoring-as-codeobservabilityon-callopen-sourcesrestatus-pagestatuspagesynthetic-monitoringterraformtinybirdtursouptimeuptime-checkeruptime-monitor
import {
afterEach,
beforeEach,
describe,
expect,
test,
vi,
type Mock,
} from "vitest";
import { createStatuspageClient } from "./client";
import {
MOCK_COMPONENTS,
MOCK_COMPONENT_GROUPS,
MOCK_INCIDENTS,
MOCK_PAGES,
MOCK_SUBSCRIBERS,
} from "./fixtures";
const originalFetch = globalThis.fetch;
function mockFetch(data: unknown, status = 200) {
globalThis.fetch = vi.fn(() =>
Promise.resolve(
new Response(JSON.stringify(data), {
status,
statusText: status === 200 ? "OK" : "Unauthorized",
headers: { "Content-Type": "application/json" },
}),
),
) as typeof globalThis.fetch;
}
/**
* Mock fetch for paginated endpoints: returns `data` on first call, `[]` on subsequent calls.
*/
function mockFetchPaginated(data: unknown, status = 200) {
let callCount = 0;
globalThis.fetch = vi.fn(() => {
callCount++;
const body = callCount === 1 ? data : [];
return Promise.resolve(
new Response(JSON.stringify(body), {
status,
statusText: status === 200 ? "OK" : "Unauthorized",
headers: { "Content-Type": "application/json" },
}),
);
}) as typeof globalThis.fetch;
}
describe("StatuspageClient", () => {
let client: ReturnType<typeof createStatuspageClient>;
beforeEach(() => {
client = createStatuspageClient("test-api-key");
});
afterEach(() => {
globalThis.fetch = originalFetch;
});
test("getPages returns parsed pages", async () => {
mockFetch(MOCK_PAGES);
const pages = await client.getPages();
expect(pages).toEqual(MOCK_PAGES);
expect(pages).toHaveLength(1);
expect(pages[0].name).toBe("Acme Corp Status");
});
test("getComponents returns parsed components", async () => {
mockFetchPaginated(MOCK_COMPONENTS);
const components = await client.getComponents("sp_page_001");
expect(components).toEqual(MOCK_COMPONENTS);
expect(components).toHaveLength(4);
expect(components[2].status).toBe("degraded_performance");
});
test("getComponentGroups returns parsed groups", async () => {
mockFetchPaginated(MOCK_COMPONENT_GROUPS);
const groups = await client.getComponentGroups("sp_page_001");
expect(groups).toEqual(MOCK_COMPONENT_GROUPS);
expect(groups).toHaveLength(1);
expect(groups[0].name).toBe("Core Services");
expect(groups[0].components).toEqual(["sp_comp_001", "sp_comp_002"]);
});
test("getIncidents returns parsed incidents with updates", async () => {
mockFetchPaginated(MOCK_INCIDENTS);
const incidents = await client.getIncidents("sp_page_001");
expect(incidents).toEqual(MOCK_INCIDENTS);
expect(incidents).toHaveLength(3);
expect(incidents[0].incident_updates).toHaveLength(4);
expect(incidents[0].postmortem_body).toBeTruthy();
expect(incidents[1].status).toBe("identified");
expect(incidents[2].scheduled_for).toBe("2024-06-20T02:00:00.000Z");
});
test("getIncidents accepts postmortem status on incidents and updates", async () => {
const [incident] = MOCK_INCIDENTS;
const updates = incident.incident_updates ?? [];
mockFetchPaginated([
{
...incident,
status: "postmortem",
incident_updates: [
{ ...updates[0], id: "sp_upd_postmortem", status: "postmortem" },
...updates,
],
},
]);
const incidents = await client.getIncidents("sp_page_001");
expect(incidents[0].status).toBe("postmortem");
expect(incidents[0].incident_updates?.[0].status).toBe("postmortem");
});
test("getIncidents accepts maintenance impact and empty component status", async () => {
const scheduled = MOCK_INCIDENTS[2];
mockFetchPaginated([
{
...scheduled,
impact: "maintenance",
components: [{ ...MOCK_COMPONENTS[0], status: "" }],
},
]);
const incidents = await client.getIncidents("sp_page_001");
expect(incidents[0].impact).toBe("maintenance");
expect(incidents[0].components?.[0].status).toBe("");
});
test("getIncidents parses unrecognised values as unknown", async () => {
const [incident] = MOCK_INCIDENTS;
const updates = incident.incident_updates ?? [];
mockFetchPaginated([
{
...incident,
status: "brand_new_status",
impact: "brand_new_impact",
incident_updates: [{ ...updates[0], status: "brand_new_status" }],
},
]);
const incidents = await client.getIncidents("sp_page_001");
expect(incidents[0].status).toBe("unknown");
expect(incidents[0].impact).toBe("unknown");
expect(incidents[0].incident_updates?.[0].status).toBe("unknown");
});
test("getSubscribers accepts teams subscribers", async () => {
mockFetchPaginated([{ ...MOCK_SUBSCRIBERS[2], mode: "teams" }]);
const subscribers = await client.getSubscribers("sp_page_001");
expect(subscribers[0].mode).toBe("teams");
});
test("getSubscribers accepts subscribers with omitted optional fields", async () => {
const {
phone_number,
phone_country,
display_phone_number,
obfuscated_channel_name,
workspace_name,
components,
quarantined_at,
...subscriber
} = MOCK_SUBSCRIBERS[0];
mockFetchPaginated([subscriber]);
const subscribers = await client.getSubscribers("sp_page_001");
expect(subscribers[0].email).toBe(MOCK_SUBSCRIBERS[0].email);
expect(subscribers[0].workspace_name).toBeUndefined();
});
test("getSubscribers accepts the real API shape without page_id", async () => {
mockFetchPaginated([
{
id: "ht9cy29jdb3r",
mode: "email",
email: "[email protected]",
created_at: "03-14-22 12:19 CET",
confirmed_at: "03-14-22 12:19 CET",
skip_confirmation_notification: false,
quarantined_at: null,
purge_at: null,
},
]);
const subscribers = await client.getSubscribers("sp_page_001");
expect(subscribers[0].email).toBe("[email protected]");
expect(subscribers[0].page_id).toBeUndefined();
});
test("getIncidents accepts incidents with omitted optional fields", async () => {
const [incident] = MOCK_INCIDENTS;
const { metadata, postmortem_body, shortlink, ...rest } = incident;
const updates = (incident.incident_updates ?? []).map(
({ affected_components, display_at, deliver_notifications, ...u }) => u,
);
mockFetchPaginated([{ ...rest, incident_updates: updates }]);
const incidents = await client.getIncidents("sp_page_001");
expect(incidents[0].postmortem_body).toBeUndefined();
expect(
incidents[0].incident_updates?.[0].affected_components,
).toBeUndefined();
});
test("getSubscribers returns parsed subscribers", async () => {
mockFetchPaginated(MOCK_SUBSCRIBERS);
const subscribers = await client.getSubscribers("sp_page_001");
expect(subscribers).toEqual(MOCK_SUBSCRIBERS);
expect(subscribers).toHaveLength(5);
expect(subscribers[0].mode).toBe("email");
expect(subscribers[2].mode).toBe("webhook");
expect(subscribers[3].mode).toBe("sms");
expect(subscribers[4].mode).toBe("slack");
});
test("throws on API error (401)", async () => {
mockFetch({ error: "Unauthorized" }, 401);
await expect(client.getPages()).rejects.toThrow(
"Statuspage API error: 401 Unauthorized for /pages",
);
});
test("getPage returns a single parsed page", async () => {
mockFetch(MOCK_PAGES[0]);
const singlePage = await client.getPage("sp_page_001");
expect(singlePage).toEqual(MOCK_PAGES[0]);
expect(singlePage.id).toBe("sp_page_001");
expect(singlePage.name).toBe("Acme Corp Status");
});
test("getPage calls correct URL path", async () => {
mockFetch(MOCK_PAGES[0]);
await client.getPage("sp_page_001");
const fetchMock = globalThis.fetch as unknown as Mock;
const [url] = fetchMock.mock.calls[0] as [string, RequestInit];
expect(url).toBe("https://api.statuspage.io/v1/pages/sp_page_001");
});
test("getPage throws on non-200 response", async () => {
mockFetch({ error: "Not Found" }, 404);
await expect(client.getPage("nonexistent")).rejects.toThrow(
"Statuspage API error: 404",
);
});
test("getPage throws on schema mismatch", async () => {
mockFetch({ id: 123, unexpected: true });
await expect(client.getPage("sp_page_001")).rejects.toThrow();
});
test("sends correct auth header", async () => {
mockFetch(MOCK_PAGES);
await client.getPages();
const fetchMock = globalThis.fetch as unknown as Mock;
expect(fetchMock).toHaveBeenCalledTimes(1);
const [url, options] = fetchMock.mock.calls[0] as [string, RequestInit];
expect(url).toBe("https://api.statuspage.io/v1/pages");
expect((options.headers as Record<string, string>).Authorization).toBe(
"OAuth test-api-key",
);
});
});