` never
// opened the container: the splitter cut at every blank line and marked
// the fragments memoizable — the container's body escaped the div and
// the broken render got cached.
const blocks = splitStreamingBlocks('
\n\nbody\n\n
\n\nafter')
expect(blocks).toHaveLength(2)
expect(blocks[0].text).toContain('body')
expect(blocks[0].text).toContain('
')
expect(blocks.every((b) => !b.memoizable)).toBe(true)
})
it('releases the latch when a container closes MID-LINE', () => {
// A close tag not at column 0..3 (`some text `) never
// decremented, so `htmlDepth` latched >0 and the whole rest of the
// message collapsed into ONE non-memoizable unit for the rest of the
// stream.
const blocks = splitStreamingBlocks(
'')
expect(blocks[0].memoizable).toBe(false)
})
it('caps the latch so an unclosed container cannot disable cutting forever', () => {
const paras = Array.from({ length: 300 }, (_, i) => `para ${i}`).join('\n\n')
const blocks = splitStreamingBlocks(`\n\n${paras}`)
// Past the cap the splitter resumes cutting…
expect(blocks.length).toBeGreaterThan(1)
// …but nothing inside the still-open container is ever cached.
expect(blocks.every((b) => !b.memoizable)).toBe(true)
})
it('reports each unit’s document-wide startLine', () => {
const blocks = splitStreamingBlocks('one\n\ntwo\n\nthree')
expect(blocks.map((b) => b.startLine)).toEqual([1, 3, 5])
})
})
describe('completeStreamingTail', () => {
it('closes an unterminated fence', () => {
expect(completeStreamingTail('text\n\n```js\nconst x =')).toMatch(/```$/)
})
it('leaves balanced fences alone', () => {
const md = '```js\ncode\n```'
expect(completeStreamingTail(md)).toBe(md)
})
it('does NOT touch stray emphasis/link openers (no false-positive close)', () => {
for (const md of ['2 * 3 is six', 'a lone _ under', 'an open [ bracket', 'tick ` alone']) {
expect(completeStreamingTail(md)).toBe(md)
}
})
it('does not append a bogus fence when ``` and ~~~ both appear (closed block)', () => {
const md = '```\n~~~\n```'
expect(completeStreamingTail(md)).toBe(md)
})
it('does not append a bogus fence to a ```-block containing a ~~~ line', () => {
const md = '```text\nline\n~~~\n\nstill in code\n```\n\nafter'
expect(completeStreamingTail(md)).toBe(md)
})
it('closes an unterminated ~~~ fence with ~~~ (the RECORDED opener)', () => {
expect(completeStreamingTail('~~~py\nx =')).toBe('~~~py\nx =\n~~~')
})
it('closes with a run at least as long as the opener', () => {
expect(completeStreamingTail('````\nstuff')).toBe('````\nstuff\n````')
})
})
describe('MarkdownEngine streaming vs completion equivalence', () => {
const textOf = (html: string) =>
html
.replace(/