a1956deb63
Co-authored-by: Cursor <cursoragent@cursor.com>
58 lines
1.9 KiB
TypeScript
58 lines
1.9 KiB
TypeScript
import assert from "node:assert/strict";
|
||
import test from "node:test";
|
||
|
||
import {
|
||
clampSessionLimit,
|
||
compareSessionCursor,
|
||
encodeSessionCursor,
|
||
nextSessionCursor,
|
||
parseSessionCursor,
|
||
SESSION_PAGE_SIZE,
|
||
} from "../src/lib/session-cursor.ts";
|
||
|
||
const leftId = "11111111-1111-4111-8111-111111111111";
|
||
const rightId = "22222222-2222-4222-8222-222222222222";
|
||
const stamp = "2026-09-06T04:00:00.000Z";
|
||
|
||
test("encodeSessionCursor round-trips a valid pair", () => {
|
||
const encoded = encodeSessionCursor(stamp, leftId);
|
||
assert.deepEqual(parseSessionCursor(encoded), { updatedAt: stamp, id: leftId });
|
||
});
|
||
|
||
test("parseSessionCursor returns null for illegal strings", () => {
|
||
assert.equal(parseSessionCursor(null), null);
|
||
assert.equal(parseSessionCursor(""), null);
|
||
assert.equal(parseSessionCursor("not-a-date,11111111-1111-4111-8111-111111111111"), null);
|
||
assert.equal(parseSessionCursor(`${stamp},not-a-uuid`), null);
|
||
assert.equal(parseSessionCursor(stamp), null);
|
||
});
|
||
|
||
test("compareSessionCursor uses id when the timestamp is the same", () => {
|
||
const sameTime = compareSessionCursor(
|
||
{ updatedAt: stamp, id: leftId },
|
||
{ updatedAt: stamp, id: rightId },
|
||
);
|
||
assert.ok(sameTime > 0);
|
||
const newerTime = compareSessionCursor(
|
||
{ updatedAt: "2026-09-06T05:00:00.000Z", id: leftId },
|
||
{ updatedAt: stamp, id: rightId },
|
||
);
|
||
assert.ok(newerTime < 0);
|
||
});
|
||
|
||
test("clampSessionLimit defaults to 40 and clamps 1–100", () => {
|
||
assert.equal(clampSessionLimit(null), SESSION_PAGE_SIZE);
|
||
assert.equal(clampSessionLimit("0"), 1);
|
||
assert.equal(clampSessionLimit("101"), 100);
|
||
assert.equal(clampSessionLimit("40"), 40);
|
||
});
|
||
|
||
test("nextSessionCursor is null when the page is short, else encodes the limit-th row", () => {
|
||
const rows = [
|
||
{ id: leftId, updated_at: stamp },
|
||
{ id: rightId, updated_at: stamp },
|
||
];
|
||
assert.equal(nextSessionCursor(rows, 40), null);
|
||
assert.equal(nextSessionCursor(rows, 1), encodeSessionCursor(stamp, leftId));
|
||
});
|