| 1 | // @vitest-environment happy-dom |
| 2 | |
| 3 | import { afterEach, describe, expect, it, vi } from 'vitest' |
| 4 | |
| 5 | vi.mock('electron', () => ({ |
| 6 | app: { |
| 7 | getPath: () => '/tmp' |
| 8 | }, |
| 9 | BrowserWindow: class BrowserWindow {}, |
| 10 | ipcMain: {}, |
| 11 | session: {} |
| 12 | })) |
| 13 | |
| 14 | vi.mock('@electron-toolkit/utils', () => ({ |
| 15 | is: { |
| 16 | dev: false |
| 17 | } |
| 18 | })) |
| 19 | |
| 20 | import { buildBasePageStyleTag, buildFitScript } from '../../../src/main/presentation/html/page-writer-core' |
| 21 | import { requireSlideSizePreset } from '../../../src/shared/slide-size' |
| 22 | |
| 23 | describe('generated page font floors', () => { |
| 24 | const slideSize = requireSlideSizePreset('wide-16-9') |
| 25 | |
| 26 | afterEach(() => { |
| 27 | document.head.innerHTML = '' |
| 28 | document.body.innerHTML = '' |
| 29 | vi.restoreAllMocks() |
| 30 | }) |
| 31 | |
| 32 | it('enforces semantic font floors in the generated page DOM', () => { |
| 33 | document.head.innerHTML = `${buildBasePageStyleTag(slideSize)}<style>.text-sm { font-size: 14px; }</style>` |
| 34 | document.body.innerHTML = ` |
| 35 | <main class="ppt-page-root" data-ppt-guard-root="1"> |
| 36 | <div class="ppt-page-fit-scope"> |
| 37 | <div class="ppt-page-content"> |
| 38 | <section data-ppt-readable-fonts="1"> |
| 39 | <p id="body" class="text-sm">正文</p> |
| 40 | <h3 id="heading" style="font-size: 16px">标题</h3> |
| 41 | <div id="block-title" data-block-id="title" style="font-size: 16px">区块标题</div> |
| 42 | <footer id="footer" style="font-size: 11px">来源</footer> |
| 43 | </section> |
| 44 | </div> |
| 45 | </div> |
| 46 | </main> |
| 47 | ` |
| 48 | vi.spyOn(window, 'requestAnimationFrame').mockImplementation((callback) => { |
| 49 | callback(0) |
| 50 | return 1 |
| 51 | }) |
| 52 | |
| 53 | const script = buildFitScript(slideSize).replace(/^<script[^>]*>/, '').replace(/<\/script>$/, '') |
| 54 | Function(script)() |
| 55 | window.dispatchEvent(new Event('load')) |
| 56 | |
| 57 | expect(getComputedStyle(document.querySelector('#body')!).fontSize).toBe('18px') |
| 58 | expect(getComputedStyle(document.querySelector('#heading')!).fontSize).toBe('24px') |
| 59 | expect(getComputedStyle(document.querySelector('#block-title')!).fontSize).toBe('24px') |
| 60 | expect(getComputedStyle(document.querySelector('#footer')!).fontSize).toBe('12px') |
| 61 | }) |
| 62 | }) |
| 63 |