import { afterEach, expect, it, vi } from 'vitest'; import { fetchCampaigns } from '../../src/lib/ui/campaigns.js'; afterEach(() => vi.unstubAllGlobals()); it('loads history beyond the API page limit without truncating or fetching unwanted older rows', async () => { const requests: URL[] = []; vi.stubGlobal('fetch', vi.fn(async (input: string) => { const url = new URL(input, 'http://localhost'); requests.push(url); const start = Number(url.searchParams.get('offset')); const count = Number(url.searchParams.get('limit')); return Response.json({ total: 900, deployments: Array.from({ length: count }, (_, i) => ({ id: start + i })) }); })); const result = await fetchCampaigns<{ id: number }>({ server: 'test-server', view: 'history' }, 525); expect(result.total).toBe(900); expect(result.deployments.map(row => row.id)).toEqual(Array.from({ length: 525 }, (_, i) => i)); expect(requests.map(url => Object.fromEntries(url.searchParams))).toEqual([ { server: 'test-server', view: 'history', offset: '0', limit: '500' }, { server: 'test-server', view: 'history', offset: '500', limit: '25' } ]); }); it('loads all active campaigns independently of the history limit', async () => { vi.stubGlobal('fetch', vi.fn(async (input: string) => { const url = new URL(input, 'http://localhost'); const start = Number(url.searchParams.get('offset')); return Response.json({ total: 550, deployments: Array.from({ length: start ? 50 : 500 }, (_, i) => ({ id: start + i })) }); })); const result = await fetchCampaigns<{ id: number }>({ active: 'true' }, Infinity); expect(result.deployments).toHaveLength(550); expect(result.deployments[549].id).toBe(549); });