import { describe, it, expect, beforeEach, afterEach } from 'vitest' import { existsSync, mkdtempSync, rmSync } from 'fs' import { join } from 'path' import { tmpdir } from 'os' import { BookRegistryService } from '../../src/main/services/book-registry' import { closeAllBookDbs } from '../../src/main/db/connection' describe('BookRegistryService', () => { let dir: string let svc: BookRegistryService beforeEach(() => { dir = mkdtempSync(join(tmpdir(), 'bilin-books-')) svc = new BookRegistryService(dir) }) afterEach(() => { closeAllBookDbs() rmSync(dir, { recursive: true, force: true }) }) it('creates book with registry entry, sqlite file, and default volume', () => { const meta = svc.create({ name: '测试书', category: '玄幻', createSampleChapter: true }) expect(meta.name).toBe('测试书') expect(svc.list()).toHaveLength(1) expect(existsSync(join(dir, 'books', `${meta.id}.sqlite`))).toBe(true) const opened = svc.open(meta.id) expect(opened.volumes).toHaveLength(1) expect(opened.volumes[0].name).toBe('第一卷') expect(opened.chapters.length).toBeGreaterThanOrEqual(1) expect(meta.lastChapterId).toBeTruthy() }) })