import { describe, expect, test } from "bun:test"; import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { assertDiskFileIsAllowedRasterImage, IMAGE_MAGIC_MISMATCH_MESSAGE } from "./image-magic-bytes"; describe("assertDiskFileIsAllowedRasterImage", () => { test("rejects non-image bytes", () => { const dir = mkdtempSync(join(tmpdir(), "img-magic-")); try { const p = join(dir, "x.bin"); writeFileSync(p, Buffer.from("not an image")); expect(() => assertDiskFileIsAllowedRasterImage(p)).toThrow(IMAGE_MAGIC_MISMATCH_MESSAGE); } finally { rmSync(dir, { recursive: true }); } }); test("accepts jpeg magic prefix", () => { const dir = mkdtempSync(join(tmpdir(), "img-magic-")); try { const p = join(dir, "x.jpg"); const buf = Buffer.alloc(20, 0); buf[0] = 0xff; buf[1] = 0xd8; buf[2] = 0xff; buf[3] = 0xe0; writeFileSync(p, buf); expect(() => assertDiskFileIsAllowedRasterImage(p)).not.toThrow(); } finally { rmSync(dir, { recursive: true }); } }); });