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
This commit is contained in:
SnapOtter
2026-07-03 19:32:25 +08:00
committed by GitHub
parent 3d4a84d068
commit bf417a509e
18 changed files with 527 additions and 242 deletions
+107
View File
@@ -0,0 +1,107 @@
/**
* Unit tests for lib/multipart-parts.ts, the keep-alive-safe replacement for
* @fastify/multipart's request.parts().
*
* The scenario that broke the plugin: the whole request body is already
* buffered (request "end"/"close" fire immediately once piped) while the
* consumer awaits slow storage writes between parts. The plugin's iterator
* queued its end marker on request "close" and dropped every part emitted
* after it; the busboy-driven iterator must deliver all parts regardless of
* consumer pacing.
*/
import { PassThrough } from "node:stream";
import type { FastifyRequest } from "fastify";
import { describe, expect, it } from "vitest";
import { multipartParts } from "../../../apps/api/src/lib/multipart-parts.js";
const BOUNDARY = "----UnitBoundary1234";
function multipartBody(
parts: Array<{ name: string; filename?: string; content: string | Buffer }>,
): Buffer {
const chunks: Buffer[] = [];
for (const part of parts) {
let header = `--${BOUNDARY}\r\n`;
if (part.filename) {
header += `Content-Disposition: form-data; name="${part.name}"; filename="${part.filename}"\r\n`;
header += "Content-Type: application/octet-stream\r\n\r\n";
} else {
header += `Content-Disposition: form-data; name="${part.name}"\r\n\r\n`;
}
chunks.push(Buffer.from(header), Buffer.from(part.content), Buffer.from("\r\n"));
}
chunks.push(Buffer.from(`--${BOUNDARY}--\r\n`));
return Buffer.concat(chunks);
}
/** Fake request whose raw stream has the whole body buffered up front. */
function fakeRequest(body: Buffer): FastifyRequest {
const raw = new PassThrough();
Object.assign(raw, {
headers: { "content-type": `multipart/form-data; boundary=${BOUNDARY}` },
});
// Whole body available immediately: "end"/"close" fire as soon as the
// pipe drains the stream, exactly like a warm keep-alive socket.
raw.end(body);
return { raw } as unknown as FastifyRequest;
}
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
async function drain(stream: NodeJS.ReadableStream): Promise<Buffer> {
const chunks: Buffer[] = [];
for await (const chunk of stream) chunks.push(chunk as Buffer);
return Buffer.concat(chunks);
}
describe("multipartParts", () => {
it("yields every part when the consumer is slower than the body arrival", async () => {
const body = multipartBody([
{ name: "file", filename: "photo.jpg", content: Buffer.alloc(256 * 1024, 7) },
{ name: "mask", filename: "mask.png", content: Buffer.alloc(8 * 1024, 9) },
{ name: "clientJobId", content: "abc-123" },
{ name: "format", content: "png" },
{ name: "quality", content: "95" },
]);
const seen: string[] = [];
const sizes: Record<string, number> = {};
for await (const part of multipartParts(fakeRequest(body))) {
if (part.type === "file") {
const buf = await drain(part.file);
sizes[part.fieldname] = buf.length;
seen.push(`file:${part.fieldname}`);
// Slow consumer: the raw stream has long since closed by now.
await sleep(25);
} else {
seen.push(`field:${part.fieldname}=${part.value}`);
}
}
expect(seen).toEqual([
"file:file",
"file:mask",
"field:clientJobId=abc-123",
"field:format=png",
"field:quality=95",
]);
expect(sizes.file).toBe(256 * 1024);
expect(sizes.mask).toBe(8 * 1024);
});
it("propagates malformed multipart as an error", async () => {
const raw = new PassThrough();
Object.assign(raw, {
headers: { "content-type": `multipart/form-data; boundary=${BOUNDARY}` },
});
raw.end(Buffer.from("this is not multipart at all"));
const request = { raw } as unknown as FastifyRequest;
await expect(async () => {
for await (const part of multipartParts(request)) {
if (part.type === "file") await drain(part.file);
}
}).rejects.toThrow();
});
});
+4 -4
View File
@@ -118,7 +118,7 @@ describe("registerUpload", () => {
it("registers multipart with correct file size limit", async () => {
uploadConfig.MAX_UPLOAD_SIZE_MB = 10;
uploadConfig.MAX_BATCH_SIZE = 5;
const app = { register: vi.fn().mockResolvedValue(undefined) };
const app = { register: vi.fn().mockResolvedValue(undefined), addHook: vi.fn() };
await registerUpload(app as never);
expect(app.register).toHaveBeenCalledWith(mockMultipartPlugin, {
@@ -132,7 +132,7 @@ describe("registerUpload", () => {
it("passes undefined for fileSize when MAX_UPLOAD_SIZE_MB is 0", async () => {
uploadConfig.MAX_UPLOAD_SIZE_MB = 0;
uploadConfig.MAX_BATCH_SIZE = 5;
const app = { register: vi.fn().mockResolvedValue(undefined) };
const app = { register: vi.fn().mockResolvedValue(undefined), addHook: vi.fn() };
await registerUpload(app as never);
expect(app.register).toHaveBeenCalledWith(mockMultipartPlugin, {
@@ -146,7 +146,7 @@ describe("registerUpload", () => {
it("passes undefined for files when MAX_BATCH_SIZE is 0", async () => {
uploadConfig.MAX_UPLOAD_SIZE_MB = 10;
uploadConfig.MAX_BATCH_SIZE = 0;
const app = { register: vi.fn().mockResolvedValue(undefined) };
const app = { register: vi.fn().mockResolvedValue(undefined), addHook: vi.fn() };
await registerUpload(app as never);
expect(app.register).toHaveBeenCalledWith(mockMultipartPlugin, {
@@ -160,7 +160,7 @@ describe("registerUpload", () => {
it("passes undefined for both limits when both are 0", async () => {
uploadConfig.MAX_UPLOAD_SIZE_MB = 0;
uploadConfig.MAX_BATCH_SIZE = 0;
const app = { register: vi.fn().mockResolvedValue(undefined) };
const app = { register: vi.fn().mockResolvedValue(undefined), addHook: vi.fn() };
await registerUpload(app as never);
expect(app.register).toHaveBeenCalledWith(mockMultipartPlugin, {
-149
View File
@@ -641,153 +641,4 @@ describe("createToolRoute", () => {
);
});
});
describe("multipart field recovery", () => {
it("recovers settings from part.fields when the iterator drops trailing fields", async () => {
const app = createMockApp();
const id = "resize";
createToolRoute(app as never, makeMockConfig(id));
const handler = app.routes[apiToolPath(id)];
const reply = createMockReply();
// Simulate the @fastify/multipart race: the iterator yields only the
// file part; the settings field is present only on part.fields.
const req = {
parts: () => ({
[Symbol.asyncIterator]: async function* () {
yield {
type: "file",
filename: "test.png",
file: (async function* () {
yield Buffer.from("png-data");
})(),
fields: {
settings: { value: '{"x":1}' },
},
};
},
}),
headers: {},
log: { warn: vi.fn(), error: vi.fn(), info: vi.fn() },
};
await handler(req, reply);
// Settings must be recovered as {x:1}, not fall through to defaults ({})
const enqueueCall = vi.mocked(enqueueToolJob).mock.calls[0][0];
expect(enqueueCall.settings).toEqual({ x: 1 });
expect(reply.send).toHaveBeenCalledWith(
expect.objectContaining({ jobId: expect.any(String) }),
);
});
it("does not overwrite settings already collected from the iterator", async () => {
const app = createMockApp();
const id = "resize";
createToolRoute(app as never, makeMockConfig(id));
const handler = app.routes[apiToolPath(id)];
const reply = createMockReply();
// Both the iterator and part.fields carry settings; the iterator value wins
const req = {
parts: () => ({
[Symbol.asyncIterator]: async function* () {
yield {
type: "file",
filename: "test.png",
file: (async function* () {
yield Buffer.from("png-data");
})(),
fields: {
settings: { value: '{"from":"fields"}' },
},
};
yield {
type: "field",
fieldname: "settings",
value: '{"from":"iterator"}',
file: (async function* () {})(),
fields: {
settings: { value: '{"from":"fields"}' },
},
};
},
}),
headers: {},
log: { warn: vi.fn(), error: vi.fn(), info: vi.fn() },
};
await handler(req, reply);
const enqueueCall = vi.mocked(enqueueToolJob).mock.calls[0][0];
expect(enqueueCall.settings).toEqual({ from: "iterator" });
});
it("recovers fileId and clientJobId from part.fields", async () => {
const app = createMockApp();
const id = "resize";
createToolRoute(app as never, makeMockConfig(id));
const handler = app.routes[apiToolPath(id)];
const reply = createMockReply();
const req = {
parts: () => ({
[Symbol.asyncIterator]: async function* () {
yield {
type: "file",
filename: "test.png",
file: (async function* () {
yield Buffer.from("png-data");
})(),
fields: {
settings: { value: "{}" },
fileId: { value: "f-123" },
clientJobId: { value: "cj-456" },
},
};
},
}),
headers: {},
log: { warn: vi.fn(), error: vi.fn(), info: vi.fn() },
};
await handler(req, reply);
const enqueueCall = vi.mocked(enqueueToolJob).mock.calls[0][0];
expect(enqueueCall.fileId).toBe("f-123");
expect(enqueueCall.clientJobId).toBe("cj-456");
});
it("handles array-form fields from part.fields", async () => {
const app = createMockApp();
const id = "resize";
createToolRoute(app as never, makeMockConfig(id));
const handler = app.routes[apiToolPath(id)];
const reply = createMockReply();
const req = {
parts: () => ({
[Symbol.asyncIterator]: async function* () {
yield {
type: "file",
filename: "test.png",
file: (async function* () {
yield Buffer.from("png-data");
})(),
fields: {
settings: [{ value: '{"arr":true}' }],
},
};
},
}),
headers: {},
log: { warn: vi.fn(), error: vi.fn(), info: vi.fn() },
};
await handler(req, reply);
const enqueueCall = vi.mocked(enqueueToolJob).mock.calls[0][0];
expect(enqueueCall.settings).toEqual({ arr: true });
});
});
});