Openstatus www.openstatus.dev
at 4c0f4c00a38753a5d0dfd7e7b7b7706dec6f1503 130 lines 3.1 kB view raw
1import { expect, test } from "bun:test"; 2 3import { flyRegions } from "@openstatus/db/src/schema/constants"; 4import { TRPCError } from "@trpc/server"; 5import { appRouter } from "../root"; 6import { createInnerTRPCContext } from "../trpc"; 7 8function getTestContext(limits?: unknown) { 9 return createInnerTRPCContext({ 10 req: undefined, 11 session: { 12 user: { 13 id: "1", 14 }, 15 }, 16 workspace: { 17 id: 1, 18 // @ts-expect-error 19 limits: limits || { 20 monitors: 10, 21 periodicity: ["1m"], 22 regions: ["fra"], 23 }, 24 }, 25 }); 26} 27 28const monitorData = { 29 name: "Test Monitor", 30 url: "https://example.com", 31 jobType: "http" as const, 32 method: "GET" as const, 33 periodicity: "1m" as const, 34 regions: [flyRegions[0]], 35 statusAssertions: [], 36 headerAssertions: [], 37 textBodyAssertions: [], 38 notifications: [], 39 pages: [], 40 tags: [], 41}; 42 43test("Create Monitor", async () => { 44 const ctx = getTestContext(); 45 const caller = appRouter.createCaller(ctx); 46 47 const createdMonitor = await caller.monitor.create(monitorData); 48 49 expect(createdMonitor).toMatchObject({ 50 id: expect.any(Number), 51 name: "Test Monitor", 52 url: "https://example.com", 53 workspaceId: 1, 54 }); 55}); 56 57test("Get Monitor by ID", async () => { 58 const ctx = getTestContext(); 59 const caller = appRouter.createCaller(ctx); 60 61 const createdMonitor = await caller.monitor.create(monitorData); 62 const monitorId = createdMonitor.id; 63 64 const fetchedMonitor = await caller.monitor.get({ 65 id: monitorId, 66 }); 67 68 expect(fetchedMonitor).not.toBeNull(); 69 expect(fetchedMonitor).toMatchObject({ 70 id: monitorId, 71 name: "Test Monitor", 72 url: "https://example.com", 73 }); 74}); 75 76test("Update Monitor", async () => { 77 const ctx = getTestContext(); 78 const caller = appRouter.createCaller(ctx); 79 80 const createdMonitor = await caller.monitor.create(monitorData); 81 const monitorId = createdMonitor.id; 82 83 await caller.monitor.update({ 84 ...monitorData, 85 id: monitorId, 86 name: "Updated Test Monitor", 87 }); 88 89 const updatedMonitor = await caller.monitor.get({ 90 id: monitorId, 91 }); 92 93 expect(updatedMonitor).not.toBeNull(); 94 expect(updatedMonitor?.name).toBe("Updated Test Monitor"); 95}); 96 97test("Delete Monitor", async () => { 98 const ctx = getTestContext(); 99 const caller = appRouter.createCaller(ctx); 100 101 const createdMonitor = await caller.monitor.create(monitorData); 102 const monitorId = createdMonitor.id; 103 104 await caller.monitor.delete({ 105 id: monitorId, 106 }); 107 108 const fetchedMonitor = await caller.monitor.get({ 109 id: monitorId, 110 }); 111 expect(fetchedMonitor).toBeNull(); 112}); 113 114test.todo("Monitor Limit Reached", async () => { 115 const ctx = getTestContext(); 116 // @ts-expect-error 117 ctx.workspace.limits.monitors = 1; 118 const caller = appRouter.createCaller(ctx); 119 120 await caller.monitor.create(monitorData); 121 122 await caller.monitor.create(monitorData); 123 124 await expect(await caller.monitor.create(monitorData)).rejects.toThrow( 125 new TRPCError({ 126 code: "FORBIDDEN", 127 message: "You reached your monitor limits.", 128 }), 129 ); 130});