import { afterEach, describe, expect, it, vi } from 'vitest' import { fetchJobs, parseJobsResponse } from './jobs' afterEach(() => { vi.unstubAllGlobals() }) describe('parseJobsResponse', () => { it('returns typed jobs from the API envelope', () => { const result = parseJobsResponse({ data: [ { id: 'job-1', type: 'demo', payload: { message: 'hello' }, state: 'running', priority: 2, available_at: '2026-09-03T13:00:00Z', attempt: 1, max_attempts: 5, idempotency_key: 'demo:1', created_at: '2026-09-03T13:00:00Z', updated_at: '2026-09-03T13:00:01Z', }, ], }) expect(result).toHaveLength(1) expect(result[0]).toMatchObject({ id: 'job-1', state: 'running', attempt: 1 }) }) }) describe('fetchJobs', () => { it('loads the latest 50 jobs from the read-only list endpoint', async () => { const fetchMock = vi.fn().mockResolvedValue({ ok: true, json: async () => ({ data: [] }), }) vi.stubGlobal('fetch', fetchMock) await expect(fetchJobs()).resolves.toEqual([]) expect(fetchMock).toHaveBeenCalledWith('/api/v1/jobs?limit=50', { headers: { Accept: 'application/json' }, signal: undefined, }) }) })