Files
FabledCurator/frontend/test/sources.spec.js
T
2026-05-20 20:45:45 -04:00

107 lines
3.7 KiB
JavaScript

import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { setActivePinia, createPinia } from 'pinia'
import { useSourcesStore } from '../src/stores/sources.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('sources store', () => {
beforeEach(() => setActivePinia(createPinia()))
afterEach(() => vi.restoreAllMocks())
it('loadAll caches under the "all" key', async () => {
const s = useSourcesStore()
stubFetch(() => ({
status: 200,
body: [{ id: 1, artist_id: 5, artist_name: 'Alice', artist_slug: 'alice',
platform: 'patreon', url: 'https://p/a', enabled: true,
config_overrides: null, last_checked_at: null,
last_error: null, check_interval_override: null }],
}))
await s.loadAll()
expect(s.allSources.map(r => r.id)).toEqual([1])
})
it('loadForArtist filters by artist_id', async () => {
const s = useSourcesStore()
const calls = []
stubFetch((url) => {
calls.push(url)
return { status: 200, body: [] }
})
await s.loadForArtist(7)
expect(calls[0]).toContain('artist_id=7')
})
it('create invalidates the all-cache and the artist-cache', async () => {
const s = useSourcesStore()
s.byArtist.set(null, [])
s.byArtist.set(5, [])
stubFetch(() => ({
status: 201,
body: { id: 1, artist_id: 5, artist_name: 'Alice', artist_slug: 'alice',
platform: 'patreon', url: 'https://p/a', enabled: true,
config_overrides: null, last_checked_at: null,
last_error: null, check_interval_override: null },
}))
await s.create({ artist_id: 5, platform: 'patreon', url: 'https://p/a' })
expect(s.byArtist.has(null)).toBe(false)
expect(s.byArtist.has(5)).toBe(false)
})
it('findOrCreateArtist posts to /api/artists', async () => {
const s = useSourcesStore()
const calls = []
stubFetch((url, init) => {
calls.push({ url, body: init.body ? JSON.parse(init.body) : null })
return { status: 201, body: { id: 1, name: 'Alice', slug: 'alice', created: true } }
})
const out = await s.findOrCreateArtist('Alice')
expect(out.artist.name).toBe('Alice')
expect(out.created).toBe(true)
expect(calls[0].body).toEqual({ name: 'Alice' })
})
it('autocompleteArtist hits /api/artists/autocomplete', async () => {
const s = useSourcesStore()
const urls = []
stubFetch((url) => { urls.push(url); return { status: 200, body: [] } })
await s.autocompleteArtist('al')
expect(urls[0]).toContain('/api/artists/autocomplete')
expect(urls[0]).toContain('q=al')
})
it('checkNow posts to /api/sources/<id>/check', async () => {
const s = useSourcesStore()
const calls = []
stubFetch((url, init) => {
calls.push({ url, method: init?.method, body: init?.body ? JSON.parse(init.body) : null })
return { status: 202, body: { download_event_id: 7, status: 'pending' } }
})
const out = await s.checkNow(5)
expect(calls[0].url).toBe('/api/sources/5/check')
expect(calls[0].method).toBe('POST')
expect(out.download_event_id).toBe(7)
})
it('checkNow exposes the 409 download_event_id via error.body', async () => {
const s = useSourcesStore()
stubFetch(() => ({ status: 409, body: { download_event_id: 12, status: 'already_running' } }))
try {
await s.checkNow(5)
} catch (e) {
expect(e.body?.download_event_id).toBe(12)
return
}
throw new Error('Expected checkNow to throw on 409')
})
})