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.9 kB ยท 177 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178import { Events } from "@openstatus/analytics";import type { Workspace } from "@openstatus/db/src/schema";import { afterEach, beforeEach, describe, expect, type mock, type Stub, stub, test,} from "@openstatus/test-utils";import { Hono } from "hono";
import type { Variables } from "@/types";
import { apiTrackMiddleware } from "./track";
// @openstatus/analytics is swapped for a double (test.importmap.json) whose// setupAnalytics/track spies are exposed here on globalThis.const { track: mockTrack, setupAnalytics: mockSetupAnalytics } = ( globalThis as Record<string, unknown>).__analyticsSpies as { track: ReturnType<typeof mock>; setupAnalytics: ReturnType<typeof mock>;};
/** Let the fire-and-forget analytics chain settle. */const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
const TEST_WORKSPACE = { id: 42, slug: "test-ws", plan: "free", name: "",} as Workspace;
/** * Minimal app with `workspace` pre-set (simulating `authMiddleware`) and * `apiTrackMiddleware` mounted the way the V1 router mounts it. */function makeApp(opts: { withWorkspace?: boolean } = {}) { const app = new Hono<{ Variables: Variables }>(); app.use("*", async (c, next) => { if (opts.withWorkspace !== false) c.set("workspace", TEST_WORKSPACE); await next(); }); app.use("*", apiTrackMiddleware()); app.get("/whoami", (c) => c.text("ok")); app.get("/monitor/:id", (c) => c.text("missing", 404)); return app;}
function cliHeaders(invocation: string) { return { "user-agent": "openstatus-cli/v1.3.2 (darwin; arm64)", "x-openstatus-cli-command": "whoami", "x-openstatus-cli-invocation": invocation, };}
function cliCommandCalls() { return mockTrack.mock.calls.filter( ([event]) => (event as { name: string }).name === Events.CliCommand.name, );}
describe("apiTrackMiddleware", () => { let random: Stub<Math, [], number>;
beforeEach(() => { mockSetupAnalytics.mockClear(); mockTrack.mockClear(); // Keep every sampled request unless a test says otherwise. random = stub(Math, "random", () => 0); });
afterEach(() => { random.restore(); });
test("fires api_request for GET requests with the route pattern", async () => { const res = await makeApp().request("/monitor/123", { headers: { "user-agent": "curl/8" }, }); await flush();
expect(res.status).toBe(404); expect(mockTrack).toHaveBeenCalledTimes(1); expect(mockTrack).toHaveBeenCalledWith({ ...Events.ApiRequest, service: "v1", method: "GET /monitor/:id", success: false, sampleRate: 0.1, }); });
test("drops non-CLI requests outside the sample", async () => { random.restore(); random = stub(Math, "random", () => 0.5);
const res = await makeApp().request("/whoami", { headers: { "user-agent": "curl/8" }, }); await flush();
expect(res.status).toBe(200); expect(mockSetupAnalytics).not.toHaveBeenCalled(); expect(mockTrack).not.toHaveBeenCalled(); });
test("always tracks CLI requests", async () => { random.restore(); random = stub(Math, "random", () => 0.99);
await makeApp().request("/whoami", { headers: cliHeaders(crypto.randomUUID()), }); await flush();
expect(mockTrack).toHaveBeenCalledWith( expect.objectContaining({ ...Events.ApiRequest, sampleRate: 1 }), ); expect(cliCommandCalls()).toHaveLength(1); });
test("skips requests that match no route", async () => { const res = await makeApp().request("/nope", { headers: { "user-agent": "curl/8" }, }); await flush();
expect(res.status).toBe(404); expect(mockSetupAnalytics).not.toHaveBeenCalled(); });
test("skips requests without a workspace", async () => { const res = await makeApp({ withWorkspace: false }).request("/whoami", { headers: cliHeaders(crypto.randomUUID()), }); await flush();
expect(res.status).toBe(200); expect(mockSetupAnalytics).not.toHaveBeenCalled(); expect(mockTrack).not.toHaveBeenCalled(); });
test("tags CLI requests and fires cli_command once per invocation", async () => { const app = makeApp(); const invocation = crypto.randomUUID();
for (let i = 0; i < 2; i++) { await app.request("/whoami", { headers: cliHeaders(invocation) }); await flush(); }
expect(mockTrack).toHaveBeenCalledWith({ ...Events.ApiRequest, service: "v1", method: "GET /whoami", success: true, sampleRate: 1, cliCommand: "whoami", cliVersion: "1.3.2", }); expect(cliCommandCalls()).toHaveLength(1); expect(cliCommandCalls()[0][0]).toEqual({ ...Events.CliCommand, command: "whoami", invocation, version: "1.3.2", os: "darwin", arch: "arm64", }); });});