Files
HealthLog/src/lib/mcp/__tests__/pagination.test.ts
T
Marc-André Bombeck b40a963cb1 feat(mcp): support arbitrary date ranges and per-analyte lab history
Add an optional explicit {from,to} range to the comparison and
changepoint reads, served over the same rollup tier the five fixed
trailing windows use (reach back to the lower bound, filter to the
span) — so a before-vs-after-a-date question is answerable without
recomputing analytics. Add a per-analyte lab trajectory read (latest to
recent history) over the existing lab resolver, page-bounded so a heavy
panel cannot balloon the response. Introduce an opaque, stable base64url
cursor helper that decodes any malformed or foreign token to the first
page rather than throwing.
2026-06-28 01:20:57 +02:00

37 lines
1.3 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { encodeOffsetCursor, decodeOffsetCursor } from "../pagination";
describe("opaque cursor pagination", () => {
it("round-trips an offset through encode → decode", () => {
for (const offset of [0, 1, 8, 50, 200, 9999]) {
expect(decodeOffsetCursor(encodeOffsetCursor(offset))).toBe(offset);
}
});
it("encodes deterministically (stable token for the same offset)", () => {
expect(encodeOffsetCursor(8)).toBe(encodeOffsetCursor(8));
});
it("emits an opaque token, not the raw number", () => {
const cursor = encodeOffsetCursor(8);
expect(cursor).not.toBe("8");
expect(cursor).not.toContain("8");
});
it("degrades a missing / malformed / foreign cursor to the first page", () => {
expect(decodeOffsetCursor(undefined)).toBe(0);
expect(decodeOffsetCursor("")).toBe(0);
expect(decodeOffsetCursor("not-base64!@#")).toBe(0);
expect(
decodeOffsetCursor(Buffer.from("[1,2,3]").toString("base64url")),
).toBe(0);
expect(decodeOffsetCursor(42 as unknown)).toBe(0);
});
it("clamps a negative or non-finite offset to zero", () => {
expect(encodeOffsetCursor(-5)).toBe(encodeOffsetCursor(0));
expect(encodeOffsetCursor(Number.NaN)).toBe(encodeOffsetCursor(0));
});
});