feat(fc3a): sources Pinia store + vitest

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-20 12:56:22 -04:00
parent c415cb45d9
commit ded5e5f642
2 changed files with 200 additions and 0 deletions
+109
View File
@@ -0,0 +1,109 @@
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { useApi } from '../composables/useApi.js'
export const useSourcesStore = defineStore('sources', () => {
const api = useApi()
// keyed cache: null => all sources, number => artist_id filtered
const byArtist = ref(new Map())
const platforms = ref([])
const platformsLoaded = ref(false)
const loading = ref(false)
const error = ref(null)
const allSources = computed(() => byArtist.value.get(null) ?? [])
async function loadAll() {
loading.value = true
error.value = null
try {
const body = await api.get('/api/sources')
byArtist.value.set(null, body)
} catch (e) {
error.value = e
} finally {
loading.value = false
}
}
async function loadForArtist(artistId) {
loading.value = true
error.value = null
try {
const body = await api.get('/api/sources', { params: { artist_id: artistId } })
byArtist.value.set(artistId, body)
} catch (e) {
error.value = e
} finally {
loading.value = false
}
}
async function loadPlatforms() {
if (platformsLoaded.value) return
const body = await api.get('/api/sources/platforms')
platforms.value = body.platforms
platformsLoaded.value = true
}
function _invalidate(artistId) {
byArtist.value.delete(null)
if (artistId != null) byArtist.value.delete(artistId)
}
async function create(payload) {
const body = await api.post('/api/sources', { body: payload })
_invalidate(payload.artist_id)
return body
}
async function update(id, patch, artistIdHint = null) {
const body = await api.patch(`/api/sources/${id}`, { body: patch })
_invalidate(artistIdHint ?? body.artist_id)
return body
}
async function remove(id, artistIdHint = null) {
await api.delete(`/api/sources/${id}`)
_invalidate(artistIdHint)
}
async function findOrCreateArtist(name) {
const body = await api.post('/api/artists', { body: { name } })
return { artist: { id: body.id, name: body.name, slug: body.slug }, created: body.created }
}
async function autocompleteArtist(query, limit = 20) {
if (!query || !query.trim()) return []
return await api.get('/api/artists/autocomplete', { params: { q: query, limit } })
}
function sourcesByArtistGrouped() {
// returns [{artist: {id,name,slug}, sources: [...]}, ...]
const arr = byArtist.value.get(null) ?? []
const groups = new Map()
for (const s of arr) {
const key = s.artist_id
if (!groups.has(key)) {
groups.set(key, {
artist: { id: s.artist_id, name: s.artist_name, slug: s.artist_slug },
sources: [],
})
}
groups.get(key).sources.push(s)
}
return Array.from(groups.values()).sort(
(a, b) => a.artist.name.localeCompare(b.artist.name)
)
}
return {
byArtist, platforms, platformsLoaded, loading, error,
allSources,
loadAll, loadForArtist, loadPlatforms,
create, update, remove,
findOrCreateArtist, autocompleteArtist,
sourcesByArtistGrouped,
}
})
+91
View File
@@ -0,0 +1,91 @@
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('loadPlatforms caches the platforms list', async () => {
const s = useSourcesStore()
let n = 0
stubFetch(() => { n++; return { status: 200, body: { platforms: ['patreon'] } } })
await s.loadPlatforms()
await s.loadPlatforms() // second call should hit cache
expect(n).toBe(1)
expect(s.platforms).toEqual(['patreon'])
})
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')
})
})