Skip to content

Black Friday

Annual Pro is $90 for the first year, normally $120. Or start monthly at $6 for two months, then $12.

Ends Dec 4, 11:59 PM PT.

Create an account
← Public packages

@danj/audible-auth

Locked Audible gateway using one owner-provisioned Kody login.

FollowFork

Files

outcome-version.test.ts

60 lines · 2.9 KB · TypeScript
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')
beforeEach(reset)
function deferred<T>() {
 let resolve = (_value: T) => {}
 const promise = new Promise<T>(done => { resolve = done })
 return { promise, resolve }
}
async function burst() {
 const a = deferred<Response>(), b = deferred<Response>(), entered = deferred<void>()
 let count = 0
 f.provider = async url => { if (++count === 2) entered.resolve(); return url.includes('library') ? a.promise : b.promise }
 const first = request({ path: '/1.0/library' }).catch(() => undefined)
 const second = request({ path: '/1.0/wishlist' }).catch(() => undefined)
 await entered.promise
 return { a, b, first, second }
}
test('version: an older admitted success cannot erase a newer failure wait', async () => {
 const { a, b, first, second } = await burst()
 a.resolve(new Response(null, { status: 401 })); await first
 const wait = await readBackoff()
 b.resolve(Response.json({})); await second
 assert.deepEqual(await readBackoff(), wait)
})
test('version: failures from one admitted burst count only once', async () => {
 const { a, b, first, second } = await burst()
 a.resolve(new Response(null, { status: 401 })); await first
 const wait = await readBackoff()
 b.resolve(new Response(null, { status: 401 })); await second
 assert.equal(wait.failures, 1)
 assert.deepEqual(await readBackoff(), wait)
})
test('version: a late admitted failure cannot extend or escalate the current wait', async t => {
 t.mock.timers.enable({ apis: ['Date'], now: 1000 })
 const { a, b, first, second } = await burst()
 a.resolve(new Response(null, { status: 401 })); await first
 const wait = await readBackoff()
 t.mock.timers.setTime(2000)
 b.resolve(new Response(null, { status: 401 })); await second
 assert.deepEqual(await readBackoff(), wait)
})
test('version: together-admitted timeouts are one observation; a later admission completes the pair', async t => {
 t.mock.timers.enable({ apis: ['Date', 'setTimeout'], now: 0 })
 const entered = deferred<void>()
 let calls = 0
 f.provider = () => { if (++calls === 2) entered.resolve(); return new Promise(() => {}) }
 const a = assert.rejects(request({ path: '/1.0/library' }), /within 20 s/)
 const b = assert.rejects(request({ path: '/1.0/wishlist' }), /within 20 s/)
 await entered.promise; t.mock.timers.tick(20_000); await Promise.all([a,b])
 const first = await readBackoff()
 assert.equal(first.failures, 0); assert.equal(first.timeoutPending, true)
 const later = assert.rejects(request({ path: '/1.0/library' }), /within 20 s/)
 for (let i = 0; i < 10; i++) await new Promise(done => setImmediate(done))
 t.mock.timers.tick(20_000); await later
 const state = await readBackoff()
 assert.equal(state.failures, 1); assert.equal(state.timeoutPending, false); assert.equal(state.reason, 'timeouts')
})