Files
genoffice/apps/docs/tests/phased-content.test.ts
T
merrick-2002andGenOffice 69b4ce0560 Sync snapshot (2026-09-06) (#243)
- Docs: Word-style dark page, phased fast open for long documents, fonts embedded in the DOCX, spellcheck toggle, hyperlink editing, and ~80 Word fidelity fixes (CJK/JP metrics, header/footer, tables, anchored pictures)
- Sheets: hold formula recalculation while file data streams in, large-workbook scroll/load performance, combo charts, duplicate/unique conditional formatting by display text, RTL frozen-pane fix, workbooks whose ZIP entries carry a leading slash
- Slides: MTX-compressed embedded font support, CJK/Latin face selection and PowerPoint script fallbacks, theme colors kept on paragraph defaults and bullets, more reliable AI edits with a layout audit after each batch
- PDF: AI page ops, text block move/align, notes and markup editing, watermark and header/footer, form check boxes, metadata; the cloud Export as Word fallback is removed, conversion is always on-device
- AI: Gemini tool schema translation, OpenCode Zen/Go providers, markdown tables and code blocks in chat, current date in the system prompt
- Shell: Office-style ribbon collapse across all editors, native Windows ARM64 installer, BUILD_DIR override for packaging

Co-authored-by: GenOffice <merrick-2002@users.noreply.github.com>
2026-09-07 00:18:14 +08:00

169 lines
5.7 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import type { PmNode } from '../src/renderer/editor/convert'
import {
PHASE1_BLOCKS,
PHASE_CHUNK_BLOCKS,
PHASED_MIN_BLOCKS,
cancelPhasedContent,
isPhasedContentPending,
setContentPhased,
waitForFullContent,
type PhasedContentHost,
} from '../src/renderer/phased-content'
const docOf = (blocks: number): PmNode => ({
type: 'doc',
content: Array.from({ length: blocks }, (_, i) => ({
type: 'docParagraph',
attrs: { docxIndex: i },
})),
})
/** host that records events; appends mark dirty like the editor's onUpdate does */
function makeHost(destroyed = false) {
const state = {
mounted: [] as PmNode[],
events: [] as string[],
dirty: false,
loading: false,
}
const host: PhasedContentHost = {
setContent: (d) => {
state.mounted = [...(d.content ?? [])]
state.events.push(`set:${state.mounted.length}`)
},
appendNodes: (nodes) => {
state.mounted.push(...nodes)
state.dirty = true
state.events.push(`append:${nodes.length}`)
},
isDestroyed: () => destroyed,
resetHistory: () => state.events.push('resetHistory'),
setLoading: (v) => {
state.loading = v
state.events.push(`loading:${v}`)
},
getDirty: () => state.dirty,
setDirty: (v) => {
state.dirty = v
},
}
return { host, state }
}
/** scheduler the test drains manually, one chunk per drain() call */
function makeScheduler() {
const queue: Array<() => void> = []
return {
schedule: (cb: () => void) => queue.push(cb),
drain: () => queue.shift()?.(),
pending: () => queue.length,
}
}
describe('setContentPhased', () => {
it('mounts small documents in one pass without the loading flag', () => {
const { host, state } = makeHost()
const s = makeScheduler()
setContentPhased(host, docOf(PHASED_MIN_BLOCKS), s.schedule)
expect(state.events).toEqual([`set:${PHASED_MIN_BLOCKS}`])
expect(s.pending()).toBe(0)
})
it('streams a large document in chunks and lands the full content', async () => {
const blocks = PHASE1_BLOCKS + PHASE_CHUNK_BLOCKS * 2 + 7
const { host, state } = makeHost()
const s = makeScheduler()
setContentPhased(host, docOf(blocks), s.schedule)
expect(state.mounted.length).toBe(PHASE1_BLOCKS)
expect(state.loading).toBe(true)
while (s.pending() > 0) s.drain()
expect(state.mounted.length).toBe(blocks)
// every block landed exactly once, in order
expect(state.mounted.map((n) => n.attrs?.docxIndex)).toEqual(
Array.from({ length: blocks }, (_, i) => i),
)
expect(state.loading).toBe(false)
expect(state.events).toContain('resetHistory')
await expect(waitForFullContent()).resolves.toBeUndefined()
})
it('streaming is not an edit: the dirty flag survives every chunk', () => {
const { host, state } = makeHost()
const s = makeScheduler()
setContentPhased(host, docOf(PHASED_MIN_BLOCKS + 50), s.schedule)
while (s.pending() > 0) s.drain()
expect(state.dirty).toBe(false)
})
it('cancelPhasedContent drops the pending tail and releases the save gate', async () => {
const blocks = PHASE1_BLOCKS + PHASE_CHUNK_BLOCKS * 3
const { host, state } = makeHost()
const s = makeScheduler()
setContentPhased(host, docOf(blocks), s.schedule)
s.drain() // first chunk lands
const landed = state.mounted.length
cancelPhasedContent()
while (s.pending() > 0) s.drain()
expect(state.mounted.length).toBe(landed)
expect(state.loading).toBe(false)
await expect(waitForFullContent()).resolves.toBeUndefined()
})
it('a second phased mount cancels the first tail', () => {
const blocks = PHASE1_BLOCKS + PHASE_CHUNK_BLOCKS * 3
const first = makeHost()
const s1 = makeScheduler()
setContentPhased(first.host, docOf(blocks), s1.schedule)
const second = makeHost()
const s2 = makeScheduler()
setContentPhased(second.host, docOf(blocks), s2.schedule)
while (s1.pending() > 0) s1.drain()
while (s2.pending() > 0) s2.drain()
// the first document keeps only its phase-1 mount; the second is complete
expect(first.state.mounted.length).toBe(PHASE1_BLOCKS)
expect(second.state.mounted.length).toBe(blocks)
expect(first.state.loading).toBe(false)
expect(second.state.loading).toBe(false)
})
it('a rejected chunk falls back to the one-pass mount and releases the save gate', async () => {
const blocks = PHASE1_BLOCKS + PHASE_CHUNK_BLOCKS * 2
const { host, state } = makeHost()
const { appendNodes, setContent } = host
let appends = 0
host.appendNodes = (nodes) => {
if (++appends === 2) throw new Error('schema refused')
appendNodes(nodes)
}
host.setContent = (d) => {
setContent(d)
state.dirty = true
}
const s = makeScheduler()
setContentPhased(host, docOf(blocks), s.schedule)
state.dirty = false
while (s.pending() > 0) s.drain()
expect(state.mounted.length).toBe(blocks)
expect(state.events.at(-3)).toBe(`set:${blocks}`)
// the remount is not an edit either
expect(state.dirty).toBe(false)
expect(state.loading).toBe(false)
await expect(waitForFullContent()).resolves.toBeUndefined()
})
it('reports a pending tail only while chunks remain', () => {
const { host } = makeHost()
const s = makeScheduler()
setContentPhased(host, docOf(PHASED_MIN_BLOCKS), s.schedule)
expect(isPhasedContentPending()).toBe(false)
setContentPhased(host, docOf(PHASED_MIN_BLOCKS + 1), s.schedule)
expect(isPhasedContentPending()).toBe(true)
while (s.pending() > 0) s.drain()
expect(isPhasedContentPending()).toBe(false)
setContentPhased(host, docOf(PHASED_MIN_BLOCKS + 1), s.schedule)
cancelPhasedContent()
expect(isPhasedContentPending()).toBe(false)
})
})