返回 oh-my-ppt
page-fragment-normalizer.test.ts
根目录 / tests / unit / tools / page-fragment-normalizer.test.ts
1 import { describe, expect, it } from 'vitest'
2
3 import { normalizeCreativePageFragment } from '../../../src/main/presentation/html/page-fragment-normalizer'
4
5 describe('normalizeCreativePageFragment block ids', () => {
6 it('adds stable block ids to nested inline text runs', () => {
7 const html = normalizeCreativePageFragment(`
8 <p>Normal <span class="accent"><strong>red text</strong></span> normal</p>
9 `)
10
11 expect(html).toContain('<p data-block-id="text">')
12 expect(html).toMatch(/<span class="accent" data-block-id="text-\d+">/)
13 expect(html).toMatch(/<strong data-block-id="text-\d+">red text<\/strong>/)
14 })
15
16 it('marks generated fragments for semantic font-floor enforcement', () => {
17 const html = normalizeCreativePageFragment('<div><h2>Title</h2><p>Body</p></div>')
18
19 expect(html).toContain('data-ppt-readable-fonts="1"')
20 })
21
22 it('reallocates duplicate model-authored data-block-id values so persisted-page validation passes', () => {
23 const html = normalizeCreativePageFragment(`
24 <section data-block-id="select-arcsin1-5kQfdkFj">First</section>
25 <section data-block-id="select-arcsin1-5kQfdkFj">Second</section>
26 <section data-block-id="select-arcsin1-5kQfdkFj">Third</section>
27 `)
28
29 const ids = Array.from(html.matchAll(/data-block-id="([^"]+)"/g)).map((m) => m[1])
30 // The content main always gets id="content"; the three duplicate sections must each
31 // end up with a distinct id built from the model's chosen base.
32 expect(ids.filter((id) => id.startsWith('select-arcsin1-5')).length).toBe(3)
33 expect(new Set(ids).size).toBe(ids.length)
34 })
35
36 it('strips model-authored block ids without removing semantic page structure when requested', () => {
37 const html = normalizeCreativePageFragment(
38 '<section data-block-id="outer"><h2 data-block-id="title">Title</h2><p>Body</p></section>',
39 { blockIdMode: 'strip' }
40 )
41
42 expect(html).not.toContain('data-block-id=')
43 expect(html).toContain('data-page-scaffold="1"')
44 expect(html).toContain('data-role="content"')
45 expect(html).toContain('<h2 data-role="title">Title</h2>')
46 })
47 })
48
48 lines TYPESCRIPT