Skip to content

Open-source status page, incident response and uptime monitoring

add test (#1557)

Thibault Le Ouay30186467b0ca
diff --git a/apps/server/package.json b/apps/server/package.json
index 76b667b06..f4edeae83 100644
--- a/apps/server/package.json
+++ b/apps/server/package.json
@@ -40,7 +40,7 @@
"devDependencies": {
"@openstatus/tsconfig": "workspace:*",
"@types/validator": "13.11.6",
- "bun-types": "1.0.11",
+ "bun-types": "1.3.1",
"dotenv": "16.3.1"
}
}
diff --git a/packages/notifications/email/package.json b/packages/notifications/email/package.json
index 822d739b3..0f7af64bb 100644
--- a/packages/notifications/email/package.json
+++ b/packages/notifications/email/package.json
@@ -3,6 +3,9 @@
"version": "0.0.0",
"main": "src/index.ts",
"description": "Log drains Vercel integration.",
+ "scripts": {
+ "test": "bun test"
+ },
"dependencies": {
"@openstatus/db": "workspace:*",
"@openstatus/emails": "workspace:*",
@@ -18,6 +21,7 @@
"devDependencies": {
"@openstatus/tsconfig": "workspace:*",
"@types/node": "22.10.2",
+ "bun-types": "1.3.1",
"typescript": "5.7.2"
}
}
diff --git a/packages/notifications/email/src/index.test.ts b/packages/notifications/email/src/index.test.ts
new file mode 100644
index 000000000..b74cd0b7a
--- /dev/null
+++ b/packages/notifications/email/src/index.test.ts
@@ -0,0 +1,119 @@
+import {
+ describe,
+ mock,
+ // jest,
+ test,
+} from "bun:test";
+
+import { selectNotificationSchema } from "@openstatus/db/src/schema";
+import { sendAlert, sendDegraded, sendRecovery } from "./index";
+
+mock.module("@openstatus/emails/src/client", () => ({
+ EmailClient: mock(() => ({
+ sendMonitorAlert: mock(async () => {}),
+ })),
+}));
+
+describe("Email Notifications", () => {
+ test("Send degraded", async () => {
+ const monitor = {
+ 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, // or "down", "degraded"
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ region: "us-east-1",
+ };
+
+ const a = {
+ id: 1,
+ name: "email Notification",
+ provider: "email",
+ workspaceId: 1,
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ data: '{"email":"[email protected]"}',
+ };
+
+ const n = selectNotificationSchema.parse(a);
+ await sendDegraded({
+ // @ts-expect-error
+ monitor,
+ notification: n,
+ statusCode: 500,
+ message: "Something went wrong",
+ cronTimestamp: Date.now(),
+ });
+ });
+
+ test("Send Recovered", async () => {
+ const monitor = {
+ 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, // or "down", "degraded"
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ region: "us-east-1",
+ };
+
+ const a = {
+ id: 1,
+ name: "Email Notification",
+ provider: "email",
+ workspaceId: 1,
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ data: '{"email":"[email protected]"}',
+ };
+
+ const n = selectNotificationSchema.parse(a);
+ await sendRecovery({
+ // @ts-expect-error
+ monitor,
+ notification: n,
+ statusCode: 500,
+ message: "Something went wrong",
+ cronTimestamp: Date.now(),
+ });
+ });
+
+ test("Send Alert", async () => {
+ const monitor = {
+ 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, // or "down", "degraded"
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ region: "us-east-1",
+ };
+ const a = {
+ id: 1,
+ name: "PagerDuty Notification",
+ provider: "email",
+ workspaceId: 1,
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ data: '{"email":"[email protected]"}',
+ };
+
+ const n = selectNotificationSchema.parse(a);
+
+ await sendAlert({
+ // @ts-expect-error
+ monitor,
+ notification: n,
+ statusCode: 500,
+ message: "Something went wrong",
+ cronTimestamp: Date.now(),
+ });
+ });
+});
diff --git a/packages/notifications/email/src/index.ts b/packages/notifications/email/src/index.ts
index 7022a658b..a7bb4a786 100644
--- a/packages/notifications/email/src/index.ts
+++ b/packages/notifications/email/src/index.ts
@@ -9,8 +9,6 @@ import { EmailClient } from "@openstatus/emails/src/client";
import { regionDict } from "@openstatus/regions";
import { env } from "../env";
-const emailClient = new EmailClient({ apiKey: env.RESEND_API_KEY });
-
export const sendAlert = async ({
monitor,
notification,
@@ -29,6 +27,8 @@ export const sendAlert = async ({
region?: Region;
latency?: number;
}) => {
+ const emailClient = new EmailClient({ apiKey: env.RESEND_API_KEY });
+
const config = emailDataSchema.safeParse(JSON.parse(notification.data));
if (!config.success) return;
@@ -63,6 +63,8 @@ export const sendRecovery = async ({
region?: Region;
latency?: number;
}) => {
+ const emailClient = new EmailClient({ apiKey: env.RESEND_API_KEY });
+
const config = emailDataSchema.safeParse(JSON.parse(notification.data));
if (!config.success) return;
@@ -95,6 +97,8 @@ export const sendDegraded = async ({
region?: Region;
latency?: number;
}) => {
+ const emailClient = new EmailClient({ apiKey: env.RESEND_API_KEY });
+
const config = emailDataSchema.safeParse(JSON.parse(notification.data));
if (!config.success) return;
diff --git a/packages/notifications/email/tsconfig.json b/packages/notifications/email/tsconfig.json
index a8a8c9309..b4382e67e 100644
--- a/packages/notifications/email/tsconfig.json
+++ b/packages/notifications/email/tsconfig.json
@@ -1,4 +1,7 @@
{
"extends": "@openstatus/tsconfig/nextjs.json",
- "include": ["src", "*.ts"]
+ "include": ["src", "*.ts"],
+ "compilerOptions": {
+ "types": ["bun-types"]
+ }
}
diff --git a/packages/notifications/pagerduty/package.json b/packages/notifications/pagerduty/package.json
index e4d0b8e18..8abf3831d 100644
--- a/packages/notifications/pagerduty/package.json
+++ b/packages/notifications/pagerduty/package.json
@@ -17,6 +17,7 @@
"@types/node": "20.8.0",
"@types/react": "19.2.2",
"@types/react-dom": "19.2.2",
+ "bun-types": "1.3.1",
"typescript": "5.7.2"
}
}
diff --git a/packages/notifications/pagerduty/src/index.test.ts b/packages/notifications/pagerduty/src/index.test.ts
index abaee1ee3..edd1e83b7 100644
--- a/packages/notifications/pagerduty/src/index.test.ts
+++ b/packages/notifications/pagerduty/src/index.test.ts
@@ -15,6 +15,7 @@ describe("PagerDuty Notifications", () => {
let fetchMock: any = undefined;
beforeEach(() => {
+ // @ts-expect-error
fetchMock = spyOn(global, "fetch").mockImplementation(() =>
Promise.resolve(new Response(null, { status: 200 })),
);
diff --git a/packages/notifications/pagerduty/tsconfig.json b/packages/notifications/pagerduty/tsconfig.json
index a8a8c9309..b4382e67e 100644
--- a/packages/notifications/pagerduty/tsconfig.json
+++ b/packages/notifications/pagerduty/tsconfig.json
@@ -1,4 +1,7 @@
{
"extends": "@openstatus/tsconfig/nextjs.json",
- "include": ["src", "*.ts"]
+ "include": ["src", "*.ts"],
+ "compilerOptions": {
+ "types": ["bun-types"]
+ }
}
diff --git a/packages/notifications/slack/package.json b/packages/notifications/slack/package.json
index fb039a3f9..55efc51ff 100644
--- a/packages/notifications/slack/package.json
+++ b/packages/notifications/slack/package.json
@@ -2,6 +2,9 @@
"name": "@openstatus/notification-slack",
"version": "0.0.0",
"main": "src/index.ts",
+ "scripts": {
+ "test": "bun test"
+ },
"dependencies": {
"@openstatus/db": "workspace:*",
"zod": "3.24.2"
@@ -9,6 +12,7 @@
"devDependencies": {
"@openstatus/tsconfig": "workspace:*",
"@types/node": "22.10.2",
+ "bun-types": "1.3.1",
"typescript": "5.7.2"
}
}
diff --git a/packages/notifications/slack/src/index.test.ts b/packages/notifications/slack/src/index.test.ts
new file mode 100644
index 000000000..ec520c1df
--- /dev/null
+++ b/packages/notifications/slack/src/index.test.ts
@@ -0,0 +1,132 @@
+import {
+ afterEach,
+ beforeEach,
+ describe,
+ expect,
+ jest,
+ spyOn,
+ test,
+} from "bun:test";
+import { selectNotificationSchema } from "@openstatus/db/src/schema";
+import { sendAlert, sendDegraded, sendRecovery } from "./index";
+
+describe("Slack Notifications", () => {
+ // biome-ignore lint/suspicious/noExplicitAny: <explanation>
+ let fetchMock: any = undefined;
+
+ beforeEach(() => {
+ // @ts-expect-error
+ fetchMock = spyOn(global, "fetch").mockImplementation(() =>
+ Promise.resolve(new Response(null, { status: 200 })),
+ );
+ });
+
+ afterEach(() => {
+ jest.resetAllMocks();
+ });
+
+ test("Send degraded", async () => {
+ const monitor = {
+ 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, // or "down", "degraded"
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ region: "us-east-1",
+ };
+
+ const a = {
+ id: 1,
+ name: "slack Notification",
+ provider: "slack",
+ workspaceId: 1,
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ data: '{"slack":"https://hooks.slack.com/services/url"}',
+ };
+
+ const n = selectNotificationSchema.parse(a);
+ await sendDegraded({
+ // @ts-expect-error
+ monitor,
+ notification: n,
+ statusCode: 500,
+ message: "Something went wrong",
+ cronTimestamp: Date.now(),
+ });
+ expect(fetchMock).toHaveBeenCalled();
+ });
+
+ test("Send Recovered", async () => {
+ const monitor = {
+ 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, // or "down", "degraded"
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ region: "us-east-1",
+ };
+
+ const a = {
+ id: 1,
+ name: "slack Notification",
+ provider: "slack",
+ workspaceId: 1,
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ data: '{"slack":"https://hooks.slack.com/services/url"}',
+ };
+
+ const n = selectNotificationSchema.parse(a);
+ await sendRecovery({
+ // @ts-expect-error
+ monitor,
+ notification: n,
+ statusCode: 500,
+ message: "Something went wrong",
+ cronTimestamp: Date.now(),
+ });
+ expect(fetchMock).toHaveBeenCalled();
+ });
+
+ test("Send Alert", async () => {
+ const monitor = {
+ 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, // or "down", "degraded"
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ region: "us-east-1",
+ };
+ const a = {
+ id: 1,
+ name: "slack Notification",
+ provider: "slack",
+ workspaceId: 1,
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ data: '{"slack":"https://hooks.slack.com/services/url"}',
+ };
+
+ const n = selectNotificationSchema.parse(a);
+
+ await sendAlert({
+ // @ts-expect-error
+ monitor,
+ notification: n,
+ statusCode: 500,
+ message: "Something went wrong",
+ cronTimestamp: Date.now(),
+ });
+ expect(fetchMock).toHaveBeenCalled();
+ });
+});
diff --git a/packages/notifications/slack/tsconfig.json b/packages/notifications/slack/tsconfig.json
index a8a8c9309..b4382e67e 100644
--- a/packages/notifications/slack/tsconfig.json
+++ b/packages/notifications/slack/tsconfig.json
@@ -1,4 +1,7 @@
{
"extends": "@openstatus/tsconfig/nextjs.json",
- "include": ["src", "*.ts"]
+ "include": ["src", "*.ts"],
+ "compilerOptions": {
+ "types": ["bun-types"]
+ }
}
diff --git a/packages/notifications/twillio-sms/package.json b/packages/notifications/twillio-sms/package.json
index a72df5259..f15d41642 100644
--- a/packages/notifications/twillio-sms/package.json
+++ b/packages/notifications/twillio-sms/package.json
@@ -2,6 +2,9 @@
"name": "@openstatus/notification-twillio-sms",
"version": "0.0.0",
"main": "src/index.ts",
+ "scripts": {
+ "test": "bun test"
+ },
"dependencies": {
"@openstatus/db": "workspace:*",
"@t3-oss/env-core": "0.7.1",
@@ -12,6 +15,7 @@
"@openstatus/tsconfig": "workspace:*",
"@types/node": "22.10.2",
"@types/validator": "13.12.0",
+ "bun-types": "1.3.1",
"typescript": "5.7.2"
}
}
diff --git a/packages/notifications/twillio-sms/src/index.test.ts b/packages/notifications/twillio-sms/src/index.test.ts
new file mode 100644
index 000000000..f47d7b86f
--- /dev/null
+++ b/packages/notifications/twillio-sms/src/index.test.ts
@@ -0,0 +1,132 @@
+import {
+ afterEach,
+ beforeEach,
+ describe,
+ expect,
+ jest,
+ spyOn,
+ test,
+} from "bun:test";
+import { selectNotificationSchema } from "@openstatus/db/src/schema";
+import { sendAlert, sendDegraded, sendRecovery } from "./index";
+
+describe("Slack Notifications", () => {
+ // biome-ignore lint/suspicious/noExplicitAny: <explanation>
+ let fetchMock: any = undefined;
+
+ beforeEach(() => {
+ // @ts-expect-error
+ fetchMock = spyOn(global, "fetch").mockImplementation(() =>
+ Promise.resolve(new Response(null, { status: 200 })),
+ );
+ });
+
+ afterEach(() => {
+ jest.resetAllMocks();
+ });
+
+ test("Send degraded", async () => {
+ const monitor = {
+ 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, // or "down", "degraded"
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ region: "us-east-1",
+ };
+
+ const a = {
+ id: 1,
+ name: "slack Notification",
+ provider: "slack",
+ workspaceId: 1,
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ data: '{"sms":"+33623456789"}',
+ };
+
+ const n = selectNotificationSchema.parse(a);
+ await sendDegraded({
+ // @ts-expect-error
+ monitor,
+ notification: n,
+ statusCode: 500,
+ message: "Something went wrong",
+ cronTimestamp: Date.now(),
+ });
+ expect(fetchMock).toHaveBeenCalled();
+ });
+
+ test("Send Recovered", async () => {
+ const monitor = {
+ 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, // or "down", "degraded"
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ region: "us-east-1",
+ };
+
+ const a = {
+ id: 1,
+ name: "slack Notification",
+ provider: "slack",
+ workspaceId: 1,
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ data: '{"sms":"+33623456789"}',
+ };
+
+ const n = selectNotificationSchema.parse(a);
+ await sendRecovery({
+ // @ts-expect-error
+ monitor,
+ notification: n,
+ statusCode: 500,
+ message: "Something went wrong",
+ cronTimestamp: Date.now(),
+ });
+ expect(fetchMock).toHaveBeenCalled();
+ });
+
+ test("Send Alert", async () => {
+ const monitor = {
+ 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, // or "down", "degraded"
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ region: "us-east-1",
+ };
+ const a = {
+ id: 1,
+ name: "slack Notification",
+ provider: "slack",
+ workspaceId: 1,
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ data: '{"sms":"+33623456789"}',
+ };
+
+ const n = selectNotificationSchema.parse(a);
+
+ await sendAlert({
+ // @ts-expect-error
+ monitor,
+ notification: n,
+ statusCode: 500,
+ message: "Something went wrong",
+ cronTimestamp: Date.now(),
+ });
+ expect(fetchMock).toHaveBeenCalled();
+ });
+});
diff --git a/packages/notifications/twillio-sms/src/index.ts b/packages/notifications/twillio-sms/src/index.ts
index e13d51ff6..041042ceb 100644
--- a/packages/notifications/twillio-sms/src/index.ts
+++ b/packages/notifications/twillio-sms/src/index.ts
@@ -37,7 +37,7 @@ export const sendAlert = async ({
);
try {
- fetch(
+ await fetch(
`https://api.twilio.com/2010-04-01/Accounts/${env.TWILLIO_ACCOUNT_ID}/Messages.json`,
{
method: "post",
@@ -85,7 +85,7 @@ export const sendRecovery = async ({
body.set("Body", `Your monitor ${name} / ${monitor.url} is up again`);
try {
- fetch(
+ await fetch(
`https://api.twilio.com/2010-04-01/Accounts/${env.TWILLIO_ACCOUNT_ID}/Messages.json`,
{
method: "post",
@@ -131,7 +131,7 @@ export const sendDegraded = async ({
body.set("Body", `Your monitor ${name} / ${monitor.url} is degraded `);
try {
- fetch(
+ await fetch(
`https://api.twilio.com/2010-04-01/Accounts/${env.TWILLIO_ACCOUNT_ID}/Messages.json`,
{
method: "post",
diff --git a/packages/notifications/twillio-sms/tsconfig.json b/packages/notifications/twillio-sms/tsconfig.json
index a8a8c9309..b4382e67e 100644
--- a/packages/notifications/twillio-sms/tsconfig.json
+++ b/packages/notifications/twillio-sms/tsconfig.json
@@ -1,4 +1,7 @@
{
"extends": "@openstatus/tsconfig/nextjs.json",
- "include": ["src", "*.ts"]
+ "include": ["src", "*.ts"],
+ "compilerOptions": {
+ "types": ["bun-types"]
+ }
}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index deb154cff..c87c4a9b8 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -502,8 +502,8 @@ importers:
specifier: 13.11.6
version: 13.11.6
bun-types:
- specifier: 1.0.11
- version: 1.0.11
+ specifier: 1.3.1
+ version: 1.3.1(@types/[email protected])
dotenv:
specifier: 16.3.1
version: 16.3.1
@@ -1497,6 +1497,9 @@ importers:
'@types/node':
specifier: 22.10.2
version: 22.10.2
+ bun-types:
+ specifier: 1.3.1
+ version: 1.3.1(@types/[email protected])
typescript:
specifier: 5.7.2
version: 5.7.2
@@ -1584,6 +1587,9 @@ importers:
'@types/react-dom':
specifier: 19.2.2
version: 19.2.2(@types/[email protected])
+ bun-types:
+ specifier: 1.3.1
+ version: 1.3.1(@types/[email protected])
typescript:
specifier: 5.7.2
version: 5.7.2
@@ -1603,6 +1609,9 @@ importers:
'@types/node':
specifier: 22.10.2
version: 22.10.2
+ bun-types:
+ specifier: 1.3.1
+ version: 1.3.1(@types/[email protected])
typescript:
specifier: 5.7.2
version: 5.7.2
@@ -1631,6 +1640,9 @@ importers:
'@types/validator':
specifier: 13.12.0
version: 13.12.0
+ bun-types:
+ specifier: 1.3.1
+ version: 1.3.1(@types/[email protected])
typescript:
specifier: 5.7.2
version: 5.7.2
@@ -7094,12 +7106,14 @@ packages:
resolution: {integrity: sha512-qwVpFEHNfhYJIzNRBvd2C1kyo6jz3ZSMPyyuR47OPdiKWlbYnZNyDWuyR175qDnAJLiCo5fBBqPb3RiXgWlkOQ==}
- resolution: {integrity: sha512-XaDwjnBlkdTOtBEAcXhDnPSKFMlwFK/526z0iyairYIDhZJMzZM1QU4D7XRiEI2SpKQWexn0S/LN9Mwx5xSJNg==}
-
resolution: {integrity: sha512-2dNB+dBwAcFW7RSd4y5vKycRjouKVklSwPk4EjBKWvcMYUBOqZGGNzV7+b2tfKBG3BeRXnozbnegVKR1azuATg==}
+ resolution: {integrity: sha512-NMrcy7smratanWJ2mMXdpatalovtxVggkj11bScuWuiOoXTiKIu2eVS1/7qbyI/4yHedtsn175n4Sm4JcdHLXw==}
+ peerDependencies:
+ '@types/react': ^19
+
resolution: {integrity: sha512-i/Gln4tbzKNuxP70OWhJRZz1MRfvqExowP7U6JKoI8cntFrtxg7RJK3jvz7wQW54UuvNC8tbKHHri5fy74FVqg==}
peerDependencies:
@@ -18374,10 +18388,13 @@ snapshots:
dependencies:
semver: 7.7.2
-
+ dependencies:
+ '@types/node': 24.0.8
+ '@types/react': 19.2.2
+
dependencies:
'@types/node': 24.0.8