返回 oh-my-ppt
chart-height.test.ts
根目录 / tests / unit / tools / chart-height.test.ts
1 import { describe, expect, it } from 'vitest'
2 import fs from 'fs'
3 import path from 'path'
4 import {
5 CHART_FRAME_HEIGHT_COMMENT_MARKER,
6 CHART_FRAME_HEIGHT_MAX,
7 CHART_FRAME_HEIGHT_MIN,
8 extractChartHeightFromComment,
9 normalizeChartHeight,
10 parseChartHeightClass
11 } from '../../../src/main/presentation/html/chart-height'
12
13 const readSource = (relativePath: string): string =>
14 fs.readFileSync(path.join(process.cwd(), relativePath), 'utf-8')
15
16 describe('chart-height shared helper — pure functions', () => {
17 it('parses Tailwind h-[Npx] classes and rejects non-height / zero classes', () => {
18 expect(parseChartHeightClass('h-[400px]')).toBe(400)
19 expect(parseChartHeightClass('h-[400.5px]')).toBe(400.5)
20 // variant prefix must be stripped by the caller, not here
21 expect(parseChartHeightClass('md:h-[400px]')).toBeNull()
22 expect(parseChartHeightClass('h-full')).toBeNull()
23 expect(parseChartHeightClass('flex-1')).toBeNull()
24 expect(parseChartHeightClass('h-[0px]')).toBeNull()
25 expect(parseChartHeightClass('relative')).toBeNull()
26 })
27
28 it('clamps heights into the valid chart-frame range', () => {
29 expect(normalizeChartHeight(400)).toBe(400)
30 expect(normalizeChartHeight('560')).toBe(560)
31 expect(normalizeChartHeight(CHART_FRAME_HEIGHT_MIN)).toBe(CHART_FRAME_HEIGHT_MIN)
32 expect(normalizeChartHeight(CHART_FRAME_HEIGHT_MAX)).toBe(CHART_FRAME_HEIGHT_MAX)
33 expect(normalizeChartHeight(CHART_FRAME_HEIGHT_MIN - 1)).toBeNull()
34 expect(normalizeChartHeight(CHART_FRAME_HEIGHT_MAX + 1)).toBeNull()
35 expect(normalizeChartHeight('not-a-number')).toBeNull()
36 })
37
38 it('reads the intended height from a @ppt-chart-height marker comment', () => {
39 expect(
40 extractChartHeightFromComment(
41 `height calc ${CHART_FRAME_HEIGHT_COMMENT_MARKER}=560: chart height = hero/main = 560`
42 )
43 ).toBe(560)
44 // out-of-range marker values are dropped (cannot be trusted as a height)
45 expect(extractChartHeightFromComment(`${CHART_FRAME_HEIGHT_COMMENT_MARKER}=1200`)).toBeNull()
46 // natural-language "chart height = 560" without the marker is ignored
47 expect(extractChartHeightFromComment('chart height = 560')).toBeNull()
48 })
49 })
50
51 describe('chart-height helpers are shared, not duplicated', () => {
52 const pageWriter = readSource('src/main/presentation/html/page-writer-core.ts')
53 const htmlUtils = readSource('src/main/presentation/html/html-utils.ts')
54
55 it('both tool modules import the shared helpers', () => {
56 expect(pageWriter).toContain("from './chart-height'")
57 expect(htmlUtils).toContain("from './chart-height'")
58 })
59
60 it('the duplicated local definitions were removed', () => {
61 // The comment-marker parsing + clamping now live in chart-height.ts only;
62 // page-writer keeps just a thin wrapper that turns the number into h-[Npx].
63 expect(pageWriter).not.toContain('function normalizeChartFrameHeight')
64 expect(pageWriter).not.toContain('function extractChartHeightClassFromComment')
65 expect(htmlUtils).not.toContain('const normalizeChartFrameHeight')
66 expect(htmlUtils).not.toContain('const extractChartHeightFromComment')
67 expect(htmlUtils).not.toContain('CHART_HEIGHT_CLASS_RE')
68 })
69
70 it('the drifted h-[Npx] regexes are gone — both files use parseChartHeightClass', () => {
71 // page-writer previously used a case-sensitive, 0px-excluding regex while
72 // html-utils used a case-insensitive one; they had drifted. The 0px
73 // negative-lookahead fragment is distinctive of the old page-writer regex.
74 expect(pageWriter).not.toContain('(?!0+(?:')
75 expect(pageWriter).toContain('parseChartHeightClass')
76 expect(htmlUtils).toContain('parseChartHeightClass')
77 })
78 })
79
79 lines TYPESCRIPT