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.
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197import { expect } from "@std/expect";import { describe, test } from "@std/testing/bdd";
import { selectWindow, toPipeParams, trimToTick,} from "../response-logs-cursor";
const DAY_MS = 24 * 60 * 60 * 1000;
/** Expand `[tick, rowsInTick]` pairs into rows, in the order given. */function rowsOf(ticks: [number, number][]) { return ticks.flatMap(([cronTimestamp, count]) => Array.from({ length: count }, () => ({ cronTimestamp })), );}
describe("selectWindow", () => { const now = 1_700_000_000_000;
test("falls back to the widest view when the range is open-ended", () => { expect(selectWindow(undefined, undefined, now)).toBe("14d"); });
test("picks the view by how old the oldest requested row is", () => { expect(selectWindow(now - DAY_MS, now, now)).toBe("1d"); expect(selectWindow(now - DAY_MS - 1, now, now)).toBe("7d"); expect(selectWindow(now - 7 * DAY_MS, now, now)).toBe("7d"); expect(selectWindow(now - 7 * DAY_MS - 1, now, now)).toBe("14d"); expect(selectWindow(now - 14 * DAY_MS, now, now)).toBe("14d"); });
test("keeps an expired short range on a view that still holds it", () => { // A one-day span that ended a week ago has aged out of the 1 d view. expect(selectWindow(now - 9 * DAY_MS, now - 8 * DAY_MS, now)).toBe("14d"); });});
describe("toPipeParams", () => { test("drops absent and empty filters", () => { expect( toPipeParams({ regions: [], status: undefined, trigger: undefined, statusCodes: undefined, latencyMin: undefined, latencyMax: undefined, }), ).toEqual({}); });
test("keeps every filter that carries a value", () => { expect( toPipeParams({ regions: ["ams"], status: ["error"], trigger: ["cron"], statusCodes: [500], latencyMin: 0, latencyMax: 100, }), ).toEqual({ regions: ["ams"], status: ["error"], trigger: ["cron"], statusCodes: [500], latencyMin: 0, latencyMax: 100, }); });});
describe("trimToTick", () => { test("returns null cursors for an empty page", () => { expect( trimToTick({ rows: [], limit: 50, fetchLimit: 55, direction: "next", hasCursor: false, }), ).toEqual({ rows: [], nextCursor: null, prevCursor: null, truncatedTick: false, }); });
test("stops on a clean tick boundary", () => { const result = trimToTick({ rows: rowsOf([ [300, 2], [200, 2], [100, 2], ]), limit: 4, fetchLimit: 6, direction: "next", hasCursor: false, }); expect(result.rows.length).toBe(4); expect(result.nextCursor).toBe(200); expect(result.prevCursor).toBe(null); });
test("drops a trailing tick the pipe's own LIMIT may have cut", () => { const result = trimToTick({ rows: rowsOf([ [300, 2], [200, 2], ]), limit: 4, fetchLimit: 4, direction: "next", hasCursor: false, }); expect(result.rows.length).toBe(2); expect(result.nextCursor).toBe(300); expect(result.truncatedTick).toBe(false); });
test("returns a tick wider than the page size whole", () => { const result = trimToTick({ rows: rowsOf([[300, 4]]), limit: 2, fetchLimit: 6, direction: "next", hasCursor: false, }); expect(result.rows.length).toBe(4); expect(result.truncatedTick).toBe(false); // Short of the ceiling, so the tick is whole and the source is drained. expect(result.nextCursor).toBe(null); });
test("flags a lone tick that filled the fetch ceiling as truncated", () => { // The pipe may have cut this tick, and there is no earlier tick to drop // back to; paging past its (exclusive) cursor would lose the remainder. const result = trimToTick({ rows: rowsOf([[300, 4]]), limit: 2, fetchLimit: 4, direction: "next", hasCursor: false, }); expect(result.truncatedTick).toBe(true); });
test("reports no next page when the source ran dry", () => { const result = trimToTick({ rows: rowsOf([[300, 2]]), limit: 4, fetchLimit: 6, direction: "next", hasCursor: false, }); expect(result.rows.length).toBe(2); expect(result.nextCursor).toBe(null); expect(result.prevCursor).toBe(null); });
test("only offers a page back once the request came from a cursor", () => { const args = { rows: rowsOf([ [300, 2], [200, 2], ]), limit: 4, fetchLimit: 6, direction: "next" as const, }; expect(trimToTick({ ...args, hasCursor: false }).prevCursor).toBe(null); expect(trimToTick({ ...args, hasCursor: true }).prevCursor).toBe(300); });
test("walks the other way for the prev direction", () => { const result = trimToTick({ rows: rowsOf([ [100, 2], [200, 2], [300, 2], ]), limit: 4, fetchLimit: 6, direction: "prev", hasCursor: true, }); expect(result.rows.length).toBe(4); expect(result.prevCursor).toBe(200); expect(result.nextCursor).toBe(100); });});