Skip to content

Open-source status page, incident response and uptime monitoring

🧪 fixed tests-2 (#625)

Kushagra Sharma2a6cf5134588
diff --git a/apps/server/src/v1/incidents.test.ts b/apps/server/src/v1/incidents.test.ts
index 95f842564..efbbb69e7 100644
--- a/apps/server/src/v1/incidents.test.ts
+++ b/apps/server/src/v1/incidents.test.ts
@@ -1,6 +1,6 @@
import { expect, test } from "bun:test";
-
import { api } from ".";
+import { iso8601Regex } from "./test-utils";
test("GET one Incident", async () => {
const res = await api.request("/incident/2", {
@@ -11,7 +11,7 @@ test("GET one Incident", async () => {
expect(res.status).toBe(200);
expect(await res.json()).toMatchObject({
id: 2,
- startedAt: expect.any(String),
+ startedAt: expect.stringMatching(iso8601Regex),
monitorId: 1,
acknowledgedAt: null,
resolvedAt: null,
@@ -32,12 +32,11 @@ test("Update an incident ", async () => {
}),
});
expect(res.status).toBe(200);
-
expect(await res.json()).toMatchObject({
- acknowledgedAt: "2023-11-08T21:03:13.000Z",
+ acknowledgedAt: expect.stringMatching(iso8601Regex),
monitorId: 1,
id: 2,
- startedAt: expect.any(String),
+ startedAt: expect.stringMatching(iso8601Regex),
resolvedAt: null,
resolvedBy: null,
acknowledgedBy: null,
@@ -84,8 +83,7 @@ test("Update an incident with invalid data should return 403", async () => {
"x-openstatus-key": "1",
"content-type": "application/json",
},
- body: JSON.stringify({
- //passing incorrect body
+ body: JSON.stringify({//passing incorrect body
acknowledgedAt: "2023-11-0",
}),
});
@@ -111,7 +109,7 @@ test("Get all Incidents", async () => {
acknowledgedAt: null,
monitorId: 1,
id: 1,
- startedAt: expect.any(String),
+ startedAt: expect.stringMatching(iso8601Regex),
resolvedAt: null,
resolvedBy: null,
acknowledgedBy: null,
diff --git a/apps/server/src/v1/incidents.ts b/apps/server/src/v1/incidents.ts
index 7c4041fe8..dbea79fae 100644
--- a/apps/server/src/v1/incidents.ts
+++ b/apps/server/src/v1/incidents.ts
@@ -5,6 +5,7 @@ import { incidentTable } from "@openstatus/db/src/schema/incidents";
import type { Variables } from "./index";
import { ErrorSchema } from "./shared";
+import { isoDate } from "./utils";
const incidentsApi = new OpenAPIHono<{ Variables: Variables }>();
@@ -27,13 +28,7 @@ const IncidentSchema = z.object({
description: "The id of the incident",
example: 1,
}),
- startedAt: z
- .preprocess((val) => {
- if (val) {
- return new Date(String(val)).toISOString();
- }
- return new Date().toISOString();
- }, z.string())
+ startedAt: isoDate
.openapi({
description: "The date the incident started",
}),
@@ -46,13 +41,7 @@ const IncidentSchema = z.object({
})
.nullable(),
- acknowledgedAt: z
- .preprocess((val) => {
- if (val) {
- return new Date(String(val)).toISOString();
- }
- return new Date().toISOString();
- }, z.string())
+ acknowledgedAt:isoDate
.openapi({
description: "The date the incident was acknowledged",
})
@@ -66,13 +55,7 @@ const IncidentSchema = z.object({
})
.nullable(),
- resolvedAt: z
- .preprocess((val) => {
- if (val) {
- return new Date(String(val)).toISOString();
- }
- return new Date().toISOString();
- }, z.string())
+ resolvedAt: isoDate
.openapi({
description: "The date the incident was resolved",
})
diff --git a/apps/server/src/v1/monitor.test.ts b/apps/server/src/v1/monitor.test.ts
index ed7271076..ee5de8b69 100644
--- a/apps/server/src/v1/monitor.test.ts
+++ b/apps/server/src/v1/monitor.test.ts
@@ -1,6 +1,6 @@
import { expect, test } from "bun:test";
-
import { api } from ".";
+import { iso8601Regex } from "./test-utils";
test("GET one monitor", async () => {
const res = await api.request("/monitor/1", {
@@ -277,6 +277,6 @@ test.todo("Get monitor daily Summary");
// ok: 4,
// count: 13,
// avgLatency: 1,
-// day: "31-01-2024"
+// day: expect.stringMatching(iso8601Regex)
// });
// });
diff --git a/apps/server/src/v1/monitor.ts b/apps/server/src/v1/monitor.ts
index ecb342e23..a36db2e33 100644
--- a/apps/server/src/v1/monitor.ts
+++ b/apps/server/src/v1/monitor.ts
@@ -13,6 +13,7 @@ import { Redis } from "@openstatus/upstash";
import { env } from "../env";
import type { Variables } from "./index";
import { ErrorSchema } from "./shared";
+import { isoDate } from "./utils";
const tb = new Tinybird({ token: env.TINY_BIRD_API_KEY });
const redis = Redis.fromEnv();
@@ -482,7 +483,7 @@ const dailyStatsSchema = z.object({
.int()
.openapi({ description: "The total number of request" }),
avgLatency: z.number().int().openapi({ description: "The average latency" }),
- day: z.string().openapi({ description: "the date of the event" }),
+ day: isoDate,
});
const dailyStatsSchemaArray = z
diff --git a/apps/server/src/v1/statusReport.test.ts b/apps/server/src/v1/statusReport.test.ts
index 0527cd8d8..d53f37dc0 100644
--- a/apps/server/src/v1/statusReport.test.ts
+++ b/apps/server/src/v1/statusReport.test.ts
@@ -1,6 +1,6 @@
import { expect, test } from "bun:test";
-
import { api } from ".";
+import { iso8601Regex } from "./test-utils";
test("GET one status report", async () => {
const res = await api.request("/status_report/1", {
@@ -98,7 +98,7 @@ test("Delete a status report", async () => {
});
test("create a status report update", async () => {
- const res = await api.request("/status_report/3/update", {
+ const res = await api.request("/status_report/1/update", {
method: "POST",
headers: {
"x-openstatus-key": "1",
@@ -114,7 +114,7 @@ test("create a status report update", async () => {
expect(await res.json()).toMatchObject({
status: "investigating",
id: expect.any(String),
- date: "Wed Nov 08 2023 21:03:13 GMT+0000 (Coordinated Universal Time)",
+ date: expect.stringMatching(iso8601Regex),
message: "Test Status Report",
});
});
@@ -162,8 +162,7 @@ test("Create a status report update with invalid data should return 403", async
"x-openstatus-key": "1",
"content-type": "application/json",
},
- body: JSON.stringify({
- //passing in incompelete body
+ body: JSON.stringify({//passing in incompelete body
date: "2023-11-08T21:03:13.000Z",
message: "Test Status Report",
}),
diff --git a/apps/server/src/v1/statusReport.ts b/apps/server/src/v1/statusReport.ts
index 2e87a3472..ebc46c815 100644
--- a/apps/server/src/v1/statusReport.ts
+++ b/apps/server/src/v1/statusReport.ts
@@ -15,6 +15,7 @@ import { allPlans } from "@openstatus/plans";
import type { Variables } from "./index";
import { ErrorSchema } from "./shared";
+import { isoDate } from "./utils";
import { statusUpdateSchema } from "./statusReportUpdate";
const statusReportApi = new OpenAPIHono<{ Variables: Variables }>();
@@ -37,9 +38,10 @@ const createStatusReportUpdateSchema = z.object({
status: z.enum(statusReportStatus).openapi({
description: "The status of the update",
}),
- date: z.string().openapi({
- description: "The date of the update in ISO 8601 format",
- }),
+ date: isoDate
+ .openapi({
+ description: "The date of the update in ISO8601 format",
+ }),
message: z.string().openapi({
description: "The message of the update",
}),
diff --git a/apps/server/src/v1/statusReportUpdate.test.ts b/apps/server/src/v1/statusReportUpdate.test.ts
index 646a701d4..168c2c76e 100644
--- a/apps/server/src/v1/statusReportUpdate.test.ts
+++ b/apps/server/src/v1/statusReportUpdate.test.ts
@@ -1,6 +1,6 @@
import { expect, test } from "bun:test";
-
import { api } from ".";
+import { iso8601Regex } from "./test-utils";
test("GET one status report update ", async () => {
const res = await api.request("/status_report_update/1", {
@@ -45,7 +45,7 @@ test("create one status report update without auth key should return 401", async
},
body: JSON.stringify({
status: "investigating",
- date: "2023-11-08T21:03:13.000Z",
+ date: expect.stringMatching(iso8601Regex),
message: "test",
status_report_id: 1,
}),
diff --git a/apps/server/src/v1/statusReportUpdate.ts b/apps/server/src/v1/statusReportUpdate.ts
index a3a102561..2d6f12e0e 100644
--- a/apps/server/src/v1/statusReportUpdate.ts
+++ b/apps/server/src/v1/statusReportUpdate.ts
@@ -14,6 +14,7 @@ import { allPlans } from "@openstatus/plans";
import type { Variables } from ".";
import { ErrorSchema } from "./shared";
+import { isoDate } from "./utils";
const statusReportUpdateApi = new OpenAPIHono<{ Variables: Variables }>();
@@ -36,11 +37,10 @@ export const statusUpdateSchema = z.object({
description: "The status of the update",
}),
id: z.coerce.string().openapi({ description: "The id of the update" }),
- date: z
- .preprocess((val) => String(val), z.string())
- .openapi({
- description: "The date of the update in ISO 8601 format",
- }),
+ date: isoDate
+ .openapi({
+ description: "The date of the update in ISO8601 format",
+ }),
message: z.string().openapi({
description: "The message of the update",
}),
@@ -53,8 +53,9 @@ const createStatusReportUpdateSchema = z.object({
status: z.enum(statusReportStatus).openapi({
description: "The status of the update",
}),
- date: z.string().datetime().openapi({
- description: "The date of the update in ISO 8601 format",
+ date: isoDate
+ .openapi({
+ description: "The date of the update in ISO8601 format",
}),
message: z.string().openapi({
description: "The message of the update",
diff --git a/apps/server/src/v1/test-utils.ts b/apps/server/src/v1/test-utils.ts
new file mode 100644
index 000000000..4ec5b39f4
--- /dev/null
+++ b/apps/server/src/v1/test-utils.ts
@@ -0,0 +1 @@
+export const iso8601Regex: RegExp = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z$/;
\ No newline at end of file
diff --git a/apps/server/src/v1/utils.ts b/apps/server/src/v1/utils.ts
index ce35ebfa5..c1308f237 100644
--- a/apps/server/src/v1/utils.ts
+++ b/apps/server/src/v1/utils.ts
@@ -1,6 +1,7 @@
import { db, eq } from "@openstatus/db";
import { selectWorkspaceSchema, workspace } from "@openstatus/db/src/schema";
import { allPlans } from "@openstatus/plans";
+import { z } from "@hono/zod-openapi";
/**
* TODO: move the plan limit into the Unkey `{ meta }` to avoid an additional db call.
@@ -11,6 +12,14 @@ import { allPlans } from "@openstatus/plans";
* That remindes me we need to downgrade the frequency/periodicity of monitors to 10m if the user downgrades their plan.
*/
+export const isoDate = z
+.preprocess((val) => {
+ if (val) {
+ return new Date(String(val)).toISOString();
+ }
+ return new Date().toISOString();
+}, z.string())
+
export async function getWorkspace(id: number) {
const _workspace = await db
.select()
diff --git a/packages/db/src/seed.mts b/packages/db/src/seed.mts
index 0b414dffb..25a79bd00 100644
--- a/packages/db/src/seed.mts
+++ b/packages/db/src/seed.mts
@@ -157,17 +157,6 @@ async function main() {
})
.run();
- await db
- .insert(statusReport)
- .values({
- id: 3,
- workspaceId: 1,
- title: "Test Status Report",
- status: "investigating",
- updatedAt: new Date(),
- })
- .run();
-
await db
.insert(incidentTable)
.values({