import assert from 'node:assert/strict'
import { beforeEach, test } from 'node:test'
import { fixture as f, reset } from './helpers/runtime.ts'
const request = (await import('../request.ts')).default
const { readBackoff } = await import('../backoff.ts')
const { disconnect } = await import('../disconnect.ts')
const a = '00000000-0000-4000-8000-000000000001', b = '00000000-0000-4000-8000-000000000002'
beforeEach(() => { reset(); f.description = JSON.stringify({ registration: a, deviceName: 'Before' }) })
test('registration: website metadata edits cannot clear an existing wait', async () => {
f.provider = async () => new Response(null, { status: 401 })
await assert.rejects(request({ path: '/1.0/library' }))
const wait = await readBackoff()
f.updatedAt = 'edited'; f.description = JSON.stringify({ registration: a, deviceName: 'After', note: 'owner edit' })
f.provider = async () => Response.json({})
await assert.rejects(request({ path: '/1.0/wishlist' }), /login wait/)
assert.deepEqual(await readBackoff(), wait); assert.equal(f.fetches, 1)
})
test('registration: website metadata edits cannot clear a Disconnect', async () => {
await disconnect()
const state = await readBackoff()
f.updatedAt = 'edited'; f.description = JSON.stringify({ registration: a, deviceName: 'After' })
await assert.rejects(request({ path: '/1.0/library' }), { name: 'NotConnectedError' })
assert.deepEqual(await readBackoff(), state); assert.equal(f.fetches, 1)
})
test('registration: a new UUID resets the wait even with unchanged updated_at', async () => {
f.provider = async () => new Response(null, { status: 401 })
await assert.rejects(request({ path: '/1.0/library' }))
const wait = await readBackoff()
f.description = JSON.stringify({ registration: b })
f.provider = async () => Response.json({})
await request({ path: '/1.0/wishlist' })
const state = await readBackoff()
assert.equal(state.generation, wait.generation + 1); assert.equal(state.failures, 0)
})
test('registration: missing, malformed and invalid markers fail closed without clearing state', async () => {
f.provider = async () => new Response(null, { status: 401 })
await assert.rejects(request({ path: '/1.0/library' }))
const wait = await readBackoff()
for (const description of ['', '{}', 'null', 'malformed', JSON.stringify({ registration: 'invalid' })]) {
f.description = description; f.updatedAt = 'edited'
await assert.rejects(request({ path: '/1.0/wishlist' }), /registration marker/)
assert.deepEqual(await readBackoff(), wait)
}
assert.equal(f.fetches, 1)
})