Skip to content
← Public packages

@kentcdodds/audible

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

src/__tests__/nas-filename-candidates.test.ts

143 lines · 4.3 KB · TypeScript
import assert from 'node:assert/strict'
import { test } from 'node:test'
import {
	openAudibleColonTitle,
	listNasFilenameCandidates,
	resolveNasExists,
} from '../nas-filename-candidates.ts'

test('openAudibleColonTitle rewrites ASCII and fullwidth colons to "- "', () => {
	assert.equal(
		openAudibleColonTitle(
			'Spelling for Kids Grades 6-7: 600 English Words to Practice for the Spelling Bee',
		),
		'Spelling for Kids Grades 6-7- 600 English Words to Practice for the Spelling Bee',
	)
	assert.equal(
		openAudibleColonTitle('The Unselected Journals of Emma M. Lion: Vol. 1'),
		'The Unselected Journals of Emma M. Lion- Vol. 1',
	)
	assert.equal(openAudibleColonTitle('Foo: Bar'), 'Foo- Bar')
	assert.equal(openAudibleColonTitle('Foo:  Bar'), 'Foo- Bar')
	assert.equal(openAudibleColonTitle('No colon here'), null)
	assert.equal(openAudibleColonTitle(''), null)
})

test('listNasFilenameCandidates includes OA alternate when title has colon', async () => {
	const calls: string[] = []
	const home = {
		audiobook_library_filename: async ({ title }: { title: string }) => {
			calls.push(title)
			if (title.includes('- ')) {
				return {
					filename:
						'The Unselected Journals of Emma M. Lion- Vol. 1.m4b',
				}
			}
			return {
				filename: 'The Unselected Journals of Emma M. Lion Vol. 1.m4b',
			}
		},
	}
	const result = await listNasFilenameCandidates(
		home,
		'The Unselected Journals of Emma M. Lion: Vol. 1',
	)
	assert.equal(
		result.canonicalFilename,
		'The Unselected Journals of Emma M. Lion Vol. 1.m4b',
	)
	assert.deepEqual(result.candidates, [
		'The Unselected Journals of Emma M. Lion Vol. 1.m4b',
		'The Unselected Journals of Emma M. Lion- Vol. 1.m4b',
	])
	assert.equal(calls.length, 2)
})

test('listNasFilenameCandidates skips second call when no colon', async () => {
	let calls = 0
	const home = {
		audiobook_library_filename: async () => {
			calls++
			return { filename: 'Hatchet.m4b' }
		},
	}
	const result = await listNasFilenameCandidates(home, 'Hatchet')
	assert.deepEqual(result.candidates, ['Hatchet.m4b'])
	assert.equal(calls, 1)
})

test('resolveNasExists returns matched OpenAudible filename when canonical missing', async () => {
	const home = {
		audiobook_library_filename: async ({ title }: { title: string }) => {
			if (title.includes('- ')) {
				return {
					filename:
						'Spelling for Kids Grades 6-7- 600 English Words to Practice for the Spelling Bee.m4b',
				}
			}
			return {
				filename:
					'Spelling for Kids Grades 6-7 600 English Words to Practice for the Spelling Bee.m4b',
			}
		},
		audiobook_exists: async ({ filename }: { filename: string }) => ({
			exists: filename.includes('- '),
		}),
	}
	const resolved = await resolveNasExists(
		home,
		'Spelling for Kids Grades 6-7: 600 English Words to Practice for the Spelling Bee',
	)
	assert.equal(resolved.onNas, true)
	assert.equal(
		resolved.filename,
		'Spelling for Kids Grades 6-7- 600 English Words to Practice for the Spelling Bee.m4b',
	)
	assert.equal(
		resolved.canonicalFilename,
		'Spelling for Kids Grades 6-7 600 English Words to Practice for the Spelling Bee.m4b',
	)
})

test('resolveNasExists uses mediarss Title - hit when home probes miss', async () => {
	const home = {
		audiobook_library_filename: async () => ({ filename: 'Meltdown.m4b' }),
		audiobook_exists: async () => ({ exists: false }),
	}
	const resolved = await resolveNasExists(home, 'Meltdown', {
		findViaMediarss: async () =>
			'Meltdown - Why Our Systems Fail and What We Can Do About It.m4b',
	})
	assert.equal(resolved.onNas, true)
	assert.equal(resolved.matchSource, 'mediarss_title_dash')
	assert.equal(
		resolved.filename,
		'Meltdown - Why Our Systems Fail and What We Can Do About It.m4b',
	)
})

test('resolveNasExists still tries mediarss when home Unauthorized', async () => {
	const home = {
		audiobook_library_filename: async () => {
			throw new Error('Unauthorized')
		},
		audiobook_exists: async () => {
			throw new Error('Unauthorized')
		},
	}
	const resolved = await resolveNasExists(
		home,
		'Fablehaven, Book 2',
		{
			findViaMediarss: async () =>
				'Fablehaven, Book 2 - Rise of the Evening Star.m4b',
		},
	)
	assert.equal(resolved.onNas, true)
	assert.equal(resolved.matchSource, 'mediarss_title_dash')
	assert.equal(
		resolved.filename,
		'Fablehaven, Book 2 - Rise of the Evening Star.m4b',
	)
})