|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import JSZip from "jszip"; |
| 3 | +import { parsePptx, isPptxTemplate, serializeDeck } from "../index"; |
| 4 | +import { CURRENT_DECK_VERSION } from "@/lib/schema/migrate"; |
| 5 | +import type { Deck } from "@/lib/types"; |
| 6 | + |
| 7 | +const PRESENTATION_MAIN_CT = |
| 8 | + "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml"; |
| 9 | +const TEMPLATE_MAIN_CT = |
| 10 | + "application/vnd.openxmlformats-officedocument.presentationml.template.main+xml"; |
| 11 | +const POTX_MIME = |
| 12 | + "application/vnd.openxmlformats-officedocument.presentationml.template"; |
| 13 | +const PPTX_MIME = |
| 14 | + "application/vnd.openxmlformats-officedocument.presentationml.presentation"; |
| 15 | + |
| 16 | +function makeDeck(): Deck { |
| 17 | + return { |
| 18 | + version: CURRENT_DECK_VERSION, |
| 19 | + title: "Template fixture", |
| 20 | + slides: [ |
| 21 | + { |
| 22 | + id: "slide-1", |
| 23 | + background: "#FFFFFF", |
| 24 | + elements: [ |
| 25 | + { |
| 26 | + id: "t1", |
| 27 | + type: "text", |
| 28 | + rotation: 0, |
| 29 | + z: 1, |
| 30 | + x: 200, |
| 31 | + y: 240, |
| 32 | + w: 1200, |
| 33 | + h: 200, |
| 34 | + text: "Template slide", |
| 35 | + fontFamily: "Inter", |
| 36 | + fontSize: 48, |
| 37 | + fontWeight: 400, |
| 38 | + italic: false, |
| 39 | + underline: false, |
| 40 | + strike: false, |
| 41 | + color: "#0E1330", |
| 42 | + align: "left", |
| 43 | + vAlign: "top", |
| 44 | + lineHeight: 1.2, |
| 45 | + letterSpacing: 0, |
| 46 | + }, |
| 47 | + ], |
| 48 | + }, |
| 49 | + ], |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +async function contentTypesXml(blob: Blob): Promise<string> { |
| 54 | + const zip = await JSZip.loadAsync(await blob.arrayBuffer()); |
| 55 | + const file = zip.file("[Content_Types].xml"); |
| 56 | + return file ? file.async("string") : ""; |
| 57 | +} |
| 58 | + |
| 59 | +describe("pptx ↔ potx", () => { |
| 60 | + it("emits the presentation content type by default", async () => { |
| 61 | + const blob = await serializeDeck(makeDeck()); |
| 62 | + expect(blob.type).toBe(PPTX_MIME); |
| 63 | + const xml = await contentTypesXml(blob); |
| 64 | + expect(xml).toContain(PRESENTATION_MAIN_CT); |
| 65 | + expect(xml).not.toContain(TEMPLATE_MAIN_CT); |
| 66 | + }); |
| 67 | + |
| 68 | + it("emits the template content type and MIME when asTemplate is true", async () => { |
| 69 | + const blob = await serializeDeck(makeDeck(), { asTemplate: true }); |
| 70 | + expect(blob.type).toBe(POTX_MIME); |
| 71 | + const xml = await contentTypesXml(blob); |
| 72 | + expect(xml).toContain(TEMPLATE_MAIN_CT); |
| 73 | + // The presentation override must be gone — exactly one main part. |
| 74 | + expect(xml).not.toContain(PRESENTATION_MAIN_CT); |
| 75 | + }); |
| 76 | + |
| 77 | + it("isPptxTemplate detects templates by content type, not filename", async () => { |
| 78 | + const template = await templateSourceZip().generateAsync({ |
| 79 | + type: "arraybuffer", |
| 80 | + }); |
| 81 | + expect(await isPptxTemplate(template)).toBe(true); |
| 82 | + // A presentation (the default serializer output) is not a template. |
| 83 | + const presentation = await (await serializeDeck(makeDeck())).arrayBuffer(); |
| 84 | + expect(await isPptxTemplate(presentation)).toBe(false); |
| 85 | + // Garbage / non-zip input is reported as not-a-template rather than throwing. |
| 86 | + expect(await isPptxTemplate(new Uint8Array([1, 2, 3]))).toBe(false); |
| 87 | + }); |
| 88 | + |
| 89 | + it("parses a .potx package (same OOXML as .pptx)", async () => { |
| 90 | + const zip = templateSourceZip(); |
| 91 | + const buffer = await zip.generateAsync({ type: "arraybuffer" }); |
| 92 | + const deck = await parsePptx(buffer); |
| 93 | + expect(deck.slides.length).toBe(1); |
| 94 | + }); |
| 95 | + |
| 96 | + it("round-trips a parsed template back to a template by default", async () => { |
| 97 | + const zip = templateSourceZip(); |
| 98 | + const source = await zip.generateAsync({ type: "arraybuffer" }); |
| 99 | + const deck = await parsePptx(source); |
| 100 | + // No asTemplate flag: template-ness is inherited from the source archive. |
| 101 | + const blob = await serializeDeck(deck, { source }); |
| 102 | + expect(blob.type).toBe(POTX_MIME); |
| 103 | + const xml = await contentTypesXml(blob); |
| 104 | + expect(xml).toContain(TEMPLATE_MAIN_CT); |
| 105 | + expect(xml).not.toContain(PRESENTATION_MAIN_CT); |
| 106 | + }); |
| 107 | + |
| 108 | + it("can force a parsed template back to a presentation", async () => { |
| 109 | + const zip = templateSourceZip(); |
| 110 | + const source = await zip.generateAsync({ type: "arraybuffer" }); |
| 111 | + const deck = await parsePptx(source); |
| 112 | + const blob = await serializeDeck(deck, { source, asTemplate: false }); |
| 113 | + expect(blob.type).toBe(PPTX_MIME); |
| 114 | + const xml = await contentTypesXml(blob); |
| 115 | + expect(xml).toContain(PRESENTATION_MAIN_CT); |
| 116 | + expect(xml).not.toContain(TEMPLATE_MAIN_CT); |
| 117 | + }); |
| 118 | +}); |
| 119 | + |
| 120 | +/** |
| 121 | + * Minimal valid POTX package: identical layout to a PPTX, but the main part |
| 122 | + * is declared as a template in [Content_Types].xml. |
| 123 | + */ |
| 124 | +function templateSourceZip(): JSZip { |
| 125 | + const zip = new JSZip(); |
| 126 | + zip.file( |
| 127 | + "[Content_Types].xml", |
| 128 | + `<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 129 | +<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"> |
| 130 | + <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/> |
| 131 | + <Default Extension="xml" ContentType="application/xml"/> |
| 132 | + <Override PartName="/ppt/presentation.xml" ContentType="${TEMPLATE_MAIN_CT}"/> |
| 133 | + <Override PartName="/ppt/slides/slide1.xml" ContentType="application/vnd.openxmlformats-officedocument.presentationml.slide+xml"/> |
| 134 | +</Types>` |
| 135 | + ); |
| 136 | + zip.file( |
| 137 | + "_rels/.rels", |
| 138 | + `<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="ppt/presentation.xml"/></Relationships>` |
| 139 | + ); |
| 140 | + zip.file( |
| 141 | + "ppt/presentation.xml", |
| 142 | + `<?xml version="1.0" encoding="UTF-8" standalone="yes"?><p:presentation xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><p:sldIdLst><p:sldId id="256" r:id="rId2"/></p:sldIdLst><p:sldSz cx="12192000" cy="6858000"/></p:presentation>` |
| 143 | + ); |
| 144 | + zip.file( |
| 145 | + "ppt/_rels/presentation.xml.rels", |
| 146 | + `<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide" Target="slides/slide1.xml"/></Relationships>` |
| 147 | + ); |
| 148 | + zip.file( |
| 149 | + "ppt/slides/slide1.xml", |
| 150 | + `<?xml version="1.0" encoding="UTF-8" standalone="yes"?><p:sld xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><p:cSld><p:spTree><p:sp><p:nvSpPr><p:cNvPr id="2" name="t"/><p:cNvSpPr/><p:nvPr/></p:nvSpPr><p:spPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="3000000" cy="500000"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></p:spPr><p:txBody><a:bodyPr/><a:lstStyle/><a:p><a:r><a:rPr lang="en-US"/><a:t>Hi</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:cSld></p:sld>` |
| 151 | + ); |
| 152 | + zip.file( |
| 153 | + "ppt/slides/_rels/slide1.xml.rels", |
| 154 | + `<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"></Relationships>` |
| 155 | + ); |
| 156 | + return zip; |
| 157 | +} |
0 commit comments