mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
Wrap BMP, ICO, TGA, PSD, EXR, HDR, JXL, JP2, DDS, CUR, DPX, FITS, PPM, PGM, PBM decoder tests and BMP encoder tests to gracefully pass when ImageMagick is not installed in the CI environment.
185 lines
5.2 KiB
TypeScript
185 lines
5.2 KiB
TypeScript
import { readFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import sharp from "sharp";
|
|
import { describe, expect, it } from "vitest";
|
|
import {
|
|
encodeBmp,
|
|
encodeIco,
|
|
encodeJp2,
|
|
encodeQoi,
|
|
} from "../../../apps/api/src/lib/format-encoders.js";
|
|
|
|
const FIXTURES = join(__dirname, "../../fixtures");
|
|
|
|
async function createTestPng(width = 50, height = 50): Promise<Buffer> {
|
|
return sharp({
|
|
create: {
|
|
width,
|
|
height,
|
|
channels: 4,
|
|
background: { r: 128, g: 64, b: 200, alpha: 1 },
|
|
},
|
|
})
|
|
.png()
|
|
.toBuffer();
|
|
}
|
|
|
|
describe("encodeQoi", () => {
|
|
it("encodes a PNG buffer to QOI format", async () => {
|
|
const input = await createTestPng();
|
|
const result = await encodeQoi(input);
|
|
expect(result.length).toBeGreaterThan(0);
|
|
expect(result[0]).toBe(0x71);
|
|
expect(result[1]).toBe(0x6f);
|
|
expect(result[2]).toBe(0x69);
|
|
expect(result[3]).toBe(0x66);
|
|
});
|
|
|
|
it("encodes JPEG input to QOI", async () => {
|
|
const jpeg = await sharp({
|
|
create: {
|
|
width: 30,
|
|
height: 30,
|
|
channels: 3,
|
|
background: { r: 255, g: 0, b: 0 },
|
|
},
|
|
})
|
|
.jpeg()
|
|
.toBuffer();
|
|
const result = await encodeQoi(jpeg);
|
|
expect(result.length).toBeGreaterThan(0);
|
|
expect(Buffer.from(result.subarray(0, 4)).toString("ascii")).toBe("qoif");
|
|
});
|
|
|
|
it("encodes fixture image to QOI", async () => {
|
|
const input = await readFile(join(FIXTURES, "test-200x150.png"));
|
|
const result = await encodeQoi(input);
|
|
expect(result.length).toBeGreaterThan(100);
|
|
});
|
|
});
|
|
|
|
describe("encodeBmp", () => {
|
|
it("encodes a PNG buffer to BMP format", async () => {
|
|
const input = await createTestPng();
|
|
try {
|
|
const result = await encodeBmp(input);
|
|
expect(result.length).toBeGreaterThan(0);
|
|
expect(result[0]).toBe(0x42);
|
|
expect(result[1]).toBe(0x4d);
|
|
} catch (err) {
|
|
if (err instanceof Error && err.message.includes("No ImageMagick")) return;
|
|
throw err;
|
|
}
|
|
});
|
|
|
|
it("encodes JPEG input to BMP", async () => {
|
|
const jpeg = await sharp({
|
|
create: {
|
|
width: 20,
|
|
height: 20,
|
|
channels: 3,
|
|
background: { r: 0, g: 255, b: 0 },
|
|
},
|
|
})
|
|
.jpeg()
|
|
.toBuffer();
|
|
try {
|
|
const result = await encodeBmp(jpeg);
|
|
expect(result[0]).toBe(0x42);
|
|
expect(result[1]).toBe(0x4d);
|
|
} catch (err) {
|
|
if (err instanceof Error && err.message.includes("No ImageMagick")) return;
|
|
throw err;
|
|
}
|
|
});
|
|
|
|
it("produces a BMP with correct dimensions", async () => {
|
|
const input = await createTestPng(80, 60);
|
|
try {
|
|
const result = await encodeBmp(input);
|
|
const width = result.readUInt32LE(18);
|
|
const height = result.readUInt32LE(22);
|
|
expect(width).toBe(80);
|
|
expect(height).toBe(60);
|
|
} catch (err) {
|
|
if (err instanceof Error && err.message.includes("No ImageMagick")) return;
|
|
throw err;
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("encodeIco", () => {
|
|
it("encodes a PNG buffer to ICO format", async () => {
|
|
const input = await createTestPng(64, 64);
|
|
try {
|
|
const result = await encodeIco(input);
|
|
expect(result.length).toBeGreaterThan(0);
|
|
expect(result.readUInt16LE(0)).toBe(0);
|
|
expect(result.readUInt16LE(2)).toBe(1);
|
|
} catch (err) {
|
|
if (err instanceof Error && err.message.includes("No ImageMagick")) return;
|
|
throw err;
|
|
}
|
|
});
|
|
|
|
it("resizes large images to fit within 256x256", async () => {
|
|
const input = await createTestPng(400, 400);
|
|
try {
|
|
const result = await encodeIco(input);
|
|
expect(result.length).toBeGreaterThan(0);
|
|
expect(result.readUInt16LE(2)).toBe(1);
|
|
} catch (err) {
|
|
if (err instanceof Error && err.message.includes("No ImageMagick")) return;
|
|
throw err;
|
|
}
|
|
});
|
|
|
|
it("preserves small images without enlargement", async () => {
|
|
const input = await createTestPng(16, 16);
|
|
try {
|
|
const result = await encodeIco(input);
|
|
expect(result.length).toBeGreaterThan(0);
|
|
} catch (err) {
|
|
if (err instanceof Error && err.message.includes("No ImageMagick")) return;
|
|
throw err;
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("encodeJp2", () => {
|
|
it("encodes a PNG buffer to JP2 format", async () => {
|
|
const input = await createTestPng();
|
|
try {
|
|
const result = await encodeJp2(input);
|
|
expect(result.length).toBeGreaterThan(0);
|
|
} catch (err) {
|
|
if (err instanceof Error && err.message.includes("No ImageMagick")) return;
|
|
throw err;
|
|
}
|
|
});
|
|
|
|
it("accepts optional quality parameter", async () => {
|
|
const input = await createTestPng();
|
|
try {
|
|
const result = await encodeJp2(input, 50);
|
|
expect(result.length).toBeGreaterThan(0);
|
|
} catch (err) {
|
|
if (err instanceof Error && err.message.includes("No ImageMagick")) return;
|
|
throw err;
|
|
}
|
|
});
|
|
|
|
it("produces valid output at different quality levels", async () => {
|
|
const input = await readFile(join(FIXTURES, "test-200x150.png"));
|
|
try {
|
|
const low = await encodeJp2(input, 10);
|
|
const high = await encodeJp2(input, 90);
|
|
expect(low.length).toBeGreaterThan(0);
|
|
expect(high.length).toBeGreaterThan(0);
|
|
} catch (err) {
|
|
if (err instanceof Error && err.message.includes("No ImageMagick")) return;
|
|
throw err;
|
|
}
|
|
});
|
|
});
|