fix: sync removes albums no longer in subsonic, classify non-json responses as api errors
This commit is contained in:
@@ -154,4 +154,53 @@ describe('library sync', () => {
|
||||
expect(state.albums).toBe(503)
|
||||
await app.close()
|
||||
})
|
||||
|
||||
it('re-sync removes albums that disappeared from subsonic', async () => {
|
||||
// sync 1 (from settings PUT): 1 album 'Kept Album'
|
||||
// sync 2 (explicit POST): 1 album 'Only Album' — 'Kept Album' must be gone
|
||||
let syncCount = 0
|
||||
const shrinking = (async (input: any) => {
|
||||
const url = new URL(String(input))
|
||||
if (url.pathname.endsWith('/rest/ping')) {
|
||||
return new Response(JSON.stringify({ 'subsonic-response': { status: 'ok' } }), {
|
||||
status: 200,
|
||||
headers: { 'content-type': 'application/json' },
|
||||
})
|
||||
}
|
||||
if (url.pathname.endsWith('/rest/getAlbumList2')) {
|
||||
syncCount++
|
||||
const album =
|
||||
syncCount === 1
|
||||
? { id: 1, name: 'Kept Album', artist: 'Artist A' }
|
||||
: { id: 9, name: 'Only Album', artist: 'Artist B' }
|
||||
return new Response(
|
||||
JSON.stringify({ 'subsonic-response': { status: 'ok', albumList2: { album: [album] } } }),
|
||||
{ status: 200, headers: { 'content-type': 'application/json' } }
|
||||
)
|
||||
}
|
||||
return new Response('nope', { status: 404 })
|
||||
}) as typeof fetch
|
||||
const app = await buildTestApp(shrinking)
|
||||
const cookie = await setupAdmin(app)
|
||||
await app.inject({
|
||||
method: 'PUT',
|
||||
url: '/api/settings',
|
||||
...auth(cookie),
|
||||
payload: { subsonicUrl: 'http://n.local', subsonicUsername: 'sam', subsonicPassword: 'pass' },
|
||||
})
|
||||
|
||||
const first = await waitForDone(app, cookie)
|
||||
expect(first.albums).toBe(1)
|
||||
expect(await app.inject({ method: 'GET', url: '/api/library/albums?q=Kept', ...auth(cookie) }).then((r) => r.json())).toMatchObject({ albums: [expect.objectContaining({ title: 'Kept Album' })] })
|
||||
|
||||
await app.inject({ method: 'POST', url: '/api/library/sync', ...auth(cookie) })
|
||||
const second = await waitForDone(app, cookie)
|
||||
expect(second.albums).toBe(1)
|
||||
|
||||
const stale = await app.inject({ method: 'GET', url: '/api/library/albums?q=Kept', ...auth(cookie) })
|
||||
expect(stale.json().albums).toHaveLength(0) // stale album removed
|
||||
const kept = await app.inject({ method: 'GET', url: '/api/library/albums?q=Only', ...auth(cookie) })
|
||||
expect(kept.json().albums).toHaveLength(1)
|
||||
await app.close()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user