import { describe, it, expect, beforeEach, afterEach } from 'vitest' import { DatabaseSync } from 'node:sqlite' import { migrate } from '../../src/main/db/migrate' import { VolumeRepository } from '../../src/main/db/repositories/volume.repo' import { ChapterRepository } from '../../src/main/db/repositories/chapter.repo' import { wordFreqService } from '../../src/main/services/wordfreq.service' import type { SqliteDb } from '../../src/main/db/types' describe('WordFreqService', () => { let db: SqliteDb beforeEach(() => { db = new DatabaseSync(':memory:') migrate(db) const vol = new VolumeRepository(db).create('卷一', 0) new ChapterRepository(db).create( vol.id, '第一章', 0, '

林远走进青云山。The hero walked slowly.

' ) }) afterEach(() => { db.close() }) it('counts CJK characters and latin words', () => { const result = wordFreqService.analyze(db, { kind: 'book' }) expect(result.words.length).toBeGreaterThan(0) const tokens = result.words.map((w) => w.token) expect(tokens).toContain('林') expect(tokens).toContain('the') }) it('analyzes single chapter scope', () => { const ch = (db.prepare('SELECT id FROM chapters LIMIT 1').get() as { id: string }).id const result = wordFreqService.analyze(db, { kind: 'chapter', chapterId: ch }) expect(result.words.length).toBeGreaterThan(0) }) })