mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
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
174 lines
5.8 KiB
TypeScript
174 lines
5.8 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const uploadConfig = vi.hoisted(() => ({
|
|
MAX_UPLOAD_SIZE_MB: 10,
|
|
MAX_BATCH_SIZE: 5,
|
|
}));
|
|
|
|
vi.mock("../../../apps/api/src/config.js", () => ({ env: uploadConfig }));
|
|
|
|
const _mockStaticRegister = vi.fn().mockResolvedValue(undefined);
|
|
vi.mock("@fastify/static", () => ({ default: "fastify-static-plugin" }));
|
|
|
|
const mockMultipartPlugin = vi.fn();
|
|
vi.mock("@fastify/multipart", () => ({ default: mockMultipartPlugin }));
|
|
|
|
vi.mock("node:fs", async (importOriginal) => {
|
|
const actual = (await importOriginal()) as Record<string, unknown>;
|
|
return {
|
|
...actual,
|
|
existsSync: vi.fn(() => true),
|
|
};
|
|
});
|
|
|
|
describe("registerStatic", () => {
|
|
let registerStatic: typeof import("../../../apps/api/src/plugins/static.js").registerStatic;
|
|
let existsSyncMock: ReturnType<typeof vi.fn>;
|
|
|
|
beforeEach(async () => {
|
|
vi.clearAllMocks();
|
|
const staticModule = await import("../../../apps/api/src/plugins/static.js");
|
|
registerStatic = staticModule.registerStatic;
|
|
const fsMod = await import("node:fs");
|
|
existsSyncMock = fsMod.existsSync as ReturnType<typeof vi.fn>;
|
|
});
|
|
|
|
it("registers static plugin when dist path exists", async () => {
|
|
existsSyncMock.mockReturnValue(true);
|
|
const app = {
|
|
register: vi.fn().mockResolvedValue(undefined),
|
|
setNotFoundHandler: vi.fn(),
|
|
hasReplyDecorator: vi.fn().mockReturnValue(false),
|
|
log: { warn: vi.fn() },
|
|
};
|
|
|
|
await registerStatic(app as never);
|
|
expect(app.register).toHaveBeenCalledWith("fastify-static-plugin", {
|
|
root: expect.stringContaining("web/dist"),
|
|
prefix: "/",
|
|
wildcard: false,
|
|
decorateReply: true,
|
|
});
|
|
expect(app.setNotFoundHandler).toHaveBeenCalled();
|
|
});
|
|
|
|
it("sets SPA not-found handler that returns 404 for API routes", async () => {
|
|
existsSyncMock.mockReturnValue(true);
|
|
let notFoundHandler: (request: unknown, reply: unknown) => void;
|
|
const app = {
|
|
register: vi.fn().mockResolvedValue(undefined),
|
|
setNotFoundHandler: vi.fn((handler: typeof notFoundHandler) => {
|
|
notFoundHandler = handler;
|
|
}),
|
|
hasReplyDecorator: vi.fn().mockReturnValue(false),
|
|
log: { warn: vi.fn() },
|
|
};
|
|
|
|
await registerStatic(app as never);
|
|
|
|
const reply = { code: vi.fn().mockReturnThis(), send: vi.fn(), sendFile: vi.fn() };
|
|
notFoundHandler?.({ url: "/api/v1/tools" }, reply);
|
|
expect(reply.code).toHaveBeenCalledWith(404);
|
|
expect(reply.send).toHaveBeenCalledWith({ error: "Not found", code: "NOT_FOUND" });
|
|
});
|
|
|
|
it("sets SPA not-found handler that serves index.html for non-API routes", async () => {
|
|
existsSyncMock.mockReturnValue(true);
|
|
let notFoundHandler: (request: unknown, reply: unknown) => void;
|
|
const app = {
|
|
register: vi.fn().mockResolvedValue(undefined),
|
|
setNotFoundHandler: vi.fn((handler: typeof notFoundHandler) => {
|
|
notFoundHandler = handler;
|
|
}),
|
|
hasReplyDecorator: vi.fn().mockReturnValue(false),
|
|
log: { warn: vi.fn() },
|
|
};
|
|
|
|
await registerStatic(app as never);
|
|
|
|
const reply = { code: vi.fn().mockReturnThis(), send: vi.fn(), sendFile: vi.fn() };
|
|
notFoundHandler?.({ url: "/resize" }, reply);
|
|
expect(reply.sendFile).toHaveBeenCalledWith("index.html");
|
|
});
|
|
|
|
it("logs warning and skips registration when dist path does not exist", async () => {
|
|
existsSyncMock.mockReturnValue(false);
|
|
const app = {
|
|
register: vi.fn().mockResolvedValue(undefined),
|
|
setNotFoundHandler: vi.fn(),
|
|
log: { warn: vi.fn() },
|
|
};
|
|
|
|
await registerStatic(app as never);
|
|
expect(app.log.warn).toHaveBeenCalledWith(expect.stringContaining("SPA dist not found"));
|
|
expect(app.register).not.toHaveBeenCalled();
|
|
expect(app.setNotFoundHandler).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("registerUpload", () => {
|
|
let registerUpload: typeof import("../../../apps/api/src/plugins/upload.js").registerUpload;
|
|
|
|
beforeEach(async () => {
|
|
vi.clearAllMocks();
|
|
const uploadModule = await import("../../../apps/api/src/plugins/upload.js");
|
|
registerUpload = uploadModule.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), addHook: vi.fn() };
|
|
|
|
await registerUpload(app as never);
|
|
expect(app.register).toHaveBeenCalledWith(mockMultipartPlugin, {
|
|
limits: {
|
|
fileSize: 10 * 1024 * 1024,
|
|
files: 5,
|
|
},
|
|
});
|
|
});
|
|
|
|
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), addHook: vi.fn() };
|
|
|
|
await registerUpload(app as never);
|
|
expect(app.register).toHaveBeenCalledWith(mockMultipartPlugin, {
|
|
limits: {
|
|
fileSize: undefined,
|
|
files: 5,
|
|
},
|
|
});
|
|
});
|
|
|
|
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), addHook: vi.fn() };
|
|
|
|
await registerUpload(app as never);
|
|
expect(app.register).toHaveBeenCalledWith(mockMultipartPlugin, {
|
|
limits: {
|
|
fileSize: 10 * 1024 * 1024,
|
|
files: undefined,
|
|
},
|
|
});
|
|
});
|
|
|
|
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), addHook: vi.fn() };
|
|
|
|
await registerUpload(app as never);
|
|
expect(app.register).toHaveBeenCalledWith(mockMultipartPlugin, {
|
|
limits: {
|
|
fileSize: undefined,
|
|
files: undefined,
|
|
},
|
|
});
|
|
});
|
|
});
|