import { beforeEach, describe, expect, it, vi } from 'vitest'; import { getLogs } from './logs'; const samplePage = { totalCount: 2, page: [ { timestamp: '2026-07-07T00:00:00Z', level: 'Warning', message: 'uh oh' }, { timestamp: '2026-07-07T00:01:00Z', level: 'Information', message: 'all good' } ] }; describe('getLogs', () => { beforeEach(() => { window.localStorage.clear(); vi.restoreAllMocks(); }); it('GETs /api/logs with no query string when called without params', async () => { const fetchSpy = vi.spyOn(window, 'fetch').mockResolvedValue( new Response(JSON.stringify(samplePage), { headers: { 'Content-Type': 'application/json' }, status: 200 }) ); await expect(getLogs()).resolves.toMatchObject({ totalCount: 2 }); const [url, init] = fetchSpy.mock.calls[0]; expect(url).toBe('/api/logs'); expect((init?.method ?? 'GET').toUpperCase()).toBe('GET'); }); it('builds the query string from filter, pageNum and pageSize', async () => { const fetchSpy = vi.spyOn(window, 'fetch').mockResolvedValue( new Response(JSON.stringify(samplePage), { headers: { 'Content-Type': 'application/json' }, status: 200 }) ); await getLogs({ filter: 'boom', pageNum: 2, pageSize: 50 }); const [url] = fetchSpy.mock.calls[0]; expect(url).toBe('/api/logs?filter=boom&pageNum=2&pageSize=50'); }); it('rejects with the ApiError status on failure', async () => { vi.spyOn(window, 'fetch').mockResolvedValue( new Response(JSON.stringify({ status: 500, title: 'Server Error' }), { headers: { 'Content-Type': 'application/json' }, status: 500 }) ); await expect(getLogs()).rejects.toMatchObject({ status: 500 }); }); });