Files
SnapOtter/tests/integration/tools/image/resize.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

330 lines
10 KiB
TypeScript

/**
* Integration tests for the resize tool (/api/v1/tools/image/resize).
*
* This is a Sharp-based tool (no AI sidecar). All processing tests should
* return 200. Output dimensions are verified by downloading the result and
* reading metadata with sharp.
*/
import sharp from "sharp";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { fixtures, readFixture } from "../../../fixtures/index.js";
import {
buildTestApp,
createMultipartPayload,
loginAsAdmin,
type TestApp,
} from "../../test-server.js";
const PNG = readFixture(fixtures.image.base.png200);
const JPG = readFixture(fixtures.image.base.jpg100);
const HEIC = readFixture(fixtures.image.base.heic200);
const TINY = readFixture(fixtures.image.edge.px1);
const WEBP = readFixture(fixtures.image.base.webp50);
let testApp: TestApp;
let app: TestApp["app"];
let adminToken: string;
beforeAll(async () => {
testApp = await buildTestApp();
app = testApp.app;
adminToken = await loginAsAdmin(app);
}, 30_000);
afterAll(async () => {
await testApp.cleanup();
}, 10_000);
/** Helper: POST to resize, assert 200, download result, return sharp metadata. */
async function resizeAndMeta(
settings: Record<string, unknown>,
file = PNG,
filename = "test.png",
fileCt = "image/png",
) {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename, contentType: fileCt, content: file },
{ name: "settings", content: JSON.stringify(settings) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/image/resize",
headers: {
authorization: `Bearer ${adminToken}`,
"content-type": contentType,
},
body,
});
expect(res.statusCode).toBe(200);
const result = JSON.parse(res.body);
expect(result.downloadUrl).toBeDefined();
const dlRes = await app.inject({
method: "GET",
url: result.downloadUrl,
headers: { authorization: `Bearer ${adminToken}` },
});
expect(dlRes.statusCode).toBe(200);
return sharp(dlRes.rawPayload).metadata();
}
describe("Resize", () => {
// ── Processing with dimension verification ───────────────────────
it("route exists and responds to POST", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
{ name: "settings", content: JSON.stringify({ width: 100 }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/image/resize",
headers: {
authorization: `Bearer ${adminToken}`,
"content-type": contentType,
},
body,
});
expect(res.statusCode).toBe(200);
});
it("resizes to explicit width (contain preserves aspect ratio)", async () => {
const meta = await resizeAndMeta({ width: 100 });
expect(meta.width).toBe(100);
// contain fit: 200x150 -> 100 wide means height = 75
expect(meta.height).toBe(75);
});
it("resizes to explicit height (contain preserves aspect ratio)", async () => {
const meta = await resizeAndMeta({ height: 60 });
// contain fit: 200x150 -> 60 tall means width = 80
expect(meta.width).toBe(80);
expect(meta.height).toBe(60);
});
it("resizes to both width and height with contain fit", async () => {
const meta = await resizeAndMeta({ width: 100, height: 100, fit: "contain" });
// contain: fits within 100x100 box, output dimensions match the box
expect(meta.width).toBe(100);
expect(meta.height).toBe(100);
});
it("resizes with cover fit", async () => {
const meta = await resizeAndMeta({ width: 100, height: 100, fit: "cover" });
expect(meta.width).toBe(100);
expect(meta.height).toBe(100);
});
it("resizes with fill fit (stretches)", async () => {
const meta = await resizeAndMeta({ width: 50, height: 200, fit: "fill" });
expect(meta.width).toBe(50);
expect(meta.height).toBe(200);
});
it("resizes with inside fit", async () => {
const meta = await resizeAndMeta({ width: 100, height: 100, fit: "inside" });
// inside: same as contain but never enlarges
expect(meta.width).toBe(100);
expect(meta.height).toBe(75);
});
it("resizes by percentage", async () => {
const meta = await resizeAndMeta({ percentage: 50 });
// 50% of 200x150 = 100x75
expect(meta.width).toBe(100);
expect(meta.height).toBe(75);
});
it("treats 1000 percent as 10x at the product boundary", async () => {
const meta = await resizeAndMeta({ percentage: 1000 }, TINY, "tiny.png", "image/png");
expect(meta.width).toBe(10);
expect(meta.height).toBe(10);
});
it("respects withoutEnlargement flag", async () => {
const meta = await resizeAndMeta({ width: 400, height: 300, withoutEnlargement: true });
// Should not enlarge beyond original 200x150
expect(meta.width).toBeLessThanOrEqual(200);
expect(meta.height).toBeLessThanOrEqual(150);
});
it("works with JPEG input", async () => {
const meta = await resizeAndMeta({ width: 50 }, JPG, "test.jpg", "image/jpeg");
expect(meta.width).toBe(50);
expect(meta.height).toBe(50); // 100x100 -> 50x50
});
it("works with WebP input", async () => {
const meta = await resizeAndMeta({ width: 25 }, WEBP, "test.webp", "image/webp");
expect(meta.width).toBe(25);
expect(meta.height).toBe(25); // 50x50 -> 25x25
});
it("handles HEIC input", { timeout: 120_000 }, async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "photo.heic", contentType: "image/heic", content: HEIC },
{ name: "settings", content: JSON.stringify({ width: 100 }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/image/resize",
headers: {
authorization: `Bearer ${adminToken}`,
"content-type": contentType,
},
body,
});
expect(res.statusCode).toBe(200);
const result = JSON.parse(res.body);
expect(result.processedSize).toBeGreaterThan(0);
});
it("handles 1x1 pixel input", async () => {
const meta = await resizeAndMeta(
{ width: 10, height: 10, fit: "fill" },
TINY,
"tiny.png",
"image/png",
);
expect(meta.width).toBe(10);
expect(meta.height).toBe(10);
});
// ── Validation ───────────────────────────────────────────────────
it("rejects requests without a file", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "settings", content: JSON.stringify({ width: 100 }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/image/resize",
headers: {
authorization: `Bearer ${adminToken}`,
"content-type": contentType,
},
body,
});
expect(res.statusCode).toBe(400);
const result = JSON.parse(res.body);
expect(result.error).toMatch(/no file/i);
});
it("rejects invalid settings JSON", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
{ name: "settings", content: "not json{{{" },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/image/resize",
headers: {
authorization: `Bearer ${adminToken}`,
"content-type": contentType,
},
body,
});
expect(res.statusCode).toBe(400);
const result = JSON.parse(res.body);
expect(result.error).toMatch(/json/i);
});
it("rejects invalid fit value", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
{
name: "settings",
content: JSON.stringify({ width: 100, fit: "stretch" }),
},
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/image/resize",
headers: {
authorization: `Bearer ${adminToken}`,
"content-type": contentType,
},
body,
});
expect(res.statusCode).toBe(400);
const result = JSON.parse(res.body);
expect(result.error).toMatch(/invalid settings/i);
});
it("rejects the deterministic oversized percentage before processing", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
{
name: "settings",
content: JSON.stringify({ percentage: 896202.8004871072 }),
},
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/image/resize",
headers: {
authorization: `Bearer ${adminToken}`,
"content-type": contentType,
},
body,
});
expect(res.statusCode).toBe(400);
expect(JSON.parse(res.body).details).toMatch(/less than or equal to 1000/i);
});
it("rejects a percentage immediately above the product boundary", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "tiny.png", contentType: "image/png", content: TINY },
{
name: "settings",
content: JSON.stringify({ percentage: 1000.000001 }),
},
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/image/resize",
headers: {
authorization: `Bearer ${adminToken}`,
"content-type": contentType,
},
body,
});
expect(res.statusCode).toBe(400);
expect(JSON.parse(res.body).details).toMatch(/less than or equal to 1000/i);
});
it("rejects unauthenticated requests", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
{ name: "settings", content: JSON.stringify({ width: 100 }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/image/resize",
headers: { "content-type": contentType },
body,
});
expect(res.statusCode).toBe(401);
});
});