46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
|
|
import {
|
|
decodeKeysetCursor,
|
|
encodeKeysetCursor,
|
|
paginateByKeyset,
|
|
} from '../src/platforms/xiaoheihe/cursor.js';
|
|
|
|
describe('xhh keyset cursor', () => {
|
|
it('encodes and decodes cursor payload', () => {
|
|
const encoded = encodeKeysetCursor({ key: 'abc-123' });
|
|
const decoded = decodeKeysetCursor(encoded);
|
|
expect(decoded).toEqual({ key: 'abc-123' });
|
|
});
|
|
|
|
it('throws on invalid cursor payload', () => {
|
|
expect(() => decodeKeysetCursor('not-base64')).toThrow();
|
|
});
|
|
|
|
it('paginates deterministically without duplicates', () => {
|
|
const items = [
|
|
{ id: 'a' },
|
|
{ id: 'b' },
|
|
{ id: 'c' },
|
|
{ id: 'd' },
|
|
{ id: 'e' },
|
|
];
|
|
|
|
const page1 = paginateByKeyset(items, 2, undefined, (item) => item.id);
|
|
expect(page1.items.map((i) => i.id)).toEqual(['a', 'b']);
|
|
expect(page1.nextCursor).toBeTruthy();
|
|
|
|
const page2 = paginateByKeyset(items, 2, decodeKeysetCursor(page1.nextCursor), (item) => item.id);
|
|
expect(page2.items.map((i) => i.id)).toEqual(['c', 'd']);
|
|
expect(page2.nextCursor).toBeTruthy();
|
|
|
|
const page3 = paginateByKeyset(items, 2, decodeKeysetCursor(page2.nextCursor), (item) => item.id);
|
|
expect(page3.items.map((i) => i.id)).toEqual(['e']);
|
|
expect(page3.hasMore).toBe(false);
|
|
|
|
const combined = [...page1.items, ...page2.items, ...page3.items].map((i) => i.id);
|
|
expect(combined).toEqual(['a', 'b', 'c', 'd', 'e']);
|
|
});
|
|
});
|
|
|