import { afterEach, expect, it, vi } from 'vitest' import { adminApi } from '../src/api/admin' afterEach(() => { vi.unstubAllGlobals() localStorage.clear() }) it('crée un compte via POST /api/admin/users avec authentification sans persister le secret', async () => { localStorage.setItem('token', 'admin-token') const account = { id: 'new-1', email: 'reader@example.test', role: 'user', created_at: '2026-11-20' } const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify({ data: account }), { status: 201 })) vi.stubGlobal('fetch', fetchMock) const input = { email: account.email, role: 'user' as const, password: 'safe-pass-123' } expect(await adminApi.createUser(input)).toEqual(account) expect(fetchMock).toHaveBeenCalledWith('/api/admin/users', { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: 'Bearer admin-token' }, body: JSON.stringify(input), }) expect(JSON.stringify(localStorage)).not.toContain(input.password) expect(JSON.stringify(sessionStorage)).not.toContain(input.password) }) it('envoie les plafonds via les endpoints admin et préserve le plafond lors du basculement seul', async () => { const fetchMock = vi.fn().mockImplementation(() => Promise.resolve(new Response(JSON.stringify({ data: { ok: true } }), { status: 200 }))) vi.stubGlobal('fetch', fetchMock) await adminApi.updateSettings([{ key: 'summary_max_articles', value: '50' }]) await adminApi.updateSource('source-1', true, 20) await adminApi.updateSource('source-1', false) expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/admin/settings', expect.objectContaining({ method: 'PUT', body: JSON.stringify({ settings: [{ key: 'summary_max_articles', value: '50' }] }), })) expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/admin/sources/source-1', expect.objectContaining({ method: 'PUT', body: JSON.stringify({ enabled: true, article_limit: 20 }), })) expect(fetchMock).toHaveBeenNthCalledWith(3, '/api/admin/sources/source-1', expect.objectContaining({ method: 'PUT', body: JSON.stringify({ enabled: false }), })) }) it('remplace les flux RSS par PUT sans modifier le plafond et sans appeler les feeds', async () => { const urls = ['https://feed.quanwenrss.com/feed/bloomberg-latest.xml'] const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify({ data: { ok: true } }), { status: 200 })) vi.stubGlobal('fetch', fetchMock) await adminApi.updateSourceFeeds('source-b', false, urls) expect(fetchMock).toHaveBeenCalledExactlyOnceWith('/api/admin/sources/source-b', expect.objectContaining({ method: 'PUT', body: JSON.stringify({ enabled: false, feed_urls: urls }) })) })