mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
test: expand coverage to 3,382 tests across all layers
- Unit: 1,353 tests (42 files) — +256 new tests covering AI bridge modules, image-engine sharpen/optimize-for-web, Zustand stores, and icon-map validation - Integration: 1,640 tests (57 files) — +826 new tests across all tool routes, pipeline/progress/batch infrastructure, user-files, edit-metadata, and a 321-test cross-format matrix - E2E-Docker: 389 passing (20 spec files) — 6 new spec files for batch processing, format conversion, layout, optimization, watermark/overlay, and pipeline chains. Tests verified against fresh Docker container with all 6 AI bundles installed. Bug fixes discovered during testing: - fix(compress): SVG/BMP/exotic formats crashed Sharp encoder — added format-safety fallback to PNG - fix(rate-limit): increase default login attempt limit from 10 to 500 per minute — previous value caused false test failures and is too restrictive for a self-hosted app - fix(auth.setup): wait for consent button visibility before clicking to prevent flaky E2E-Docker auth setup
This commit is contained in:
@@ -193,4 +193,276 @@ describe("image-to-base64", () => {
|
||||
const json = JSON.parse(res.body);
|
||||
expect(json.error).toContain("Invalid settings");
|
||||
});
|
||||
|
||||
// ── Output format: webp ──────────────────────────────────────────
|
||||
it("converts to webp output format", async () => {
|
||||
const { body, contentType } = createMultipartPayload([
|
||||
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
|
||||
{ name: "settings", content: JSON.stringify({ outputFormat: "webp" }) },
|
||||
]);
|
||||
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/v1/tools/image-to-base64",
|
||||
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
||||
body,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
const json = JSON.parse(res.body);
|
||||
expect(json.results[0].mimeType).toBe("image/webp");
|
||||
expect(json.results[0].dataUri).toMatch(/^data:image\/webp;base64,/);
|
||||
});
|
||||
|
||||
// ── Output format: avif ──────────────────────────────────────────
|
||||
it("converts to avif output format", async () => {
|
||||
const { body, contentType } = createMultipartPayload([
|
||||
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
|
||||
{ name: "settings", content: JSON.stringify({ outputFormat: "avif" }) },
|
||||
]);
|
||||
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/v1/tools/image-to-base64",
|
||||
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
||||
body,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
const json = JSON.parse(res.body);
|
||||
expect(json.results[0].mimeType).toBe("image/avif");
|
||||
expect(json.results[0].dataUri).toMatch(/^data:image\/avif;base64,/);
|
||||
});
|
||||
|
||||
// ── Output format: png ───────────────────────────────────────────
|
||||
it("converts to png output format", async () => {
|
||||
const { body, contentType } = createMultipartPayload([
|
||||
{ name: "file", filename: "test.jpg", contentType: "image/jpeg", content: JPG },
|
||||
{ name: "settings", content: JSON.stringify({ outputFormat: "png" }) },
|
||||
]);
|
||||
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/v1/tools/image-to-base64",
|
||||
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
||||
body,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
const json = JSON.parse(res.body);
|
||||
expect(json.results[0].mimeType).toBe("image/png");
|
||||
});
|
||||
|
||||
// ── Quality affects encoded size ─────────────────────────────────
|
||||
it("lower quality produces smaller encoded size for jpeg", async () => {
|
||||
const largeJPG = readFileSync(join(FIXTURES, "content", "portrait-color.jpg"));
|
||||
const makeRequest = async (quality: number) => {
|
||||
const { body, contentType } = createMultipartPayload([
|
||||
{ name: "file", filename: "test.jpg", contentType: "image/jpeg", content: largeJPG },
|
||||
{ name: "settings", content: JSON.stringify({ outputFormat: "jpeg", quality }) },
|
||||
]);
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/v1/tools/image-to-base64",
|
||||
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
||||
body,
|
||||
});
|
||||
return JSON.parse(res.body);
|
||||
};
|
||||
|
||||
const highQ = await makeRequest(95);
|
||||
const lowQ = await makeRequest(10);
|
||||
expect(lowQ.results[0].encodedSize).toBeLessThan(highQ.results[0].encodedSize);
|
||||
});
|
||||
|
||||
// ── Both maxWidth and maxHeight combined ─────────────────────────
|
||||
it("applies maxWidth and maxHeight together", async () => {
|
||||
const { body, contentType } = createMultipartPayload([
|
||||
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
|
||||
{ name: "settings", content: JSON.stringify({ maxWidth: 80, maxHeight: 60 }) },
|
||||
]);
|
||||
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/v1/tools/image-to-base64",
|
||||
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
||||
body,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
const json = JSON.parse(res.body);
|
||||
expect(json.results[0].width).toBeLessThanOrEqual(80);
|
||||
expect(json.results[0].height).toBeLessThanOrEqual(60);
|
||||
});
|
||||
|
||||
// ── WebP input preserves format in original mode ─────────────────
|
||||
it("preserves WebP format in original mode", async () => {
|
||||
const WEBP = readFileSync(join(FIXTURES, "test-50x50.webp"));
|
||||
const { body, contentType } = createMultipartPayload([
|
||||
{ name: "file", filename: "test.webp", contentType: "image/webp", content: WEBP },
|
||||
{ name: "settings", content: JSON.stringify({}) },
|
||||
]);
|
||||
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/v1/tools/image-to-base64",
|
||||
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
||||
body,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
const json = JSON.parse(res.body);
|
||||
expect(json.results[0].mimeType).toBe("image/webp");
|
||||
expect(json.results[0].width).toBe(50);
|
||||
expect(json.results[0].height).toBe(50);
|
||||
});
|
||||
|
||||
// ── HEIC input gets converted to JPEG in original mode ───────────
|
||||
it("converts HEIC to JPEG in original mode", async () => {
|
||||
const HEIC = readFileSync(join(FIXTURES, "test-200x150.heic"));
|
||||
const { body, contentType } = createMultipartPayload([
|
||||
{ name: "file", filename: "photo.heic", contentType: "image/heic", content: HEIC },
|
||||
{ name: "settings", content: JSON.stringify({}) },
|
||||
]);
|
||||
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/v1/tools/image-to-base64",
|
||||
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
||||
body,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
const json = JSON.parse(res.body);
|
||||
expect(json.results[0].mimeType).toBe("image/jpeg");
|
||||
expect(json.results[0].width).toBeGreaterThan(0);
|
||||
expect(json.results[0].height).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
// ── overheadPercent is calculated correctly ───────────────────────
|
||||
it("overheadPercent reflects base64 expansion", async () => {
|
||||
const { body, contentType } = createMultipartPayload([
|
||||
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
|
||||
{ name: "settings", content: JSON.stringify({}) },
|
||||
]);
|
||||
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/v1/tools/image-to-base64",
|
||||
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
||||
body,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
const json = JSON.parse(res.body);
|
||||
const r = json.results[0];
|
||||
// Base64 always expands data, so overhead should be positive
|
||||
expect(r.overheadPercent).toBeGreaterThan(0);
|
||||
// Verify the calculation: (encodedSize - originalSize) / originalSize * 100
|
||||
const expected = Math.round(((r.encodedSize - r.originalSize) / r.originalSize) * 1000) / 10;
|
||||
expect(r.overheadPercent).toBe(expected);
|
||||
});
|
||||
|
||||
// ── Reject quality above 100 ──────────────────────────────────────
|
||||
it("rejects quality above 100", async () => {
|
||||
const { body, contentType } = createMultipartPayload([
|
||||
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
|
||||
{ name: "settings", content: JSON.stringify({ quality: 101 }) },
|
||||
]);
|
||||
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/v1/tools/image-to-base64",
|
||||
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
||||
body,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(400);
|
||||
});
|
||||
|
||||
// ── maxWidth=0 means no resize ────────────────────────────────────
|
||||
it("maxWidth=0 means no width resize (pass-through)", async () => {
|
||||
const { body, contentType } = createMultipartPayload([
|
||||
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
|
||||
{ name: "settings", content: JSON.stringify({ maxWidth: 0 }) },
|
||||
]);
|
||||
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/v1/tools/image-to-base64",
|
||||
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
||||
body,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
const json = JSON.parse(res.body);
|
||||
expect(json.results[0].width).toBe(200);
|
||||
expect(json.results[0].height).toBe(150);
|
||||
});
|
||||
|
||||
// ── withoutEnlargement: maxWidth larger than image ────────────────
|
||||
it("does not enlarge when maxWidth exceeds image width", async () => {
|
||||
const { body, contentType } = createMultipartPayload([
|
||||
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
|
||||
{ name: "settings", content: JSON.stringify({ maxWidth: 9999 }) },
|
||||
]);
|
||||
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/v1/tools/image-to-base64",
|
||||
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
||||
body,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
const json = JSON.parse(res.body);
|
||||
// Should not enlarge beyond original dimensions
|
||||
expect(json.results[0].width).toBe(200);
|
||||
expect(json.results[0].height).toBe(150);
|
||||
});
|
||||
|
||||
// ── SVG input passthrough in original mode ────────────────────────
|
||||
it("passes through SVG without conversion in original mode", async () => {
|
||||
const SVG = readFileSync(join(FIXTURES, "test-100x100.svg"));
|
||||
const { body, contentType } = createMultipartPayload([
|
||||
{ name: "file", filename: "icon.svg", contentType: "image/svg+xml", content: SVG },
|
||||
{ name: "settings", content: JSON.stringify({}) },
|
||||
]);
|
||||
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/v1/tools/image-to-base64",
|
||||
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
||||
body,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
const json = JSON.parse(res.body);
|
||||
expect(json.results[0].mimeType).toBe("image/svg+xml");
|
||||
expect(json.results[0].dataUri).toMatch(/^data:image\/svg\+xml;base64,/);
|
||||
});
|
||||
|
||||
// ── Resize + format conversion combined ───────────────────────────
|
||||
it("applies resize and format conversion together", async () => {
|
||||
const { body, contentType } = createMultipartPayload([
|
||||
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
|
||||
{
|
||||
name: "settings",
|
||||
content: JSON.stringify({ outputFormat: "jpeg", maxWidth: 50, maxHeight: 50 }),
|
||||
},
|
||||
]);
|
||||
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/v1/tools/image-to-base64",
|
||||
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
||||
body,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
const json = JSON.parse(res.body);
|
||||
expect(json.results[0].mimeType).toBe("image/jpeg");
|
||||
expect(json.results[0].width).toBeLessThanOrEqual(50);
|
||||
expect(json.results[0].height).toBeLessThanOrEqual(50);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user