mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
244 lines
7.7 KiB
TypeScript
244 lines
7.7 KiB
TypeScript
/**
|
|||
|
|
* Integration tests for the vectorize tool.
|
||
|
|
*
|
||
|
|
* Converts raster images to SVG via potrace (black-and-white) or
|
||
|
|
* @neplex/vectorizer (color). Custom route with settings for colorMode,
|
||
|
|
* threshold, path mode, and more.
|
||
|
|
*/
|
||
|
|
|
||
|
|
import { readFileSync } from "node:fs";
|
||
|
|
import { join } from "node:path";
|
||
|
|
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 SMALL_PNG = readFileSync(join(FIXTURES, "test-1x1.png"));
|
||
|
|
|
||
|
|
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);
|
||
|
|
|
||
|
|
describe("vectorize", () => {
|
||
|
|
it("vectorizes a PNG with default settings (bw mode)", 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/vectorize",
|
||
|
|
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.jobId).toBeDefined();
|
||
|
|
expect(json.processedSize).toBeGreaterThan(0);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("output is valid SVG", 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/vectorize",
|
||
|
|
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}` },
|
||
|
|
});
|
||
|
|
expect(dlRes.statusCode).toBe(200);
|
||
|
|
const svgContent = dlRes.rawPayload.toString("utf-8");
|
||
|
|
expect(svgContent).toContain("<svg");
|
||
|
|
expect(svgContent).toContain("</svg>");
|
||
|
|
});
|
||
|
|
|
||
|
|
it("vectorizes in color mode", async () => {
|
||
|
|
const { body, contentType } = createMultipartPayload([
|
||
|
|
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
|
||
|
|
{ name: "settings", content: JSON.stringify({ colorMode: "color" }) },
|
||
|
|
]);
|
||
|
|
|
||
|
|
const res = await app.inject({
|
||
|
|
method: "POST",
|
||
|
|
url: "/api/v1/tools/vectorize",
|
||
|
|
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 svgContent = dlRes.rawPayload.toString("utf-8");
|
||
|
|
expect(svgContent).toContain("<svg");
|
||
|
|
});
|
||
|
|
|
||
|
|
it("respects custom threshold in bw mode", async () => {
|
||
|
|
const { body, contentType } = createMultipartPayload([
|
||
|
|
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
|
||
|
|
{ name: "settings", content: JSON.stringify({ colorMode: "bw", threshold: 200 }) },
|
||
|
|
]);
|
||
|
|
|
||
|
|
const res = await app.inject({
|
||
|
|
method: "POST",
|
||
|
|
url: "/api/v1/tools/vectorize",
|
||
|
|
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
||
|
|
body,
|
||
|
|
});
|
||
|
|
|
||
|
|
expect(res.statusCode).toBe(200);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("supports invert option", async () => {
|
||
|
|
const { body, contentType } = createMultipartPayload([
|
||
|
|
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
|
||
|
|
{ name: "settings", content: JSON.stringify({ invert: true }) },
|
||
|
|
]);
|
||
|
|
|
||
|
|
const res = await app.inject({
|
||
|
|
method: "POST",
|
||
|
|
url: "/api/v1/tools/vectorize",
|
||
|
|
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
||
|
|
body,
|
||
|
|
});
|
||
|
|
|
||
|
|
expect(res.statusCode).toBe(200);
|
||
|
|
});
|
||
|
|
|
||
|
|
it.each(["none", "polygon", "spline"] as const)("supports pathMode: %s", async (pathMode) => {
|
||
|
|
const { body, contentType } = createMultipartPayload([
|
||
|
|
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
|
||
|
|
{ name: "settings", content: JSON.stringify({ pathMode }) },
|
||
|
|
]);
|
||
|
|
|
||
|
|
const res = await app.inject({
|
||
|
|
method: "POST",
|
||
|
|
url: "/api/v1/tools/vectorize",
|
||
|
|
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
||
|
|
body,
|
||
|
|
});
|
||
|
|
|
||
|
|
expect(res.statusCode).toBe(200);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("respects color mode settings (colorPrecision, layerDifference)", async () => {
|
||
|
|
const { body, contentType } = createMultipartPayload([
|
||
|
|
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
|
||
|
|
{
|
||
|
|
name: "settings",
|
||
|
|
content: JSON.stringify({
|
||
|
|
colorMode: "color",
|
||
|
|
colorPrecision: 4,
|
||
|
|
layerDifference: 16,
|
||
|
|
filterSpeckle: 8,
|
||
|
|
cornerThreshold: 90,
|
||
|
|
}),
|
||
|
|
},
|
||
|
|
]);
|
||
|
|
|
||
|
|
const res = await app.inject({
|
||
|
|
method: "POST",
|
||
|
|
url: "/api/v1/tools/vectorize",
|
||
|
|
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
||
|
|
body,
|
||
|
|
});
|
||
|
|
|
||
|
|
expect(res.statusCode).toBe(200);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("download URL filename ends with .svg", async () => {
|
||
|
|
const { body, contentType } = createMultipartPayload([
|
||
|
|
{ name: "file", filename: "myimage.png", contentType: "image/png", content: PNG },
|
||
|
|
{ name: "settings", content: JSON.stringify({}) },
|
||
|
|
]);
|
||
|
|
|
||
|
|
const res = await app.inject({
|
||
|
|
method: "POST",
|
||
|
|
url: "/api/v1/tools/vectorize",
|
||
|
|
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
||
|
|
body,
|
||
|
|
});
|
||
|
|
|
||
|
|
expect(res.statusCode).toBe(200);
|
||
|
|
const json = JSON.parse(res.body);
|
||
|
|
expect(json.downloadUrl).toMatch(/\.svg$/);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("rejects request without a file", async () => {
|
||
|
|
const { body, contentType } = createMultipartPayload([
|
||
|
|
{ name: "settings", content: JSON.stringify({}) },
|
||
|
|
]);
|
||
|
|
|
||
|
|
const res = await app.inject({
|
||
|
|
method: "POST",
|
||
|
|
url: "/api/v1/tools/vectorize",
|
||
|
|
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
||
|
|
body,
|
||
|
|
});
|
||
|
|
|
||
|
|
expect(res.statusCode).toBe(400);
|
||
|
|
const json = JSON.parse(res.body);
|
||
|
|
expect(json.error).toContain("No image file");
|
||
|
|
});
|
||
|
|
|
||
|
|
it("rejects invalid settings JSON", async () => {
|
||
|
|
const { body, contentType } = createMultipartPayload([
|
||
|
|
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
|
||
|
|
{ name: "settings", content: "not-json" },
|
||
|
|
]);
|
||
|
|
|
||
|
|
const res = await app.inject({
|
||
|
|
method: "POST",
|
||
|
|
url: "/api/v1/tools/vectorize",
|
||
|
|
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
||
|
|
body,
|
||
|
|
});
|
||
|
|
|
||
|
|
expect(res.statusCode).toBe(400);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("rejects threshold out of range", async () => {
|
||
|
|
const { body, contentType } = createMultipartPayload([
|
||
|
|
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
|
||
|
|
{ name: "settings", content: JSON.stringify({ threshold: 999 }) },
|
||
|
|
]);
|
||
|
|
|
||
|
|
const res = await app.inject({
|
||
|
|
method: "POST",
|
||
|
|
url: "/api/v1/tools/vectorize",
|
||
|
|
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
||
|
|
body,
|
||
|
|
});
|
||
|
|
|
||
|
|
expect(res.statusCode).toBe(400);
|
||
|
|
});
|
||
|
|
});
|