import { describe, expect, it } from "vitest"; import { MAX_SOURCE_BYTES, OfficeFileError, detectOfficeFormat, extensionOf, familyForFormat, formatBytes, formatLabel, validateOfficePackagePrefix, } from "../../src/office/formats"; describe("office format handling", () => { it.each([ ["REPORT.DOCX", "docx"], ["REPORT.DOCM", "docx"], ["template.xltx", "xlsx"], ["show.ppsm", "pptx"], ["notes.odt", "odt"], ["budget.xlsx", "xlsx"], ["budget.ODS", "ods"], ["deck.pptx", "pptx"], ["deck.odp", "odp"], ] as const)("detects %s", (name, expected) => { expect(detectOfficeFormat({ name, size: 42 })).toBe(expected); }); it("rejects renamed compound-binary, RTF and non-package signatures", () => { expect(() => validateOfficePackagePrefix( Uint8Array.from([0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1]), "docx", ), ).toThrow(/OLE Compound File/u); expect(() => validateOfficePackagePrefix(new TextEncoder().encode("{\\rtf1"), "docx"), ).toThrow(/Rich Text Format/u); expect(() => validateOfficePackagePrefix(new TextEncoder().encode("not zip!"), "xlsx"), ).toThrow(/ZIP-based/u); expect(() => validateOfficePackagePrefix(Uint8Array.from([0x50, 0x4b, 3, 4]), "pptx"), ).not.toThrow(); }); it("rejects empty, oversized, unsupported and legacy inputs explicitly", () => { const cases = [ [{ name: "empty.docx", size: 0 }, "empty-file"], [{ name: "huge.docx", size: MAX_SOURCE_BYTES + 1 }, "file-too-large"], [{ name: "old.doc", size: 42 }, "legacy-format"], [{ name: "notes.txt", size: 42 }, "unsupported-format"], ] as const; for (const [file, code] of cases) { try { detectOfficeFormat(file); throw new Error("Expected detection to fail"); } catch (error) { expect(error).toBeInstanceOf(OfficeFileError); expect((error as OfficeFileError).code).toBe(code); } } }); it("maps family, labels and display values", () => { expect(extensionOf("archive.name.PPTX")).toBe("pptx"); expect(familyForFormat("docx")).toBe("document"); expect(familyForFormat("ods")).toBe("spreadsheet"); expect(familyForFormat("odp")).toBe("presentation"); expect(formatLabel("xlsx")).toBe("Excel workbook"); expect(formatBytes(1536)).toBe("1.50 KiB"); }); });