返回 oh-my-ppt
page-writer-chart-height.test.ts
根目录 / tests / unit / tools / page-writer-chart-height.test.ts
1 import { describe, expect, it, vi } from 'vitest'
2
3 vi.mock('electron', () => ({
4 app: {
5 getPath: () => '/tmp',
6 },
7 BrowserWindow: class BrowserWindow {},
8 ipcMain: {},
9 session: {},
10 }))
11
12 vi.mock('@electron-toolkit/utils', () => ({
13 is: {
14 dev: false,
15 },
16 }))
17
18 import { preprocessPageHtml } from '../../../src/main/presentation/html/page-writer-core'
19
20 describe('preprocessPageHtml chart height stabilization', () => {
21 it('uses the dedicated chart height comment marker when adding a missing frame height', () => {
22 const html = preprocessPageHtml(`
23 <section>
24 <!-- height calc @ppt-chart-height=560: chart slot = 560; chart height = hero/main = 560 -->
25 <div class="relative w-full overflow-hidden">
26 <canvas id="chart"></canvas>
27 </div>
28 </section>
29 `)
30
31 expect(html).toContain('ppt-chart-frame')
32 expect(html).toContain('h-[560px]')
33 expect(html).not.toContain('h-[240px]')
34 })
35
36 it('uses a whole-page marker when the page has exactly one chart', () => {
37 const html = preprocessPageHtml(`
38 <section>
39 <!-- height calc @ppt-chart-height=400: single-chart page, chart height = 400 -->
40 <div class="grid grid-cols-[1fr_280px]">
41 <div class="card">
42 <h3>关键指标变化幅度对比</h3>
43 <div class="relative w-full overflow-hidden">
44 <canvas id="chart"></canvas>
45 </div>
46 </div>
47 <aside>Insight</aside>
48 </div>
49 </section>
50 `)
51
52 expect(html).toContain('ppt-chart-frame')
53 expect(html).toContain('h-[400px]')
54 expect(html).not.toContain('h-[240px]')
55 })
56
57 it('does not use one whole-page marker for multiple charts', () => {
58 const html = preprocessPageHtml(`
59 <section>
60 <!-- height calc @ppt-chart-height=400: ambiguous multi-chart page -->
61 <header>Two charts below</header>
62 <div><canvas id="chart-a"></canvas></div>
63 <div><canvas id="chart-b"></canvas></div>
64 </section>
65 `)
66
67 expect(html).toContain('h-[240px]')
68 expect(html).not.toContain('h-[400px]')
69 })
70
71 it('ignores unmarked natural-language chart height comments', () => {
72 const html = preprocessPageHtml(`
73 <section>
74 <!-- height calc: chart slot = 560; chart height = hero/main = 560 -->
75 <div class="relative w-full overflow-hidden">
76 <canvas id="chart"></canvas>
77 </div>
78 </section>
79 `)
80
81 expect(html).toContain('ppt-chart-frame')
82 expect(html).toContain('h-[240px]')
83 expect(html).not.toContain('h-[560px]')
84 })
85 })
86
86 lines TYPESCRIPT