Files
SnapOtter/tests/integration/tools/image/erase-object-mask.test.ts
T
SnapOtterandGitHub bf417a509e fix: first-run QA sweep of the single-container image (#413)
Fixes found by manually testing a fresh install end to end:

- auth: the must-change-password gate returned 403 on public routes
  including /api/v1/health, so every fresh install showed a false
  "Reconnecting to server" banner on the forced password change
  screen. Public routes are now exempt (they need no session at all).
  Adds the gate's first direct tests.
- multipart: @fastify/multipart's parts() iterator (9.4.0 and 10.0.0)
  ends on the request stream's "close", which on a reused keep-alive
  connection fires while an earlier part is still streaming to storage,
  silently dropping the parts behind it. The object eraser lost its
  mask file on every second POST per connection. Replaced with a
  busboy-driven iterator (lib/multipart-parts.ts) that ends on busboy's
  own "finish", installed for all routes via a preValidation hook;
  the tool-factory field-recovery workaround for the same bug is now
  unnecessary and removed.
- eraser: the mask canvas backing store is natural resolution, but
  "absolute inset-0" does not stretch replaced elements, so the
  canvas rendered at intrinsic size and the brush ring, strokes, and
  exported mask were all misscaled on photos larger than the viewport.
  The canvas now gets an explicit CSS box at the fitted size.
- compare slider: solid white divider with a dark halo so it stays
  visible over light images; still initialised at the painted region.
- tool page: the AI bundle install prompt now centers in the content
  area instead of hugging the top.
- api docs: disabled Scalar's cloud features (Ask AI, Generate MCP,
  Open API Client, dev toolbar), hid the "Powered by Scalar" footer
  link, and set the page title to "SnapOtter API Reference". The docs
  CSP blocks those cloud calls by design, so the buttons were dead UI.
- docker: embedded Redis comes from packages.redis.io pinned to the
  8.x major (was Debian's 7.0.15), matching the Compose stack and the
  documented claim. Build fails fast if the major ever drifts.
- docs: DOCKERHUB.md quick start now leads with the one-command docker
  run (matching the README) with Compose as the production path;
  README says embedded Postgres 17 + Redis 8.

Claude-Session: https://claude.ai/code/session_01XGB4pGvTvb7sUX4JN745U7
2026-07-03 19:32:25 +08:00

125 lines
4.7 KiB
TypeScript

/**
* Integration tests for the erase-object multipart contract
* (/api/v1/tools/image/erase-object).
*
* Every AI-matrix test stops at the 501 FEATURE_NOT_INSTALLED guard, so the
* file+mask parse path behind it had no coverage. These tests mock the
* install gate open and replay the exact multipart shape the web client
* sends. The AI sidecar is not running in tests, so a successful parse
* yields 202 (job enqueued); any 4xx means the request was misparsed.
*/
import { randomUUID } from "node:crypto";
import { Agent as HttpAgent, request as httpRequest } from "node:http";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import { fixtures, readFixture } from "../../../fixtures/index.js";
import {
buildTestApp,
createMultipartPayload,
loginAsAdmin,
type TestApp,
} from "../../test-server.js";
vi.mock("../../../../apps/api/src/lib/feature-status.js", async (importOriginal) => {
const mod =
await importOriginal<typeof import("../../../../apps/api/src/lib/feature-status.js")>();
return { ...mod, isToolInstalled: () => true };
});
const JPG = readFixture(fixtures.image.base.jpg100);
const PNG = readFixture(fixtures.image.base.png200);
// Large first file: widens the window in which the request stream fully
// buffers (firing "close") while the first part is still streaming to
// storage, which is what dropped the trailing mask part.
const LARGE_JPG = readFixture(fixtures.image.stressLarge);
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 multipart contract", () => {
it("accepts the web client's file + mask + fields shape", async () => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "photo.jpg", contentType: "image/jpeg", content: JPG },
{ name: "mask", filename: "mask.png", contentType: "image/png", content: PNG },
{ name: "clientJobId", content: randomUUID() },
{ name: "format", content: "png" },
{ name: "quality", content: "95" },
]);
const res = await app.inject({
method: "POST",
url: "/api/v1/tools/image/erase-object",
headers: { authorization: `Bearer ${adminToken}`, "content-type": contentType },
payload: body,
});
expect(res.statusCode, res.body).toBeLessThan(400);
});
it("keeps trailing parts across reused keep-alive connections", async () => {
// Regression for the @fastify/multipart parts() race: its iterator ends
// on the REQUEST stream's "close", which on a warm keep-alive connection
// fires while the first file is still streaming to storage, dropping the
// trailing mask part. inject() cannot reproduce it (no real socket), so
// this test runs real sequential HTTP posts over one connection. Before
// the busboy-driven iterator fix, the second post reliably 400'd with
// "No mask image provided".
await app.listen({ port: 0, host: "127.0.0.1" });
const address = app.server.address();
const port = typeof address === "object" && address !== null ? address.port : 0;
const agent = new HttpAgent({ keepAlive: true, maxSockets: 1 });
const post = (): Promise<{ status: number; body: string }> =>
new Promise((resolve, reject) => {
const { body, contentType } = createMultipartPayload([
{ name: "file", filename: "photo.jpg", contentType: "image/jpeg", content: LARGE_JPG },
{ name: "mask", filename: "mask.png", contentType: "image/png", content: PNG },
{ name: "clientJobId", content: randomUUID() },
{ name: "format", content: "png" },
{ name: "quality", content: "95" },
]);
const req = httpRequest(
{
host: "127.0.0.1",
port,
path: "/api/v1/tools/image/erase-object",
method: "POST",
agent,
headers: {
authorization: `Bearer ${adminToken}`,
"content-type": contentType,
"content-length": body.length,
},
},
(res) => {
let text = "";
res.on("data", (chunk) => {
text += chunk;
});
res.on("end", () => resolve({ status: res.statusCode ?? 0, body: text }));
},
);
req.on("error", reject);
req.end(body);
});
try {
const results = [await post(), await post(), await post()];
for (const [i, r] of results.entries()) {
expect(r.status, `request ${i + 1} of 3: ${r.body}`).toBeLessThan(400);
}
} finally {
agent.destroy();
}
}, 30_000);
});