Something went wrong. Try again.
A free, open-source, self-hosted place to write a novel.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351import { beforeEach, describe, expect, it } from 'vitest';import { eq } from 'drizzle-orm';import { strFromU8, unzipSync } from 'fflate';import { db, tables } from '#lib/server/db/index.ts';import { prepareDocx, readDocx } from './docx-import.ts';import { buildDocxExport, exportDocx, SUBMISSION_PRESET } from './docx-write.ts';import { buildManuscriptJson } from './export.ts';
const OWNER = 'test-owner-docx-export';let projectId: string;
function reset() { db.delete(tables.sceneNote).run(); db.delete(tables.plotCardNote).run(); db.delete(tables.plotCard).run(); db.delete(tables.storyline).run(); db.delete(tables.note).run(); db.delete(tables.noteType).run(); db.delete(tables.scene).run(); db.delete(tables.chapter).run(); db.delete(tables.part).run(); db.delete(tables.book).run(); db.delete(tables.projectMember).run(); db.delete(tables.project).run();
projectId = crypto.randomUUID(); db.insert(tables.project) .values({ id: projectId, ownerId: OWNER, title: 'Coastline Chronicles' }) .run();}
function seedTwoChapterProject() { const bookId = crypto.randomUUID(); db.insert(tables.book).values({ id: bookId, projectId, title: 'Book One', ordinal: 0 }).run();
const ch1Id = crypto.randomUUID(); db.insert(tables.chapter) .values({ id: ch1Id, bookId, title: 'The Lighthouse', ordinal: 0 }) .run();
const ch2Id = crypto.randomUUID(); db.insert(tables.chapter).values({ id: ch2Id, bookId, title: 'The Mudflats', ordinal: 1 }).run();
db.insert(tables.scene) .values({ id: crypto.randomUUID(), chapterId: ch1Id, title: 'First Keeper', body: { type: 'doc', content: [ { type: 'paragraph', content: [ { type: 'text', text: 'The light turned in the ' }, { type: 'text', text: 'fog', marks: [{ type: 'strong' }] }, { type: 'text', text: '.' } ] } ] }, bodyText: 'The light turned in the fog.', wordCount: 6, ordinal: 0 }) .run();
db.insert(tables.scene) .values({ id: crypto.randomUUID(), chapterId: ch1Id, title: 'Second Keeper', body: { type: 'doc', content: [ { type: 'paragraph', content: [{ type: 'text', text: 'He climbed the iron spiral stairs.' }] } ] }, bodyText: 'He climbed the iron spiral stairs.', wordCount: 6, ordinal: 1 }) .run();
db.insert(tables.scene) .values({ id: crypto.randomUUID(), chapterId: ch2Id, title: 'Low Tide', body: { type: 'doc', content: [ { type: 'paragraph', content: [ { type: 'text', text: 'The sea left ' }, { type: 'text', text: 'silver tracks', marks: [{ type: 'em' }] }, { type: 'text', text: ' behind.' } ] } ] }, bodyText: 'The sea left silver tracks behind.', wordCount: 6, ordinal: 0 }) .run();}
/** * One scene holding every block the editor can produce. A list and a quote keep * their text in nested blocks, so a writer that reads one level of children * loses the words without saying so. */function seedRichScene() { const bookId = crypto.randomUUID(); db.insert(tables.book).values({ id: bookId, projectId, title: 'Book One', ordinal: 0 }).run();
const chapterId = crypto.randomUUID(); db.insert(tables.chapter) .values({ id: chapterId, bookId, title: 'The Ledger', ordinal: 0 }) .run();
db.insert(tables.scene) .values({ id: crypto.randomUUID(), chapterId, title: 'Inventory', body: { type: 'doc', content: [ { type: 'paragraph', content: [{ type: 'text', text: 'The keeper counted what was left.' }] }, { type: 'bulletList', content: [ { type: 'listItem', content: [ { type: 'paragraph', content: [{ type: 'text', text: 'twelve lanterns' }] }, { type: 'bulletList', content: [ { type: 'listItem', content: [ { type: 'paragraph', content: [{ type: 'text', text: 'all of them chipped' }] } ] } ] } ] }, { type: 'listItem', content: [ { type: 'paragraph', content: [{ type: 'text', text: 'one lamp with no wick' }] } ] } ] }, { type: 'orderedList', content: [ { type: 'listItem', content: [ { type: 'paragraph', content: [{ type: 'text', text: 'close the shutters' }] } ] } ] }, { type: 'blockquote', content: [ { type: 'paragraph', content: [{ type: 'text', text: 'Nothing burns in this weather.' }] } ] }, { type: 'horizontalRule' }, { type: 'paragraph', content: [{ type: 'text', text: 'He shut the door anyway.' }] } ] }, bodyText: 'The keeper counted what was left. twelve lanterns all of them chipped one lamp with no wick close the shutters Nothing burns in this weather. He shut the door anyway.', wordCount: 30, ordinal: 0 }) .run();}
beforeEach(reset);
describe('DOCX export round trip', () => { it('exports a two-chapter project as one DOCX and recovers chapter and scene structure', () => { seedTwoChapterProject();
const docx = buildDocxExport(projectId)!; expect(docx.filename).toBe('coastline-chronicles.docx'); expect(docx.bytes.byteLength).toBeGreaterThan(0);
const doc = readDocx(docx.bytes); expect(doc.paragraphs.length).toBeGreaterThan(0);
const plan = prepareDocx(docx.bytes).plan; expect(plan.chapters).toHaveLength(2); expect(plan.chapters[0].title).toBe('The Lighthouse'); expect(plan.chapters[0].scenes).toHaveLength(2);
expect(plan.chapters[1].title).toBe('The Mudflats'); expect(plan.chapters[1].scenes).toHaveLength(1);
const text = doc.paragraphs.map((p) => p.text).join(' '); expect(text).toContain('The light turned in the fog.'); expect(text).toContain('He climbed the iron spiral stairs.'); expect(text).toContain('The sea left silver tracks behind.'); });
it('keeps the words inside a list, a quote and a rule', () => { seedRichScene();
const docx = buildDocxExport(projectId)!; const xml = strFromU8(unzipSync(docx.bytes)['word/document.xml']);
// A real Word list and a real quote, not a paragraph with a bullet typed in. expect(xml).toContain('<w:numPr>'); expect(xml).toContain('<w:pStyle w:val="Quote"/>'); expect(xml).toContain('<w:pStyle w:val="ListParagraph"/>');
const doc = readDocx(docx.bytes); const text = doc.paragraphs.map((p) => p.text);
expect(text).toContain('twelve lanterns'); expect(text).toContain('all of them chipped'); expect(text).toContain('one lamp with no wick'); expect(text).toContain('close the shutters'); expect(text).toContain('Nothing burns in this weather.'); expect(text).toContain('He shut the door anyway.');
expect(doc.counts.unknownStyles).toBe(0);
// Import flattens a list into paragraphs, so the writer is told what became of // the numbering. const plan = prepareDocx(docx.bytes).plan; const lists = plan.warnings.find((w) => w.code === 'lists'); expect(lists?.count).toBe(4); });
it('accepts preset overrides and applies page break per chapter', () => { seedTwoChapterProject(); const manuscript = buildManuscriptJson(projectId);
const customBytes = exportDocx(manuscript, { ...SUBMISSION_PRESET, sceneSeparator: '***', firstLineIndentTwips: 0 });
const plan = prepareDocx(customBytes).plan; expect(plan.chapters).toHaveLength(2); expect(plan.chapters[0].scenes).toHaveLength(2); });
/** * A saved body carries the editor's mark names, `bold` and `italic`. The * fixtures above use the Markdown names, so only this case proves the export * reads what the database holds. */ it('writes the mark names the editor stores', () => { const bookId = crypto.randomUUID(); db.insert(tables.book).values({ id: bookId, projectId, title: 'Book One', ordinal: 0 }).run();
const chapterId = crypto.randomUUID(); db.insert(tables.chapter).values({ id: chapterId, bookId, title: 'One', ordinal: 0 }).run();
db.insert(tables.scene) .values({ id: crypto.randomUUID(), chapterId, title: 'Scene', body: { type: 'doc', content: [ { type: 'paragraph', content: [ { type: 'text', text: 'bold', marks: [{ type: 'bold' }] }, { type: 'text', text: ' ' }, { type: 'text', text: 'italic', marks: [{ type: 'italic' }] } ] } ] }, bodyText: 'bold italic', wordCount: 2, ordinal: 0 }) .run();
const xml = strFromU8(unzipSync(buildDocxExport(projectId)!.bytes)['word/document.xml']);
expect(xml).toContain('<w:b/>'); expect(xml).toContain('<w:i/>'); });});
describe('the author on a submission', () => { it('prints the project author in the running header and the properties', () => { seedTwoChapterProject(); db.update(tables.project) .set({ author: 'Maren Holt' }) .where(eq(tables.project.id, projectId)) .run();
const parts = unzipSync(buildDocxExport(projectId)!.bytes);
expect(strFromU8(parts['word/header1.xml'])).toContain('Maren Holt / Coastline Chronicles /'); expect(strFromU8(parts['docProps/core.xml'])).toContain('<dc:creator>Maren Holt</dc:creator>'); });
it('falls back to Author when the project names none', () => { seedTwoChapterProject();
const header = strFromU8(unzipSync(buildDocxExport(projectId)!.bytes)['word/header1.xml']);
expect(header).toContain('Author / Coastline Chronicles /'); });});