47 lines
1.5 KiB
TypeScript
47 lines
1.5 KiB
TypeScript
import { expect, test } from "bun:test";
|
|
import { mkdtempSync, readFileSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { optimizeImage } from "../src/index.ts";
|
|
|
|
test("build optimizer emits deterministic bounded variants and a manifest", async () => {
|
|
const root = mkdtempSync(join(tmpdir(), "wrnexus-images-"));
|
|
const input = join(root, "Hero image.png");
|
|
writeFileSync(input, new Uint8Array([1, 2, 3]));
|
|
const manifest = await optimizeImage(input, {
|
|
outputDir: join(root, "output"),
|
|
widths: [800, 400, 800],
|
|
formats: ["webp", "avif"],
|
|
processor: {
|
|
async transform(_input, options) {
|
|
return {
|
|
data: new TextEncoder().encode(`${options.width}:${options.format}:${options.quality}`),
|
|
width: options.width,
|
|
height: options.width / 2,
|
|
};
|
|
},
|
|
},
|
|
});
|
|
expect(manifest.variants).toHaveLength(4);
|
|
expect(manifest.variants.map(({ width, format }) => `${width}:${format}`)).toEqual([
|
|
"400:webp",
|
|
"800:webp",
|
|
"400:avif",
|
|
"800:avif",
|
|
]);
|
|
expect(readFileSync(manifest.variants[0]!.path, "utf8")).toBe("400:webp:80");
|
|
});
|
|
|
|
test("build optimizer rejects variant explosions and unsupported formats", async () => {
|
|
const processor = { transform: async () => ({ data: new Uint8Array(), width: 1, height: 1 }) };
|
|
await expect(
|
|
optimizeImage("input.png", {
|
|
outputDir: ".",
|
|
widths: [100, 200],
|
|
formats: ["webp", "avif"],
|
|
maxVariants: 3,
|
|
processor,
|
|
}),
|
|
).rejects.toThrow("exceeds");
|
|
});
|