106 lines
3.2 KiB
TypeScript
106 lines
3.2 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { describe, it } from "node:test"
|
|
import { extractDocxHeaderFooter } from "@/lib/drive/docx-header-footer-import"
|
|
import { exportDocsToDocx } from "@/lib/drive/docs-docx-export"
|
|
import { applyParagraphStylesForDocxExport } from "@/lib/drive/docs-export-prepare"
|
|
import { buildDocsExportSnapshot } from "@/lib/drive/docs-export-snapshot"
|
|
import { defaultDocumentParagraphStyles } from "@/lib/drive/docs-paragraph-styles"
|
|
import { buildPageSetupFromDraft, defaultPageMarginsMm } from "@/lib/drive/doc-page-setup"
|
|
import { readUserPageSetupDefaults } from "@/lib/drive/docs-page-defaults"
|
|
import type { TipTapJSON } from "@/lib/drive/richtext-import"
|
|
import { resolveDocumentPageLayout } from "@/lib/drive/doc-page-setup"
|
|
|
|
function baseSnapshot(body: TipTapJSON, pageSetup = buildPageSetupFromDraft(readUserPageSetupDefaults("a4"), null)) {
|
|
return {
|
|
title: "Test",
|
|
sourceName: "test.docx",
|
|
body,
|
|
pageSetup,
|
|
pageLayout: resolveDocumentPageLayout(pageSetup, "a4"),
|
|
paragraphStyles: defaultDocumentParagraphStyles(),
|
|
pageCount: 1,
|
|
getPageStackElement: () => null,
|
|
}
|
|
}
|
|
|
|
describe("docs-export", () => {
|
|
it("applyParagraphStylesForDocxExport converts styleId to heading", () => {
|
|
const content: TipTapJSON = {
|
|
type: "doc",
|
|
content: [
|
|
{
|
|
type: "paragraph",
|
|
attrs: { styleId: "heading1" },
|
|
content: [{ type: "text", text: "Title" }],
|
|
},
|
|
],
|
|
}
|
|
|
|
const prepared = applyParagraphStylesForDocxExport(content, defaultDocumentParagraphStyles())
|
|
const block = (prepared.content as TipTapJSON[])[0]
|
|
assert.equal(block.type, "heading")
|
|
assert.equal((block.attrs as { level?: number }).level, 1)
|
|
})
|
|
|
|
it("exportDocsToDocx includes header/footer in archive", async () => {
|
|
const body: TipTapJSON = {
|
|
type: "doc",
|
|
content: [
|
|
{
|
|
type: "paragraph",
|
|
content: [{ type: "text", text: "Corps du document" }],
|
|
},
|
|
],
|
|
}
|
|
|
|
const pageSetup = {
|
|
...buildPageSetupFromDraft(readUserPageSetupDefaults("a4"), null),
|
|
marginsMm: defaultPageMarginsMm(),
|
|
header: {
|
|
content: {
|
|
type: "doc",
|
|
content: [
|
|
{
|
|
type: "paragraph",
|
|
content: [{ type: "text", text: "En-tête test" }],
|
|
},
|
|
],
|
|
},
|
|
},
|
|
footer: {
|
|
content: {
|
|
type: "doc",
|
|
content: [
|
|
{
|
|
type: "paragraph",
|
|
content: [{ type: "text", text: "Pied test" }],
|
|
},
|
|
],
|
|
},
|
|
},
|
|
}
|
|
|
|
const blob = await exportDocsToDocx(baseSnapshot(body, pageSetup))
|
|
const buffer = await blob.arrayBuffer()
|
|
const extracted = await extractDocxHeaderFooter(buffer)
|
|
|
|
assert.ok(extracted.header?.content)
|
|
assert.ok(extracted.footer?.content)
|
|
})
|
|
|
|
it("buildDocsExportSnapshot returns null without editor", () => {
|
|
assert.equal(
|
|
buildDocsExportSnapshot({
|
|
editor: null,
|
|
sourceName: "x.docx",
|
|
pageSetup: null,
|
|
fallbackFormatId: "a4",
|
|
paragraphStyles: defaultDocumentParagraphStyles(),
|
|
pageCount: 1,
|
|
getPageStackElement: () => null,
|
|
}),
|
|
null
|
|
)
|
|
})
|
|
})
|