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.
8.4 kB ยท 309 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310import { expect } from "@std/expect";import { describe, it } from "@std/testing/bdd";
import type { HttpAssertionRequest } from "./types";import { DnsRecordAssertion, HeaderAssertion, JsonBodyAssertion, StatusAssertion, TextBodyAssertion, assertion,} from "./v1";
function http(req: Partial<HttpAssertionRequest> = {}): HttpAssertionRequest { return { status: 200, header: {}, body: "", ...req };}
describe("StatusAssertion", () => { const cases = [ ["eq", 200, 200, true], ["eq", 500, 200, false], ["not_eq", 500, 200, true], ["not_eq", 200, 200, false], ["gt", 201, 200, true], ["gt", 200, 200, false], ["gte", 200, 200, true], ["gte", 199, 200, false], ["lt", 199, 200, true], ["lt", 200, 200, false], ["lte", 200, 200, true], ["lte", 201, 200, false], ] as const;
for (const [compare, status, target, expected] of cases) { it(`${status} ${compare} ${target} -> ${expected}`, () => { const a = new StatusAssertion({ version: "v1", type: "status", compare, target, }); expect(a.assert(http({ status })).success).toBe(expected); }); }
it("prefixes the failure message", () => { const a = new StatusAssertion({ version: "v1", type: "status", compare: "eq", target: 200, }); expect(a.assert(http({ status: 404 }))).toEqual({ success: false, message: "Status: Expected 404 to be equal to 200", }); });
it("rejects a DNS request", () => { const a = new StatusAssertion({ version: "v1", type: "status", compare: "eq", target: 200, }); expect(a.assert({ records: {} })).toEqual({ success: false, message: "Invalid request type for status assertion", }); });});
describe("HeaderAssertion", () => { function header( compare: "contains" | "eq" | "empty" | "not_empty", target = "", ) { return new HeaderAssertion({ version: "v1", type: "header", key: "Content-Type", compare, target, }); }
it("matches the header key case-insensitively", () => { const req = http({ header: { "content-type": "application/json" } }); expect(header("eq", "application/json").assert(req).success).toBe(true); });
it("treats a missing header as an empty string", () => { expect(header("empty").assert(http()).success).toBe(true); expect(header("not_empty").assert(http())).toEqual({ success: false, message: "Header Content-Type: Expected to not be empty", }); });
it("supports substring matching", () => { const req = http({ header: { "Content-Type": "text/html; charset=utf-8" }, }); expect(header("contains", "charset").assert(req).success).toBe(true); expect(header("contains", "json").assert(req).success).toBe(false); });
it("rejects a DNS request", () => { expect(header("eq", "x").assert({ records: {} }).success).toBe(false); });});
describe("TextBodyAssertion", () => { const body = "hello world"; const cases = [ ["contains", "world", true], ["contains", "mars", false], ["not_contains", "mars", true], ["not_contains", "world", false], ["eq", "hello world", true], ["eq", "hello", false], ["not_eq", "hello", true], ["not_eq", "hello world", false], ["not_empty", "", true], ["empty", "", false], // string comparisons are lexicographic ["gt", "hello", true], ["lt", "z", true], ["gte", "hello world", true], ["lte", "a", false], ] as const;
for (const [compare, target, expected] of cases) { it(`"${body}" ${compare} "${target}" -> ${expected}`, () => { const a = new TextBodyAssertion({ version: "v1", type: "textBody", compare, target, }); expect(a.assert(http({ body })).success).toBe(expected); }); }
it("prefixes the failure message", () => { const a = new TextBodyAssertion({ version: "v1", type: "textBody", compare: "contains", target: "ok", }); expect(a.assert(http({ body: "nope" }))).toEqual({ success: false, message: "Body: Expected nope to contain ok", }); });});
describe("JsonBodyAssertion", () => { function json( path: string, compare: "contains" | "not_contains" | "eq" | "not_eq" | "empty", target: string, ) { return new JsonBodyAssertion({ version: "v1", type: "jsonBody", path, compare, target, }); } const body = JSON.stringify({ status: "ok", tags: ["a", "b"] });
it("finds a value with contains", () => { expect( json("$.status", "contains", "ok").assert(http({ body })).success, ).toBe(true); });
it("fails not_contains when the value is present", () => { expect( json("$.status", "not_contains", "ok").assert(http({ body })).success, ).toBe(false); });
it("fails on an invalid JSON body", () => { expect( json("$.status", "eq", "ok").assert(http({ body: "<html>" })), ).toEqual({ success: false, message: "Unable to parse json" }); });
it("rejects a DNS request", () => { expect(json("$.status", "eq", "ok").assert({ records: {} }).success).toBe( false, ); });
// JSONPath returns an array, which is compared to the target as-is, so // these fail today. Un-ignore once the matched value is unwrapped. it.ignore("passes eq when the matched value equals the target", () => { expect(json("$.status", "eq", "ok").assert(http({ body })).success).toBe( true, ); });
it.ignore("fails not_eq when the matched value equals the target", () => { expect( json("$.status", "not_eq", "ok").assert(http({ body })).success, ).toBe(false); });
it.ignore("matches substrings with contains", () => { expect( json("$.status", "contains", "o").assert(http({ body })).success, ).toBe(true); });});
describe("DnsRecordAssertion", () => { function dns( compare: "contains" | "not_contains" | "eq" | "not_eq", target: string, ) { return new DnsRecordAssertion({ version: "v1", type: "dnsRecord", key: "A", compare, target, }); } const req = { records: { A: ["1.1.1.1", "8.8.8.8"] } };
const cases = [ ["eq", "1.1.1.1", true], ["eq", "1.1.1", false], ["not_eq", "9.9.9.9", true], ["not_eq", "8.8.8.8", false], ["contains", "8.8", true], ["contains", "9.9", false], ["not_contains", "9.9", true], ["not_contains", "1.1", false], ] as const;
for (const [compare, target, expected] of cases) { it(`A ${compare} ${target} -> ${expected}`, () => { expect(dns(compare, target).assert(req).success).toBe(expected); }); }
it("treats a missing record type as no records", () => { expect(dns("not_contains", "1.1").assert({ records: {} }).success).toBe( true, ); expect(dns("eq", "1.1.1.1").assert({ records: {} })).toEqual({ success: false, message: "DNS Record A: Expected DNS records [] to equal 1.1.1.1", }); });
it("lists all records in the failure message", () => { expect(dns("eq", "9.9.9.9").assert(req).message).toBe( "DNS Record A: Expected DNS records [1.1.1.1, 8.8.8.8] to equal 9.9.9.9", ); });
it("rejects an HTTP request", () => { expect(dns("eq", "1.1.1.1").assert(http())).toEqual({ success: false, message: "Invalid request type for DNS record assertion", }); });});
describe("assertion schema", () => { it("defaults version to v1", () => { const parsed = assertion.parse({ type: "status", compare: "eq", target: 200, }); expect(parsed.version).toBe("v1"); });
it("rejects a non-positive status target", () => { expect( assertion.safeParse({ type: "status", compare: "eq", target: 0 }).success, ).toBe(false); });
it("rejects a string compare on a status assertion", () => { expect( assertion.safeParse({ type: "status", compare: "contains", target: 200 }) .success, ).toBe(false); });
it("rejects an unknown DNS record type", () => { expect( assertion.safeParse({ type: "dnsRecord", key: "SRV", compare: "eq", target: "x", }).success, ).toBe(false); });});