Project Files
src / __tests__ / pagination.test.ts
import { describe, it, expect } from "vitest";
// Pure pagination logic extracted from imap.ts for unit testing
function paginate(allUids: number[], offset: number, limit: number) {
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,
};
}
describe("pagination", () => {
const uids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // oldest → newest
it("first page returns newest-first", () => {
const r = paginate(uids, 0, 3);
expect(r.page).toEqual([10, 9, 8]);
expect(r.total).toBe(10);
expect(r.hasMore).toBe(true);
expect(r.nextOffset).toBe(3);
});
it("second page continues from offset", () => {
const r = paginate(uids, 3, 3);
expect(r.page).toEqual([7, 6, 5]);
expect(r.hasMore).toBe(true);
});
it("last page has hasMore=false", () => {
const r = paginate(uids, 9, 3);
expect(r.page).toEqual([1]);
expect(r.hasMore).toBe(false);
expect(r.nextOffset).toBeNull();
});
it("offset beyond total returns empty page", () => {
const r = paginate(uids, 20, 3);
expect(r.page).toEqual([]);
expect(r.hasMore).toBe(false);
});
it("limit larger than total returns all", () => {
const r = paginate(uids, 0, 100);
expect(r.page).toHaveLength(10);
expect(r.hasMore).toBe(false);
});
it("empty uid list", () => {
const r = paginate([], 0, 10);
expect(r.page).toEqual([]);
expect(r.total).toBe(0);
expect(r.hasMore).toBe(false);
});
it("exactly fills a page — hasMore false", () => {
const r = paginate(uids, 0, 10);
expect(r.hasMore).toBe(false);
expect(r.page).toHaveLength(10);
});
it("offset=0 limit=1 paginates through all one by one", () => {
for (let i = 0; i < 10; i++) {
const r = paginate(uids, i, 1);
expect(r.page).toEqual([10 - i]);
expect(r.hasMore).toBe(i < 9);
}
});
});