|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { paginate, type Page, type FetchPage } from '../src/lib/pagination.js'; |
| 3 | + |
| 4 | +describe('paginate', () => { |
| 5 | + it('collects all items across pages until nextToken is null', async () => { |
| 6 | + const pages: Page<string>[] = [ |
| 7 | + { items: ['a', 'b'], nextToken: 'cursor1' }, |
| 8 | + { items: ['c'], nextToken: null }, |
| 9 | + ]; |
| 10 | + let callIndex = 0; |
| 11 | + const fetchPage: FetchPage<string> = async () => pages[callIndex++]!; |
| 12 | + |
| 13 | + const result = await paginate(fetchPage); |
| 14 | + |
| 15 | + expect(result.items).toEqual(['a', 'b', 'c']); |
| 16 | + expect(result.nextToken).toBeNull(); |
| 17 | + }); |
| 18 | + |
| 19 | + it('breaks out of loop when API returns empty items with non-null nextToken', async () => { |
| 20 | + // This is the bug from issue #35: API returns { items: [], nextToken: 'cursor' } |
| 21 | + // Without the fix, paginate() would loop forever requesting the same empty page. |
| 22 | + const fetchPage: FetchPage<string> = vi.fn(async (): Promise<Page<string>> => ({ |
| 23 | + items: [], |
| 24 | + nextToken: 'cursor', |
| 25 | + })); |
| 26 | + |
| 27 | + const result = await paginate(fetchPage); |
| 28 | + |
| 29 | + expect(result.items).toEqual([]); |
| 30 | + // nextToken should reflect what the server returned, not null |
| 31 | + expect(result.nextToken).toBe('cursor'); |
| 32 | + // Must have only fetched once — no infinite loop |
| 33 | + expect(fetchPage).toHaveBeenCalledTimes(1); |
| 34 | + }); |
| 35 | + |
| 36 | + it('respects maxItems cap', async () => { |
| 37 | + const pages: Page<string>[] = [ |
| 38 | + { items: ['a', 'b', 'c'], nextToken: 'cursor1' }, |
| 39 | + { items: ['d', 'e'], nextToken: null }, |
| 40 | + ]; |
| 41 | + let callIndex = 0; |
| 42 | + const fetchPage: FetchPage<string> = async () => pages[callIndex++]!; |
| 43 | + |
| 44 | + const result = await paginate(fetchPage, { maxItems: 2 }); |
| 45 | + |
| 46 | + expect(result.items).toEqual(['a', 'b']); |
| 47 | + }); |
| 48 | + |
| 49 | + it('handles single page with null nextToken', async () => { |
| 50 | + const fetchPage: FetchPage<string> = async () => ({ |
| 51 | + items: ['x'], |
| 52 | + nextToken: null, |
| 53 | + }); |
| 54 | + |
| 55 | + const result = await paginate(fetchPage); |
| 56 | + |
| 57 | + expect(result.items).toEqual(['x']); |
| 58 | + expect(result.nextToken).toBeNull(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('handles consecutive empty pages then data', async () => { |
| 62 | + // Edge case: first page empty (with token), second page has data |
| 63 | + const pages: Page<string>[] = [ |
| 64 | + { items: [], nextToken: 'cursor1' }, |
| 65 | + { items: ['a'], nextToken: null }, |
| 66 | + ]; |
| 67 | + let callIndex = 0; |
| 68 | + const fetchPage: FetchPage<string> = vi.fn(async (): Promise<Page<string>> => pages[callIndex++]!); |
| 69 | + |
| 70 | + const result = await paginate(fetchPage); |
| 71 | + |
| 72 | + // With the fix, the first empty page breaks the loop immediately. |
| 73 | + // This is correct behavior — if the first page is empty, there's |
| 74 | + // no reason to keep fetching. |
| 75 | + expect(result.items).toEqual([]); |
| 76 | + expect(result.nextToken).toBe('cursor1'); |
| 77 | + expect(fetchPage).toHaveBeenCalledTimes(1); |
| 78 | + }); |
| 79 | +}); |
0 commit comments