Files
SnapOtter/tests/unit/api/format-encoders.test.ts
T
SnapOtterandGitHub d10d0f544f fix: release QA hardening across processing, media, security, and CI gates (#649)
A release-readiness QA pass over the whole product. The commits split into
defects a user would hit and gates that were reporting green while measuring
nothing.

## Fixes that change behaviour

Rate limiting was bypassable on every install: TRUST_PROXY defaulted to true, so
request.ip came from a client-set header and a forged X-Forwarded-For got past
the login limiter. The default is now a private-network trust list.

A transient Postgres outage stranded in-flight jobs, leaving finished output on
disk with no row pointing at it. A reconciler now resolves those rows and adopts
the bytes rather than dropping the work.

A Redis connection that moved to a new address wedged every read-blocked
consumer, so completions stopped signalling while health still answered 200.
Socket timeouts plus subscriber pings recover it.

Installing more than one AI bundle left the shared venv multi-versioned and
silently broke three tools. The installer now reconciles distributions to one
version each.

Converting an image to JXL at quality 1 through 4 returned a 500, because
libjxl 0.7 rejects the distance those values compute. The quality is floored at
what the encoder honours. A missing ffmpeg was also reported to the user as a
corrupt upload; it now says the engine is unavailable.

RAW uploads reached an unpatched LibRaw on arm64, so it is built from source at
0.22.2, and the release scan was split so it can fail on an unfixed critical
instead of hiding it behind ignore-unfixed.

## Gates that could not fail

Two mutation lanes ran zero mutants because Stryker crawled the gitignored docs
build; coverage discarded its whole report on any failing test; the lint gate
skipped root tests, scripts, and two workspaces; and several generated matrices
counted a host missing ffmpeg as a passing tool. Each now measures what it
claims.

Full evidence and the outstanding release items are tracked locally and are not
part of this branch.
2026-07-27 15:37:30 +08:00

205 lines
5.9 KiB
TypeScript

import sharp from "sharp";
import { describe, expect, it } from "vitest";
import {
cjxlQuality,
encodeBmp,
encodeIco,
encodeJp2,
encodeQoi,
MIN_CJXL_QUALITY,
} from "../../../apps/api/src/lib/format-encoders.js";
import { fixtures, readFixture } from "../../fixtures/index.js";
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 = readFixture(fixtures.image.base.png200);
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 = readFixture(fixtures.image.base.png200);
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;
}
});
});
describe("cjxlQuality", () => {
it("floors the quality at what libjxl 0.7 will honour", () => {
// Anything below the floor made cjxl reject the computed distance and crash
// the conversion, so it clamps up to the lowest quality that encodes.
for (const q of [1, 2, 3, 4]) {
expect(cjxlQuality(q)).toBe(MIN_CJXL_QUALITY);
}
});
it("passes an in-range quality through unchanged", () => {
expect(cjxlQuality(5)).toBe(5);
expect(cjxlQuality(50)).toBe(50);
expect(cjxlQuality(100)).toBe(100);
});
it("defaults when no quality is given and rounds a fractional one", () => {
expect(cjxlQuality(undefined)).toBe(75);
expect(cjxlQuality(80.4)).toBe(80);
});
});