Files
MorphDoc/packages/docx-engine/tests/pandoc-runtime.test.ts
T
SkyJourneyandClaude Sonnet 5 c4df499680 release: 发布 v0.6.4 环境自适应与 macOS 打包
新增能力:桌面端新增 macOS 打包支持,electron-builder 增加独立 mac 配置块,
产出 dmg,按 arm64、x64 分别单独构建、不产出 universal 包;Pandoc 运行时清单
新增 darwin/arm64、darwin/x64 两个官方发行项(含独立许可证文件来源),桌面
Pandoc 候选路径与 prepare-pandoc-runtime.mjs 均已泛化为按 --platform/--arch
参数选取目标,不再只认 Windows x64。AGENTS.md 不再硬编码 Windows 绝对路径与
强制 PowerShell 语法,改为按当前 Shell 与仓库实际位置自适应。

问题修复:修复解压内置 Pandoc 时未保留 Unix 可执行权限位的问题(unzipSync
不保留压缩包内权限,已补 chmod 0o755);修复根 package.json 中
build:web-runtime/dev/desktop:dev 三个脚本的构建顺序错误
(@md-to-pdf/application 被排在其依赖的 @md-to-pdf/preview-engine 之前,在
没有历史 dist 残留的全新环境中会导致类型解析失败);修正
packages/docx-engine/tests/pandoc-runtime.test.ts 与
apps/desktop/tests/markdown-file.test.ts 中依赖宿主 OS 或路径分隔符的测试
断言,避免这些用例只能在特定平台上通过。

验证结果:docx-engine 包 93 项测试、desktop 包 62 项测试(61 通过 + 1 项
Windows 专属用例按预期跳过)均通过;全项目类型检查、生产构建通过,
git diff --check 无空白错误。跳过依赖 Git 忽略的真实客户文档语料(tmp/)的
test:docx-real-world-corpus 与 test:docx-release-gate-suites 后,其余全部
工作区测试均通过;这两步需要接入真实语料的机器上单独执行。本机 macOS
(Apple Silicon)实测 npm run package:mac:arm64 全链路成功,内置 Pandoc
3.9.0.2 完成下载、哈希校验与真实 DOCX 冒烟转换;实际截图确认窗口、macOS
原生菜单栏、编辑器工具栏、中文字体与实时预览渲染正常。

兼容与部署:版本统一为 0.6.4。本版本仅完成开发基础设施与验证,未构建正式
发行文件:没有产出真实签名的 dmg、没有重新构建 Windows 安装包,也没有重新
构建 Docker 镜像;正式 macOS 发行产物与 Windows/Docker 同步验证留待 DOCX
发布门禁阶段完成后一并发布。

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K5N6pcbjV4jVfXrcH3NcLP
2026-08-26 19:32:38 +08:00

177 lines
5.1 KiB
TypeScript

