/** * Component: Narrator Extraction Utility Tests * Documentation: documentation/integrations/audible.md */ import { describe, expect, it } from 'vitest'; import * as cheerio from 'cheerio'; import { extractAllNarrators } from '@/lib/utils/extract-narrator'; function load(html: string) { const $ = cheerio.load(`
${html}
`); return { $, $el: $('#item') }; } describe('extractAllNarrators', () => { it('returns the single narrator name when only one searchNarrator link is present', () => { const { $, $el } = load( `Andy Serkis`, ); expect(extractAllNarrators($, $el)).toBe('Andy Serkis'); }); it('joins multiple narrator names from separate searchNarrator links', () => { const { $, $el } = load(` Kristin Atherton, Roy McMillan, Clare Corbett, Tom Bateman, Patience Tomlinson, Shaheen Khan `); expect(extractAllNarrators($, $el)).toBe( 'Kristin Atherton, Roy McMillan, Clare Corbett, Tom Bateman, Patience Tomlinson, Shaheen Khan', ); }); it('preserves document order (downstream sorts before comparing, but order should be stable)', () => { const { $, $el } = load(` Zelda Alice Mallory `); expect(extractAllNarrators($, $el)).toBe('Zelda, Alice, Mallory'); }); it('falls back to .narratorLabel text when no searchNarrator links exist', () => { const { $, $el } = load( `Narrated by: Single Narrator`, ); expect(extractAllNarrators($, $el)).toBe('Narrated by: Single Narrator'); }); it('prefers searchNarrator links over .narratorLabel when both are present', () => { const { $, $el } = load(` Narrated by: ONLY ONE First Second `); expect(extractAllNarrators($, $el)).toBe('First, Second'); }); it('returns empty string when neither links nor .narratorLabel exist', () => { const { $, $el } = load(`some other content`); expect(extractAllNarrators($, $el)).toBe(''); }); it('skips empty link text and joins only non-empty names', () => { const { $, $el } = load(` Bob Diana `); expect(extractAllNarrators($, $el)).toBe('Bob, Diana'); }); it('trims whitespace from each captured name', () => { const { $, $el } = load(` Alice Bob `); expect(extractAllNarrators($, $el)).toBe('Alice, Bob'); }); it('falls back to .narratorLabel when all searchNarrator links are empty', () => { const { $, $el } = load(` Fallback Narrator `); expect(extractAllNarrators($, $el)).toBe('Fallback Narrator'); }); });