import sharp from "sharp"; import { describe, expect, it } from "vitest"; import { inspectImage } from "./inspect"; describe("image decoding before publication", () => { it("accepts real decoded image dimensions", async () => { const bytes = await sharp({ create: { width: 8, height: 12, channels: 3, background: "red" } }).png().toBuffer(); await expect(inspectImage(bytes, "image/png")).resolves.toEqual({ width: 8, height: 12 }); await expect(inspectImage(bytes, "image/jpeg")).rejects.toMatchObject({ status: 422 }); await expect(inspectImage(bytes.subarray(0, 40), "image/png")).rejects.toMatchObject({ status: 422 }); }); it("rejects a magic-byte-only fake", async () => { await expect(inspectImage(Uint8Array.from([137, 80, 78, 71, 13, 10, 26, 10]), "image/png")) .rejects.toMatchObject({ status: 422 }); }); });