52 lines
1.6 KiB
TypeScript
52 lines
1.6 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
MAX_SOURCE_BYTES,
|
|
OfficeFileError,
|
|
detectOfficeFormat,
|
|
extensionOf,
|
|
familyForFormat,
|
|
formatBytes,
|
|
formatLabel,
|
|
} from "../../src/office/formats";
|
|
|
|
describe("office format handling", () => {
|
|
it.each([
|
|
["REPORT.DOCX", "docx"],
|
|
["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 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");
|
|
});
|
|
});
|