pagination.test.js (3148B)
1 import test from 'node:test'; 2 import assert from 'node:assert/strict'; 3 import { 4 escapeLikeTerm, 5 pagedResult, 6 parsePagination, 7 searchClause, 8 } from '../src/lib/pagination.js'; 9 10 function expectHttpError(fn, status, code) { 11 try { 12 fn(); 13 } catch (error) { 14 assert.equal(error.status, status); 15 assert.equal(error.code, code); 16 return; 17 } 18 assert.fail('Expected an HTTP error'); 19 } 20 21 test('parsePagination applies defaults and parses integer parameters', () => { 22 assert.deepEqual(parsePagination({}), { limit: 25, offset: 0, q: null }); 23 assert.deepEqual(parsePagination({ limit: '10', offset: '30' }), { 24 limit: 10, 25 offset: 30, 26 q: null, 27 }); 28 assert.deepEqual(parsePagination({ limit: 3, offset: 7 }), { 29 limit: 3, 30 offset: 7, 31 q: null, 32 }); 33 }); 34 35 test('parsePagination clamps limits above the maximum', () => { 36 assert.equal(parsePagination({ limit: '100' }).limit, 100); 37 assert.equal(parsePagination({ limit: '101' }).limit, 100); 38 assert.equal(parsePagination({ limit: '10000' }).limit, 100); 39 }); 40 41 test('parsePagination rejects invalid limits', () => { 42 for (const limit of ['0', '-5', '1.5', '', 'abc', 'NaN', 'Infinity']) { 43 expectHttpError(() => parsePagination({ limit }), 400, 'invalid_request'); 44 } 45 }); 46 47 test('parsePagination rejects invalid offsets', () => { 48 for (const offset of ['-1', '1.5', 'abc', 'NaN']) { 49 expectHttpError(() => parsePagination({ offset }), 400, 'invalid_request'); 50 } 51 }); 52 53 test('parsePagination trims and bounds the search term', () => { 54 assert.equal(parsePagination({ q: ' hello ' }).q, 'hello'); 55 assert.equal(parsePagination({ q: ' ' }).q, null); 56 assert.equal(parsePagination({ q: 'x'.repeat(128) }).q.length, 128); 57 expectHttpError(() => parsePagination({ q: 'x'.repeat(129) }), 400, 'invalid_request'); 58 }); 59 60 test('pagedResult builds the rows and page envelope', () => { 61 assert.deepEqual( 62 pagedResult('sessions', [{ id: 'a' }], { limit: 25, offset: 50, total: 41 }), 63 { 64 sessions: [{ id: 'a' }], 65 page: { limit: 25, offset: 50, total: 41 }, 66 } 67 ); 68 }); 69 70 test('escapeLikeTerm escapes backslash, percent and underscore', () => { 71 assert.equal(escapeLikeTerm('a\\b%c_d'), 'a\\\\b\\%c\\_d'); 72 assert.equal(escapeLikeTerm('plain'), 'plain'); 73 }); 74 75 test('searchClause OR-matches whitelisted columns with an escaped term', () => { 76 const { clause, params } = searchClause('100%_a\\b', ['s.user_agent', 's.ip']); 77 assert.equal( 78 clause, 79 "(s.user_agent LIKE {search} ESCAPE '\\' OR s.ip LIKE {search} ESCAPE '\\')" 80 ); 81 assert.deepEqual(params, { search: '%100\\%\\_a\\\\b%' }); 82 }); 83 84 test('searchClause accepts a custom parameter name and single columns', () => { 85 const { clause, params } = searchClause('x', ['s.client_id'], 'filter'); 86 assert.equal(clause, "(s.client_id LIKE {filter} ESCAPE '\\')"); 87 assert.deepEqual(params, { filter: '%x%' }); 88 }); 89 90 test('searchClause rejects identifiers that are not qualified column names', () => { 91 assert.throws(() => searchClause('x', ['s.ip; DROP TABLE users'])); 92 assert.throws(() => searchClause('x', ['1 = 1'])); 93 assert.throws(() => searchClause('x', [])); 94 });