import { calculatePagination, validatePaginationParams, Cache, batchData, createCursor, decodeCursor } from '../index'; describe('lazy-render-server', () => { describe('calculatePagination', () => { test('calculates pagination correctly for page 1', () => { const items = [1, 2, 3, 4, 5]; const result = calculatePagination(items, 1, 10, 50); expect(result.data).toEqual(items); expect(result.pagination.page).toBe(1); expect(result.pagination.limit).toBe(10); expect(result.pagination.total).toBe(50); expect(result.pagination.totalPages).toBe(5); expect(result.pagination.hasMore).toBe(true); }); test('calculates pagination correctly for last page', () => { const items = [41, 42, 43, 44, 45]; const result = calculatePagination(items, 5, 10, 50); expect(result.pagination.hasMore).toBe(false); expect(result.pagination.nextCursor).toBeUndefined(); expect(result.pagination.prevCursor).toBe('4'); }); test('includes meta information when provided', () => { const items = [1, 2, 3]; const result = calculatePagination(items, 1, 10, 30, { sortBy: 'createdAt', sortOrder: 'desc' }); expect(result.meta).toBeDefined(); expect(result.meta?.sortBy).toBe('createdAt'); expect(result.meta?.sortOrder).toBe('desc'); expect(result.meta?.timestamp).toBeDefined(); }); }); describe('validatePaginationParams', () => { test('validates correct parameters', () => { const result = validatePaginationParams({ page: 1, limit: 50 }); expect(result.errors).toHaveLength(0); expect(result.page).toBe(1); expect(result.limit).toBe(50); }); test('handles invalid page', () => { const result = validatePaginationParams({ page: -1, limit: 50 }); expect(result.errors).toHaveLength(1); expect(result.page).toBe(1); // Defaults to 1 }); test('handles invalid limit', () => { const result = validatePaginationParams({ page: 1, limit: 2000 }); expect(result.errors).toHaveLength(1); expect(result.limit).toBe(1000); // Caps at 1000 }); test('handles missing parameters', () => { const result = validatePaginationParams({ page: 0, limit: 0 }); expect(result.page).toBe(1); // Defaults expect(result.limit).toBe(50); // Defaults }); }); describe('Cache', () => { test('stores and retrieves values', () => { const cache = new Cache(); cache.set('key1', 'value1'); expect(cache.get('key1')).toBe('value1'); }); test('returns null for expired values', (done) => { const cache = new Cache(100); // 100ms TTL cache.set('key1', 'value1'); expect(cache.get('key1')).toBe('value1'); setTimeout(() => { expect(cache.get('key1')).toBeNull(); done(); }, 150); }); test('deletes values', () => { const cache = new Cache(); cache.set('key1', 'value1'); cache.delete('key1'); expect(cache.get('key1')).toBeNull(); }); test('clears all values', () => { const cache = new Cache(); cache.set('key1', 'value1'); cache.set('key2', 'value2'); cache.clear(); expect(cache.size()).toBe(0); }); test('tracks size correctly', () => { const cache = new Cache(); expect(cache.size()).toBe(0); cache.set('key1', 'value1'); expect(cache.size()).toBe(1); cache.set('key2', 'value2'); expect(cache.size()).toBe(2); }); }); describe('batchData', () => { test('batches data correctly', () => { const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; const batches = batchData(data, 3); expect(batches).toHaveLength(4); expect(batches[0]).toEqual([1, 2, 3]); expect(batches[1]).toEqual([4, 5, 6]); expect(batches[2]).toEqual([7, 8, 9]); expect(batches[3]).toEqual([10]); }); test('handles empty array', () => { const batches = batchData([], 5); expect(batches).toHaveLength(0); }); test('handles batch size larger than data', () => { const data = [1, 2, 3]; const batches = batchData(data, 10); expect(batches).toHaveLength(1); expect(batches[0]).toEqual([1, 2, 3]); }); }); describe('cursor functions', () => { test('creates and decodes cursor', () => { const cursor = createCursor(5, 50); expect(cursor).toBeDefined(); const decoded = decodeCursor(cursor); expect(decoded).toEqual({ page: 5, limit: 50 }); }); test('returns null for invalid cursor', () => { const decoded = decodeCursor('invalid-cursor'); expect(decoded).toBeNull(); }); }); });