返回 oh-my-ppt
pptx-compat.test.ts
根目录 / tests / unit / html-pptx / pptx-compat.test.ts
1 import { describe, expect, it } from 'vitest'
2 import { readFileSync } from 'node:fs'
3 import { pathToFileURL } from 'node:url'
4 import os from 'os'
5 import path from 'path'
6 import { unzipSync } from 'fflate'
7
8 // 回归背景(两处缺陷都会让 PowerPoint 拒绝文件,已通过真机自动开合定位):
9 // 1. html2pptx 0.0.10 在 16:9 幻灯片上输出 <p:sldSz type="wide">,"wide" 不是
10 // ECMA-376 ST_SlideSizeType 的合法枚举值 → PowerPoint 弹「发现内容问题…修复」
11 // 对话框(嵌入字体与否均触发)。修复(html2pptx >= 0.2.0 库内完成):输出合法值
12 // "screen16x9"。
13 // 2. app.xml 的 <AppVersion>00.09:0000</AppVersion> 含冒号,不符合 ECMA-376 的
14 // XX.YYYY 数字格式。仅修 sldSz 后 PowerPoint 会直接报「无法读取」。
15 // 修复(html2pptx >= 0.2.0 库内完成):"00.0000"。
16 // 本测试锁住这两点。
17
18 const html2pptxNode = (await import(
19 pathToFileURL(path.resolve('node_modules/@arcsin1/html2pptx/dist/node.js')).href
20 )) as { writeHtmlToPptx: (outputPath: string, document: unknown) => void }
21
22 // ECMA-376 ST_SlideSizeType 合法枚举(html2pptx 可能输出的子集)
23 const VALID_SLIDE_SIZE_TYPES = new Set(['screen16x9', 'screen4x3', 'custom', 'screen16x10'])
24
25 describe('exported pptx opens without PowerPoint repair dialog', () => {
26 it('writes a valid sldSz type (never the invalid "wide")', async () => {
27 const outPath = path.join(os.tmpdir(), `oh-my-ppt-sldsz-${Date.now()}.pptx`)
28 html2pptxNode.writeHtmlToPptx(outPath, {
29 title: 'sldSz 回归',
30 slides: [
31 {
32 texts: [
33 { text: '兼容性', x: 1, y: 1, w: 8, h: 1, fontSize: 24, fontFace: 'Arial' }
34 ]
35 }
36 ]
37 })
38
39 const zip = unzipSync(readFileSync(outPath))
40 const presentation = Buffer.from(zip['ppt/presentation.xml']).toString('utf-8')
41 const sldSzMatch = presentation.match(/<p:sldSz\b[^>]*>/)
42 expect(sldSzMatch).not.toBeNull()
43 const sldSzTag = sldSzMatch![0]
44 expect(sldSzTag).not.toContain('type="wide"')
45 const typeMatch = sldSzTag.match(/type="([^"]+)"/)
46 if (typeMatch) {
47 expect(VALID_SLIDE_SIZE_TYPES.has(typeMatch[1])).toBe(true)
48 }
49 // 16:9 默认尺寸下必须带合法 type(修复前此处是非法的 "wide")
50 expect(sldSzTag).toContain('cx="12192000"')
51 expect(sldSzTag).toContain('cy="6858000"')
52 expect(typeMatch).not.toBeNull()
53 expect(typeMatch![1]).toBe('screen16x9')
54
55 // app.xml 的 AppVersion 必须是 XX.YYYY 纯数字格式(修复前是含冒号的 "00.09:0000")
56 const appXml = Buffer.from(zip['docProps/app.xml']).toString('utf-8')
57 const appVersion = appXml.match(/<AppVersion>([^<]+)<\/AppVersion>/)?.[1]
58 expect(appVersion).toBeDefined()
59 expect(appVersion).toMatch(/^\d{2}\.\d{4}$/)
60 })
61 })
62
62 lines TYPESCRIPT