|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for the pagination total capping logic used in search.vue. |
| 5 | + * |
| 6 | + * The search page caps the displayed pagination total to EAGER_LOAD_SIZE |
| 7 | + * so that page links only reflect pages that can actually be fetched. |
| 8 | + * Without the cap, a search returning total=92,000 items would show 3,680 |
| 9 | + * pages at 25 items/page, but navigation beyond page 20 silently fails. |
| 10 | + */ |
| 11 | + |
| 12 | +const EAGER_LOAD_SIZE = { algolia: 500, npm: 500 } as const |
| 13 | + |
| 14 | +function paginationTotal( |
| 15 | + effectiveTotal: number, |
| 16 | + provider: keyof typeof EAGER_LOAD_SIZE, |
| 17 | +): number { |
| 18 | + const cap = EAGER_LOAD_SIZE[provider] |
| 19 | + return Math.min(effectiveTotal, cap) |
| 20 | +} |
| 21 | + |
| 22 | +describe('paginationTotal capping logic', () => { |
| 23 | + it('returns the total as-is when it is below the cap', () => { |
| 24 | + expect(paginationTotal(100, 'npm')).toBe(100) |
| 25 | + expect(paginationTotal(100, 'algolia')).toBe(100) |
| 26 | + }) |
| 27 | + |
| 28 | + it('returns the cap when the total exceeds it', () => { |
| 29 | + expect(paginationTotal(92_000, 'npm')).toBe(500) |
| 30 | + expect(paginationTotal(92_000, 'algolia')).toBe(500) |
| 31 | + }) |
| 32 | + |
| 33 | + it('returns exactly the cap when the total equals the cap', () => { |
| 34 | + expect(paginationTotal(500, 'npm')).toBe(500) |
| 35 | + expect(paginationTotal(500, 'algolia')).toBe(500) |
| 36 | + }) |
| 37 | + |
| 38 | + it('returns 0 when total is 0', () => { |
| 39 | + expect(paginationTotal(0, 'npm')).toBe(0) |
| 40 | + }) |
| 41 | + |
| 42 | + it('caps algolia and npm identically (both have 500 limit)', () => { |
| 43 | + const total = 10_000 |
| 44 | + expect(paginationTotal(total, 'algolia')).toBe(paginationTotal(total, 'npm')) |
| 45 | + }) |
| 46 | + |
| 47 | + it('page count derived from capped total stays within fetchable range', () => { |
| 48 | + const pageSize = 25 |
| 49 | + const rawTotal = 92_000 |
| 50 | + const cappedTotal = paginationTotal(rawTotal, 'npm') |
| 51 | + const maxPages = Math.ceil(cappedTotal / pageSize) |
| 52 | + // Should be 20 pages (500 / 25), not 3680 (92000 / 25) |
| 53 | + expect(maxPages).toBe(20) |
| 54 | + }) |
| 55 | +}) |
0 commit comments