50 lines
1.7 KiB
TypeScript
50 lines
1.7 KiB
TypeScript
import { BadRequestException } from '@nestjs/common';
|
|
import { describe, expect, it } from 'vitest';
|
|
import { NovelParserService } from './novel-parser.service';
|
|
|
|
describe('NovelParserService', () => {
|
|
const service = new NovelParserService();
|
|
|
|
it('extracts plain text buffers', async () => {
|
|
const result = await service.extractText(
|
|
'local://novels/test.txt',
|
|
'text/plain',
|
|
Buffer.from('第1章 开始\n这是正文。')
|
|
);
|
|
|
|
expect(result.extractor).toBe('plain_text');
|
|
expect(result.text).toContain('这是正文');
|
|
});
|
|
|
|
it('cleans text and splits chapters by headings', () => {
|
|
const parsed = service.parseText(`
|
|
第1章 重生
|
|
她在暴雨里醒来,决定重新夺回属于自己的事业。
|
|
https://example.com
|
|
|
|
第二章 反击
|
|
会议室里,所有人都等着看她出错,她却拿出了完整方案。
|
|
`);
|
|
|
|
expect(parsed.chapter_count).toBe(2);
|
|
expect(parsed.parse_report.strategy).toBe('heading');
|
|
expect(parsed.parse_report.removed_line_count).toBe(1);
|
|
expect(parsed.chapters[0].title).toBe('第1章 重生');
|
|
expect(parsed.chapters[1].content).toContain('完整方案');
|
|
});
|
|
|
|
it('falls back to chunk splitting when headings are missing', () => {
|
|
const parsed = service.parseText(
|
|
'她醒来时,窗外正在下雨。她意识到命运已经重新开始,于是把所有证据重新整理,准备迎接第一场反击。'
|
|
);
|
|
|
|
expect(parsed.chapter_count).toBe(1);
|
|
expect(parsed.parse_report.strategy).toBe('word_chunk');
|
|
expect(parsed.parse_report.warnings[0]).toContain('按字数切分');
|
|
});
|
|
|
|
it('rejects text that is too short', () => {
|
|
expect(() => service.parseText('太短')).toThrow(BadRequestException);
|
|
});
|
|
});
|