import { describe, it, expect } from 'vitest'; import { cleanText, splitSentences, guessLanguage } from '../../src/text'; import { isSilent, isSilentWav, speechVariants } from '../../src/text'; describe('cleanText', () => { it('joins hyphenated line breaks and soft hyphens', () => { expect(cleanText('extra-\nordinary and won­derful')).toBe('extraordinary and wonderful'); }); it('collapses spaces and keeps paragraph breaks', () => { expect(cleanText('a b\n\n\n\nc  d')).toBe('a b\n\nc d'); }); }); describe('splitSentences', () => { it('splits on sentence ends and keeps a dialogue tag with its quote', () => { expect(splitSentences('Hello there. "Who is it?" she asked! Fine…')).toEqual(['Hello there.', '"Who is it?" she asked!', 'Fine…']); }); it('does not split after common abbreviations', () => { expect(splitSentences('Mr. Smith met Dr. Jones. They talked.')).toEqual(['Mr. Smith met Dr. Jones.', 'They talked.']); }); it('cuts very long sentences into chunks under the limit, at commas when possible', () => { const long = Array.from({ length: 30 }, (_, i) => `clause number ${i}`).join(', ') + '.'; const parts = splitSentences(long); expect(parts.length).toBeGreaterThan(2); for (const p of parts) expect(p.length).toBeLessThanOrEqual(171); expect(parts.join(' ').replace(/\s+/g, ' ')).toBe(long); }); it('treats paragraphs separately even without punctuation', () => { expect(splitSentences('Chapter One\n\nIt began')).toEqual(['Chapter One', 'It began']); }); it('drops chunks with no letters or digits', () => { expect(splitSentences('* * *\n\nReal text.')).toEqual(['Real text.']); }); it('handles a word longer than the limit without looping', () => { const parts = splitSentences('x'.repeat(500)); expect(parts.join('')).toBe('x'.repeat(500)); }); }); describe('guessLanguage', () => { it('detects English, Portuguese and French', () => { expect(guessLanguage('The cat sat on the mat and it was happy with the sun that shone for you and she was there with him '.repeat(3))).toBe('en'); expect(guessLanguage('Ela disse que não sabia como fazer isso, mas você foi com os amigos para uma festa de que gostavam '.repeat(3))).toBe('pt'); expect(guessLanguage('Le chat est dans les jardins avec une souris qui ne veut pas sortir pour nous dans la maison '.repeat(3))).toBe('fr'); }); }); describe('splitSentences edge cases (review findings)', () => { it('keeps decimals, a.m., U.S., initials and domains inside one sentence', () => { expect(splitSentences('It cost 3.5 dollars at 10 a.m. in the U.S. today. J. R. R. Tolkien wrote it. Visit example.com now.')).toEqual([ 'It cost 3.5 dollars at 10 a.m. in the U.S. today.', 'J. R. R. Tolkien wrote it.', 'Visit example.com now.', ]); }); it('splits when the next sentence starts with a quote, digit or dash', () => { expect(splitSentences('He left. "Wait!" she said. 3 days passed. — Then nothing.')).toEqual(['He left.', '"Wait!" she said.', '3 days passed.', '— Then nothing.']); }); it('splits CJK sentences without spaces after punctuation only when spaced', () => { expect(splitSentences('Привет. Как дела?')).toEqual(['Привет.', 'Как дела?']); }); }); describe('guessLanguage scripts', () => { it('detects non-Latin scripts instead of calling them English', () => { expect(guessLanguage('Это был тихий вечер, и никто не знал, что случится завтра в маленьком городе.')).toBe('ru'); expect(guessLanguage('今日はとても良い天気ですね。私は公園に行きました。')).toBe('ja'); expect(guessLanguage('这是一个关于小城市的故事,没有人知道明天会发生什么。')).toBe('zh'); }); it('returns und when there is no signal', () => { expect(guessLanguage('1234 5678 !!!')).toBe('und'); }); }); describe('CJK sentences', () => { it('ends sentences at 。!? without spaces', () => { expect(splitSentences('今日は晴れです。公園に行きました!楽しかった?')).toEqual(['今日は晴れです。', '公園に行きました!', '楽しかった?']); }); }); describe('splitter (re-review)', () => { it('still ends sentences after "no." and a single capital that is not an initial', () => { expect(splitSentences('She said no. He left.')).toEqual(['She said no.', 'He left.']); expect(splitSentences('It was Plan B. Then rain.')).toEqual(['It was Plan B.', 'Then rain.']); expect(splitSentences('See No. 5 here. J. R. R. Tolkien wrote it.')).toEqual(['See No. 5 here.', 'J. R. R. Tolkien wrote it.']); }); }); describe('silent-output retry (Kokoro fp16 returns silence for some inputs)', () => { it('offers rewordings that drop quote marks and calm exclamation marks, never the original', () => { expect(speechVariants('Poor Alice!')).toEqual(['Poor Alice.']); const v = speechVariants('to hear the Rabbit say to itself, "Oh dear!'); expect(v[0]).toBe('to hear the Rabbit say to itself, Oh dear!'); expect(v).toContain('to hear the Rabbit say to itself, Oh dear.'); expect(speechVariants('Plain sentence.')).toEqual([]); }); it('detects silence and broken (NaN) audio, not quiet speech', () => { expect(isSilent(new Float32Array(2400))).toBe(true); expect(isSilent(Float32Array.from([0, NaN, 0.5]))).toBe(true); expect(isSilent(Float32Array.from([0, 0.02, -0.3]))).toBe(false); }); }); describe('isSilentWav (audio cached before the retry existed)', () => { const wav = (samples: number[]) => { const b = new ArrayBuffer(44 + samples.length * 2); const dv = new DataView(b); samples.forEach((v, i) => dv.setInt16(44 + i * 2, v, true)); return b; }; it('finds an all-zero file silent and speech not', () => { expect(isSilentWav(wav(new Array(2400).fill(0)))).toBe(true); expect(isSilentWav(wav([0, 0, 5000, -3000]))).toBe(false); expect(isSilentWav(new ArrayBuffer(44))).toBe(true); }); });