feat(fc3c): downloads Pinia store + sources.checkNow action + vitests
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref } from 'vue'
|
||||
import { useApi } from '../composables/useApi.js'
|
||||
|
||||
export const useDownloadsStore = defineStore('downloads', () => {
|
||||
const api = useApi()
|
||||
|
||||
const events = ref([])
|
||||
const cursor = ref(null)
|
||||
const hasMore = ref(true)
|
||||
const filter = ref({ status: null, source_id: null, artist_id: null })
|
||||
const selected = ref(null)
|
||||
const loading = ref(false)
|
||||
const error = ref(null)
|
||||
|
||||
function _params(extra = {}) {
|
||||
const out = { limit: 50, ...extra }
|
||||
if (filter.value.status) out.status = filter.value.status
|
||||
if (filter.value.source_id != null) out.source_id = filter.value.source_id
|
||||
if (filter.value.artist_id != null) out.artist_id = filter.value.artist_id
|
||||
return out
|
||||
}
|
||||
|
||||
async function loadFirst() {
|
||||
loading.value = true
|
||||
error.value = null
|
||||
try {
|
||||
const body = await api.get('/api/downloads', { params: _params() })
|
||||
events.value = body
|
||||
cursor.value = body.length ? body[body.length - 1].id : null
|
||||
hasMore.value = body.length === 50
|
||||
} catch (e) {
|
||||
error.value = e
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function loadMore() {
|
||||
if (!hasMore.value || cursor.value == null) return
|
||||
loading.value = true
|
||||
try {
|
||||
const body = await api.get('/api/downloads', { params: _params({ before: cursor.value }) })
|
||||
events.value.push(...body)
|
||||
cursor.value = body.length ? body[body.length - 1].id : cursor.value
|
||||
hasMore.value = body.length === 50
|
||||
} catch (e) {
|
||||
error.value = e
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function loadOne(id) {
|
||||
selected.value = await api.get(`/api/downloads/${id}`)
|
||||
return selected.value
|
||||
}
|
||||
|
||||
async function applyFilter(patch) {
|
||||
filter.value = { ...filter.value, ...patch }
|
||||
await loadFirst()
|
||||
}
|
||||
|
||||
function closeDetail() {
|
||||
selected.value = null
|
||||
}
|
||||
|
||||
return {
|
||||
events, cursor, hasMore, filter, selected, loading, error,
|
||||
loadFirst, loadMore, loadOne, applyFilter, closeDetail,
|
||||
}
|
||||
})
|
||||
@@ -70,6 +70,20 @@ export const useSourcesStore = defineStore('sources', () => {
|
||||
return await api.get('/api/artists/autocomplete', { params: { q: query, limit } })
|
||||
}
|
||||
|
||||
// FC-3c: trigger a download for one source. Returns {download_event_id,status}.
|
||||
const checkingIds = ref(new Set())
|
||||
|
||||
async function checkNow(id) {
|
||||
checkingIds.value = new Set(checkingIds.value).add(id)
|
||||
try {
|
||||
return await api.post(`/api/sources/${id}/check`)
|
||||
} finally {
|
||||
const next = new Set(checkingIds.value)
|
||||
next.delete(id)
|
||||
checkingIds.value = next
|
||||
}
|
||||
}
|
||||
|
||||
function sourcesByArtistGrouped() {
|
||||
// returns [{artist: {id,name,slug}, sources: [...]}, ...]
|
||||
const arr = byArtist.value.get(null) ?? []
|
||||
@@ -92,8 +106,10 @@ export const useSourcesStore = defineStore('sources', () => {
|
||||
return {
|
||||
byArtist, loading, error,
|
||||
allSources,
|
||||
checkingIds,
|
||||
loadAll, loadForArtist,
|
||||
create, update, remove,
|
||||
checkNow,
|
||||
findOrCreateArtist, autocompleteArtist,
|
||||
sourcesByArtistGrouped,
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
||||
import { setActivePinia, createPinia } from 'pinia'
|
||||
import { useDownloadsStore } from '../src/stores/downloads.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('downloads store', () => {
|
||||
beforeEach(() => setActivePinia(createPinia()))
|
||||
afterEach(() => vi.restoreAllMocks())
|
||||
|
||||
it('loadFirst populates events and cursor', async () => {
|
||||
const s = useDownloadsStore()
|
||||
stubFetch(() => ({
|
||||
status: 200,
|
||||
body: [
|
||||
{ id: 10, status: 'ok', summary: {} },
|
||||
{ id: 9, status: 'error', summary: {} },
|
||||
],
|
||||
}))
|
||||
await s.loadFirst()
|
||||
expect(s.events.map(e => e.id)).toEqual([10, 9])
|
||||
expect(s.cursor).toBe(9)
|
||||
expect(s.hasMore).toBe(false)
|
||||
})
|
||||
|
||||
it('loadMore appends and updates cursor', async () => {
|
||||
const s = useDownloadsStore()
|
||||
s.events = [{ id: 10, summary: {} }, { id: 9, summary: {} }]
|
||||
s.cursor = 9
|
||||
s.hasMore = true
|
||||
stubFetch(() => ({
|
||||
status: 200,
|
||||
body: [{ id: 8, status: 'ok', summary: {} }],
|
||||
}))
|
||||
await s.loadMore()
|
||||
expect(s.events.map(e => e.id)).toEqual([10, 9, 8])
|
||||
expect(s.cursor).toBe(8)
|
||||
})
|
||||
|
||||
it('applyFilter merges and reloads', async () => {
|
||||
const s = useDownloadsStore()
|
||||
const urls = []
|
||||
stubFetch((url) => {
|
||||
urls.push(url)
|
||||
return { status: 200, body: [] }
|
||||
})
|
||||
await s.applyFilter({ status: 'error' })
|
||||
expect(s.filter.status).toBe('error')
|
||||
expect(urls[0]).toContain('status=error')
|
||||
})
|
||||
|
||||
it('loadOne populates selected', async () => {
|
||||
const s = useDownloadsStore()
|
||||
stubFetch(() => ({ status: 200, body: { id: 42, metadata: { run_stats: {} } } }))
|
||||
const out = await s.loadOne(42)
|
||||
expect(s.selected.id).toBe(42)
|
||||
expect(out.id).toBe(42)
|
||||
})
|
||||
|
||||
it('closeDetail clears selected', async () => {
|
||||
const s = useDownloadsStore()
|
||||
s.selected = { id: 1 }
|
||||
s.closeDetail()
|
||||
expect(s.selected).toBe(null)
|
||||
})
|
||||
})
|
||||
@@ -78,4 +78,29 @@ describe('sources store', () => {
|
||||
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')
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user