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

backoff-owner-actions.test.ts

37 lines · 1.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 { disconnect } = await import('../disconnect.ts')
const { readBackoff } = await import('../backoff.ts')
beforeEach(reset)
test('owner Disconnect bypasses the wait after two login_failed outcomes', async t => {
 t.mock.timers.enable({ apis: ['Date'], now: 0 })
 f.provider = async () => { throw Object.assign(new Error('The login exchange did not complete.'), { code: 'login_failed' }) }
 await assert.rejects(request({ path: '/1.0/library' }))
 t.mock.timers.setTime((await readBackoff()).nextAllowedAt)
 await assert.rejects(request({ path: '/1.0/library' }))
 const waiting = await readBackoff()
 assert.equal(waiting.failures, 2); assert.ok(waiting.nextAllowedAt > Date.now())
 f.provider = async (url, init) => {
  assert.equal(url, 'https://api.amazon.ca/auth/deregister'); assert.equal(init?.method, 'POST')
  return Response.json({})
 }
 await disconnect()
 const state = await readBackoff()
 assert.equal(state.failures, 0); assert.equal(state.nextAllowedAt, 0)
 assert.equal(state.reason, null); assert.equal(state.timeoutPending, false)
 assert.equal(state.generation, waiting.generation + 1); assert.equal(state.disconnected, true)
 assert.equal(f.fetches, 3)
})
test('a healthy success does not rewrite an already clean row', async t => {
 t.mock.timers.enable({ apis: ['Date'], now: 0 })
 await request({ path: '/1.0/library' })
 const state = await readBackoff()
 const before = (await f.store.sql('SELECT total_changes() AS changes')).rows[0].changes
 t.mock.timers.setTime(1000)
 await request({ path: '/1.0/wishlist' })
 assert.deepEqual(await readBackoff(), state)
 assert.equal((await f.store.sql('SELECT total_changes() AS changes')).rows[0].changes, before)
 assert.equal(f.fetches, 2)
})