新增通用 CSS 到 OOXML 翻译修复,统一字体、字距、精确行距、段落、列表、表格、引用、代码块与行内代码连续性,不引入按主题 ID 分支。 新增 MdTP Mono 并统一 Serif、Sans、Mono 三字体包的 Chromium 与 DOCX 使用链;字体声明、嵌入部件和 Word/WPS 实际采用均进入硬门禁。 重建封面整页及正文语义块视觉差分,14 套主题、纵横两个方向、五组页边距共 140 个真实场景全部通过,阻断失败和诊断失败均为零。 源码服务、Docker Web API 与实际安装 Desktop 的 red-briefing 导出均包含 5 个字体部件;Word/WPS 原生渲染和逐页复核通过。修复 Docker 构建上下文与运行层复用软链接,并完善 v0.6.1 版本、发行说明和发布归集。 验证:npm test(116 个文件、616 项测试)、npm run typecheck、npm run build、git diff --check 全部通过。Desktop 安装器与 ZIP、Docker v0.6.1 镜像已生成;Windows 产物仍为未签名内部发行。
223 lines
5.8 KiB
TypeScript
223 lines
5.8 KiB
TypeScript
import {
|
|
copyFile,
|
|
mkdtemp,
|
|
rm,
|
|
writeFile
|
|
} from "node:fs/promises";
|
|
import { existsSync } from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import {
|
|
defaultExportConfig,
|
|
type PreparedDocxMedia,
|
|
type ThemeManifest
|
|
} from "@md-to-pdf/core";
|
|
import type { DocxThemeTokenSet } from "@md-to-pdf/docx-theme-engine";
|
|
import {
|
|
PandocDocxConversionError,
|
|
PandocDocxConverter,
|
|
type PandocProcessRunner,
|
|
type PandocRuntimeProvider
|
|
} from "../src/index.js";
|
|
import { createTestBaselineReference } from "./reference-test-fixture.js";
|
|
|
|
function theme(): ThemeManifest {
|
|
return {
|
|
manifestVersion: 1,
|
|
id: "test-theme",
|
|
name: "测试主题",
|
|
version: "1.0.0",
|
|
description: "测试",
|
|
author: "测试",
|
|
license: "内部许可",
|
|
entry: "theme.css",
|
|
domPreset: "generic",
|
|
defaultFontSize: "16px",
|
|
supportedFeatures: [],
|
|
category: "general",
|
|
compatibleProfiles: [],
|
|
docxStyle: { preset: "technical" },
|
|
bundled: true
|
|
};
|
|
}
|
|
|
|
const emptyMedia: PreparedDocxMedia = {
|
|
resources: [],
|
|
echartsErrors: [],
|
|
mermaidErrors: [],
|
|
warnings: [],
|
|
totalBytes: 0
|
|
};
|
|
|
|
const themeTokens: DocxThemeTokenSet = {
|
|
schemaVersion: 1,
|
|
themeId: "test-theme",
|
|
themeFingerprint: "c".repeat(64),
|
|
mode: "auto-with-overrides",
|
|
basePreset: "technical",
|
|
slots: [],
|
|
diagnostics: []
|
|
};
|
|
|
|
function input() {
|
|
return {
|
|
markdown: "# 测试",
|
|
fileName: "测试.md",
|
|
language: "zh-CN",
|
|
exportConfig: defaultExportConfig,
|
|
theme: theme(),
|
|
themeTokens,
|
|
fonts: [],
|
|
metadata: {
|
|
title: "测试",
|
|
author: "测试人",
|
|
subject: "",
|
|
keywords: [],
|
|
language: "zh-CN"
|
|
},
|
|
semanticDocument: {
|
|
schemaVersion: 1,
|
|
titlePolicy: {
|
|
metadataTitle: "suppress",
|
|
firstBodyHeading: "keep"
|
|
},
|
|
regions: []
|
|
},
|
|
media: emptyMedia
|
|
};
|
|
}
|
|
|
|
function argumentAfter(arguments_: readonly string[], name: string) {
|
|
const index = arguments_.indexOf(name);
|
|
if (index < 0 || !arguments_[index + 1]) {
|
|
throw new Error(`缺少参数 ${name}`);
|
|
}
|
|
return arguments_[index + 1]!;
|
|
}
|
|
|
|
describe("Pandoc DOCX 转换器", () => {
|
|
let temporaryRoot: string;
|
|
const baseline = createTestBaselineReference();
|
|
const runtime: PandocRuntimeProvider = {
|
|
async getExecutablePath() {
|
|
return "pandoc";
|
|
},
|
|
async getDefaultReferenceDocx() {
|
|
return baseline;
|
|
}
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
temporaryRoot = await mkdtemp(
|
|
path.join(os.tmpdir(), "docx-converter-test-")
|
|
);
|
|
});
|
|
|
|
afterEach(async () => {
|
|
await rm(temporaryRoot, { recursive: true, force: true });
|
|
});
|
|
|
|
it("使用固定参数转换、校验并清理请求级临时目录", async () => {
|
|
let requestDirectory = "";
|
|
const runner = vi.fn<PandocProcessRunner>(
|
|
async (_executable, arguments_, options) => {
|
|
requestDirectory = options.cwd!;
|
|
expect(arguments_).toContain("--lua-filter");
|
|
expect(arguments_).toContain("--data-dir");
|
|
expect(arguments_).toContain("--resource-path");
|
|
expect(argumentAfter(arguments_, "--from")).toBe(
|
|
"markdown+yaml_metadata_block+pipe_tables+footnotes+task_lists+tex_math_dollars-raw_html"
|
|
);
|
|
expect(
|
|
options.env?.MD_TO_PDF_DOCX_MEDIA_MAP
|
|
).toContain("media-map.json");
|
|
expect(
|
|
options.env?.MD_TO_PDF_DOCX_STRUCTURE_PLAN
|
|
).toContain("structure-plan.json");
|
|
const structurePlan = JSON.parse(
|
|
await import("node:fs/promises").then(({ readFile }) =>
|
|
readFile(
|
|
options.env!.MD_TO_PDF_DOCX_STRUCTURE_PLAN!,
|
|
"utf8"
|
|
)
|
|
)
|
|
);
|
|
expect(structurePlan.titlePolicy.metadataTitle).toBe(
|
|
"suppress"
|
|
);
|
|
await copyFile(
|
|
argumentAfter(arguments_, "--reference-doc"),
|
|
argumentAfter(arguments_, "--output")
|
|
);
|
|
return {
|
|
outcome: "completed",
|
|
exitCode: 0,
|
|
stdout: new Uint8Array(),
|
|
stderr: ""
|
|
};
|
|
}
|
|
);
|
|
const converter = new PandocDocxConverter({
|
|
runtime,
|
|
runner,
|
|
temporaryRoot
|
|
});
|
|
const result = await converter.convert(input());
|
|
|
|
expect(result.docx.byteLength).toBeGreaterThan(0);
|
|
expect(result.validation.partCount).toBeGreaterThan(8);
|
|
expect(runner).toHaveBeenCalledTimes(1);
|
|
expect(existsSync(requestDirectory)).toBe(false);
|
|
});
|
|
|
|
it("超时和无效输出均返回稳定错误并清理目录", async () => {
|
|
let timeoutDirectory = "";
|
|
const timeoutRunner = vi.fn<PandocProcessRunner>(
|
|
async (_executable, _arguments, options) => {
|
|
timeoutDirectory = options.cwd!;
|
|
return {
|
|
outcome: "timeout",
|
|
exitCode: null,
|
|
stdout: new Uint8Array(),
|
|
stderr: ""
|
|
};
|
|
}
|
|
);
|
|
await expect(
|
|
new PandocDocxConverter({
|
|
runtime,
|
|
runner: timeoutRunner,
|
|
temporaryRoot
|
|
}).convert(input())
|
|
).rejects.toMatchObject({
|
|
code: "DOCX_RENDER_TIMEOUT"
|
|
} satisfies Partial<PandocDocxConversionError>);
|
|
expect(existsSync(timeoutDirectory)).toBe(false);
|
|
|
|
const invalidRunner = vi.fn<PandocProcessRunner>(
|
|
async (_executable, arguments_) => {
|
|
await writeFile(
|
|
argumentAfter(arguments_, "--output"),
|
|
"not a docx"
|
|
);
|
|
return {
|
|
outcome: "completed",
|
|
exitCode: 0,
|
|
stdout: new Uint8Array(),
|
|
stderr: ""
|
|
};
|
|
}
|
|
);
|
|
await expect(
|
|
new PandocDocxConverter({
|
|
runtime,
|
|
runner: invalidRunner,
|
|
temporaryRoot
|
|
}).convert(input())
|
|
).rejects.toMatchObject({
|
|
code: "DOCX_OUTPUT_INVALID"
|
|
} satisfies Partial<PandocDocxConversionError>);
|
|
});
|
|
});
|