Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
3.7 kB ยท 116 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117import { db, eq, inArray } from "@openstatus/db";import { incident, incidentEvent, incidentPostmortem,} from "@openstatus/db/src/schema";import { createIncident } from "@openstatus/db/src/test/factories";import { clearAuditLogFor } from "@openstatus/services/test/helpers";import { TRPCError } from "@trpc/server";import { afterAll, expect, test } from "vitest";
import { edgeRouter } from "../edge";import { createInnerTRPCContext } from "../trpc";
const created: number[] = [];
function caller() { const ctx = createInnerTRPCContext({ req: undefined, session: { user: { id: "1" } }, // @ts-expect-error - minimal workspace for test workspace: { id: 1 }, }); return edgeRouter.createCaller(ctx);}
afterAll(async () => { if (created.length === 0) return; const events = await db .delete(incidentEvent) .where(inArray(incidentEvent.incidentId, created)) .returning({ id: incidentEvent.id }); const postmortems = await db .delete(incidentPostmortem) .where(inArray(incidentPostmortem.incidentId, created)) .returning({ id: incidentPostmortem.id }); await db.delete(incident).where(inArray(incident.id, created)); await clearAuditLogFor({ entityType: "incident", entityIds: created }); await clearAuditLogFor({ entityType: "incident_postmortem", entityIds: postmortems.map((p) => p.id), }); await clearAuditLogFor({ entityType: "incident_event", entityIds: events.map((e) => e.id), });});
test("declare, move and read an incident through tRPC", async () => { const api = caller(); const row = await api.incident.declare({ title: "Router test", severity: "minor", commanderId: 1, }); if (!row) throw new Error("declare returned nothing"); created.push(row.id);
await api.incident.setStatus({ id: row.id, status: "mitigated" }); const got = await api.incident.get({ id: row.id }); expect(got?.status).toBe("mitigated"); expect(got?.allowedTransitions).toEqual(["resolved", "open", "canceled"]); expect(got?.deletable).toBe(false);
await api.incident.addNote({ id: row.id, message: "Looking into it" }); const events = await api.incident.listEvents({ id: row.id }); expect(events?.map((e) => e.type)).toEqual([ "note", "status_changed", "declared", ]); expect(events?.[0]).toMatchObject({ source: "dashboard", sourceRef: null });});
test("another workspace's incident is not found", async () => { const theirs = await createIncident(3); created.push(theirs.id); try { await caller().incident.get({ id: theirs.id }); throw new Error("Should have thrown"); } catch (e) { expect(e).toBeInstanceOf(TRPCError); expect((e as TRPCError).code).toBe("NOT_FOUND"); } const stillThere = await db .select() .from(incident) .where(eq(incident.id, theirs.id)) .get(); expect(stillThere).toBeDefined();});
test("a postmortem saved through tRPC is the user's, whatever the client says", async () => { const api = caller(); const row = await api.incident.declare({ title: "Postmortem test", severity: "minor", commanderId: 1, }); if (!row) throw new Error("declare returned nothing"); created.push(row.id); await api.incident.setStatus({ id: row.id, status: "resolved" });
const draft = await api.incident.draftPostmortem({ id: row.id, content: "## Summary", // @ts-expect-error - not part of the input; the agent has its own route draftedBy: "agent", }); expect(draft?.draftedBy).toBe("user"); const events = await api.incident.listEvents({ id: row.id }); expect(events?.[0]).toMatchObject({ type: "postmortem_drafted", source: "dashboard", });});