Files
edentext/tests/unit/derive-filename.test.ts
T
Steffen Becker 066737463d perf: the ODT exporter and both importers load on first use
The heading constants move to styles/headings.ts and the filename logic to
storage/documentName.ts, which were all the editor needed of the exporter
at startup. Main chunk 2.1 MB → 1.37 MB (gzip 615 KB → 411 KB).
2026-09-04 19:30:12 +02:00

38 lines
1.5 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { deriveFilename } from '../../src/lib/storage/documentName';
const doc = (...content: any[]) => ({ type: 'doc', content }) as any;
const heading = (text: string) => ({ type: 'heading', attrs: { level: 1 }, content: [{ type: 'text', text }] });
const para = (text: string) => ({ type: 'paragraph', content: [{ type: 'text', text }] });
describe('deriveFilename', () => {
it('derives the name from the first non-empty heading', () => {
expect(deriveFilename(doc(heading('Invoice Report 2026')))).toBe('Invoice-Report-2026.odt');
});
it('skips leading paragraphs and uses the first heading', () => {
expect(deriveFilename(doc(para('intro'), heading('Annual Summary')))).toBe('Annual-Summary.odt');
});
it('strips disallowed characters and collapses whitespace', () => {
expect(deriveFilename(doc(heading('Hä?llo / World!')))).toBe('Hllo-World.odt');
});
it('truncates to 50 characters before sanitizing', () => {
const long = 'a'.repeat(80);
expect(deriveFilename(doc(heading(long)))).toBe(`${'a'.repeat(50)}.odt`);
});
it('falls back to document.odt when there is no heading', () => {
expect(deriveFilename(doc(para('just a paragraph')))).toBe('document.odt');
});
it('falls back to document.odt when the heading sanitizes to empty', () => {
expect(deriveFilename(doc(heading('???')))).toBe('document.odt');
});
it('falls back to document.odt for an empty document', () => {
expect(deriveFilename(doc())).toBe('document.odt');
});
});