Something went wrong. Try again.
A free, open-source, self-hosted place to write a novel.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221import { beforeAll, describe, expect, it } from 'vitest';import { eq } from 'drizzle-orm';import { sql } from 'drizzle-orm';import { db, tables } from '#lib/server/db/index.ts';import { isDocEnvelope, prepareDocumentBody, sameDocument } from '#lib/server/document.ts';import { saveSceneBody } from '#lib/server/manuscript.ts';import { createNote, saveNoteBody } from '#lib/server/notes.ts';import { createProject } from '#lib/server/projects.ts';import { loadManuscript } from '#lib/server/manuscript.ts';import { restoreSnapshot } from '#lib/server/snapshots.ts';import { restoreItemVersion } from '#lib/server/item-history.ts';import { search } from '#lib/server/search.ts';
beforeAll(() => { db.delete(tables.project).run(); db.all(sql`delete from search_index`);});
describe('document envelope and body preparation', () => { it('accepts only top-level doc envelopes', () => { expect(isDocEnvelope({ type: 'doc' })).toBe(true); expect(isDocEnvelope({ type: 'doc', content: [] })).toBe(true); expect(isDocEnvelope(null)).toBe(false); expect(isDocEnvelope([])).toBe(false); expect(isDocEnvelope('doc')).toBe(false); expect(isDocEnvelope({ type: 'paragraph' })).toBe(false); expect(isDocEnvelope({ type: 'doc', content: 'bad' })).toBe(false); expect(isDocEnvelope({ type: 'doc', content: {} })).toBe(false); });
it('derives bodyText and wordCount strictly from the normalised document', () => { const prepared = prepareDocumentBody({ type: 'doc', content: [ { type: 'heading', attrs: { level: 2 }, content: [{ type: 'text', text: 'Saltmarsh' }] }, { type: 'paragraph', content: [ { type: 'text', text: 'Bright', marks: [{ type: 'bold' }, { type: 'unknown' }] }, { type: 'text', text: ' lanterns' }, { type: 'text', text: ' burned.', marks: [{ type: 'italic' }] } ] } ] });
expect(prepared.bodyText).toBe('Saltmarsh\n\nBright lanterns burned.'); expect(prepared.wordCount).toBe(4); const paragraph = prepared.body.content[1] as { content?: { marks?: { type: string }[] }[]; }; expect(paragraph.content?.[0].marks).toEqual([{ type: 'bold' }]); expect(paragraph.content?.[2].marks).toEqual([{ type: 'italic' }]);
const empty = prepareDocumentBody(null); expect(empty.bodyText).toBe(''); expect(empty.wordCount).toBe(0); });
it('treats formatting-only differences as distinct documents', () => { const plain = prepareDocumentBody({ type: 'doc', content: [{ type: 'paragraph', content: [{ type: 'text', text: 'Bright lanterns.' }] }] }); const bold = prepareDocumentBody({ type: 'doc', content: [ { type: 'paragraph', content: [{ type: 'text', text: 'Bright lanterns.', marks: [{ type: 'bold' }] }] } ] }); const linked = prepareDocumentBody({ type: 'doc', content: [ { type: 'paragraph', content: [ { type: 'text', text: 'Bright lanterns.', marks: [{ type: 'link', attrs: { href: 'https://example.test' } }] } ] } ] }); const heading = prepareDocumentBody({ type: 'doc', content: [ { type: 'heading', attrs: { level: 2 }, content: [{ type: 'text', text: 'Bright lanterns.' }] } ] }); const list = prepareDocumentBody({ type: 'doc', content: [ { type: 'bulletList', content: [ { type: 'listItem', content: [ { type: 'paragraph', content: [{ type: 'text', text: 'Bright lanterns.' }] } ] } ] } ] });
expect( sameDocument(plain, { bodyText: plain.bodyText, body: { content: [{ content: [{ text: 'Bright lanterns.', type: 'text' }], type: 'paragraph' }], type: 'doc' } }) ).toBe(true); expect(sameDocument(plain, bold)).toBe(false); expect(sameDocument(plain, linked)).toBe(false); expect(sameDocument(plain, heading)).toBe(false); expect(sameDocument(plain, list)).toBe(false); });});
describe('restoring snapshots and versions', () => { it('recomputes scene text, word count, and search index from snapshot.body', () => { const projectId = createProject('doc-restore-scene', 'Harbour'); const sceneId = loadManuscript(projectId).books[0].chapters[0].scenes[0].id; saveSceneBody( sceneId, { type: 'doc', content: [{ type: 'paragraph', content: [{ type: 'text', text: 'Current prose.' }] }] }, { baseRevision: 0 } );
const snapshotId = crypto.randomUUID(); db.insert(tables.sceneSnapshot) .values({ id: snapshotId, sceneId, body: { type: 'doc', content: [ { type: 'paragraph', content: [ { type: 'text', text: 'Lanterns over Saltmarsh.', marks: [{ type: 'bold' }] } ] } ] }, bodyText: 'Forged snapshot words', wordCount: 999, source: 'manual' }) .run();
expect(restoreSnapshot(projectId, sceneId, snapshotId)).toBe(true);
const row = db.select().from(tables.scene).where(eq(tables.scene.id, sceneId)).get()!; expect(row.bodyText).toBe('Lanterns over Saltmarsh.'); expect(row.wordCount).toBe(3); expect(search(projectId, 'Lanterns', 'owner').scenes.map((hit) => hit.id)).toEqual([sceneId]); expect(search(projectId, 'Forged', 'owner').scenes).toEqual([]); });
it('recomputes note bodyText and search index from version.body', () => { const projectId = createProject('doc-restore-note', 'Harbour'); const noteId = createNote(projectId, 'Skiff', null); saveNoteBody( noteId, { type: 'doc', content: [{ type: 'paragraph', content: [{ type: 'text', text: 'Later note.' }] }] }, { baseRevision: 0 }, 'Current summary' );
const versionId = crypto.randomUUID(); db.insert(tables.itemVersion) .values({ id: versionId, projectId, kind: 'note', itemId: noteId, title: 'Restored Skiff', summary: 'Restored summary', body: { type: 'doc', content: [ { type: 'paragraph', content: [{ type: 'text', text: 'Oars in the reedbed.' }] } ] }, bodyText: 'Stale mismatched summary words', source: 'edit' }) .run();
expect(restoreItemVersion(projectId, versionId).changed).toBe(true);
const row = db.select().from(tables.note).where(eq(tables.note.id, noteId)).get()!; expect(row.bodyText).toBe('Oars in the reedbed.'); expect(search(projectId, 'reedbed', 'owner').notes.map((hit) => hit.id)).toEqual([noteId]); expect(search(projectId, 'mismatched', 'owner').notes).toEqual([]); });});