2026-04-25 21:53:13 +08:00
|
|
|
/**
|
2026-06-21 00:30:10 +08:00
|
|
|
* Integration tests for the erase-object AI tool (/api/v1/tools/image/erase-object).
|
2026-04-25 21:53:13 +08:00
|
|
|
*
|
|
|
|
|
* This tool requires BOTH an image and a mask file. The Python sidecar may not
|
|
|
|
|
* be running, so processing tests accept both 200 (sidecar available) and
|
|
|
|
|
* 501 (feature not installed). Validation paths are always testable.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
2026-06-20 05:07:46 +08:00
|
|
|
import { fixtures, readFixture } from "../../../fixtures/index.js";
|
|
|
|
|
import {
|
|
|
|
|
buildTestApp,
|
|
|
|
|
createMultipartPayload,
|
|
|
|
|
loginAsAdmin,
|
|
|
|
|
type TestApp,
|
|
|
|
|
} from "../../test-server.js";
|
2026-04-25 21:53:13 +08:00
|
|
|
|
2026-06-20 04:20:17 +08:00
|
|
|
const PNG = readFixture(fixtures.image.base.png200);
|
|
|
|
|
const HEIC = readFixture(fixtures.image.base.heic200);
|
|
|
|
|
const TINY = readFixture(fixtures.image.edge.px1);
|
2026-04-25 21:53:13 +08:00
|
|
|
// Use the same PNG as a mask (any valid image works for test purposes)
|
2026-06-20 04:20:17 +08:00
|
|
|
const MASK = readFixture(fixtures.image.base.png200);
|
2026-04-25 21:53:13 +08:00
|
|
|
|
|
|
|
|
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("erase-object", () => {
|
|
|
|
|
// ── Processing (sidecar-dependent) ────────────────────────────────
|
|
|
|
|
|
2026-05-01 00:11:03 +08:00
|
|
|
it("responds to the route with image and mask (202 or 501)", async () => {
|
2026-04-25 21:53:13 +08:00
|
|
|
const { body, contentType } = createMultipartPayload([
|
|
|
|
|
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
|
|
|
|
|
{ name: "mask", filename: "mask.png", contentType: "image/png", content: MASK },
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const res = await app.inject({
|
|
|
|
|
method: "POST",
|
2026-06-20 12:20:07 +08:00
|
|
|
url: "/api/v1/tools/image/erase-object",
|
2026-04-25 21:53:13 +08:00
|
|
|
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
|
|
|
|
body,
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-01 00:11:03 +08:00
|
|
|
expect([202, 501]).toContain(res.statusCode);
|
2026-04-25 21:53:13 +08:00
|
|
|
}, 60_000);
|
|
|
|
|
|
2026-05-01 00:11:03 +08:00
|
|
|
it("processes with default format and quality (202 or 501)", async () => {
|
2026-04-25 21:53:13 +08:00
|
|
|
const { body, contentType } = createMultipartPayload([
|
|
|
|
|
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
|
|
|
|
|
{ name: "mask", filename: "mask.png", contentType: "image/png", content: MASK },
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const res = await app.inject({
|
|
|
|
|
method: "POST",
|
2026-06-20 12:20:07 +08:00
|
|
|
url: "/api/v1/tools/image/erase-object",
|
2026-04-25 21:53:13 +08:00
|
|
|
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
|
|
|
|
body,
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-01 00:11:03 +08:00
|
|
|
expect([202, 501]).toContain(res.statusCode);
|
|
|
|
|
if (res.statusCode === 202) {
|
|
|
|
|
const result = JSON.parse(res.body);
|
|
|
|
|
expect(result.jobId).toBeDefined();
|
|
|
|
|
expect(result.async).toBe(true);
|
2026-04-25 21:53:13 +08:00
|
|
|
}
|
|
|
|
|
}, 60_000);
|
|
|
|
|
|
2026-05-01 00:11:03 +08:00
|
|
|
it("accepts explicit format=jpg and quality=80 (202 or 501)", async () => {
|
2026-04-25 21:53:13 +08:00
|
|
|
const { body, contentType } = createMultipartPayload([
|
|
|
|
|
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
|
|
|
|
|
{ name: "mask", filename: "mask.png", contentType: "image/png", content: MASK },
|
|
|
|
|
{ name: "format", content: "jpg" },
|
|
|
|
|
{ name: "quality", content: "80" },
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const res = await app.inject({
|
|
|
|
|
method: "POST",
|
2026-06-20 12:20:07 +08:00
|
|
|
url: "/api/v1/tools/image/erase-object",
|
2026-04-25 21:53:13 +08:00
|
|
|
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
|
|
|
|
body,
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-01 00:11:03 +08:00
|
|
|
expect([202, 501]).toContain(res.statusCode);
|
2026-04-25 21:53:13 +08:00
|
|
|
}, 60_000);
|
|
|
|
|
|
2026-05-01 00:11:03 +08:00
|
|
|
it("accepts format=webp (202 or 501)", async () => {
|
2026-04-25 21:53:13 +08:00
|
|
|
const { body, contentType } = createMultipartPayload([
|
|
|
|
|
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
|
|
|
|
|
{ name: "mask", filename: "mask.png", contentType: "image/png", content: MASK },
|
|
|
|
|
{ name: "format", content: "webp" },
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const res = await app.inject({
|
|
|
|
|
method: "POST",
|
2026-06-20 12:20:07 +08:00
|
|
|
url: "/api/v1/tools/image/erase-object",
|
2026-04-25 21:53:13 +08:00
|
|
|
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
|
|
|
|
body,
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-01 00:11:03 +08:00
|
|
|
expect([202, 501]).toContain(res.statusCode);
|
2026-04-25 21:53:13 +08:00
|
|
|
}, 60_000);
|
|
|
|
|
|
2026-05-01 19:51:47 +08:00
|
|
|
it(
|
|
|
|
|
"handles HEIC image input (202 or 501)",
|
|
|
|
|
{ timeout: 120_000 },
|
|
|
|
|
async () => {
|
|
|
|
|
const { body, contentType } = createMultipartPayload([
|
|
|
|
|
{ name: "file", filename: "photo.heic", contentType: "image/heic", content: HEIC },
|
|
|
|
|
{ name: "mask", filename: "mask.png", contentType: "image/png", content: MASK },
|
|
|
|
|
]);
|
2026-04-25 21:53:13 +08:00
|
|
|
|
2026-05-01 19:51:47 +08:00
|
|
|
const res = await app.inject({
|
|
|
|
|
method: "POST",
|
2026-06-20 12:20:07 +08:00
|
|
|
url: "/api/v1/tools/image/erase-object",
|
2026-05-01 19:51:47 +08:00
|
|
|
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
|
|
|
|
body,
|
|
|
|
|
});
|
2026-04-25 21:53:13 +08:00
|
|
|
|
2026-05-01 19:51:47 +08:00
|
|
|
expect([202, 501]).toContain(res.statusCode);
|
|
|
|
|
},
|
|
|
|
|
60_000,
|
|
|
|
|
);
|
2026-04-25 21:53:13 +08:00
|
|
|
|
|
|
|
|
it("handles 1x1 pixel image input (200, 422, or 501)", async () => {
|
|
|
|
|
const { body, contentType } = createMultipartPayload([
|
|
|
|
|
{ name: "file", filename: "tiny.png", contentType: "image/png", content: TINY },
|
|
|
|
|
{ name: "mask", filename: "mask.png", contentType: "image/png", content: TINY },
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const res = await app.inject({
|
|
|
|
|
method: "POST",
|
2026-06-20 12:20:07 +08:00
|
|
|
url: "/api/v1/tools/image/erase-object",
|
2026-04-25 21:53:13 +08:00
|
|
|
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
|
|
|
|
body,
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-01 00:11:03 +08:00
|
|
|
expect([202, 422, 501]).toContain(res.statusCode);
|
2026-04-25 21:53:13 +08:00
|
|
|
}, 60_000);
|
|
|
|
|
|
|
|
|
|
// ── Validation (always testable) ──────────────────────────────────
|
|
|
|
|
|
|
|
|
|
it("rejects requests without an image file (400)", async () => {
|
|
|
|
|
const { body, contentType } = createMultipartPayload([
|
|
|
|
|
{ name: "mask", filename: "mask.png", contentType: "image/png", content: MASK },
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const res = await app.inject({
|
|
|
|
|
method: "POST",
|
2026-06-20 12:20:07 +08:00
|
|
|
url: "/api/v1/tools/image/erase-object",
|
2026-04-25 21:53:13 +08:00
|
|
|
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
|
|
|
|
body,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 400 when sidecar is available, 501 when not (isToolInstalled check fires first)
|
|
|
|
|
expect([400, 501]).toContain(res.statusCode);
|
|
|
|
|
if (res.statusCode === 400) {
|
|
|
|
|
const json = JSON.parse(res.body);
|
|
|
|
|
expect(json.error).toMatch(/no image/i);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("rejects requests without a mask file (400)", async () => {
|
|
|
|
|
const { body, contentType } = createMultipartPayload([
|
|
|
|
|
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const res = await app.inject({
|
|
|
|
|
method: "POST",
|
2026-06-20 12:20:07 +08:00
|
|
|
url: "/api/v1/tools/image/erase-object",
|
2026-04-25 21:53:13 +08:00
|
|
|
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
|
|
|
|
body,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 400 for missing mask or 501 for sidecar not installed
|
|
|
|
|
expect([400, 501]).toContain(res.statusCode);
|
|
|
|
|
if (res.statusCode === 400) {
|
|
|
|
|
const json = JSON.parse(res.body);
|
|
|
|
|
expect(json.error).toMatch(/mask/i);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("rejects invalid format value (400)", async () => {
|
|
|
|
|
const { body, contentType } = createMultipartPayload([
|
|
|
|
|
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
|
|
|
|
|
{ name: "mask", filename: "mask.png", contentType: "image/png", content: MASK },
|
|
|
|
|
{ name: "format", content: "bmp" },
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const res = await app.inject({
|
|
|
|
|
method: "POST",
|
2026-06-20 12:20:07 +08:00
|
|
|
url: "/api/v1/tools/image/erase-object",
|
2026-04-25 21:53:13 +08:00
|
|
|
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
|
|
|
|
body,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect([400, 501]).toContain(res.statusCode);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("rejects quality out of range (400)", async () => {
|
|
|
|
|
const { body, contentType } = createMultipartPayload([
|
|
|
|
|
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
|
|
|
|
|
{ name: "mask", filename: "mask.png", contentType: "image/png", content: MASK },
|
|
|
|
|
{ name: "quality", content: "200" },
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const res = await app.inject({
|
|
|
|
|
method: "POST",
|
2026-06-20 12:20:07 +08:00
|
|
|
url: "/api/v1/tools/image/erase-object",
|
2026-04-25 21:53:13 +08:00
|
|
|
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
|
|
|
|
body,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect([400, 501]).toContain(res.statusCode);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("rejects unauthenticated requests (401)", async () => {
|
|
|
|
|
const { body, contentType } = createMultipartPayload([
|
|
|
|
|
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
|
|
|
|
|
{ name: "mask", filename: "mask.png", contentType: "image/png", content: MASK },
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const res = await app.inject({
|
|
|
|
|
method: "POST",
|
2026-06-20 12:20:07 +08:00
|
|
|
url: "/api/v1/tools/image/erase-object",
|
2026-04-25 21:53:13 +08:00
|
|
|
headers: { "content-type": contentType },
|
|
|
|
|
body,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(401);
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-01 00:11:03 +08:00
|
|
|
it("accepts format=avif (202 or 501)", async () => {
|
2026-04-25 21:53:13 +08:00
|
|
|
const { body, contentType } = createMultipartPayload([
|
|
|
|
|
{ name: "file", filename: "test.png", contentType: "image/png", content: PNG },
|
|
|
|
|
{ name: "mask", filename: "mask.png", contentType: "image/png", content: MASK },
|
|
|
|
|
{ name: "format", content: "avif" },
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const res = await app.inject({
|
|
|
|
|
method: "POST",
|
2026-06-20 12:20:07 +08:00
|
|
|
url: "/api/v1/tools/image/erase-object",
|
2026-04-25 21:53:13 +08:00
|
|
|
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
|
|
|
|
body,
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-01 00:11:03 +08:00
|
|
|
expect([202, 501]).toContain(res.statusCode);
|
2026-04-25 21:53:13 +08:00
|
|
|
}, 60_000);
|
2026-05-13 13:45:08 +08:00
|
|
|
|
|
|
|
|
it("handles two sequential erase requests (202 or 501)", async () => {
|
|
|
|
|
for (const filename of ["first.png", "second.png"]) {
|
|
|
|
|
const { body, contentType } = createMultipartPayload([
|
|
|
|
|
{ name: "file", filename, contentType: "image/png", content: PNG },
|
|
|
|
|
{ name: "mask", filename: "mask.png", contentType: "image/png", content: MASK },
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const res = await app.inject({
|
|
|
|
|
method: "POST",
|
2026-06-20 12:20:07 +08:00
|
|
|
url: "/api/v1/tools/image/erase-object",
|
2026-05-13 13:45:08 +08:00
|
|
|
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
|
|
|
|
|
body,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect([202, 501]).toContain(res.statusCode);
|
|
|
|
|
if (res.statusCode === 202) {
|
|
|
|
|
const result = JSON.parse(res.body);
|
|
|
|
|
expect(result.jobId).toBeDefined();
|
|
|
|
|
expect(result.async).toBe(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, 120_000);
|
2026-04-25 21:53:13 +08:00
|
|
|
});
|