import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' import { setActivePinia, createPinia } from 'pinia' import { usePlatformsStore } from '../src/stores/platforms.js' function stubFetch(handler) { globalThis.fetch = vi.fn(async (url, init) => { const { status, body } = handler(url, init) return { ok: status >= 200 && status < 300, status, statusText: String(status), text: async () => (body == null ? '' : JSON.stringify(body)), } }) } describe('platforms store', () => { beforeEach(() => setActivePinia(createPinia())) afterEach(() => vi.restoreAllMocks()) it('loadAll fetches /api/platforms and populates list+byKey', async () => { const s = usePlatformsStore() stubFetch(() => ({ status: 200, body: { platforms: { patreon: { key: 'patreon', name: 'Patreon', auth_type: 'cookies' }, discord: { key: 'discord', name: 'Discord', auth_type: 'token' }, }}, })) await s.loadAll() expect(s.list.map(p => p.key).sort()).toEqual(['discord', 'patreon']) expect(s.byKey.get('patreon').auth_type).toBe('cookies') }) it('loadAll is cached on subsequent calls', async () => { const s = usePlatformsStore() let n = 0 stubFetch(() => { n++; return { status: 200, body: { platforms: {} } } }) await s.loadAll() await s.loadAll() expect(n).toBe(1) }) })