earshelf / source /tests /unit /parse.test.ts
WilburDev's picture
Deploy 2026-11-19T12:25Z
4a58e48 verified
Raw History Blame Contribute Delete
1.99 kB
import { describe, it, expect } from 'vitest';
import { parseFile, decodeText } from '../../src/parse';
describe('TXT chapters', () => {
it('ignores wrapped lines that start with "book" or "part" and keeps their words', async () => {
const txt = `CHAPTER I\n\nAlice saw that the\nbook was lying open on the table, and she\nread it.\n\nCHAPTER II\n\nThe\npart of the garden near the wall was\nquiet.\n`;
const b = await parseFile(new File([txt], 'alice.txt', { type: 'text/plain' }));
expect(b.chapters.map((c) => c.title)).toEqual(['CHAPTER I', 'CHAPTER II']);
expect(b.chapters[0].text).toContain('book was lying open');
expect(b.chapters[1].text).toContain('part of the garden');
});
});
describe('decodeText', () => {
it('reads Windows-1252 text (old Portuguese/French files) without mojibake', () => {
const bytes = new Uint8Array([0x4e, 0xe3, 0x6f, 0x20, 0x65, 0x73, 0x74, 0xe1, 0x20, 0x6c, 0xe0]); // "Não está là"
expect(decodeText(bytes)).toBe('Não está là');
});
it('honours BOMs and declared XML encodings', () => {
expect(decodeText(new Uint8Array([0xff, 0xfe, 0x48, 0x00, 0x69, 0x00]))).toBe('Hi');
const xml = new TextEncoder().encode('<?xml version="1.0" encoding="iso-8859-1"?><p>');
const withLatin1 = new Uint8Array([...xml, 0xe9, 0x74, 0xe9]); // "été"
expect(decodeText(withLatin1, true)).toContain('<p>été');
});
});
describe('TXT chapters (re-review)', () => {
it('finds chapters in Windows line endings and Gutenberg "CHAPTER I.\\nTitle" layout', async () => {
const txt = 'CHAPTER I.\r\nDown the Rabbit-Hole\r\n\r\nAlice was tired.\r\n\r\nCHAPTER II.\r\nThe Pool of Tears\r\n\r\nCuriouser and curiouser.\r\n';
const b = await parseFile(new File([txt], 'alice.txt', { type: 'text/plain' }));
expect(b.chapters.map((c) => c.title)).toEqual(['CHAPTER I. · Down the Rabbit-Hole', 'CHAPTER II. · The Pool of Tears']);
expect(b.chapters[1].text).toContain('Curiouser');
});
});