import { describe, it, expect } from 'vitest';
import { zipSync, strToU8 } from 'fflate';
import { importOdt } from '../../src/lib/import/odt';
// Two masters naming each other through style:next-style-name are LibreOffice's Left
// Page / Right Page: one mirrored setup whose left half is the even-page variant, not a
// title page handing over to a running one.
const NS =
'xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" ' +
'xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" ' +
'xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" ' +
'xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"';
function odt(chained: boolean): Uint8Array {
const layout = (name: string, usage: string, left: string, right: string) =>
``;
const next = (n: string) => (chained ? ` style:next-style-name="${n}"` : '');
const styles = `
${layout('pmR', 'right', '3.6cm', '2.7cm')}${layout('pmL', 'left', '2.7cm', '3.6cm')}
odd head
even head
`;
const content = `
first
second
`;
return zipSync({ 'content.xml': strToU8(content), 'styles.xml': strToU8(styles) });
}
const zoneText = (doc: unknown): string =>
JSON.stringify(doc).match(/"text":"([^"]*)"/)?.[1] ?? '';
describe('a left/right master pair', () => {
const section = importOdt(odt(true)).hfSections?.[1];
it('is one mirrored page setup, stored as the right (odd) page', () => {
expect(section?.margins).toEqual({ top: 2, bottom: 2, left: 3.6, right: 2.7, mirrored: true });
expect(section?.marginsFirst).toBeNull();
});
it('makes the left master the even-page variant', () => {
expect(section?.differentOddEven).toBe(true);
expect(section?.differentFirstPage).toBe(false);
expect(zoneText(section?.header)).toBe('odd head');
expect(zoneText(section?.headerEven)).toBe('even head');
});
it('leaves an unchained pair alone', () => {
const plain = importOdt(odt(false)).hfSections?.[1];
expect(plain?.differentOddEven).toBe(false);
expect(zoneText(plain?.header)).toBe('odd head');
});
});