import assert from 'node:assert/strict'
import { beforeEach, test } from 'node:test'
import { reset } from './helpers/runtime.ts'
import { DeadlineError, ResponseTimeoutError, startDeadline } from '../deadline.ts'
const { throughLogin } = await import('../login-client.ts')
const { readBackoff } = await import('../backoff.ts')
beforeEach(reset)
async function fail(error: Error) {
const deadline = startDeadline(20_000, () => new DeadlineError('Unrelated timeout wording.'))
try { await assert.rejects(throughLogin(deadline, async () => { throw error }), error) }
finally { deadline.end() }
}
test('typed deadline timeouts count even if their wording changes', async () => {
await fail(new ResponseTimeoutError('A different timeout sentence.'))
await fail(new ResponseTimeoutError('Another timeout sentence.'))
assert.equal((await readBackoff()).failures, 1)
})
test('untyped errors cannot count as timeouts by copying the visible message', async () => {
await fail(new Error('Audible API GET /1.0/library → no answer within 20 s'))
await fail(new Error('Audible API GET /1.0/library → no answer within 20 s'))
assert.equal((await readBackoff()).failures, 0)
assert.equal((await readBackoff()).timeoutPending, false)
})
test('typed body or pre-send deadline errors do not count as response timeouts', async () => {
await fail(new DeadlineError('A body took too long.'))
await fail(new DeadlineError('Another body took too long.'))
assert.equal((await readBackoff()).failures, 0)
assert.equal((await readBackoff()).timeoutPending, false)
})