import { describe, expect, it } from 'vitest'; import { pagerPages, requestedPage } from '#web-runtime/layouts/components/blog-pager'; describe('requestedPage', () => { it('defaults to page 1 with no search params at all', () => { expect(requestedPage()).toBe(1); expect(requestedPage({})).toBe(1); }); it('parses a plain string value', () => { expect(requestedPage({ page: '3' })).toBe(3); }); it('takes the first entry when the param is repeated', () => { expect(requestedPage({ page: ['4', '9'] })).toBe(4); }); it('falls back to 1 for an empty repeated param', () => { expect(requestedPage({ page: [] })).toBe(1); }); it('falls back to 1 for values that are not numbers', () => { expect(requestedPage({ page: 'abc' })).toBe(1); expect(requestedPage({ page: '' })).toBe(1); expect(requestedPage({ page: undefined })).toBe(1); }); it('falls back to 1 for zero and negative pages', () => { expect(requestedPage({ page: '0' })).toBe(1); expect(requestedPage({ page: '-2' })).toBe(1); }); it('truncates decimals via parseInt (pinned behavior)', () => { expect(requestedPage({ page: '2.9' })).toBe(2); }); it('accepts numbers with trailing junk, as parseInt does (pinned behavior)', () => { expect(requestedPage({ page: '3abc' })).toBe(3); }); }); describe('pagerPages', () => { it('shows a single page as itself', () => { expect(pagerPages(1, 1)).toEqual([1]); }); it('shows every page when the range is small', () => { expect(pagerPages(2, 3)).toEqual([1, 2, 3]); expect(pagerPages(3, 4)).toEqual([1, 2, 3, 4]); }); it('windows a large range around the current page, keeping the first and last', () => { expect(pagerPages(5, 10)).toEqual([1, 4, 5, 6, 10]); }); it('anchors the window at the start when on page 1', () => { expect(pagerPages(1, 10)).toEqual([1, 2, 10]); }); it('anchors the window at the end when on the last page', () => { expect(pagerPages(10, 10)).toEqual([1, 9, 10]); }); it('deduplicates when the window overlaps the anchors', () => { expect(pagerPages(2, 2)).toEqual([1, 2]); expect(pagerPages(2, 4)).toEqual([1, 2, 3, 4]); }); it('always returns a sorted ascending list', () => { const pages = pagerPages(37, 80); expect(pages).toEqual([...pages].sort((a, b) => a - b)); expect(pages).toEqual([1, 36, 37, 38, 80]); }); });