Project Files
__tests__ / pagination.test.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
// Pure pagination logic extracted from imap.ts for unit testing
function paginate(allUids, offset, limit) {
const allNewestFirst = [...allUids].reverse();
const total = allNewestFirst.length;
const page = allNewestFirst.slice(offset, offset + limit);
return {
page,
total,
offset,
hasMore: offset + limit < total,
nextOffset: offset + limit < total ? offset + limit : null,
};
}
(0, vitest_1.describe)("pagination", () => {
const uids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // oldest → newest
(0, vitest_1.it)("first page returns newest-first", () => {
const r = paginate(uids, 0, 3);
(0, vitest_1.expect)(r.page).toEqual([10, 9, 8]);
(0, vitest_1.expect)(r.total).toBe(10);
(0, vitest_1.expect)(r.hasMore).toBe(true);
(0, vitest_1.expect)(r.nextOffset).toBe(3);
});
(0, vitest_1.it)("second page continues from offset", () => {
const r = paginate(uids, 3, 3);
(0, vitest_1.expect)(r.page).toEqual([7, 6, 5]);
(0, vitest_1.expect)(r.hasMore).toBe(true);
});
(0, vitest_1.it)("last page has hasMore=false", () => {
const r = paginate(uids, 9, 3);
(0, vitest_1.expect)(r.page).toEqual([1]);
(0, vitest_1.expect)(r.hasMore).toBe(false);
(0, vitest_1.expect)(r.nextOffset).toBeNull();
});
(0, vitest_1.it)("offset beyond total returns empty page", () => {
const r = paginate(uids, 20, 3);
(0, vitest_1.expect)(r.page).toEqual([]);
(0, vitest_1.expect)(r.hasMore).toBe(false);
});
(0, vitest_1.it)("limit larger than total returns all", () => {
const r = paginate(uids, 0, 100);
(0, vitest_1.expect)(r.page).toHaveLength(10);
(0, vitest_1.expect)(r.hasMore).toBe(false);
});
(0, vitest_1.it)("empty uid list", () => {
const r = paginate([], 0, 10);
(0, vitest_1.expect)(r.page).toEqual([]);
(0, vitest_1.expect)(r.total).toBe(0);
(0, vitest_1.expect)(r.hasMore).toBe(false);
});
(0, vitest_1.it)("exactly fills a page — hasMore false", () => {
const r = paginate(uids, 0, 10);
(0, vitest_1.expect)(r.hasMore).toBe(false);
(0, vitest_1.expect)(r.page).toHaveLength(10);
});
(0, vitest_1.it)("offset=0 limit=1 paginates through all one by one", () => {
for (let i = 0; i < 10; i++) {
const r = paginate(uids, i, 1);
(0, vitest_1.expect)(r.page).toEqual([10 - i]);
(0, vitest_1.expect)(r.hasMore).toBe(i < 9);
}
});
});