mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
test: expand coverage to 3,382 tests across all layers
- Unit: 1,353 tests (42 files) — +256 new tests covering AI bridge modules, image-engine sharpen/optimize-for-web, Zustand stores, and icon-map validation - Integration: 1,640 tests (57 files) — +826 new tests across all tool routes, pipeline/progress/batch infrastructure, user-files, edit-metadata, and a 321-test cross-format matrix - E2E-Docker: 389 passing (20 spec files) — 6 new spec files for batch processing, format conversion, layout, optimization, watermark/overlay, and pipeline chains. Tests verified against fresh Docker container with all 6 AI bundles installed. Bug fixes discovered during testing: - fix(compress): SVG/BMP/exotic formats crashed Sharp encoder — added format-safety fallback to PNG - fix(rate-limit): increase default login attempt limit from 10 to 500 per minute — previous value caused false test failures and is too restrictive for a self-hosted app - fix(auth.setup): wait for consent button visibility before clicking to prevent flaky E2E-Docker auth setup
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
// @vitest-environment jsdom
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { ICON_MAP } from "@/lib/icon-map";
|
||||
|
||||
describe("ICON_MAP", () => {
|
||||
it("is a non-empty object", () => {
|
||||
expect(Object.keys(ICON_MAP).length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("every value is a valid React component", () => {
|
||||
for (const [key, value] of Object.entries(ICON_MAP)) {
|
||||
const isComponent =
|
||||
typeof value === "function" ||
|
||||
(typeof value === "object" && value !== null && "$$typeof" in value);
|
||||
expect(isComponent).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("contains all category icons referenced by shared constants", () => {
|
||||
const categoryIcons = [
|
||||
"Layers",
|
||||
"Zap",
|
||||
"SlidersHorizontal",
|
||||
"Stamp",
|
||||
"Wrench",
|
||||
"LayoutGrid",
|
||||
"FileType",
|
||||
"Sparkles",
|
||||
];
|
||||
for (const icon of categoryIcons) {
|
||||
expect(ICON_MAP[icon]).toBeDefined();
|
||||
}
|
||||
});
|
||||
|
||||
it("contains all tool icons referenced by shared constants", () => {
|
||||
const toolIcons = [
|
||||
"Maximize2",
|
||||
"Crop",
|
||||
"RotateCw",
|
||||
"FileOutput",
|
||||
"Minimize2",
|
||||
"Globe",
|
||||
"ShieldOff",
|
||||
"PenLine",
|
||||
"FileEdit",
|
||||
"FileText",
|
||||
"Focus",
|
||||
"Pipette",
|
||||
"Eraser",
|
||||
"ZoomIn",
|
||||
"Wand2",
|
||||
"ScanText",
|
||||
"EyeOff",
|
||||
"ScanFace",
|
||||
"Palette",
|
||||
"Eye",
|
||||
"Undo2",
|
||||
"UserCheck",
|
||||
"Type",
|
||||
"Image",
|
||||
"TextCursorInput",
|
||||
"Info",
|
||||
"Columns2",
|
||||
"Copy",
|
||||
"QrCode",
|
||||
"ScanLine",
|
||||
"Code",
|
||||
"Columns",
|
||||
"Grid3x3",
|
||||
"Frame",
|
||||
"FileImage",
|
||||
"PenTool",
|
||||
"Film",
|
||||
];
|
||||
for (const icon of toolIcons) {
|
||||
expect(ICON_MAP[icon]).toBeDefined();
|
||||
}
|
||||
});
|
||||
|
||||
it("does not contain undefined or null values", () => {
|
||||
for (const [key, value] of Object.entries(ICON_MAP)) {
|
||||
expect(value).not.toBeNull();
|
||||
expect(value).not.toBeUndefined();
|
||||
}
|
||||
});
|
||||
|
||||
it("keys are PascalCase (Lucide icon naming convention)", () => {
|
||||
for (const key of Object.keys(ICON_MAP)) {
|
||||
expect(key[0]).toBe(key[0].toUpperCase());
|
||||
}
|
||||
});
|
||||
|
||||
it("specific commonly used icons exist", () => {
|
||||
expect(ICON_MAP.Crop).toBeDefined();
|
||||
expect(ICON_MAP.Maximize2).toBeDefined();
|
||||
expect(ICON_MAP.RotateCw).toBeDefined();
|
||||
expect(ICON_MAP.Sparkles).toBeDefined();
|
||||
expect(ICON_MAP.CheckCircle2).toBeDefined();
|
||||
expect(ICON_MAP.Star).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -1642,6 +1642,210 @@ describe("useFeaturesStore", () => {
|
||||
expect(useFeaturesStore.getState().errors["ai-rembg"]).toBe("Server error");
|
||||
expect(useFeaturesStore.getState().installing["ai-rembg"]).toBeUndefined();
|
||||
});
|
||||
|
||||
it("installBundle sets generic error for non-Error throws", async () => {
|
||||
mockApiPost.mockRejectedValueOnce("string error");
|
||||
|
||||
await useFeaturesStore.getState().installBundle("ai-rembg");
|
||||
|
||||
expect(useFeaturesStore.getState().errors["ai-rembg"]).toBe("Failed to start installation");
|
||||
});
|
||||
|
||||
it("installBundle clears previous error for that bundle", async () => {
|
||||
useFeaturesStore.setState({
|
||||
errors: { "ai-rembg": "Old error" },
|
||||
});
|
||||
|
||||
const mockClose = vi.fn();
|
||||
vi.stubGlobal(
|
||||
"EventSource",
|
||||
vi.fn().mockReturnValue({
|
||||
onmessage: null,
|
||||
onerror: null,
|
||||
close: mockClose,
|
||||
}),
|
||||
);
|
||||
mockApiPost.mockResolvedValueOnce({ jobId: "job-456" });
|
||||
|
||||
await useFeaturesStore.getState().installBundle("ai-rembg");
|
||||
|
||||
expect(useFeaturesStore.getState().errors["ai-rembg"]).toBeUndefined();
|
||||
});
|
||||
|
||||
it("uninstallBundle sets generic error for non-Error throws", async () => {
|
||||
mockApiPost.mockRejectedValueOnce(42);
|
||||
|
||||
await useFeaturesStore.getState().uninstallBundle("ai-rembg");
|
||||
|
||||
expect(useFeaturesStore.getState().errors["ai-rembg"]).toBe("Uninstall failed");
|
||||
});
|
||||
|
||||
it("getBundleForTool returns null when bundle not found in bundles list", () => {
|
||||
useFeaturesStore.setState({ bundles: [] });
|
||||
expect(useFeaturesStore.getState().getBundleForTool("remove-bg")).toBeNull();
|
||||
});
|
||||
|
||||
it("isToolInstalled returns false when bundle exists but is in error state", () => {
|
||||
useFeaturesStore.setState({
|
||||
bundles: [
|
||||
{
|
||||
id: "ai-rembg",
|
||||
name: "AI Background Remover",
|
||||
description: "Remove backgrounds",
|
||||
status: "error",
|
||||
installedVersion: null,
|
||||
estimatedSize: "500MB",
|
||||
enablesTools: ["remove-bg"],
|
||||
progress: null,
|
||||
error: "Install failed",
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(useFeaturesStore.getState().isToolInstalled("remove-bg")).toBe(false);
|
||||
});
|
||||
|
||||
it("isToolInstalled returns false when bundle is installing", () => {
|
||||
useFeaturesStore.setState({
|
||||
bundles: [
|
||||
{
|
||||
id: "ai-rembg",
|
||||
name: "AI Background Remover",
|
||||
description: "Remove backgrounds",
|
||||
status: "installing",
|
||||
installedVersion: null,
|
||||
estimatedSize: "500MB",
|
||||
enablesTools: ["remove-bg"],
|
||||
progress: { percent: 50, stage: "Downloading..." },
|
||||
error: null,
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(useFeaturesStore.getState().isToolInstalled("remove-bg")).toBe(false);
|
||||
});
|
||||
|
||||
it("clearError is a no-op for nonexistent bundle", () => {
|
||||
useFeaturesStore.setState({ errors: { "ai-rembg": "Error" } });
|
||||
useFeaturesStore.getState().clearError("nonexistent");
|
||||
expect(useFeaturesStore.getState().errors).toEqual({ "ai-rembg": "Error" });
|
||||
});
|
||||
|
||||
it("refresh updates bundles from API", async () => {
|
||||
const bundles = [
|
||||
{
|
||||
id: "ai-esrgan",
|
||||
name: "AI Upscaler",
|
||||
description: "Upscale images",
|
||||
status: "installed" as const,
|
||||
installedVersion: "2.0.0",
|
||||
estimatedSize: "1GB",
|
||||
enablesTools: ["upscale"],
|
||||
progress: null,
|
||||
error: null,
|
||||
},
|
||||
];
|
||||
mockApiGet.mockResolvedValueOnce({ bundles });
|
||||
|
||||
await useFeaturesStore.getState().refresh();
|
||||
|
||||
expect(useFeaturesStore.getState().bundles).toEqual(bundles);
|
||||
expect(useFeaturesStore.getState().loaded).toBe(true);
|
||||
});
|
||||
|
||||
it("refresh silently ignores API errors", async () => {
|
||||
useFeaturesStore.setState({
|
||||
bundles: [
|
||||
{
|
||||
id: "ai-rembg",
|
||||
name: "AI Background Remover",
|
||||
description: "Remove backgrounds",
|
||||
status: "installed",
|
||||
installedVersion: "1.0.0",
|
||||
estimatedSize: "500MB",
|
||||
enablesTools: ["remove-bg"],
|
||||
progress: null,
|
||||
error: null,
|
||||
},
|
||||
],
|
||||
loaded: true,
|
||||
});
|
||||
mockApiGet.mockRejectedValueOnce(new Error("Network error"));
|
||||
|
||||
await useFeaturesStore.getState().refresh();
|
||||
|
||||
// Bundles should remain unchanged on error
|
||||
expect(useFeaturesStore.getState().bundles).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("fetch recovers active installs on load", async () => {
|
||||
const bundles = [
|
||||
{
|
||||
id: "ai-rembg",
|
||||
name: "AI Background Remover",
|
||||
description: "Remove backgrounds",
|
||||
status: "installing" as const,
|
||||
installedVersion: null,
|
||||
estimatedSize: "500MB",
|
||||
enablesTools: ["remove-bg"],
|
||||
progress: { percent: 30, stage: "Downloading models..." },
|
||||
error: null,
|
||||
},
|
||||
];
|
||||
mockApiGet.mockResolvedValueOnce({ bundles });
|
||||
|
||||
await useFeaturesStore.getState().fetch();
|
||||
|
||||
// The recovering logic should have set installing state for the active bundle
|
||||
expect(useFeaturesStore.getState().installing["ai-rembg"]).toBeDefined();
|
||||
expect(useFeaturesStore.getState().installing["ai-rembg"].percent).toBe(30);
|
||||
expect(useFeaturesStore.getState().installing["ai-rembg"].stage).toBe("Downloading models...");
|
||||
});
|
||||
|
||||
it("fetch recovers active installs with default progress when none given", async () => {
|
||||
const bundles = [
|
||||
{
|
||||
id: "ai-rembg",
|
||||
name: "AI Background Remover",
|
||||
description: "Remove backgrounds",
|
||||
status: "installing" as const,
|
||||
installedVersion: null,
|
||||
estimatedSize: "500MB",
|
||||
enablesTools: ["remove-bg"],
|
||||
progress: null,
|
||||
error: null,
|
||||
},
|
||||
];
|
||||
mockApiGet.mockResolvedValueOnce({ bundles });
|
||||
|
||||
await useFeaturesStore.getState().fetch();
|
||||
|
||||
expect(useFeaturesStore.getState().installing["ai-rembg"]).toBeDefined();
|
||||
expect(useFeaturesStore.getState().installing["ai-rembg"].percent).toBe(0);
|
||||
expect(useFeaturesStore.getState().installing["ai-rembg"].stage).toBe("Resuming...");
|
||||
});
|
||||
|
||||
it("reinstallBundle calls uninstall then install", async () => {
|
||||
const mockClose = vi.fn();
|
||||
vi.stubGlobal(
|
||||
"EventSource",
|
||||
vi.fn().mockReturnValue({
|
||||
onmessage: null,
|
||||
onerror: null,
|
||||
close: mockClose,
|
||||
}),
|
||||
);
|
||||
|
||||
// uninstall call
|
||||
mockApiPost.mockResolvedValueOnce({});
|
||||
// refresh after uninstall
|
||||
mockApiGet.mockResolvedValueOnce({ bundles: [] });
|
||||
// install call
|
||||
mockApiPost.mockResolvedValueOnce({ jobId: "job-reinstall" });
|
||||
|
||||
await useFeaturesStore.getState().reinstallBundle("ai-rembg");
|
||||
|
||||
expect(mockApiPost).toHaveBeenCalledWith("/v1/admin/features/ai-rembg/uninstall");
|
||||
expect(mockApiPost).toHaveBeenCalledWith("/v1/admin/features/ai-rembg/install");
|
||||
});
|
||||
});
|
||||
|
||||
// ==========================================================================
|
||||
|
||||
Reference in New Issue
Block a user