Openstatus www.openstatus.dev

add some test (#1708)

* add some test

* ci: apply automated fixes

* why?

* ci: apply automated fixes

* fix test

* ci: apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

authored by

Thibault Le Ouay
autofix-ci[bot]
and committed by
GitHub
512b0519 a4060e89

+258 -5
+1 -1
apps/server/src/routes/v1/maintenances/get_all.test.ts
··· 11 11 }); 12 12 13 13 const result = MaintenanceSchema.array().safeParse(await res.json()); 14 - 14 + console.log(result); 15 15 expect(res.status).toBe(200); 16 16 expect(result.success).toBe(true); 17 17 expect(result.data?.length).toBeGreaterThan(0);
+125
packages/api/src/router/maintenance.test.ts
··· 1 + import { expect, test } from "bun:test"; 2 + import { appRouter } from "../root"; 3 + import { createInnerTRPCContext } from "../trpc"; 4 + 5 + function getTestContext() { 6 + return createInnerTRPCContext({ 7 + req: undefined, 8 + session: { 9 + user: { 10 + // @ts-expect-error 11 + id: 1, 12 + }, 13 + }, 14 + }); 15 + } 16 + 17 + const from = new Date(); 18 + const to = new Date(from.getTime() + 1000 * 60 * 60); 19 + 20 + test("Create Maintenance", async () => { 21 + const ctx = getTestContext(); 22 + const caller = appRouter.createCaller(ctx); 23 + 24 + const createdMaintenance = await caller.maintenance.new({ 25 + title: "Test Maintenance", 26 + message: "This is a test maintenance.", 27 + startDate: from, 28 + endDate: to, 29 + pageId: 1, 30 + }); 31 + 32 + expect(createdMaintenance).toMatchObject({ 33 + id: expect.any(Number), 34 + title: "Test Maintenance", 35 + message: "This is a test maintenance.", 36 + from, 37 + to, 38 + workspaceId: 1, 39 + }); 40 + }); 41 + 42 + test("Get Maintenance by ID", async () => { 43 + const ctx = getTestContext(); 44 + const caller = appRouter.createCaller(ctx); 45 + 46 + const createdMaintenance = await caller.maintenance.new({ 47 + title: "Test Maintenance", 48 + message: "This is a test maintenance.", 49 + startDate: from, 50 + endDate: to, 51 + pageId: 1, 52 + }); 53 + 54 + const createdMaintenanceId = createdMaintenance.id; 55 + 56 + const fetchedMaintenance = await caller.maintenance.getById({ 57 + id: createdMaintenanceId, 58 + }); 59 + 60 + expect(fetchedMaintenance).toMatchObject({ 61 + id: createdMaintenance.id, 62 + title: "Test Maintenance", 63 + message: "This is a test maintenance.", 64 + from, 65 + to, 66 + workspaceId: 1, 67 + }); 68 + }); 69 + 70 + test("Update Maintenance", async () => { 71 + const ctx = getTestContext(); 72 + const caller = appRouter.createCaller(ctx); 73 + 74 + const createdMaintenance = await caller.maintenance.new({ 75 + title: "Test Maintenance", 76 + message: "This is a test maintenance.", 77 + startDate: from, 78 + endDate: to, 79 + pageId: 1, 80 + }); 81 + 82 + const createdMaintenanceId = createdMaintenance.id; 83 + 84 + await caller.maintenance.update({ 85 + id: createdMaintenanceId, 86 + title: "Updated Test Maintenance", 87 + message: "This is an updated test maintenance.", 88 + startDate: from, 89 + endDate: to, 90 + }); 91 + 92 + const updatedMaintenance = await caller.maintenance.getById({ 93 + id: createdMaintenanceId, 94 + }); 95 + 96 + expect(updatedMaintenance).toMatchObject({ 97 + id: createdMaintenanceId, 98 + title: "Updated Test Maintenance", 99 + message: "This is an updated test maintenance.", 100 + from, 101 + to, 102 + workspaceId: 1, 103 + }); 104 + }); 105 + 106 + test("Delete Maintenance", async () => { 107 + const ctx = getTestContext(); 108 + const caller = appRouter.createCaller(ctx); 109 + 110 + const createdMaintenance = await caller.maintenance.new({ 111 + title: "Test Maintenance", 112 + message: "This is a test maintenance.", 113 + startDate: from, 114 + endDate: to, 115 + pageId: 1, 116 + }); 117 + 118 + const createdMaintenanceId = createdMaintenance.id; 119 + 120 + const deletedMaintenance = await caller.maintenance.delete({ 121 + id: createdMaintenanceId, 122 + }); 123 + 124 + expect(deletedMaintenance).toBeDefined(); 125 + });
+130
packages/api/src/router/monitor.test.ts
··· 1 + import { expect, test } from "bun:test"; 2 + 3 + import { flyRegions } from "@openstatus/db/src/schema/constants"; 4 + import { TRPCError } from "@trpc/server"; 5 + import { appRouter } from "../root"; 6 + import { createInnerTRPCContext } from "../trpc"; 7 + 8 + function 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 + 28 + const 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 + 43 + test("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 + 57 + test("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 + 76 + test("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 + 97 + test("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 + 114 + test.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 + });
+2 -4
packages/api/src/router/workspace.test.ts
··· 6 6 test("Get Test Workspace", async () => { 7 7 const ctx = createInnerTRPCContext({ 8 8 req: undefined, 9 - // @ts-expect-error some issues with types 10 9 session: { 11 10 user: { 12 11 id: "1", ··· 38 37 test("by default we get the first workspace", async () => { 39 38 const ctx = createInnerTRPCContext({ 40 39 req: undefined, 41 - // @ts-expect-error some issues with types 42 40 session: { 43 41 user: { 44 - id: "1", 42 + // @ts-expect-error some issues with types 43 + id: 1, 45 44 }, 46 45 }, 47 46 workspace: undefined, ··· 67 66 test("All workspaces", async () => { 68 67 const ctx = createInnerTRPCContext({ 69 68 req: undefined, 70 - // @ts-expect-error some issues with types 71 69 session: { 72 70 user: { 73 71 id: "1",