import { describe, expect, it } from "vitest" import { buildWorkbook } from "../core/xlsx.js" import { unzip } from "./support/unzip.js" describe("the workbook writer", () => { const book = buildWorkbook("Approved lines", [ ["Label", "Value", "Page"], ["Total", "12,50", 2], ]) const files = unzip(book) it("writes the parts a spreadsheet application needs to open the file", () => { expect([...files.keys()].sort()).toEqual([ "[Content_Types].xml", "_rels/.rels", "xl/_rels/workbook.xml.rels", "xl/styles.xml", "xl/workbook.xml", "xl/worksheets/sheet1.xml", ]) expect(files.get("xl/workbook.xml")).toContain('') }) it("keeps text as text and a number as a number, in the cell it was given", () => { const sheet = files.get("xl/worksheets/sheet1.xml") ?? "" expect(sheet).toContain('Label') expect(sheet).toContain('12,50') expect(sheet).toContain('2') }) it("writes a formula-looking value as inert text, never as a formula", () => { const sheet = unzip(buildWorkbook("S", [['=HYPERLINK("http://x")', "+1+1", "@SUM(A1)", "-2+3"]])).get( "xl/worksheets/sheet1.xml", ) ?? "" expect(sheet).not.toContain("") expect(sheet.match(/t="inlineStr"/g)).toHaveLength(4) expect(sheet).toContain("=HYPERLINK("http://x")") }) it("escapes markup and drops characters XML cannot carry", () => { const sheet = unzip(buildWorkbook("S", [["a&\u0000c\u0007d"]])).get("xl/worksheets/sheet1.xml") ?? "" expect(sheet).toContain("a<b>&cd") expect([...sheet].some((char) => char.charCodeAt(0) < 9)).toBe(false) }) it("refuses a sheet name Excel would reject", () => { for (const name of ["", "a/b", "x".repeat(32), "[a]", "a:b"]) { expect(() => buildWorkbook(name, [])).toThrow(/sheet name/) } }) it("is byte-for-byte reproducible, so an export of the same lines is the same file", () => { expect(Buffer.from(buildWorkbook("S", [["a"]])).equals(Buffer.from(buildWorkbook("S", [["a"]])))).toBe(true) }) })