Open-source status page, incident response and uptime monitoring
TypeScript alertingincident-managementmcpmcp-servermonitoringmonitoring-as-codeobservabilityon-callopen-sourcesrestatus-pagestatuspagesynthetic-monitoringterraformtinybirdtursouptimeuptime-checkeruptime-monitor
import { selectNotificationSchema } from "@openstatus/db/src/schema";
import {
afterEach,
beforeEach,
describe,
expect,
test,
vi,
type MockInstance,
} from "vitest";
import { sendAlert, sendDegraded, sendRecovery, sendTest } from "./index";
describe("OpsGenie Notifications", () => {
let fetchMock: MockInstance;
beforeEach(() => {
fetchMock = vi
.spyOn(globalThis, "fetch")
.mockImplementation(() =>
Promise.resolve(new Response(null, { status: 200 })),
);
});
afterEach(() => {
fetchMock.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: "us-east-1",
});
const createMockNotification = (region: "eu" | "us" = "us") => ({
id: 1,
name: "OpsGenie Notification",
provider: "opsgenie",
workspaceId: 1,
createdAt: new Date(),
updatedAt: new Date(),
data: JSON.stringify({
opsgenie: {
apiKey: "test-api-key-123",
region,
},
}),
});
const createMockIncident = () => ({
id: 1,
title: "API Health Check is down",
summary: "API Health Check is down",
status: "triage" as const,
monitorId: "monitor-1",
workspaceId: 1,
startedAt: Date.now(),
});
test("Send Alert with US region", async () => {
const monitor = createMockMonitor();
const notification = selectNotificationSchema.parse(
createMockNotification("us"),
);
const incident = createMockIncident();
await sendAlert({
// @ts-expect-error
monitor,
notification,
statusCode: 500,
message: "Something went wrong",
// @ts-expect-error
incident,
cronTimestamp: Date.now(),
});
expect(fetchMock).toHaveBeenCalledTimes(1);
const callArgs = fetchMock.mock.calls[0];
expect(callArgs[0]).toBe("https://api.opsgenie.com/v2/alerts");
expect(callArgs[1].method).toBe("POST");
expect(callArgs[1].headers["Content-Type"]).toBe("application/json");
expect(callArgs[1].headers.Authorization).toBe("GenieKey test-api-key-123");
const body = JSON.parse(callArgs[1].body);
expect(body.message).toBe("API Health Check is down");
expect(body.alias).toBe("monitor-1");
expect(body.details.severity).toBe("down");
expect(body.details.status).toBe(500);
expect(body.details.message).toBe("Something went wrong");
});
test("Send Alert with EU region", async () => {
const monitor = createMockMonitor();
const notification = selectNotificationSchema.parse(
createMockNotification("eu"),
);
const incident = createMockIncident();
await sendAlert({
// @ts-expect-error
monitor,
notification,
statusCode: 500,
message: "Error",
// @ts-expect-error
incident,
cronTimestamp: Date.now(),
});
expect(fetchMock).toHaveBeenCalledTimes(1);
const callArgs = fetchMock.mock.calls[0];
expect(callArgs[0]).toBe("https://api.eu.opsgenie.com/v2/alerts");
});
test("Send Degraded", async () => {
const monitor = createMockMonitor();
const notification = selectNotificationSchema.parse(
createMockNotification(),
);
const incident = createMockIncident();
await sendDegraded({
// @ts-expect-error
monitor,
notification,
statusCode: 503,
message: "Service degraded",
// @ts-expect-error
incident,
cronTimestamp: Date.now(),
});
expect(fetchMock).toHaveBeenCalledTimes(1);
const callArgs = fetchMock.mock.calls[0];
const body = JSON.parse(callArgs[1].body);
expect(body.details.severity).toBe("degraded");
expect(body.message).toBe("API Health Check is degraded");
});
for (const { region, message, url } of [
{
region: "us",
message: "Service recovered",
url: "https://api.opsgenie.com/v2/alerts/monitor-1/close?identifierType=alias",
},
{
region: "eu",
message: undefined,
url: "https://api.eu.opsgenie.com/v2/alerts/monitor-1/close?identifierType=alias",
},
] as const) {
test(`Recovery closes the monitor alias in ${region}`, async () => {
fetchMock.mockRestore();
fetchMock = vi
.spyOn(globalThis, "fetch")
.mockImplementation(() =>
Promise.resolve(new Response(null, { status: 202 })),
);
await sendRecovery({
// @ts-expect-error
monitor: createMockMonitor(),
notification: selectNotificationSchema.parse(
createMockNotification(region),
),
statusCode: 200,
message,
cronTimestamp: Date.now(),
});
expect(fetchMock).toHaveBeenCalledTimes(1);
const [requestUrl, options] = fetchMock.mock.calls[0];
expect(requestUrl).toBe(url);
expect(options.method).toBe("POST");
expect(options.headers["Content-Type"]).toBe("application/json");
expect(options.headers.Authorization).toBe("GenieKey test-api-key-123");
expect(JSON.parse(options.body)).toEqual({ source: "OpenStatus" });
});
}
test("Recovery rejects an unsuccessful close response", async () => {
fetchMock.mockRestore();
fetchMock = vi
.spyOn(globalThis, "fetch")
.mockImplementation(() =>
Promise.resolve(new Response(null, { status: 503 })),
);
await expect(
sendRecovery({
// @ts-expect-error
monitor: createMockMonitor(),
notification: selectNotificationSchema.parse(createMockNotification()),
message: "Service recovered",
cronTimestamp: Date.now(),
}),
).rejects.toThrow("503");
expect(fetchMock).toHaveBeenCalledTimes(1);
});
test("Recovery propagates a rejected close request", async () => {
const error = new Error("Connection reset");
fetchMock.mockRestore();
fetchMock = vi
.spyOn(globalThis, "fetch")
.mockImplementation(() => Promise.reject(error));
await expect(
sendRecovery({
// @ts-expect-error
monitor: createMockMonitor(),
notification: selectNotificationSchema.parse(createMockNotification()),
message: "Service recovered",
cronTimestamp: Date.now(),
}),
).rejects.toBe(error);
expect(fetchMock).toHaveBeenCalledTimes(1);
});
test("Handle fetch error gracefully", async () => {
fetchMock.mockRestore();
fetchMock = vi
.spyOn(globalThis, "fetch")
.mockImplementation(() => Promise.reject(new Error("Network error")));
const monitor = createMockMonitor();
const notification = selectNotificationSchema.parse(
createMockNotification(),
);
const incident = createMockIncident();
await expect(
sendAlert({
// @ts-expect-error
monitor,
notification,
statusCode: 500,
message: "Error",
// @ts-expect-error
incident,
cronTimestamp: Date.now(),
}),
).rejects.toThrow();
expect(fetchMock).toHaveBeenCalledTimes(1);
});
test("Send Test returns false on error", async () => {
fetchMock.mockRestore();
fetchMock = vi
.spyOn(globalThis, "fetch")
.mockImplementation(() => Promise.reject(new Error("Network error")));
const result = await sendTest({
apiKey: "test-api-key",
region: "us",
});
expect(result).toBe(false);
expect(fetchMock).toHaveBeenCalledTimes(1);
});
});