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.
5.2 kB ยท 166 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167import { FetchError } from "@openstatus/status-fetcher";import type { DetectionResult } from "@openstatus/status-fetcher";import { describe, expect, test } from "@openstatus/test-utils";
import { PROBE_TTL_MS, clearProbeStamp, decideDetectionAction, isBlocked, isSuspicious, shouldProbe,} from "./external-status-detect";
const URL = "https://status.example.com";
const fetchError = (init: { kind?: "http" | "parse" | "schema" | "network" | "timeout"; httpStatus?: number;}) => new FetchError({ url: URL, ...init });
const result = (partial: Partial<DetectionResult>): DetectionResult => ({ currentProviderValidated: false, matches: [], hostnameSuggestions: [], evidence: ["e"], ...partial,});
describe("shouldProbe", () => { test("probes once per TTL per slug and stamps on true", () => { const map = new Map<string, number>(); expect(shouldProbe("a", 1000, map)).toBe(true); expect(shouldProbe("a", 1000 + PROBE_TTL_MS - 1, map)).toBe(false); expect(shouldProbe("a", 1000 + PROBE_TTL_MS, map)).toBe(true); expect(shouldProbe("b", 1000, map)).toBe(true); });
test("clearProbeStamp refunds the TTL", () => { const map = new Map<string, number>(); expect(shouldProbe("a", 1000, map)).toBe(true); clearProbeStamp("a", map); expect(shouldProbe("a", 1001, map)).toBe(true); });});
describe("isSuspicious", () => { test("parse, schema and 4xx are suspicious", () => { expect(isSuspicious(fetchError({ kind: "parse" }))).toBe(true); expect(isSuspicious(fetchError({ kind: "schema" }))).toBe(true); expect(isSuspicious(fetchError({ kind: "http", httpStatus: 404 }))).toBe( true, ); });
test("bot challenges and rate limits are blocked, not suspicious", () => { for (const httpStatus of [401, 403, 429]) { const err = fetchError({ kind: "http", httpStatus }); expect(isBlocked(err)).toBe(true); expect(isSuspicious(err)).toBe(false); } expect(isBlocked(fetchError({ kind: "http", httpStatus: 404 }))).toBe( false, ); });
test("5xx, network, timeout and untyped errors are not", () => { expect(isSuspicious(fetchError({ kind: "http", httpStatus: 503 }))).toBe( false, ); expect(isSuspicious(fetchError({ kind: "network" }))).toBe(false); expect(isSuspicious(fetchError({ kind: "timeout" }))).toBe(false); expect(isSuspicious(fetchError({}))).toBe(false); });});
describe("decideDetectionAction", () => { const row = (apiConfig: { type: "atlassian"; endpoint?: string } | null) => ({ provider: "atlassian-statuspage", apiConfig }) as const;
test("clears config when current validates and a custom endpoint is set", () => { const action = decideDetectionAction( result({ currentProviderValidated: true }), row({ type: "atlassian", endpoint: "https://stale.example.com" }), ); expect(action.kind).toBe("clear-config"); });
test("noop transient when current validates with default endpoint", () => { const action = decideDetectionAction( result({ currentProviderValidated: true }), row(null), ); expect(action).toMatchObject({ kind: "noop", reason: "transient" }); });
test("applies a single match", () => { const action = decideDetectionAction( result({ matches: [{ type: "instatus", provider: "instatus", endpoint: URL }], }), row(null), ); expect(action).toMatchObject({ kind: "apply", provider: "instatus" }); });
test("suggests when multiple matches", () => { const action = decideDetectionAction( result({ matches: [ { type: "incidentio", provider: "incidentio", endpoint: URL }, { type: "atlassian", provider: "atlassian-statuspage", endpoint: URL, }, ], }), row(null), ); expect(action).toMatchObject({ kind: "suggest", suggestion: "atlassian-statuspage|incidentio", }); });
test("suggests the new base when the page moved", () => { const action = decideDetectionAction( result({ movedTo: { base: "https://example.status.atlassian.com", matches: [ { type: "atlassian", provider: "atlassian-statuspage", endpoint: "https://example.status.atlassian.com/api/v2/summary.json", }, ], }, hostnameSuggestions: ["uptime-robot"], }), row(null), ); expect(action).toMatchObject({ kind: "suggest", suggestion: "atlassian-statuspage at https://example.status.atlassian.com", }); });
test("suggests hostname evidence", () => { const action = decideDetectionAction( result({ hostnameSuggestions: ["uptime-robot"] }), row(null), ); expect(action).toMatchObject({ kind: "suggest", suggestion: "uptime-robot", }); });
test("noop no-evidence when nothing found", () => { const action = decideDetectionAction(result({}), row(null)); expect(action).toMatchObject({ kind: "noop", reason: "no-evidence" }); });});