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:
SnapOtter
2026-04-24 22:43:14 +08:00
parent bc82781282
commit 7f62bc32db
48 changed files with 13121 additions and 154 deletions
+477
View File
@@ -237,4 +237,481 @@ describe("svg-to-raster", () => {
expect(res.statusCode).toBe(400);
});
// ── Extended coverage: size, DPI, bg, formats, batch, complex SVGs ─
it("respects both width and height together", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "test.svg", contentType: "image/svg+xml", content: SVG },
{ name: "settings", content: JSON.stringify({ width: 300, height: 300 }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/svg-to-raster",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(200);
const json = JSON.parse(res.body);
const dlRes = await app.inject({
method: "GET",
url: json.downloadUrl,
headers: { authorization: `Bearer ${adminToken}` },
});
const meta = await sharp(Buffer.from(dlRes.rawPayload)).metadata();
// fit: inside means both w and h <= requested
expect(meta.width).toBeLessThanOrEqual(300);
expect(meta.height).toBeLessThanOrEqual(300);
});
it("uses high DPI (600) for detailed rendering", async () => {
const { body: body72, contentType: ct72 } = createMultipartPayload([
{ name: "file", filename: "test.svg", contentType: "image/svg+xml", content: SVG },
{ name: "settings", content: JSON.stringify({ dpi: 72 }) },
]);
const { body: body600, contentType: ct600 } = createMultipartPayload([
{ name: "file", filename: "test.svg", contentType: "image/svg+xml", content: SVG },
{ name: "settings", content: JSON.stringify({ dpi: 600 }) },
]);
const res72 = await app.inject({
method: "POST",
url: "/api/v1/tools/svg-to-raster",
headers: { authorization: `Bearer ${adminToken}`, "content-type": ct72 },
body: body72,
});
const res600 = await app.inject({
method: "POST",
url: "/api/v1/tools/svg-to-raster",
headers: { authorization: `Bearer ${adminToken}`, "content-type": ct600 },
body: body600,
});
expect(res72.statusCode).toBe(200);
expect(res600.statusCode).toBe(200);
const dl72 = await app.inject({
method: "GET",
url: JSON.parse(res72.body).downloadUrl,
headers: { authorization: `Bearer ${adminToken}` },
});
const dl600 = await app.inject({
method: "GET",
url: JSON.parse(res600.body).downloadUrl,
headers: { authorization: `Bearer ${adminToken}` },
});
const meta72 = await sharp(Buffer.from(dl72.rawPayload)).metadata();
const meta600 = await sharp(Buffer.from(dl600.rawPayload)).metadata();
// 600 DPI should produce a larger image than 72 DPI
expect((meta600.width ?? 0) * (meta600.height ?? 0)).toBeGreaterThan(
(meta72.width ?? 0) * (meta72.height ?? 0),
);
});
it("applies opaque background color and flattens alpha", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "test.svg", contentType: "image/svg+xml", content: SVG },
{ name: "settings", content: JSON.stringify({ backgroundColor: "#00FF00" }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/svg-to-raster",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(200);
const json = JSON.parse(res.body);
expect(json.processedSize).toBeGreaterThan(0);
});
it("converts to tiff format", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "test.svg", contentType: "image/svg+xml", content: SVG },
{ name: "settings", content: JSON.stringify({ outputFormat: "tiff" }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/svg-to-raster",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(200);
const json = JSON.parse(res.body);
expect(json.downloadUrl).toBeDefined();
// TIFF is non-previewable, so previewUrl should exist
expect(json.previewUrl).toBeDefined();
});
it("converts to gif format", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "test.svg", contentType: "image/svg+xml", content: SVG },
{ name: "settings", content: JSON.stringify({ outputFormat: "gif" }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/svg-to-raster",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(200);
const json = JSON.parse(res.body);
const dlRes = await app.inject({
method: "GET",
url: json.downloadUrl,
headers: { authorization: `Bearer ${adminToken}` },
});
const meta = await sharp(Buffer.from(dlRes.rawPayload)).metadata();
expect(meta.format).toBe("gif");
});
it("converts to avif format", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "test.svg", contentType: "image/svg+xml", content: SVG },
{ name: "settings", content: JSON.stringify({ outputFormat: "avif" }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/svg-to-raster",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(200);
const json = JSON.parse(res.body);
const dlRes = await app.inject({
method: "GET",
url: json.downloadUrl,
headers: { authorization: `Bearer ${adminToken}` },
});
const meta = await sharp(Buffer.from(dlRes.rawPayload)).metadata();
expect(meta.format).toBe("heif");
});
it("converts a complex SVG with gradients and filters", async () => {
const COMPLEX_SVG = readFileSync(join(FIXTURES, "content", "svg-logo.svg"));
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "logo.svg", contentType: "image/svg+xml", content: COMPLEX_SVG },
{ name: "settings", content: JSON.stringify({ width: 800 }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/svg-to-raster",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(200);
const json = JSON.parse(res.body);
expect(json.downloadUrl).toBeDefined();
expect(json.processedSize).toBeGreaterThan(0);
});
it("converts a QR code SVG (complex path)", async () => {
const QR_SVG = readFileSync(join(FIXTURES, "content", "qr-code.svg"));
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "qr.svg", contentType: "image/svg+xml", content: QR_SVG },
{ name: "settings", content: JSON.stringify({ outputFormat: "png", dpi: 300 }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/svg-to-raster",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(200);
const json = JSON.parse(res.body);
const dlRes = await app.inject({
method: "GET",
url: json.downloadUrl,
headers: { authorization: `Bearer ${adminToken}` },
});
const meta = await sharp(Buffer.from(dlRes.rawPayload)).metadata();
expect(meta.format).toBe("png");
expect(meta.width).toBeGreaterThan(0);
});
it("combines width, DPI, background color, and quality", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "test.svg", contentType: "image/svg+xml", content: SVG },
{
name: "settings",
content: JSON.stringify({
width: 500,
dpi: 150,
backgroundColor: "#FFFFFF",
outputFormat: "jpg",
quality: 95,
}),
},
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/svg-to-raster",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(200);
const json = JSON.parse(res.body);
const dlRes = await app.inject({
method: "GET",
url: json.downloadUrl,
headers: { authorization: `Bearer ${adminToken}` },
});
const meta = await sharp(Buffer.from(dlRes.rawPayload)).metadata();
expect(meta.format).toBe("jpeg");
expect(meta.width).toBeLessThanOrEqual(500);
});
it("strips .svg from output filename", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "my-icon.svg", contentType: "image/svg+xml", content: SVG },
{ name: "settings", content: JSON.stringify({ outputFormat: "png" }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/svg-to-raster",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(200);
const json = JSON.parse(res.body);
// downloadUrl should contain my-icon.png, not my-icon.svg.png
expect(json.downloadUrl).toContain("my-icon.png");
expect(json.downloadUrl).not.toContain(".svg.png");
});
it("returns originalSize and processedSize", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "test.svg", contentType: "image/svg+xml", content: SVG },
{ name: "settings", content: JSON.stringify({}) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/svg-to-raster",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(200);
const json = JSON.parse(res.body);
expect(json.originalSize).toBeGreaterThan(0);
expect(json.processedSize).toBeGreaterThan(0);
expect(json.jobId).toBeDefined();
});
it("rejects DPI above maximum (2400)", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "test.svg", contentType: "image/svg+xml", content: SVG },
{ name: "settings", content: JSON.stringify({ dpi: 3000 }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/svg-to-raster",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(400);
});
it("rejects invalid JSON in settings", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "test.svg", contentType: "image/svg+xml", content: SVG },
{ name: "settings", content: "{{bad json" },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/svg-to-raster",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(400);
const json = JSON.parse(res.body);
expect(json.error).toMatch(/json/i);
});
it("rejects unauthenticated requests", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "test.svg", contentType: "image/svg+xml", content: SVG },
{ name: "settings", content: JSON.stringify({}) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/svg-to-raster",
headers: { "content-type": contentType },
body,
});
expect(res.statusCode).toBe(401);
});
// ── Batch endpoint coverage ────────────────────────────────────────
describe("batch", () => {
it("converts multiple SVGs in a single batch request", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "a.svg", contentType: "image/svg+xml", content: SVG },
{ name: "file", filename: "b.svg", contentType: "image/svg+xml", content: SVG },
{ name: "settings", content: JSON.stringify({ outputFormat: "png" }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/svg-to-raster/batch",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(200);
expect(res.headers["content-type"]).toBe("application/zip");
});
it("rejects batch with no SVG files", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "settings", content: JSON.stringify({}) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/svg-to-raster/batch",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(400);
const json = JSON.parse(res.body);
expect(json.error).toMatch(/no svg/i);
});
it("handles a batch with non-SVG files (skips them, processes valid ones)", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "good.svg", contentType: "image/svg+xml", content: SVG },
{ name: "file", filename: "bad.png", contentType: "image/png", content: PNG },
{ name: "settings", content: JSON.stringify({ outputFormat: "png" }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/svg-to-raster/batch",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
// Should still succeed -- one valid SVG processed, one non-SVG skipped
expect(res.statusCode).toBe(200);
expect(res.headers["content-type"]).toBe("application/zip");
});
it("returns 422 when all batch files fail", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "bad.png", contentType: "image/png", content: PNG },
{ name: "settings", content: JSON.stringify({}) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/svg-to-raster/batch",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(422);
const json = JSON.parse(res.body);
expect(json.error).toMatch(/all files failed/i);
});
it("batch converts complex SVGs to webp format", async () => {
const COMPLEX_SVG = readFileSync(join(FIXTURES, "content", "svg-logo.svg"));
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "logo.svg", contentType: "image/svg+xml", content: COMPLEX_SVG },
{ name: "file", filename: "simple.svg", contentType: "image/svg+xml", content: SVG },
{ name: "settings", content: JSON.stringify({ outputFormat: "webp", quality: 80 }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/svg-to-raster/batch",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(200);
expect(res.headers["content-type"]).toBe("application/zip");
expect(res.headers["x-job-id"]).toBeDefined();
});
it("batch accepts clientJobId", async () => {
const clientJobId = "test-client-job-123";
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "a.svg", contentType: "image/svg+xml", content: SVG },
{ name: "settings", content: JSON.stringify({}) },
{ name: "clientJobId", content: clientJobId },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/svg-to-raster/batch",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(200);
expect(res.headers["x-job-id"]).toBe(clientJobId);
});
it("batch rejects invalid settings", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "a.svg", contentType: "image/svg+xml", content: SVG },
{ name: "settings", content: JSON.stringify({ dpi: 1 }) },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/svg-to-raster/batch",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(400);
});
it("batch rejects invalid JSON in settings", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "a.svg", contentType: "image/svg+xml", content: SVG },
{ name: "settings", content: "{{not json" },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/svg-to-raster/batch",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
body,
});
expect(res.statusCode).toBe(400);
const json = JSON.parse(res.body);
expect(json.error).toMatch(/json/i);
});
});
});