Files
SnapOtter/tests/integration/sharpening.test.ts
T
ashim-hq babca4cf97 test: comprehensive test coverage expansion (+965 tests)
Add 42 new test files covering all untested tool routes, image engine
internals, AI sidecar bridge, Zustand stores, and cross-format
compatibility. Expand e2e-docker suite with 7 spec files covering all
48 tools against a real Docker container.

Unit tests:
- Image engine: format detection, MIME mapping, metadata parsing, pipeline
- AI bridge: sidecar lifecycle, all 11 tool functions (mocked)
- Web stores: 14 Zustand stores (collage, settings, features, analytics, etc.)
- API helpers: format decoders, page range, file validation

Integration tests:
- 25 tool routes that had zero dedicated tests
- Cross-format matrix: 17 input formats x 3 tools
- Edge cases: zero-byte files, corrupted headers, path traversal, XSS, SQL injection
- Concurrent request handling and pipeline edge cases

E2E-Docker (Playwright against real container):
- 7 spec files: essential, adjustment, conversion, creative, utility, AI, pipeline
- Custom buildMultipart helper for multi-file tool uploads
- AI tools gracefully skip when sidecar not installed

Fixtures:
- Organized test media: formats/ (18 formats) + content/ (17 content types)
- Reduced from 3.1 GB unorganized samples to 33 MB structured fixtures

Bug fix:
- color-adjustments: gamma exposure used invalid single-param gamma() for
  positive values; fixed to use two-param gamma(gammaIn, gammaOut) form
2026-04-23 17:12:02 +08:00

247 lines
8.2 KiB
TypeScript

/**
* 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<string, unknown>,
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<string, unknown>,
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);
});
});