import Database from 'better-sqlite3';
import { readFileSync } from 'node:fs';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { parseTheLandBook, THE_LAND_BOOK_URLS, THE_LAND_ORIGIN } from './the-land-adapter.js';
import { processSource, seedCatalog } from './pipeline.js';
import { hash } from './queue.js';
import { robotsAllowed, sourceUrl } from './sources.js';
import { ReviewError, type Document, type SeedSeries } from './types.js';
const AT = '2026-09-19T05:30:00.000Z';
const seed: SeedSeries = { id: 'the-land', title: 'The Land', author: 'Aleron Kong', authorAliases: ['Aleron Kong'],
aliases: ['Chaos Seeds'], genres: ['litrpg'], priority: 1,
sources: THE_LAND_BOOK_URLS.map(url => ({ url, adapter: 'the-land-book' })) };
const titles = ['Founding', 'Forging', 'Alliances', 'Catacombs', 'Swarm', 'Raiders', 'Predators', 'Monsters'].map(title => `The Land: ${title}`);
const ordinals = ['debut', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', '8th', 'ninth'];
// Synthetic descriptions and representative Squarespace structure. No copied
// author/publisher/reviewer prose or downloaded HTML is committed as a fixture.
const prose = 'A traveler and a cartographer explore a valley whose roads change each night. They train a village watch, bargain for missing maps, and try to protect their new home while a rival expedition searches the surrounding mountains.';
const ending = 'Together they must decide which promises to keep before the winter roads close.';
const block = (body: string, extraClass = '') => `
${body}
`;
const statement = (number: number) => number === 1 ? 'The debut novel of the Chaos Seeds saga.'
: number === 8 ? 'From a fictional announcement by the Author, Aleron Kong, comes the 8th novel of the Chaos Seeds saga.'
: `The ${ordinals[number - 1]} vivid installment of Aleron Kong's, Chaos Seeds series.`;
function page(number: number): string {
const url = THE_LAND_BOOK_URLS[number - 1], title = titles[number - 1];
const otherLinks = THE_LAND_BOOK_URLS.map((link, index) => `
`).reverse().join('');
const navigation = ``;
const description = number === 8
// The live Monsters page puts its ordinal after promotional text and the
// substantive synopsis in a separate following block.
? block(`
Newsletter promotion unrelated to this work. Published 2020-01-01.
')}
${description}
`;
}
const parse = (number: number, html = page(number), selected = seed) => parseTheLandBook(html, selected, THE_LAND_BOOK_URLS[number - 1]);
afterEach(() => { vi.unstubAllGlobals(); });
describe('bounded Land author bibliography', () => {
it.each([1, 2, 3, 4, 5, 6, 7, 8])('reads explicit work %i without inferring its title or number from the slug or order', number => {
const book = parse(number);
expect(book).toMatchObject({ title: titles[number - 1], series: 'Chaos Seeds', number, author: 'Aleron Kong',
format: 'unknown', publicationStatus: 'unknown', coverUrl: null, releaseDate: null, narrator: null,
audioReleaseDate: null, audioRuntimeMinutes: null, links: [] });
expect(book.description).toContain(prose);
expect(book.description).toContain(ending);
expect(book.description).not.toMatch(/Newsletter|Footer/);
});
it('keeps the complete later Monsters synopsis, paragraph boundaries, and inline text', () => {
const book = parse(8, page(8).replace('cartographer', 'cartographer'));
expect(book.description).toContain(prose);
expect(book.description).toContain(`\n\n${ending}`);
expect(book.description).not.toMatch(/|Ebook 5/);
});
it('uses only the saga-specific debut statement for Founding', () => {
expect(parse(1).number).toBe(1);
expect(() => parse(1, page(1).replace(statement(1), 'Aleron Kong wrote a debut novel before starting other projects.'))).toThrow(ReviewError);
expect(() => parse(1, page(1).replace(statement(1), 'The debut novel of another saga.'))).toThrow(ReviewError);
expect(() => parse(1, page(1).replace(statement(1), 'The debut novel of another saga, not the Chaos Seeds saga.'))).toThrow(ReviewError);
expect(() => parse(8, page(8).replace('8th novel of the Chaos Seeds saga', '8th novel of another saga, not the Chaos Seeds saga'))).toThrow(ReviewError);
});
it.each([5, 7, 8])('rejects a stale self-label for work %i instead of trusting the misleading slug', number => {
const stale = number === 5 ? titles[3] : number === 7 ? titles[5] : titles[0];
const html = page(number).replaceAll(`>${titles[number - 1]}`, `>${stale}`);
expect(() => parse(number, html)).toThrow(ReviewError);
});
it('rejects conflicting active desktop/mobile labels and another active page', () => {
expect(() => parse(5, page(5).replace(`>${titles[4]}`, `>${titles[3]}`))).toThrow(ReviewError);
const active = `