import { describe, expect, it } from 'vitest'; import { grandGameSeriesBooks, parseGrandGameBook, parseNovaRoma } from './author-series.js'; import { ReviewError, type SeedSeries } from './types.js'; const grand: SeedSeries = { id: 'the-grand-game', title: 'The Grand Game', author: 'Tom Elliot', authorAliases: ['Tom Elliot'], aliases: [], genres: ['litrpg'], priority: 1, sources: [] }; const nova: SeedSeries = { id: 'portal-to-nova-roma', title: 'Portal to Nova Roma', author: 'J.R. Mathews', authorAliases: ['J.R. Mathews'], aliases: [], genres: ['litrpg'], priority: 1, sources: [] }; const grandCard = (number: number, title = `Grand Volume ${number}`, slug = `volume-${number}`) => `
Book ${number}

${title}

A carousel review should not become the source synopsis.

`; const grandBook = (title = 'The Grand Game') => `

${title}

A player enters a dangerous world.

He must find allies.
Enemies are watching.

The rules keep changing.

A difficult choice awaits.

The stakes rise.

Another challenge begins.

His journey continues.

Subscribe for new posts.

Reader: the most brilliant book ever written.

This later widget is not the book synopsis.

AMAZONAUDIBLE AUDIBLE (UK)AMAZON (UK)

Paperback published April 1, 2021. Narrated by A General Footer Credit.

`; const novaTitles = ['Portal to Nova Roma', 'Portal to Nova Roma, Venice', 'Portal to Nova Roma, The Rhine', 'Portal to Nova Roma, Paris', 'Portal to Nova Roma, Empire']; const audioAsins = ['B0BCH43V6V', 'B0BJSF2FBB', 'B0BW4WDCWK', 'B0H7SH5DYS']; const novaCard = (number: number, title = novaTitles[number - 1]) => `

Portal to Nova Roma #${number}

Book ${number} of 5: ${title}

Buy Now
`; const novaOmnibus = `

Portal to Nova Roma Omnibus

Compilation of Books 1-3

Buy Now
`; const novaAudio = (number: number, href = audioAsins[number - 1] ? `https://www.audible.com/pd/Book-${number}-Audiobook/${audioAsins[number - 1]}?ref=tracking#fragment` : '') => `

Portal to Nova Roma #${number} Audiobook

Book ${number} of 5: ${novaTitles[number - 1]}

${href ? 'Buy Now' : 'Available September 2026'}
`; const novaAudioOmnibus = `

Portal to Nova Roma Omnibus Audiobook

Books 1-3: Portal to Nova Roma

Buy Now
`; function novaPage(cards = [novaCard(1), novaCard(2), novaCard(3), novaOmnibus, novaCard(4), novaCard(5)].join(''), audio = [novaAudio(1), novaAudio(2), novaAudio(3), novaAudioOmnibus, novaAudio(4), novaAudio(5)].join('')): string { return `

Portal to Nova Roma

*** SERIES NOW COMPLETE Spring 2026 ***

An intelligence seeks another world.

A threatened city becomes his home.

He must learn to survive.

A reader calls it the best ending ever.

Cover art by An Artist https://example.com/artist

Narrated by A Narrator https://example.com/narrator

${cards}

Shop Audiobooks #1-3:

${audio}

Highlighted Reviews

A reviewer praises all five books and their ending.

- A Reader

`; } describe('The Grand Game author pages', () => { it('enumerates only the eight observed taxonomy cards without inventing book nine', () => { const html = '
' + Array.from({ length: 8 }, (_, index) => grandCard(index + 1)).reverse().join('') + 'Book 9 news' + '

Other Book 9

'; const results = grandGameSeriesBooks(html, grand); expect(results.map(result => result.book.number)).toEqual([1, 2, 3, 4, 5, 6, 7, 8]); expect(results[0]).toMatchObject({ url: 'https://tomlitrpg.com/book/volume-1/', book: { title: 'Grand Volume 1', series: grand.title, author: grand.author, format: 'unknown', description: '', releaseDate: null, audioReleaseDate: null, narrator: null, links: [] } }); }); it('uses explicit taxonomy rather than article order or numbers in a title', () => { const result = grandGameSeriesBooks(grandCard(4, 'The Ninth Challenge', 'challenge') + grandCard(2), grand); expect(result.map(entry => [entry.book.number, entry.book.title])).toEqual([[2, 'Grand Volume 2'], [4, 'The Ninth Challenge']]); }); it('excludes collections without assigning their range to a numbered work', () => { const results = grandGameSeriesBooks(grandCard(1) + grandCard(2, 'The Grand Game Omnibus Books 1–3'), grand); expect(results.map(result => result.book.number)).toEqual([1]); }); it('requires consistent explicit taxonomy and an author-owned book URL', () => { expect(() => grandGameSeriesBooks(grandCard(1).replace('>Book 1<', '>Book 9<'), grand)).toThrow(ReviewError); expect(() => grandGameSeriesBooks(grandCard(1).replace('book-tag-book-1', 'book-tag-book-9'), grand)).toThrow(ReviewError); expect(() => grandGameSeriesBooks(grandCard(1).replace('href="/book/volume-1/"', 'href="https://other.example/book/one/"'), grand)).toThrow(ReviewError); expect(() => grandGameSeriesBooks('

Book 1

', grand)).toThrow(ReviewError); }); it('deduplicates repeated cards but flags conflicting numbers, titles, or book URLs', () => { expect(grandGameSeriesBooks(grandCard(1) + grandCard(1), grand)).toHaveLength(1); expect(() => grandGameSeriesBooks(grandCard(1) + grandCard(1, 'Different Book'), grand)).toThrow(ReviewError); expect(() => grandGameSeriesBooks(grandCard(1) + grandCard(2, 'Second', 'volume-1'), grand)).toThrow(ReviewError); }); it('reads the first synopsis block direct paragraphs and excludes subscriptions and reader feedback', () => { const book = parseGrandGameBook(grandBook(), grand, 1); expect(book.description).toBe('A player enters a dangerous world.\n\nHe must find allies.\nEnemies are watching.\n\nThe rules keep changing.\n\nA difficult choice awaits.\n\nThe stakes rise.\n\nAnother challenge begins.\n\nHis journey continues.'); expect(book.description).not.toMatch(/Subscribe|brilliant|widget/); expect(book.coverUrl).toBe('https://tomlitrpg.com/covers/one.jpg'); }); it('returns only the explicitly US-labelled observed short-link lead without audio facts', () => { const book = parseGrandGameBook(grandBook(), grand, 1); expect(book.links).toEqual([{ url: 'https://amzn.to/audioLead', format: 'audiobook' }]); expect(book).toMatchObject({ format: 'unknown', releaseDate: null, publicationStatus: 'unknown', narrator: null, audioReleaseDate: null, audioRuntimeMinutes: null }); }); it('takes volume identity from the retained parent argument and refuses a missing synopsis or invalid number', () => { expect(parseGrandGameBook(grandBook('A Scion’s Duty'), grand, 6).number).toBe(6); expect(() => parseGrandGameBook(grandBook(), grand, NaN)).toThrow(ReviewError); expect(() => parseGrandGameBook(grandBook(), grand, 0)).toThrow(ReviewError); expect(() => parseGrandGameBook('

The Grand Game

Only feedback

', grand, 1)).toThrow(ReviewError); }); it('rejects the wrong selected series, author, canonical page, and omnibus book', () => { expect(() => grandGameSeriesBooks(grandCard(1), { ...grand, title: 'Another Series' })).toThrow(ReviewError); expect(() => grandGameSeriesBooks(grandCard(1), { ...grand, author: 'Another Author' })).toThrow(ReviewError); expect(() => parseGrandGameBook(grandBook(), { ...grand, authorAliases: ['Another Author'] }, 1)).toThrow(ReviewError); expect(() => parseGrandGameBook(grandBook().replace('https://tomlitrpg.com/book/the-grand-game/', 'https://other.example/book/the-grand-game/'), grand, 1)).toThrow(ReviewError); expect(() => parseGrandGameBook(grandBook('The Grand Game Omnibus'), grand, 1)).toThrow(ReviewError); }); }); describe('Portal to Nova Roma author bibliography', () => { it('returns five individually numbered ebook works, excluding the interleaved omnibus', () => { const books = parseNovaRoma(novaPage(), nova); expect(books.map(book => [book.number, book.title])).toEqual(novaTitles.map((title, index) => [index + 1, title])); expect(books.every(book => book.series === nova.title && book.author === nova.author && book.format === 'ebook')).toBe(true); expect(books[0].coverUrl).toBe('https://images.squarespace-cdn.com/covers/1.jpg'); expect(books[0].links.find(link => link.format === 'ebook')).toEqual({ url: 'https://www.amazon.com/dp/B000000001', format: 'ebook', asin: 'B000000001' }); }); it('attributes only the first-book premise and excludes completion banners, credits, reviews, and footers', () => { const books = parseNovaRoma(novaPage(), nova); expect(books[0].description).toBe('An intelligence seeks another world.\n\nA threatened city becomes his home.\n\nHe must learn to survive.'); expect(books.slice(1).map(book => book.description)).toEqual(['', '', '', '']); expect(books.map(book => book.description).join(' ')).not.toMatch(/complete|artist|narrator|reviewer|reader|subscribe/i); }); it('associates US audio product IDs with explicit cards across the omnibus boundary and strips tracking', () => { const books = parseNovaRoma(novaPage(), nova); expect(books.map(book => book.links.filter(link => link.format === 'audiobook').map(link => link.asin))).toEqual(audioAsins.map(asin => [asin]).concat([[]])); expect(books[2].links.find(link => link.format === 'audiobook')).toEqual({ url: 'https://www.audible.com/pd/Book-3-Audiobook/B0BW4WDCWK', format: 'audiobook', asin: 'B0BW4WDCWK' }); expect(books.flatMap(book => book.links).some(link => link.asin === 'B0GTBJS911')).toBe(false); }); it('leaves the fifth audio unresolved when only a month and an empty buy link are present', () => { const fifth = parseNovaRoma(novaPage(), nova)[4]; expect(fifth.links.filter(link => link.format === 'audiobook')).toEqual([]); expect(fifth).toMatchObject({ format: 'ebook', releaseDate: null, publicationStatus: 'unknown', narrator: null, audioReleaseDate: null, audioRuntimeMinutes: null }); }); it('does not derive narrator, release dates, or verified audio from ebook metadata or a completed-series notice', () => { const html = novaPage().replace('', ''); const books = parseNovaRoma(html, nova); expect(books.every(book => book.releaseDate === null && book.audioReleaseDate === null && book.narrator === null && book.publicationStatus === 'unknown')).toBe(true); }); it('never fills omitted volumes from an “of 5” count', () => { const books = parseNovaRoma(novaPage(novaCard(1) + novaCard(3), novaAudio(1) + novaAudio(3)), nova); expect(books.map(book => book.number)).toEqual([1, 3]); }); it('ignores unrelated series cards, UK products, and ASIN-like URLs on unrelated hosts', () => { const other = novaCard(1).replaceAll('Portal to Nova Roma', 'Different Series'); const badLinks = novaAudio(2, 'https://www.audible.co.uk/pd/Title/B0BJSF2FBB') + novaAudio(3, 'https://www.audible.com.evil.example/pd/Title/B0BW4WDCWK'); const books = parseNovaRoma(novaPage(novaCard(1) + novaCard(2) + novaCard(3) + other, novaAudio(1) + badLinks), nova); expect(books.map(book => book.number)).toEqual([1, 2, 3]); expect(books.slice(1).flatMap(book => book.links).filter(link => link.format === 'audiobook')).toEqual([]); }); it('clears the prior audio-card association for an unrelated text block and for a new section', () => { const foreign = '

Another author’s recommendation

Buy Now
'; const html = novaPage(novaCard(1), novaAudio(1) + foreign).replace('', '
Buy Now
'); expect(parseNovaRoma(html, nova)[0].links.filter(link => link.format === 'audiobook').map(link => link.asin)).toEqual([audioAsins[0]]); }); it('rejects mismatched ebook numbering and contradictory audio card titles instead of guessing', () => { expect(() => parseNovaRoma(novaPage(novaCard(1).replace('Book 1 of 5:', 'Book 2 of 5:'), ''), nova)).toThrow(ReviewError); expect(() => parseNovaRoma(novaPage(novaCard(1), novaAudio(1).replace('Book 1 of 5:', 'Book 2 of 5:')), nova)).toThrow(ReviewError); expect(() => parseNovaRoma(novaPage(novaCard(1), novaAudio(1).replace('Book 1 of 5: Portal to Nova Roma', 'Book 1 of 5: Portal to Nova Roma, Wrong Subtitle')), nova)).toThrow(ReviewError); }); it('rejects wrong seeds, foreign canonical pages, and a footer link posing as the series introduction', () => { expect(() => parseNovaRoma(novaPage(), { ...nova, title: 'Jake’s Magical Market' })).toThrow(ReviewError); expect(() => parseNovaRoma(novaPage(), { ...nova, author: 'Another Author' })).toThrow(ReviewError); expect(() => parseNovaRoma(novaPage(), { ...nova, authorAliases: ['Another Author'] })).toThrow(ReviewError); expect(() => parseNovaRoma(novaPage().replace('https://jrmathewsauthor.com/portal-to-nova-roma', 'https://jrmathewsauthor.com/jakes-magical-market'), nova)).toThrow(ReviewError); expect(() => parseNovaRoma(novaPage().replace('

Portal to Nova Roma

', '

Other Series

'), nova)).toThrow(ReviewError); }); });