Files
SnapOtter/tests/unit/image-engine/saturation.test.ts
T
SnapOtter b1afcfdf3c test: migrate 207 test files to typed fixture registry (phase 5)
Codemod rewrites all five ad-hoc fixture-path styles to import from the
typed registry (tests/fixtures/index.ts):
- join(__dirname, "..", "fixtures", ...) inline patterns (100 files)
- const FIXTURES = join(__dirname, ...) + join(FIXTURES, ...) (92 files)
- process.cwd()-based paths (10 files)
- path.resolve(__dirname, ...) patterns (5 files)

Registry extended with 8 new keys (portraitJpg, portraitHeic,
motorcycle, svgLogo, barcodeAvif, qrAvif, crossFormatChat,
multipageTiff) and directory accessors (fixtureDir, fixtureRoot).

Dynamic directory scanners (format-matrix-generated,
format-matrix-multimodal, hostile-inputs) preserved via fixtureDir
imports. No fixtures swapped, bytes identical.

Parity gate: dropped 0, net-new 388.
2026-06-20 04:20:17 +08:00

106 lines
3.3 KiB
TypeScript

import { createRequire } from "node:module";
import path from "node:path";
import { beforeAll, describe, expect, it } from "vitest";
const require = createRequire(
path.resolve(__dirname, "../../../packages/image-engine/src/index.ts"),
);
const sharp = require("sharp") as typeof import("sharp").default;
import { saturation } from "@snapotter/image-engine";
import { fixtures, readFixture } from "../../fixtures/index.js";
let png200x150: Buffer;
beforeAll(() => {
png200x150 = readFixture(fixtures.image.base.png200);
});
async function getMeta(img: sharp.Sharp) {
const buf = await img.toBuffer();
return sharp(buf).metadata();
}
describe("saturation", () => {
it("returns a Sharp instance at value 0 (no change)", async () => {
const img = sharp(png200x150);
const result = await saturation(img, { value: 0 });
const meta = await getMeta(result);
expect(meta.width).toBe(200);
expect(meta.height).toBe(150);
});
it("increases saturation at value +50", async () => {
const img = sharp(png200x150);
const result = await saturation(img, { value: 50 });
const buf = await result.toBuffer();
expect(buf.length).toBeGreaterThan(0);
});
it("decreases saturation at value -50", async () => {
const img = sharp(png200x150);
const result = await saturation(img, { value: -50 });
const buf = await result.toBuffer();
expect(buf.length).toBeGreaterThan(0);
});
it("fully desaturates at value -100", async () => {
const img = sharp(png200x150);
const result = await saturation(img, { value: -100 });
const buf = await result.toBuffer();
expect(buf.length).toBeGreaterThan(0);
});
it("doubles saturation at value +100", async () => {
const img = sharp(png200x150);
const result = await saturation(img, { value: 100 });
const buf = await result.toBuffer();
expect(buf.length).toBeGreaterThan(0);
});
it("throws for value below -100", async () => {
const img = sharp(png200x150);
await expect(saturation(img, { value: -101 })).rejects.toThrow(
"Saturation value must be between -100 and +100",
);
});
it("throws for value above +100", async () => {
const img = sharp(png200x150);
await expect(saturation(img, { value: 101 })).rejects.toThrow(
"Saturation value must be between -100 and +100",
);
});
it("accepts boundary value -100", async () => {
const img = sharp(png200x150);
const result = await saturation(img, { value: -100 });
const meta = await getMeta(result);
expect(meta.width).toBe(200);
});
it("accepts boundary value +100", async () => {
const img = sharp(png200x150);
const result = await saturation(img, { value: 100 });
const meta = await getMeta(result);
expect(meta.width).toBe(200);
});
it("preserves image dimensions", async () => {
const img = sharp(png200x150);
const result = await saturation(img, { value: 30 });
const meta = await getMeta(result);
expect(meta.width).toBe(200);
expect(meta.height).toBe(150);
});
it("works with JPEG input", async () => {
const jpg = readFixture(fixtures.image.base.jpg100);
const img = sharp(jpg);
const result = await saturation(img, { value: 25 });
const meta = await getMeta(result);
expect(meta.width).toBe(100);
expect(meta.height).toBe(100);
});
});