import assert from "node:assert/strict" import { readFileSync, writeFileSync, mkdirSync } from "node:fs" import vm from "node:vm" import { webcrypto } from "node:crypto" import { inflateRawSync } from 'node:zlib' import { createServer } from "vite" import { editorialFixture } from './fixtures/aiPptEditorial.mjs' import { typographyFixture } from './fixtures/aiPptTypography.mjs' import { slideDesignFixture } from './fixtures/aiPptSlideDesign.mjs' function readPptxXml(bytes, name) { const end = bytes.lastIndexOf(Buffer.from([0x50, 0x4b, 0x05, 0x06])) assert(end >= 0, 'PPTX 必须有 ZIP 中央目录') let offset = bytes.readUInt32LE(end + 16) while (bytes.readUInt32LE(offset) === 0x02014b50) { const size = bytes.readUInt32LE(offset + 20), nameSize = bytes.readUInt16LE(offset + 28) const path = bytes.subarray(offset + 46, offset + 46 + nameSize).toString() if (path === name) { const local = bytes.readUInt32LE(offset + 42) const start = local + 30 + bytes.readUInt16LE(local + 26) + bytes.readUInt16LE(local + 28) const data = bytes.subarray(start, start + size) return (bytes.readUInt16LE(offset + 10) === 8 ? inflateRawSync(data) : data).toString() } offset += 46 + nameSize + bytes.readUInt16LE(offset + 30) + bytes.readUInt16LE(offset + 32) } throw new Error(`PPTX 缺少 ${name}`) } const vite = await createServer({ server: { middlewareMode: true }, appType: "custom", logLevel: "error" }) try { const { prepareMediaForExport } = await vite.ssrLoadModule("/src/utils/aiPptExport.ts") // 加载生产导出器,在测试副本暴露其转换函数;不改写或替换生产转换逻辑。 const source = readFileSync("public/pptworker/pptJson.js", "utf8") .replace('r((r.s = "9a7e"))', 'globalThis.bundleRequire = r') .replace('Sr = function (e, t) {', 'Sr = globalThis.renderPptForTest = function (e, t) {') const context = vm.createContext({ console, setTimeout, clearTimeout, setInterval, clearInterval, TextEncoder, TextDecoder, Uint8Array, ArrayBuffer, Buffer, Blob, crypto: webcrypto, fetch: () => { throw new Error("离线导出不得请求网络") } }) context.self = context vm.runInContext(source, context) context.bundleRequire("f5f5") assert.equal(typeof context.renderPptForTest, "function") const { aiCoursewareToSlides: renderDesign } = await vite.ssrLoadModule('/src/utils/aiCoursewareToSlides.ts') const designed = renderDesign(slideDesignFixture()) const designedPpt = await context.renderPptForTest({ width: 1920, height: 1080, slides: designed.slides, theme: designed.theme, name: '模型页面协议测试' }, true) const designedBytes = Buffer.from(await designedPpt.write({ outputType: 'arraybuffer' })) mkdirSync('artifacts/ai-ppt-tests', { recursive: true }) writeFileSync('artifacts/ai-ppt-tests/model-slide-design.pptx', designedBytes) const designedXml = readPptxXml(designedBytes, 'ppt/slides/slide2.xml') assert(designedXml.includes('FCE4CE') && designedXml.includes('聆听与表达'), '模型色块与文字必须作为原生可编辑元素导出') assert(designedXml.includes('a:xfrm'), '模型布局坐标必须进入PPTX') const png = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=" const wav = Buffer.alloc(44 + 8000 * 2) wav.write("RIFF"); wav.writeUInt32LE(wav.length - 8, 4); wav.write("WAVEfmt ", 8) wav.writeUInt32LE(16, 16); wav.writeUInt16LE(1, 20); wav.writeUInt16LE(1, 22) wav.writeUInt32LE(8000, 24); wav.writeUInt32LE(16000, 28); wav.writeUInt16LE(2, 32); wav.writeUInt16LE(16, 34) wav.write("data", 36); wav.writeUInt32LE(16000, 40) for (let i = 0; i < 8000; i++) wav.writeInt16LE(Math.round(Math.sin(i * Math.PI * 2 * 440 / 8000) * 5000), 44 + i * 2) const audio = `data:audio/wav;base64,${wav.toString("base64")}` const base = { left: 80, top: 180, width: 60, height: 60, rotate: 0 } const slides = [{ id: "offline-test", remark: "参考答案:轻声哼唱\n解析:比较声音的强弱。", elements: [ { ...base, id: "title", type: "text", width: 1000, height: 90, content: '