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.
4.0 kB · 148 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149import { describe, expect, test } from "vitest";
import { type CheckerMonitor, buildCheckerPayload } from "./checker-payload";
const monitor = (overrides: Partial<CheckerMonitor> = {}): CheckerMonitor => ({ id: 42, workspaceId: 7, jobType: "http", url: "https://example.com", method: "GET", body: "", headers: [{ key: "x-api-key", value: "secret" }], assertions: null, degradedAfter: null, timeout: 45_000, retry: 3, followRedirects: true, otelEndpoint: null, otelHeaders: [], grpcService: null, grpcTls: null, ...overrides,});
const build = (m: CheckerMonitor, trigger: "cron" | "api" = "cron") => buildCheckerPayload({ monitor: m, status: "active", trigger, timestamp: 1_700_000_000_000, });
describe("buildCheckerPayload", () => { test("http keeps the request shape the checker binds", () => { expect( build( monitor({ assertions: '[{"version":"v1","type":"status","compare":"eq","target":200}]', }), "api", ), ).toMatchObject({ workspaceId: "7", monitorId: "42", url: "https://example.com", method: "GET", cronTimestamp: 1_700_000_000_000, headers: [{ key: "x-api-key", value: "secret" }], assertions: [ { version: "v1", type: "status", compare: "eq", target: 200 }, ], trigger: "api", retry: 3, followRedirects: true, }); });
test("http honours a disabled followRedirects", () => { expect(build(monitor({ followRedirects: false }))).toMatchObject({ followRedirects: false, }); });
test("defaults a missing retry to the checker default", () => { expect(build(monitor({ retry: null }))).toMatchObject({ retry: 3 }); });
test.each(["tcp", "dns"] as const)( "%s sends uri and assertions", (jobType) => { const payload = build( monitor({ jobType, url: "example.com:443", assertions: '[{"version":"v1","type":"status","compare":"eq","target":200}]', }), ); expect(payload).toMatchObject({ uri: "example.com:443" }); expect("assertions" in payload).toBe(true); expect("url" in payload).toBe(false); expect("followRedirects" in payload).toBe(false); }, );
test("icmp drops assertions a previous job type left on the row", () => { const payload = build( monitor({ jobType: "icmp", url: "1.1.1.1", assertions: '[{"version":"v1","type":"status","compare":"eq","target":200}]', }), ); expect(payload).toMatchObject({ uri: "1.1.1.1" }); expect("assertions" in payload).toBe(false); });
test("grpc sends headers as a map and defaults tls", () => { expect( build( monitor({ jobType: "grpc", url: "grpc.example.com:443", grpcService: "health", }), ), ).toMatchObject({ uri: "grpc.example.com:443", service: "health", tls: "tls", headers: { "x-api-key": "secret" }, }); });
test("forwards the OTel config when configured", () => { const payload = build( monitor({ otelEndpoint: "https://otel.example.com:4318", otelHeaders: [{ key: "Authorization", value: "Basic dGVzdA==" }], }), ); expect(payload.otelConfig).toEqual({ endpoint: "https://otel.example.com:4318", headers: { Authorization: "Basic dGVzdA==" }, }); });
test.each(["icmp", "grpc"] as const)( "%s ignores a malformed assertions string", (jobType) => { expect(() => build(monitor({ jobType, assertions: "{not json" })), ).not.toThrow(); }, );
test("http still rejects a malformed assertions string", () => { expect(() => build(monitor({ assertions: "{not json" }))).toThrow( SyntaxError, ); });
test("rejects an unsupported job type", () => { expect(() => build(monitor({ jobType: "udp" }))).toThrow("Invalid jobType"); });});