/** * Integration tests for the sharpening tool. * * Tests all three sharpening methods (adaptive, unsharp-mask, high-pass), * custom parameters, denoise option, and format support. */ import { readFileSync } from "node:fs"; import { join } from "node:path"; import sharp from "sharp"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { buildTestApp, createMultipartPayload, loginAsAdmin, type TestApp } from "./test-server.js"; const FIXTURES = join(__dirname, "..", "fixtures"); const PNG = readFileSync(join(FIXTURES, "test-200x150.png")); const JPG = readFileSync(join(FIXTURES, "test-100x100.jpg")); const WEBP = readFileSync(join(FIXTURES, "test-50x50.webp")); 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); function makePayload( settings: Record, buffer: Buffer = PNG, filename = "test.png", contentType = "image/png", ) { return createMultipartPayload([ { name: "file", filename, contentType, content: buffer }, { name: "settings", content: JSON.stringify(settings) }, ]); } async function postTool( settings: Record, buffer?: Buffer, filename?: string, ct?: string, ) { const { body: payload, contentType } = makePayload(settings, buffer, filename, ct); return app.inject({ method: "POST", url: "/api/v1/tools/sharpening", payload, headers: { "content-type": contentType, authorization: `Bearer ${adminToken}`, }, }); } // ── Adaptive method (default) ───────────────────────────────────── describe("Adaptive sharpening", () => { it("sharpens with default settings", async () => { const res = await postTool({}); expect(res.statusCode).toBe(200); const result = JSON.parse(res.body); expect(result.downloadUrl).toBeDefined(); expect(result.processedSize).toBeGreaterThan(0); }); it("sharpens with custom sigma", async () => { const res = await postTool({ method: "adaptive", sigma: 3.0 }); expect(res.statusCode).toBe(200); const result = JSON.parse(res.body); expect(result.downloadUrl).toBeDefined(); }); it("sharpens with all adaptive parameters", async () => { const res = await postTool({ method: "adaptive", sigma: 2.0, m1: 2.0, m2: 5.0, x1: 3.0, y2: 15, y3: 25, }); expect(res.statusCode).toBe(200); const result = JSON.parse(res.body); expect(result.downloadUrl).toBeDefined(); }); }); // ── Unsharp mask method ─────────────────────────────────────────── describe("Unsharp mask sharpening", () => { it("sharpens with unsharp-mask method", async () => { const res = await postTool({ method: "unsharp-mask" }); expect(res.statusCode).toBe(200); const result = JSON.parse(res.body); expect(result.downloadUrl).toBeDefined(); }); it("sharpens with custom amount and radius", async () => { const res = await postTool({ method: "unsharp-mask", amount: 200, radius: 2.5, threshold: 10, }); expect(res.statusCode).toBe(200); const result = JSON.parse(res.body); expect(result.downloadUrl).toBeDefined(); }); }); // ── High-pass method ────────────────────────────────────────────── describe("High-pass sharpening", () => { it("sharpens with high-pass method", async () => { const res = await postTool({ method: "high-pass" }); expect(res.statusCode).toBe(200); const result = JSON.parse(res.body); expect(result.downloadUrl).toBeDefined(); }); it("sharpens with custom strength and kernel size 5", async () => { const res = await postTool({ method: "high-pass", strength: 80, kernelSize: 5 }); expect(res.statusCode).toBe(200); const result = JSON.parse(res.body); expect(result.downloadUrl).toBeDefined(); }); }); // ── Denoise option ──────────────────────────────────────────────── describe("Denoise option", () => { it("applies light denoise", async () => { const res = await postTool({ denoise: "light" }); expect(res.statusCode).toBe(200); }); it("applies medium denoise", async () => { const res = await postTool({ denoise: "medium" }); expect(res.statusCode).toBe(200); }); it("applies strong denoise", async () => { const res = await postTool({ denoise: "strong" }); expect(res.statusCode).toBe(200); }); }); // ── Output verification ────────────────────────────────────────── describe("Output verification", () => { it("output differs from input (sharpening changes data)", async () => { const res = await postTool({ method: "adaptive", sigma: 5.0 }); expect(res.statusCode).toBe(200); const result = JSON.parse(res.body); const dlRes = await app.inject({ method: "GET", url: result.downloadUrl, headers: { authorization: `Bearer ${adminToken}` }, }); expect(dlRes.statusCode).toBe(200); // Sharpened output should not be identical to input expect(Buffer.compare(dlRes.rawPayload, PNG)).not.toBe(0); }); it("preserves image dimensions", async () => { const res = await postTool({ method: "unsharp-mask", amount: 300 }); expect(res.statusCode).toBe(200); const result = JSON.parse(res.body); const dlRes = await app.inject({ method: "GET", url: result.downloadUrl, headers: { authorization: `Bearer ${adminToken}` }, }); const meta = await sharp(dlRes.rawPayload).metadata(); expect(meta.width).toBe(200); expect(meta.height).toBe(150); }); }); // ── Format support ──────────────────────────────────────────────── describe("Multiple input formats", () => { it("processes JPEG input", async () => { const res = await postTool({ method: "adaptive" }, JPG, "test.jpg", "image/jpeg"); expect(res.statusCode).toBe(200); const result = JSON.parse(res.body); expect(result.downloadUrl).toBeDefined(); }); it("processes WebP input", async () => { const res = await postTool({ method: "adaptive" }, WEBP, "test.webp", "image/webp"); expect(res.statusCode).toBe(200); const result = JSON.parse(res.body); expect(result.downloadUrl).toBeDefined(); }); }); // ── Error handling ──────────────────────────────────────────────── describe("Error handling", () => { it("returns 400 when no file is provided", async () => { const { body: payload, contentType } = createMultipartPayload([ { name: "settings", content: JSON.stringify({ method: "adaptive" }) }, ]); const res = await app.inject({ method: "POST", url: "/api/v1/tools/sharpening", payload, headers: { "content-type": contentType, authorization: `Bearer ${adminToken}`, }, }); expect(res.statusCode).toBe(400); }); it("returns 400 for invalid method", async () => { const res = await postTool({ method: "gaussian" }); expect(res.statusCode).toBe(400); }); it("returns 400 for sigma out of range (too low)", async () => { const res = await postTool({ sigma: 0.1 }); expect(res.statusCode).toBe(400); }); it("returns 400 for sigma out of range (too high)", async () => { const res = await postTool({ sigma: 50 }); expect(res.statusCode).toBe(400); }); it("returns 400 for invalid kernelSize", async () => { const res = await postTool({ method: "high-pass", kernelSize: 7 }); expect(res.statusCode).toBe(400); }); it("returns 400 for invalid denoise value", async () => { const res = await postTool({ denoise: "extreme" }); expect(res.statusCode).toBe(400); }); });