Open-source status page, incident response and uptime monitoring
TypeScript alertingincident-managementmcpmcp-servermonitoringmonitoring-as-codeobservabilityon-callopen-sourcesrestatus-pagestatuspagesynthetic-monitoringterraformtinybirdtursouptimeuptime-checkeruptime-monitor
import "./test-preload.ts";
import { selectNotificationSchema } from "@openstatus/db/src/schema";
import { EmailClient } from "@openstatus/emails/src/client";
import {
afterEach,
beforeEach,
describe,
expect,
test,
vi,
type MockInstance,
} from "vitest";
import { sendAlert, sendDegraded, sendRecovery } from "./index";
// biome-ignore lint/suspicious/noExplicitAny: stub over the EmailClient method
let sendMonitorAlertMock: MockInstance;
describe("Email Notifications", () => {
beforeEach(() => {
sendMonitorAlertMock = vi
.spyOn(EmailClient.prototype, "sendMonitorAlert")
.mockImplementation(() => Promise.resolve());
});
afterEach(() => {
sendMonitorAlertMock.mockRestore();
});
const createMockMonitor = () => ({
id: "monitor-1",
name: "API Health Check",
url: "https://api.example.com/health",
jobType: "http" as const,
periodicity: "5m" as const,
status: "active" as const,
createdAt: new Date(),
updatedAt: new Date(),
region: "iad",
});
const createMockNotification = () => ({
id: 1,
name: "Email Notification",
provider: "email",
workspaceId: 1,
createdAt: new Date(),
updatedAt: new Date(),
data: '{"email":"[email protected]"}',
});
test("Send Alert", async () => {
const monitor = createMockMonitor();
const notification = selectNotificationSchema.parse(
createMockNotification(),
);
await sendAlert({
// @ts-expect-error
monitor,
notification,
statusCode: 500,
message: "Something went wrong",
latency: 1500,
regions: ["iad"],
cronTimestamp: Date.now(),
});
expect(sendMonitorAlertMock).toHaveBeenCalledTimes(1);
const callArgs = sendMonitorAlertMock.mock.calls[0][0];
expect(callArgs.name).toBe("API Health Check");
expect(callArgs.type).toBe("alert");
expect(callArgs.to).toBe("[email protected]");
expect(callArgs.url).toBe("https://api.example.com/health");
expect(callArgs.status).toBe("500");
expect(callArgs.latency).toBe("1500 ms");
expect(callArgs.monitorId).toBe("monitor-1");
expect(callArgs.region).toBe("Ashburn, Virginia, USA");
expect(callArgs.message).toBe("Something went wrong");
expect(callArgs.timestamp).toBeDefined();
});
test("Send Alert without optional fields", async () => {
const monitor = createMockMonitor();
const notification = selectNotificationSchema.parse(
createMockNotification(),
);
await sendAlert({
// @ts-expect-error
monitor,
notification,
cronTimestamp: Date.now(),
});
expect(sendMonitorAlertMock).toHaveBeenCalledTimes(1);
const callArgs = sendMonitorAlertMock.mock.calls[0][0];
expect(callArgs.status).toBeUndefined();
expect(callArgs.latency).toBe("N/A");
expect(callArgs.region).toBe("N/A");
});
test("Send Recovery", async () => {
const monitor = createMockMonitor();
const notification = selectNotificationSchema.parse(
createMockNotification(),
);
await sendRecovery({
// @ts-expect-error
monitor,
notification,
statusCode: 200,
latency: 100,
regions: ["ams"],
cronTimestamp: Date.now(),
});
expect(sendMonitorAlertMock).toHaveBeenCalledTimes(1);
const callArgs = sendMonitorAlertMock.mock.calls[0][0];
expect(callArgs.type).toBe("recovery");
expect(callArgs.name).toBe("API Health Check");
expect(callArgs.to).toBe("[email protected]");
expect(callArgs.status).toBe("200");
expect(callArgs.latency).toBe("100 ms");
expect(callArgs.region).toBe("Amsterdam, Netherlands");
});
test("Send Degraded", async () => {
const monitor = createMockMonitor();
const notification = selectNotificationSchema.parse(
createMockNotification(),
);
await sendDegraded({
// @ts-expect-error
monitor,
notification,
statusCode: 503,
latency: 2000,
regions: ["lax"],
cronTimestamp: Date.now(),
});
expect(sendMonitorAlertMock).toHaveBeenCalledTimes(1);
const callArgs = sendMonitorAlertMock.mock.calls[0][0];
expect(callArgs.type).toBe("degraded");
expect(callArgs.name).toBe("API Health Check");
expect(callArgs.status).toBe("503");
expect(callArgs.latency).toBe("2000 ms");
expect(callArgs.region).toBe("Los Angeles, California, USA");
});
test("Send Degraded passes the threshold and incident start", async () => {
const monitor = { ...createMockMonitor(), degradedAfter: 250 };
const notification = selectNotificationSchema.parse(
createMockNotification(),
);
const startedAt = new Date("2026-10-13T17:29:00Z");
await sendDegraded({
// @ts-expect-error
monitor,
notification,
latency: 300,
regions: ["ams"],
cronTimestamp: Date.now(),
// @ts-expect-error
incident: { startedAt },
});
expect(sendMonitorAlertMock).toHaveBeenCalledTimes(1);
const callArgs = sendMonitorAlertMock.mock.calls[0][0];
expect(callArgs.degradedAfter).toBe(250);
expect(callArgs.firstSeen).toBe(startedAt.toISOString());
});
test("Handles invalid notification data gracefully", async () => {
const monitor = createMockMonitor();
const invalidNotification = selectNotificationSchema.parse({
id: 1,
name: "Email Notification",
provider: "email",
workspaceId: 1,
createdAt: new Date(),
updatedAt: new Date(),
data: '{"invalid":"data"}',
});
await sendAlert({
// @ts-expect-error
monitor,
notification: invalidNotification,
cronTimestamp: Date.now(),
});
// Should not call sendMonitorAlert when data is invalid
expect(sendMonitorAlertMock).toHaveBeenCalledTimes(0);
});
});