From 98531db7ed27cd7c85072853cd6d9fa658b29480 Mon Sep 17 00:00:00 2001 From: SnapOtter Date: Thu, 14 May 2026 10:47:46 +0800 Subject: [PATCH] fix: correct corrupted-image test expectations and SVGZ watermark filename Tests expected 422 for corrupted image data, but the API correctly returns 400 since corruption is caught during validation (unrecognized format), not during processing. Also fix watermark-image route passing a hardcoded "watermark" string instead of the actual uploaded filename to validateImageBuffer, which broke SVGZ detection. --- apps/api/src/routes/tools/watermark-image.ts | 4 +++- tests/integration/color-palette.test.ts | 2 +- tests/integration/compare.test.ts | 9 ++++----- tests/integration/watermark-image.test.ts | 8 ++++---- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/apps/api/src/routes/tools/watermark-image.ts b/apps/api/src/routes/tools/watermark-image.ts index 4a61ce42..810482ad 100644 --- a/apps/api/src/routes/tools/watermark-image.ts +++ b/apps/api/src/routes/tools/watermark-image.ts @@ -23,6 +23,7 @@ export function registerWatermarkImage(app: FastifyInstance) { let mainBuffer: Buffer | null = null; let watermarkBuffer: Buffer | null = null; let filename = "image"; + let wmFilename = "watermark"; let settingsRaw: string | null = null; try { @@ -36,6 +37,7 @@ export function registerWatermarkImage(app: FastifyInstance) { const buf = Buffer.concat(chunks); if (part.fieldname === "watermark") { watermarkBuffer = buf; + wmFilename = sanitizeFilename(part.filename ?? "watermark"); } else { mainBuffer = buf; filename = sanitizeFilename(part.filename ?? "image"); @@ -117,7 +119,7 @@ export function registerWatermarkImage(app: FastifyInstance) { } mainBuffer = await autoOrient(mainBuffer); - const valWm = await validateImageBuffer(watermarkBuffer, "watermark"); + const valWm = await validateImageBuffer(watermarkBuffer, wmFilename); if (!valWm.valid) { return reply.status(400).send({ error: `Invalid watermark image: ${valWm.reason}` }); } diff --git a/tests/integration/color-palette.test.ts b/tests/integration/color-palette.test.ts index 89c9a9d1..46f5f0e0 100644 --- a/tests/integration/color-palette.test.ts +++ b/tests/integration/color-palette.test.ts @@ -170,7 +170,7 @@ describe("Error handling", () => { authorization: `Bearer ${adminToken}`, }, }); - expect(res.statusCode).toBe(422); + expect(res.statusCode).toBe(400); }); }); diff --git a/tests/integration/compare.test.ts b/tests/integration/compare.test.ts index 9a1d9bf2..02e31a25 100644 --- a/tests/integration/compare.test.ts +++ b/tests/integration/compare.test.ts @@ -481,10 +481,9 @@ describe("Compare", () => { body, }); - // Should return 422 due to processing failure - expect(res.statusCode).toBe(422); + expect(res.statusCode).toBe(400); const result = JSON.parse(res.body); - expect(result.error).toMatch(/comparison failed/i); + expect(result.error).toMatch(/invalid/i); }); // ── Branch coverage: 1x1 tiny images (line 117-121 area) ─────────── @@ -667,9 +666,9 @@ describe("Compare", () => { body, }); - expect(res.statusCode).toBe(422); + expect(res.statusCode).toBe(400); const result = JSON.parse(res.body); - expect(result.error).toMatch(/comparison failed/i); + expect(result.error).toMatch(/invalid/i); }); // ── Branch coverage: HEIF content format input ───────────────────── diff --git a/tests/integration/watermark-image.test.ts b/tests/integration/watermark-image.test.ts index 9aca327d..369d12d2 100644 --- a/tests/integration/watermark-image.test.ts +++ b/tests/integration/watermark-image.test.ts @@ -226,9 +226,9 @@ describe("watermark-image", () => { body, }); - expect(res.statusCode).toBe(422); + expect(res.statusCode).toBe(400); const json = JSON.parse(res.body); - expect(json.error).toContain("Processing failed"); + expect(json.error).toMatch(/invalid/i); }); // ── HEIC input handling ─────────────────────────────────────────── @@ -353,9 +353,9 @@ describe("watermark-image", () => { body, }); - expect(res.statusCode).toBe(422); + expect(res.statusCode).toBe(400); const json = JSON.parse(res.body); - expect(json.error).toContain("Processing failed"); + expect(json.error).toMatch(/invalid/i); }); // ── Tiny 1x1 main image ──────────────────────────────────────────