Files
SnapOtter/tests/integration/sharpening.test.ts
T
SnapOtter 733ebe8010 test: major coverage expansion — 18 new test files, ~830 new tests
Unit tests: 1354 → 1781 (+427)
- 11 new AI bridge module tests (packages/ai/ from 2/13 → 13/13 files)
- files-page-store (0% → full), pdf-to-image-store, features-store expanded
- saturation and edit-metadata image-engine operations
- analytics route, features route, web analytics lib, api-extended

Integration tests: ~2070 → 2320 (+250)
- 31 integration files expanded with branch-coverage-targeted tests
- progress.ts SSE endpoints (28% → comprehensive, +18 tests)
- gif-tools all modes (+18), pdf-to-image format variants (+13)
- Cross-format matrix expanded to 17 tools × 17 formats (467 tests)
- Adversarial: concurrent, memory pressure, unicode filenames, pipeline limits

E2E-Docker: +1020 lines across 6 spec files
- Info, colors, sharpening, base64, QR read, JXL/ICO/SVG formats
- Strip-metadata, image-enhancement, content-aware-resize expanded
- Batch pipelines, multi-format batches, HEIC input coverage
2026-04-26 12:03:08 +08:00

304 lines
10 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);
});
});
// ── HEIC input handling ─────────────────────────────────────────
describe("HEIC input", () => {
it("processes HEIC image with adaptive sharpening", async () => {
const HEIC = readFileSync(join(FIXTURES, "test-200x150.heic"));
const res = await postTool({ method: "adaptive" }, HEIC, "photo.heic", "image/heic");
expect(res.statusCode).toBe(200);
const result = JSON.parse(res.body);
expect(result.downloadUrl).toBeDefined();
});
});
// ── Edge size inputs ────────────────────────────────────────────
describe("Edge size inputs", () => {
it("processes 1x1 pixel image", async () => {
const TINY = readFileSync(join(FIXTURES, "test-1x1.png"));
const res = await postTool({ method: "adaptive" }, TINY, "tiny.png", "image/png");
expect(res.statusCode).toBe(200);
});
it("processes stress-large.jpg", async () => {
const LARGE = readFileSync(join(FIXTURES, "content", "stress-large.jpg"));
const res = await postTool(
{ method: "unsharp-mask", amount: 150 },
LARGE,
"large.jpg",
"image/jpeg",
);
expect(res.statusCode).toBe(200);
const result = JSON.parse(res.body);
expect(result.processedSize).toBeGreaterThan(0);
});
});
// ── Combined sharpening + denoise ───────────────────────────────
describe("Combined sharpening + denoise", () => {
it("applies high-pass sharpening with strong denoise", async () => {
const res = await postTool({
method: "high-pass",
strength: 80,
kernelSize: 5,
denoise: "strong",
});
expect(res.statusCode).toBe(200);
const result = JSON.parse(res.body);
expect(result.downloadUrl).toBeDefined();
});
it("applies adaptive sharpening with light denoise", async () => {
const res = await postTool({
method: "adaptive",
sigma: 1.5,
denoise: "light",
});
expect(res.statusCode).toBe(200);
});
});