feat: 建立 DOCX 共享协议与准备服务
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
DOCX_MIME_TYPE,
|
||||
DOCX_PANDOC_VERSION,
|
||||
MAXIMUM_DOCX_MARKDOWN_LENGTH,
|
||||
createDocxFileName,
|
||||
defaultExportConfig,
|
||||
docxCapabilitySchema,
|
||||
docxExportErrorResponseSchema,
|
||||
docxExportRequestSchema
|
||||
} from "../src/index.js";
|
||||
|
||||
describe("DOCX 共享协议", () => {
|
||||
it("解析跨端导出请求并补齐稳定默认值", () => {
|
||||
expect(
|
||||
docxExportRequestSchema.parse({
|
||||
markdown: "# 文档",
|
||||
exportConfig: defaultExportConfig
|
||||
})
|
||||
).toEqual({
|
||||
markdown: "# 文档",
|
||||
fileName: "文档.md",
|
||||
language: "zh-CN",
|
||||
resources: [],
|
||||
exportConfig: defaultExportConfig
|
||||
});
|
||||
});
|
||||
|
||||
it("校验导出配置与请求资源结构", () => {
|
||||
expect(
|
||||
docxExportRequestSchema.safeParse({
|
||||
markdown: "# 文档",
|
||||
exportConfig: {}
|
||||
}).success
|
||||
).toBe(false);
|
||||
expect(
|
||||
docxExportRequestSchema.safeParse({
|
||||
markdown: "# 文档",
|
||||
resources: [{ path: "", data: "" }],
|
||||
exportConfig: defaultExportConfig
|
||||
}).success
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("拒绝超过共享协议上限的 Markdown", () => {
|
||||
expect(
|
||||
docxExportRequestSchema.safeParse({
|
||||
markdown: "x".repeat(MAXIMUM_DOCX_MARKDOWN_LENGTH + 1),
|
||||
exportConfig: defaultExportConfig
|
||||
}).success
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("生成跨平台安全的 DOCX 文件名", () => {
|
||||
expect(createDocxFileName("C:\\资料\\项目报告.markdown")).toBe(
|
||||
"项目报告.docx"
|
||||
);
|
||||
expect(createDocxFileName("../../非法:*?.md")).toBe("非法___.docx");
|
||||
expect(createDocxFileName(" ")).toBe("文档.docx");
|
||||
expect(createDocxFileName(undefined)).toBe("文档.docx");
|
||||
});
|
||||
|
||||
it("只将精确版本识别为可用能力", () => {
|
||||
expect(
|
||||
docxCapabilitySchema.parse({
|
||||
format: "docx",
|
||||
status: "available",
|
||||
expectedVersion: DOCX_PANDOC_VERSION,
|
||||
detectedVersion: DOCX_PANDOC_VERSION
|
||||
})
|
||||
).toEqual({
|
||||
format: "docx",
|
||||
status: "available",
|
||||
expectedVersion: "3.9.0.2",
|
||||
detectedVersion: "3.9.0.2"
|
||||
});
|
||||
expect(
|
||||
docxCapabilitySchema.safeParse({
|
||||
format: "docx",
|
||||
status: "available",
|
||||
expectedVersion: DOCX_PANDOC_VERSION,
|
||||
detectedVersion: "3.8"
|
||||
}).success
|
||||
).toBe(false);
|
||||
expect(
|
||||
docxCapabilitySchema.safeParse({
|
||||
format: "docx",
|
||||
status: "version-mismatch",
|
||||
expectedVersion: DOCX_PANDOC_VERSION,
|
||||
detectedVersion: "3.8",
|
||||
message: "Pandoc 版本不匹配"
|
||||
}).success
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("暴露标准 DOCX MIME", () => {
|
||||
expect(DOCX_MIME_TYPE).toBe(
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
||||
);
|
||||
});
|
||||
|
||||
it("提供可跨 HTTP 与 IPC 传递的错误响应", () => {
|
||||
expect(
|
||||
docxExportErrorResponseSchema.parse({
|
||||
error: "DOCX_RUNTIME_NOT_FOUND",
|
||||
message: "未找到 Pandoc"
|
||||
})
|
||||
).toEqual({
|
||||
error: "DOCX_RUNTIME_NOT_FOUND",
|
||||
message: "未找到 Pandoc",
|
||||
retryable: false
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user