|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { describe, it } from 'node:test'; |
| 3 | +import { parsePagination, paginatedResponse } from '../pagination.js'; |
| 4 | + |
| 5 | +describe('parsePagination', () => { |
| 6 | + it('returns defaults when no query params given', () => { |
| 7 | + assert.deepEqual(parsePagination({}), { limit: 20, offset: 0 }); |
| 8 | + }); |
| 9 | + |
| 10 | + it('parses valid limit and offset', () => { |
| 11 | + assert.deepEqual(parsePagination({ limit: '10', offset: '30' }), { limit: 10, offset: 30 }); |
| 12 | + }); |
| 13 | + |
| 14 | + it('clamps limit to max 100', () => { |
| 15 | + assert.deepEqual(parsePagination({ limit: '500' }), { limit: 100, offset: 0 }); |
| 16 | + }); |
| 17 | + |
| 18 | + it('clamps limit to min 1', () => { |
| 19 | + assert.deepEqual(parsePagination({ limit: '0' }), { limit: 1, offset: 0 }); |
| 20 | + assert.deepEqual(parsePagination({ limit: '-5' }), { limit: 1, offset: 0 }); |
| 21 | + }); |
| 22 | + |
| 23 | + it('clamps offset to min 0', () => { |
| 24 | + assert.deepEqual(parsePagination({ offset: '-10' }), { limit: 20, offset: 0 }); |
| 25 | + }); |
| 26 | + |
| 27 | + it('handles non-numeric strings gracefully', () => { |
| 28 | + assert.deepEqual(parsePagination({ limit: 'abc', offset: 'xyz' }), { limit: 20, offset: 0 }); |
| 29 | + }); |
| 30 | +}); |
| 31 | + |
| 32 | +describe('paginatedResponse', () => { |
| 33 | + it('wraps data and meta into the envelope', () => { |
| 34 | + const result = paginatedResponse([{ id: '1' }], { total: 1, limit: 20, offset: 0 }); |
| 35 | + assert.deepEqual(result, { |
| 36 | + data: [{ id: '1' }], |
| 37 | + meta: { total: 1, limit: 20, offset: 0 }, |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + it('works without total in meta', () => { |
| 42 | + const result = paginatedResponse([], { limit: 20, offset: 0 }); |
| 43 | + assert.deepEqual(result, { |
| 44 | + data: [], |
| 45 | + meta: { limit: 20, offset: 0 }, |
| 46 | + }); |
| 47 | + assert.equal('total' in result.meta, false); |
| 48 | + }); |
| 49 | +}); |
0 commit comments