import { describe, expect, it } from "vitest"; import { createStoredZipBuffer } from "./zip.js"; function listZipEntries(zip: Buffer): string[] { const names: string[] = []; let offset = zip.indexOf(Buffer.from([0x50, 0x4b, 0x01, 0x02])); while (offset >= 0 && zip.readUInt32LE(offset) === 0x02014b50) { const nameLength = zip.readUInt16LE(offset + 28); const extraLength = zip.readUInt16LE(offset + 30); const commentLength = zip.readUInt16LE(offset + 32); names.push(zip.subarray(offset + 46, offset + 46 + nameLength).toString("utf8")); offset += 46 + nameLength + extraLength + commentLength; } return names; } describe("createStoredZipBuffer", () => { it("creates a standards-shaped ZIP with local files and central directory", () => { const zip = createStoredZipBuffer([ { path: "README.md", data: "hello", modifiedAt: new Date("2026-07-02T12:00:00.000Z") }, { path: "manifest.json", data: JSON.stringify({ ok: true }) }, ]); expect(zip.readUInt32LE(0)).toBe(0x04034b50); expect(zip.includes(Buffer.from("README.md"))).toBe(true); expect(zip.includes(Buffer.from("manifest.json"))).toBe(true); expect(listZipEntries(zip)).toEqual(["README.md", "manifest.json"]); expect(zip.readUInt32LE(zip.byteLength - 22)).toBe(0x06054b50); }); it("rejects empty, unsafe, and duplicate entries", () => { expect(() => createStoredZipBuffer([])).toThrow("at least one entry"); expect(() => createStoredZipBuffer([{ path: "../secret.txt", data: "x" }])).toThrow("Invalid ZIP entry path"); expect(() => createStoredZipBuffer([ { path: "same.txt", data: "1" }, { path: "same.txt", data: "2" }, ]), ).toThrow("Duplicate ZIP entry path"); }); });