import JSZip from 'jszip' import { describe, expect, it } from 'vitest' import { applySectionSettings, buildChartWorkbookXlsxBase64, latexToOmml, nextNoteId, ommlToLatex, parseDocx, patchChartWorkbookXlsxBase64, readSections, saveDocx, sectionSettingsFromXml, type SaveBlock, } from '../src/index' import { patchParagraphTexts } from '../src/text-patch' import { escapeXmlText } from '../src/xml-utils' import { buildDocx } from './helpers/build-docx' const P = (text: string) => `${text}` describe('saveDocx isUnchanged short-circuit', () => { it('a sectionStartType-only edit is not silently dropped', async () => { const bytes = await buildDocx({ bodyXml: P('a') + P('b') }) const parsed = await parseDocx(bytes) const visible: SaveBlock[] = parsed.blocks .filter((b) => !b.hidden) .map((b) => ({ kind: 'original', docxIndex: b.docxIndex! })) const saved = await saveDocx(parsed, visible, { sectionStartType: 'continuous' }) expect(saved).not.toBe(bytes) const sections = readSections(await parseDocx(saved)) expect(sections[0].startType).toBe('continuous') }) }) describe('applySectionSettings w:cols', () => { const SECT_PR = '' + '' + '' + '' + '' it('keeps explicit column widths when the count is unchanged', () => { const settings = sectionSettingsFromXml(SECT_PR) expect(settings.columns).toBe(3) const out = applySectionSettings(SECT_PR, settings) expect(out.match(/]/g)?.length).toBe(1) expect(out).toContain(' { const settings = sectionSettingsFromXml(SECT_PR) const out = applySectionSettings(SECT_PR, { ...settings, columns: 2 }) expect(out.match(/]/g)?.length).toBe(1) expect(out).toContain('w:num="2"') expect(out).not.toContain(' { const ENTRY = '' + 'outer ' + '' + 'inside box' + '' + 'tail' + 'second' + '' it('slices the outer paragraph past the embedded w:p and patches surgically', () => { const out = patchParagraphTexts(ENTRY, 'outer inside boxtail\nsecond edited') expect(out).not.toBeNull() // the textbox structure of the untouched paragraph survives byte-for-byte expect(out).toContain( 'inside box', ) expect(out).toContain('second edited') expect(out).toContain('') }) }) describe('escapeXmlText', () => { it('strips control characters that are illegal in XML 1.0 but keeps tab/newline', () => { expect(escapeXmlText('a\u0000b\u000Bc\td\ne')).toBe('abc\td\ne') expect(escapeXmlText('')).toBe('<a & b>') }) }) describe('nextNoteId', () => { it('starts at 1 for a document with no existing notes', () => { expect(nextNoteId([])).toBe('1') }) }) describe('ommlToLatex matrix environments', () => { it('recovers the named pmatrix environment instead of generic delimiters', () => { const omml = `${latexToOmml('\\begin{pmatrix} 1 & 0 \\\\ 0 & 1 \\end{pmatrix}')}` expect(ommlToLatex(omml)).toContain('\\begin{pmatrix}') }) }) describe('patchChartWorkbookXlsxBase64', () => { it('preserves unrelated workbook parts and updates Sheet1 in place', async () => { const base = await buildChartWorkbookXlsxBase64(['a', 'b'], [{ name: 'S1', values: [1, 2] }]) const zip = await JSZip.loadAsync(Buffer.from(base, 'base64')) zip.file('xl/styles.xml', '') zip.file('xl/worksheets/sheet2.xml', '') const withExtras = Buffer.from(await zip.generateAsync({ type: 'uint8array' })).toString( 'base64', ) const patched = await patchChartWorkbookXlsxBase64( withExtras, ['x', 'y'], [{ name: 'S1', values: [9, 8] }], ) expect(patched).not.toBeNull() const outZip = await JSZip.loadAsync(Buffer.from(patched!, 'base64')) expect(outZip.file('xl/styles.xml')).toBeTruthy() expect(outZip.file('xl/worksheets/sheet2.xml')).toBeTruthy() const sheet = await outZip.file('xl/worksheets/sheet1.xml')!.async('string') expect(sheet).toContain('9') expect(sheet).toContain('>x') }) })