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.
3.0 kB ยท 96 lines
TypeScript
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697import { Events } from "@openstatus/analytics";import { expect } from "@std/expect";import { describe, test } from "@std/testing/bdd";
import { claimCliCommandEvent, parseCliHeaders, trackCliCommand,} from "./cli-telemetry";
function cliHeaders(overrides: Record<string, string> = {}) { return new Headers({ "user-agent": "openstatus-cli/v1.3.2 (darwin; arm64)", "x-openstatus-cli-command": "private-locations list", "x-openstatus-cli-invocation": crypto.randomUUID(), ...overrides, });}
describe("parseCliHeaders", () => { test("reads command, invocation and the user-agent version/platform", () => { const headers = cliHeaders({ "x-openstatus-cli-invocation": "2452a64f" });
expect(parseCliHeaders(headers)).toEqual({ command: "private-locations list", invocation: "2452a64f", version: "1.3.2", os: "darwin", arch: "arm64", }); });
test("returns undefined for requests not sent by the CLI", () => { expect(parseCliHeaders(new Headers({ "user-agent": "curl/8" }))).toBe( undefined, ); });
test("rejects malformed command and invocation values", () => { expect( parseCliHeaders(cliHeaders({ "x-openstatus-cli-command": "rm -rf /" })), ).toBe(undefined); expect( parseCliHeaders( cliHeaders({ "x-openstatus-cli-invocation": "a:b:c:d:e" }), ), ).toBe(undefined); });
test("keeps the command when the user agent is not the CLI's", () => { const cli = parseCliHeaders(cliHeaders({ "user-agent": "custom" }));
expect(cli?.command).toBe("private-locations list"); expect(cli?.version).toBe(undefined); });});
describe("claimCliCommandEvent", () => { test("returns the event once per invocation and workspace", async () => { const cli = parseCliHeaders(cliHeaders()); if (!cli) throw new Error("expected CLI headers to parse");
expect(await claimCliCommandEvent(1, cli)).toEqual({ ...Events.CliCommand, command: "private-locations list", invocation: cli.invocation, version: "1.3.2", os: "darwin", arch: "arm64", }); expect(await claimCliCommandEvent(1, cli)).toBe(undefined); // Keyed per workspace: another workspace can't swallow this run. expect(await claimCliCommandEvent(2, cli)).not.toBe(undefined); });});
describe("trackCliCommand", () => { test("releases the claim when the send fails so a later request retries", async () => { const cli = parseCliHeaders(cliHeaders()); if (!cli) throw new Error("expected CLI headers to parse"); const sent: unknown[] = []; const failing = { track: () => Promise.reject(new Error("down")) }; const working = { track: (event: unknown) => { sent.push(event); return Promise.resolve(); }, };
await expect(trackCliCommand(failing, 1, cli)).rejects.toThrow("down"); await trackCliCommand(working, 1, cli); await trackCliCommand(working, 1, cli);
expect(sent).toHaveLength(1); });});