CI and images / lint (push) Successful in 2s
CI and images / extension-version (push) Successful in 2s
CI and images / frontend-build (push) Successful in 20s
CI and images / backend-lint-and-test (push) Successful in 30s
CI and images / integration (push) Successful in 2m18s
CI and images / sign-extension (push) Successful in 3s
CI and images / build-agent (push) Successful in 6s
CI and images / build-web (push) Successful in 1m44s
CI and images / smoke-web (push) Successful in 56s
CI and images / promote (push) Skipped
tests/factories.py holds image_row/make_image/make_image_async/make_tag. The 17 byte-identical _img/_tag helpers (15 modules) now import them under their old names, so no call site changed. frontend/test/support/stubFetch.js replaces 15 copies that differed only in formatting. Copies whose bodies differ (other defaults, other columns, a url-only stub) are left as they are; folding those needs a look at each caller. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LVjrnpQjRgHdvq95rASoiR
132 lines
4.9 KiB
JavaScript
132 lines
4.9 KiB
JavaScript
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
|
import { setActivePinia, createPinia } from 'pinia'
|
|
import { useSourcesStore } from '../src/stores/sources.js'
|
|
import { stubFetch } from './support/stubFetch.js'
|
|
|
|
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('update patches the source in place without dropping the cache', async () => {
|
|
const s = useSourcesStore()
|
|
s.byArtist.set(null, [
|
|
{ id: 5, artist_id: 5, enabled: true, backfill_state: null },
|
|
{ id: 6, artist_id: 5, enabled: true },
|
|
])
|
|
stubFetch(() => ({
|
|
status: 200,
|
|
body: { id: 5, artist_id: 5, enabled: false, backfill_state: null },
|
|
}))
|
|
await s.update(5, { enabled: false }, 5)
|
|
// Cache survives (no full reload) and only source 5 changed.
|
|
expect(s.byArtist.has(null)).toBe(true)
|
|
const arr = s.byArtist.get(null)
|
|
expect(arr.find(r => r.id === 5).enabled).toBe(false)
|
|
expect(arr.find(r => r.id === 6).enabled).toBe(true)
|
|
})
|
|
|
|
it('startBackfill patches backfill_state in place', async () => {
|
|
const s = useSourcesStore()
|
|
s.byArtist.set(null, [{ id: 5, artist_id: 5, backfill_state: null }])
|
|
stubFetch(() => ({ status: 200, body: { id: 5, backfill_state: 'running' } }))
|
|
await s.startBackfill(5, 5)
|
|
expect(s.byArtist.get(null).find(r => r.id === 5).backfill_state).toBe('running')
|
|
})
|
|
|
|
it('remove drops the source from the cache in place', async () => {
|
|
const s = useSourcesStore()
|
|
s.byArtist.set(null, [{ id: 5, artist_id: 5 }, { id: 6, artist_id: 5 }])
|
|
stubFetch(() => ({ status: 204, body: null }))
|
|
await s.remove(5, 5)
|
|
expect(s.byArtist.has(null)).toBe(true)
|
|
expect(s.byArtist.get(null).map(r => r.id)).toEqual([6])
|
|
})
|
|
|
|
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')
|
|
})
|
|
})
|