import path from "node:path";
import { describe, expect, it, vi } from "vitest";
import { zipSync } from "fflate";
import {
DOCX_PANDOC_VERSION,
type DocxRuntimeStatus
} from "@md-to-pdf/core";
import {
PandocRuntime,
probePandocRuntime,
type PandocProcessResult,
type PandocProcessRunner
} from "../src/index.js";
const encoder = new TextEncoder();
function processResult(
overrides: Partial<PandocProcessResult> = {}
): PandocProcessResult {
return {
outcome: "completed",
exitCode: 0,
stdout: encoder.encode(`pandoc ${DOCX_PANDOC_VERSION}\n`),
stderr: "",
...overrides
};
}
function runnerWith(result: PandocProcessResult) {
return vi.fn<PandocProcessRunner>().mockResolvedValue(result);
}
function createReferenceDocx() {
const xml = (value: string) => encoder.encode(value);
return zipSync({
"[Content_Types].xml": xml("<Types/>"),
"_rels/.rels": xml("<Relationships/>"),
"word/document.xml": xml("<document/>"),
"word/fontTable.xml": xml("<fonts/>"),
"word/numbering.xml": xml("<numbering/>"),
"word/settings.xml": xml("<settings/>"),
"word/styles.xml": xml("<styles/>"),
"word/_rels/document.xml.rels": xml("<Relationships/>"),
"word/theme/theme1.xml": xml("<theme/>")
});
}
describe("Pandoc 运行时探测", () => {
it.each([
["not-found", "not-found"],
["not-executable", "not-executable"],
["timeout", "probe-timeout"],
["aborted", "probe-timeout"],
["output-limit", "not-executable"]
] satisfies Array<[PandocProcessResult["outcome"], DocxRuntimeStatus]>)(
"将 %s 映射为 %s",
async (outcome, expectedStatus) => {
const resolution = await probePandocRuntime({
configuredPath: "C:\\trusted\\pandoc.exe",
runner: runnerWith(
processResult({ outcome, exitCode: null, stdout: new Uint8Array() })
)
});
expect(resolution.capability.status).toBe(expectedStatus);
}
);
it("拒绝不同补丁版本", async () => {
const resolution = await probePandocRuntime({
configuredPath: "C:\\trusted\\pandoc.exe",
runner: runnerWith(
processResult({
stdout: encoder.encode("pandoc 3.9.0.1\n")
})
)
});
expect(resolution.capability).toMatchObject({
status: "version-mismatch",
detectedVersion: "3.9.0.1"
});
});
it("显式路径失败时不静默回退 PATH", async () => {
const runner = runnerWith(
processResult({
outcome: "not-found",
exitCode: null,
stdout: new Uint8Array()
})
);
const resolution = await probePandocRuntime({
configuredPath: "C:\\missing\\pandoc.exe",
runner
});
expect(resolution.capability.status).toBe("not-found");
expect(runner).toHaveBeenCalledTimes(1);
});
it("内置路径不存在时回退到 PATH", async () => {
const runner = vi
.fn<PandocProcessRunner>()
.mockResolvedValueOnce(
processResult({
outcome: "not-found",
exitCode: null,
stdout: new Uint8Array()
})
)
.mockResolvedValueOnce(processResult());
const resolution = await probePandocRuntime({
platform: "win32",
architecture: "x64",
desktopResourcesPath: "C:\\app\\resources",
environment: {},
runner
});
expect(resolution).toMatchObject({
capability: { status: "available" },
executablePath: "pandoc.exe"
});
expect(runner.mock.calls[0]?.[0]).toContain(
path.join("pandoc", DOCX_PANDOC_VERSION, "windows-x86_64", "pandoc.exe")
);
});
it("macOS 内置路径不存在时回退到 PATH", async () => {
const runner = vi
.fn<PandocProcessRunner>()
.mockResolvedValueOnce(
processResult({
outcome: "not-found",
exitCode: null,
stdout: new Uint8Array()
})
)
.mockResolvedValueOnce(processResult());
const resolution = await probePandocRuntime({
platform: "darwin",
architecture: "arm64",
desktopResourcesPath: "/Applications/MorphDoc.app/Contents/Resources",
environment: {},
runner
});
expect(resolution).toMatchObject({
capability: { status: "available" },
executablePath: "pandoc"
});
expect(runner.mock.calls[0]?.[0]).toContain(
path.join("pandoc", DOCX_PANDOC_VERSION, "darwin-arm64", "pandoc")
);
});
it("缓存探测和默认 reference.docx,并向调用方返回副本", async () => {
const reference = createReferenceDocx();
const runner = vi
.fn<PandocProcessRunner>()
.mockResolvedValueOnce(processResult())
.mockResolvedValueOnce(
processResult({ stdout: reference })
);
const runtime = new PandocRuntime({
configuredPath: "C:\\trusted\\pandoc.exe",
runner
});
const first = await runtime.getDefaultReferenceDocx();
first[0] = 0;
const second = await runtime.getDefaultReferenceDocx();
expect(second[0]).not.toBe(0);
expect(runner).toHaveBeenCalledTimes(2);
expect(runner.mock.calls[1]?.[1]).toEqual([
"--print-default-data-file",
"reference.docx"
]);
});
});