Skip to content
← Public packages

@kentcdodds/audible

Personal Audible library app — browser re-auth, library, wishlist, NAS archive stub.

src/__tests__/chapters.test.ts

47 lines · 1.2 KB · TypeScript
import assert from 'node:assert/strict'
import { test } from 'node:test'
import { mapChapters, titleFromLibraryPayload } from '../chapters.ts'

test('mapChapters flattens tree chapters to startMs/lengthMs', () => {
	const chapters = mapChapters({
		chapters: [
			{
				title: 'Part 1',
				start_offset_ms: 0,
				length_ms: 100000,
				chapters: [
					{ title: 'Opening', start_offset_ms: 0, length_ms: 4000 },
					{ title: 'Chapter 1', start_offset_ms: 4000, length_ms: 96000 },
				],
			},
		],
	})
	assert.equal(chapters?.length, 2)
	assert.deepEqual(chapters?.[0], {
		title: 'Opening',
		startMs: 0,
		lengthMs: 4000,
	})
	assert.deepEqual(chapters?.[1], {
		title: 'Chapter 1',
		startMs: 4000,
		lengthMs: 96000,
	})
})

test('mapChapters accepts a flat list', () => {
	const chapters = mapChapters({
		chapters: [{ title: 'Credits', start_offset_ms: 10, length_ms: 20 }],
	})
	assert.deepEqual(chapters, [
		{ title: 'Credits', startMs: 10, lengthMs: 20 },
	])
})

test('titleFromLibraryPayload reads nested item.title', () => {
	assert.equal(
		titleFromLibraryPayload({ item: { title: 'Hatchet', asin: 'B002UZKI96' } }),
		'Hatchet',
	)
	assert.equal(titleFromLibraryPayload({ raw_title: 'Hatchet' }), 'Hatchet')
})