import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; import { listSourceCorpora, createSourceCorpus, deleteSourceCorpus } from './sourceCorpus.js'; const okResponse = (body) => ({ ok: true, status: 200, json: () => Promise.resolve(body), }); describe('sourceCorpora API', () => { beforeEach(() => { vi.stubGlobal('fetch', vi.fn()); }); afterEach(() => { vi.restoreAllMocks(); }); describe('listSourceCorpora', () => { it('fetches GET /api/source-corpus', async () => { const data = [{ id: '1', name: 'NB' }]; fetch.mockResolvedValue(okResponse(data)); const result = await listSourceCorpora(); expect(fetch).toHaveBeenCalledWith('/api/source-corpus'); expect(result).toEqual(data); }); }); describe('createSourceCorpus', () => { it('sends POST with name in JSON body', async () => { const corpus = { id: '2', name: 'New' }; fetch.mockResolvedValue(okResponse(corpus)); const result = await createSourceCorpus('New'); expect(fetch).toHaveBeenCalledWith('/api/source-corpus', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'New' }), }); expect(result).toEqual(corpus); }); }); describe('deleteSourceCorpus', () => { it('sends DELETE to /api/source-corpus/:id', async () => { fetch.mockResolvedValue(okResponse({})); await deleteSourceCorpus('abc-123'); expect(fetch).toHaveBeenCalledWith('/api/source-corpus/abc-123', { method: 'DELETE', }); }); }); it('throws on non-ok response', async () => { fetch.mockResolvedValue({ ok: false, status: 500, json: () => Promise.resolve({ error: 'Server down' }), }); await expect(listSourceCorpora()).rejects.toThrow('Server down'); }); });