Files
genoffice/apps/docs/tests/toc-headings.test.ts
T
merrick-2002andGenOffice 8f523289d6 Sync snapshot (2026-08-04) (#19)
- Docs: paragraph writing direction (LTR/RTL) toggle with auto-detection
- Docs: hardened file lifecycle, docx fidelity, and editor performance
- PDF: Export as Word via cloud conversion, non-destructive Save As, and an in-progress export dialog
- Slides: treat full-page background shapes as the page background
- Sheets: faster recalculation with a session-resident engine; better xlsx compatibility (builtin number formats, auto-fit row heights, sheet-name resolution)
- AI: surface stream and gateway errors instead of finishing empty turns; parse non-SSE JSON gateway replies; stall watchdogs
- Fixes from alpha feedback batches 3 and 4 (TOC levels, Windows menus, honest AI applies, login flow diagnostics, credits-exhausted notice)
- CI: sheets save round-trip E2E and xlsx compatibility corpus gate
- Start the 0.5 release train (v0.5.0/v0.5.1)

Co-authored-by: GenOffice <merrick-2002@users.noreply.github.com>
2026-08-04 21:26:02 +08:00

67 lines
2.3 KiB
TypeScript

import { afterEach, describe, expect, it } from 'vitest'
import { Editor } from '@tiptap/core'
import { generateTocFieldXml, parseDocx } from '@genoffice/docx-engine'
import { buildDocx } from '../../../packages/docx-engine/tests/helpers/build-docx'
import { editorExtensions } from '../src/renderer/editor/extensions'
import { blocksToPmDoc } from '../src/renderer/editor/convert'
import { collectHeadings } from '../src/renderer/editor/headings'
const editors = new Set<Editor>()
afterEach(() => {
for (const editor of editors) editor.destroy()
editors.clear()
})
const p = (pPr: string, text: string) =>
`<w:p><w:pPr>${pPr}</w:pPr><w:r><w:t>${text}</w:t></w:r></w:p>`
/** H1/H2/H3 via built-in styles, direct outlineLvl and a basedOn custom style */
async function openMixedHeadingsDoc(): Promise<Editor> {
const bytes = await buildDocx({
extraStylesXml:
'<w:style w:type="paragraph" w:styleId="MySub"><w:name w:val="My Sub"/><w:basedOn w:val="Heading2"/></w:style>',
bodyXml: [
p('<w:pStyle w:val="Heading1"/>', 'Chapter 1'),
p('', 'body text'),
p('<w:pStyle w:val="Heading2"/>', 'Section 1.1'),
p('<w:outlineLvl w:val="2"/>', 'Topic 1.1.1'),
p('<w:pStyle w:val="MySub"/>', 'Section 1.2'),
].join(''),
})
const parsed = await parseDocx(bytes)
const editor = new Editor({
element: document.createElement('div'),
extensions: editorExtensions,
content: blocksToPmDoc(parsed.blocks) as never,
})
editors.add(editor)
return editor
}
describe('TOC heading collection', () => {
it('collects H1-H3 headings with levels in document order', async () => {
const editor = await openMixedHeadingsDoc()
const headings = collectHeadings(editor.state.doc)
expect(headings.map((h) => [h.text, h.level])).toEqual([
['Chapter 1', 1],
['Section 1.1', 2],
['Topic 1.1.1', 3],
['Section 1.2', 2],
])
})
it('generates a level-indented TOC field from the collected headings', async () => {
const editor = await openMixedHeadingsDoc()
const entries = collectHeadings(editor.state.doc).map(({ level, text }) => ({ level, text }))
const frags = generateTocFieldXml(entries)
expect(frags[0]).toContain(' TOC \\o "1-3" ')
expect(frags.map((f) => /w:pStyle w:val="(TOC\d)"/.exec(f)?.[1])).toEqual([
'TOC1',
'TOC2',
'TOC3',
'TOC2',
])
})
})