feat: 建立 DOCX 共享协议与准备服务

This commit is contained in:
SkyJourney
2026-07-30 11:04:18 +08:00
parent fb3ddada39
commit f7d4efeafc
10 changed files with 512 additions and 3 deletions
@@ -17,6 +17,10 @@ import {
ApplicationRequestError,
createApplicationService
} from "../src/index.js";
import {
MAXIMUM_DOCX_MARKDOWN_LENGTH,
defaultExportConfig
} from "@md-to-pdf/core";
let temporaryDirectory: string | undefined;
@@ -269,4 +273,110 @@ describe("共享应用服务", () => {
"已忽略与内置主题同 ID 的本地主题 test-theme"
);
});
it("为 DOCX 引擎准备统一文档、主题和源请求", async () => {
const roots = await createThemeFixture();
const service = createApplicationService(roots);
const prepared = await service.prepareDocxExport({
markdown: "# DOCX 文档",
fileName: "报告.md",
language: "zh-CN",
exportConfig: {
...defaultExportConfig,
themeId: "test-theme"
}
});
expect(prepared.request).toMatchObject({
markdown: "# DOCX 文档",
fileName: "报告.md",
language: "zh-CN",
resources: []
});
expect(prepared.document.metadata.title).toBe("DOCX 文档");
expect(prepared.theme.manifest.id).toBe("test-theme");
expect(prepared.theme.source).toBe("bundled");
expect(prepared.theme.css).toContain(
"#write { font-family: Test; }"
);
});
it("以稳定错误码拒绝非法 DOCX 配置和缺失主题", async () => {
const roots = await createThemeFixture();
const service = createApplicationService(roots);
await expect(
service.prepareDocxExport({
markdown: "# 文档",
exportConfig: {}
})
).rejects.toEqual(
expect.objectContaining<ApplicationRequestError>({
statusCode: 400,
code: "INVALID_EXPORT_CONFIG"
})
);
await expect(
service.prepareDocxExport({
markdown: "# 文档",
exportConfig: {
...defaultExportConfig,
themeId: "missing-theme"
}
})
).rejects.toEqual(
expect.objectContaining<ApplicationRequestError>({
statusCode: 404,
code: "THEME_NOT_FOUND"
})
);
await expect(
service.prepareDocxExport({
markdown: 42,
exportConfig: {
...defaultExportConfig,
themeId: "test-theme"
}
})
).rejects.toEqual(
expect.objectContaining<ApplicationRequestError>({
statusCode: 400,
code: "INVALID_MARKDOWN"
})
);
await expect(
service.prepareDocxExport({
markdown: "x".repeat(MAXIMUM_DOCX_MARKDOWN_LENGTH + 1),
exportConfig: {
...defaultExportConfig,
themeId: "test-theme"
}
})
).rejects.toEqual(
expect.objectContaining<ApplicationRequestError>({
statusCode: 413,
code: "MARKDOWN_TOO_LARGE"
})
);
await expect(
service.prepareDocxExport({
markdown: "# 文档",
resources: [{ path: "", data: "" }],
exportConfig: {
...defaultExportConfig,
themeId: "test-theme"
}
})
).rejects.toEqual(
expect.objectContaining<ApplicationRequestError>({
statusCode: 400,
code: "INVALID_IMAGE_RESOURCES"
})
);
});
});