Files
SnapOtter/tests/unit/ai/background-removal.test.ts
T
SnapOtter 733ebe8010 test: major coverage expansion — 18 new test files, ~830 new tests
Unit tests: 1354 → 1781 (+427)
- 11 new AI bridge module tests (packages/ai/ from 2/13 → 13/13 files)
- files-page-store (0% → full), pdf-to-image-store, features-store expanded
- saturation and edit-metadata image-engine operations
- analytics route, features route, web analytics lib, api-extended

Integration tests: ~2070 → 2320 (+250)
- 31 integration files expanded with branch-coverage-targeted tests
- progress.ts SSE endpoints (28% → comprehensive, +18 tests)
- gif-tools all modes (+18), pdf-to-image format variants (+13)
- Cross-format matrix expanded to 17 tools × 17 formats (467 tests)
- Adversarial: concurrent, memory pressure, unicode filenames, pipeline limits

E2E-Docker: +1020 lines across 6 spec files
- Info, colors, sharpening, base64, QR read, JXL/ICO/SVG formats
- Strip-metadata, image-enhancement, content-aware-resize expanded
- Batch pipelines, multi-format batches, HEIC input coverage
2026-04-26 12:03:08 +08:00

299 lines
11 KiB
TypeScript

import { readFile, unlink, writeFile } from "node:fs/promises";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("sharp", () => {
const mockSharp = vi.fn(() => ({
png: vi.fn().mockReturnThis(),
toBuffer: vi.fn().mockResolvedValue(Buffer.from("mock-png-data")),
metadata: vi.fn().mockResolvedValue({ width: 800, height: 600 }),
}));
return { default: mockSharp };
});
vi.mock("node:fs/promises", () => ({
readFile: vi.fn().mockResolvedValue(Buffer.from("mock-output-data")),
writeFile: vi.fn().mockResolvedValue(undefined),
unlink: vi.fn().mockResolvedValue(undefined),
}));
vi.mock("../../../packages/ai/src/bridge.js", () => ({
runPythonWithProgress: vi.fn(),
parseStdoutJson: vi.fn(),
}));
import sharp from "sharp";
import { removeBackground } from "../../../packages/ai/src/background-removal.js";
import { parseStdoutJson, runPythonWithProgress } from "../../../packages/ai/src/bridge.js";
const FAKE_INPUT = Buffer.from("fake-image-data");
const FAKE_OUTPUT_DIR = "/tmp/test-output";
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(readFile).mockResolvedValue(Buffer.from("mock-output-data"));
vi.mocked(writeFile).mockResolvedValue(undefined);
vi.mocked(unlink).mockResolvedValue(undefined);
vi.mocked(runPythonWithProgress).mockResolvedValue({
stdout: '{"success": true}',
stderr: "",
});
vi.mocked(parseStdoutJson).mockReturnValue({ success: true });
vi.mocked(sharp).mockImplementation(
() =>
({
png: vi.fn().mockReturnThis(),
toBuffer: vi.fn().mockResolvedValue(Buffer.from("mock-png-data")),
metadata: vi.fn().mockResolvedValue({ width: 800, height: 600 }),
}) as unknown as ReturnType<typeof sharp>,
);
});
afterEach(() => {
vi.restoreAllMocks();
});
describe("removeBackground", () => {
describe("request serialization", () => {
it("calls remove_bg.py with input path, output path, and options JSON", async () => {
await removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR);
expect(runPythonWithProgress).toHaveBeenCalledWith(
"remove_bg.py",
expect.arrayContaining([
expect.stringContaining("rembg_in_"),
expect.stringContaining("rembg_out_"),
"{}",
]),
expect.any(Object),
);
});
it("serializes model option into the args JSON", async () => {
await removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR, { model: "u2net_human_seg" });
const args = vi.mocked(runPythonWithProgress).mock.calls[0][1];
expect(JSON.parse(args[2])).toEqual({ model: "u2net_human_seg" });
});
it("serializes backgroundColor option into the args JSON", async () => {
await removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR, { backgroundColor: "#FF0000" });
const args = vi.mocked(runPythonWithProgress).mock.calls[0][1];
expect(JSON.parse(args[2])).toEqual({ backgroundColor: "#FF0000" });
});
it("serializes both model and backgroundColor together", async () => {
await removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR, {
model: "isnet-general-use",
backgroundColor: "transparent",
});
const args = vi.mocked(runPythonWithProgress).mock.calls[0][1];
expect(JSON.parse(args[2])).toEqual({
model: "isnet-general-use",
backgroundColor: "transparent",
});
});
it("converts input to PNG via sharp before writing to disk", async () => {
await removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR);
expect(sharp).toHaveBeenCalledWith(FAKE_INPUT);
expect(writeFile).toHaveBeenCalledWith(
expect.stringContaining("rembg_in_"),
Buffer.from("mock-png-data"),
);
});
it("uses unique UUID in temp file names to prevent collisions", async () => {
await removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR);
await removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR);
const call1Args = vi.mocked(runPythonWithProgress).mock.calls[0][1];
const call2Args = vi.mocked(runPythonWithProgress).mock.calls[1][1];
// The UUID portions should differ
expect(call1Args[0]).not.toBe(call2Args[0]);
});
it("writes input to system tmpdir, output to outputDir", async () => {
await removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR);
const args = vi.mocked(runPythonWithProgress).mock.calls[0][1];
// Input path in tmpdir
expect(args[0]).toMatch(/rembg_in_/);
// Output path in outputDir
expect(args[1]).toMatch(/^\/tmp\/test-output\/rembg_out_/);
});
});
describe("response parsing", () => {
it("reads the output file and returns its buffer on success", async () => {
const outputBuf = Buffer.from("transparent-image");
vi.mocked(readFile).mockResolvedValueOnce(outputBuf);
const result = await removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR);
expect(result).toBe(outputBuf);
});
it("passes stdout through parseStdoutJson", async () => {
vi.mocked(runPythonWithProgress).mockResolvedValue({
stdout: '{"success": true, "extra": "data"}',
stderr: "",
});
await removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR);
expect(parseStdoutJson).toHaveBeenCalledWith('{"success": true, "extra": "data"}');
});
});
describe("error handling", () => {
it("throws when Python returns success: false with custom error", async () => {
vi.mocked(parseStdoutJson).mockReturnValue({
success: false,
error: "Model u2net_cloth not available",
});
await expect(removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR)).rejects.toThrow(
"Model u2net_cloth not available",
);
});
it("throws fallback error when success: false and no error string", async () => {
vi.mocked(parseStdoutJson).mockReturnValue({ success: false });
await expect(removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR)).rejects.toThrow(
"Background removal failed",
);
});
it("propagates bridge timeout errors", async () => {
vi.mocked(runPythonWithProgress).mockRejectedValue(new Error("Python script timed out"));
await expect(removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR)).rejects.toThrow(
"Python script timed out",
);
});
it("propagates parseStdoutJson errors", async () => {
vi.mocked(parseStdoutJson).mockImplementation(() => {
throw new Error("No JSON response from Python script");
});
await expect(removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR)).rejects.toThrow(
"No JSON response from Python script",
);
});
it("propagates sharp conversion errors", async () => {
vi.mocked(sharp).mockImplementation(
() =>
({
png: vi.fn().mockReturnThis(),
toBuffer: vi.fn().mockRejectedValue(new Error("Invalid image")),
metadata: vi.fn().mockResolvedValue({ width: 800, height: 600 }),
}) as unknown as ReturnType<typeof sharp>,
);
await expect(removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR)).rejects.toThrow("Invalid image");
});
});
describe("timeout calculation", () => {
it("uses 300000ms base timeout for non-birefnet models", async () => {
await removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR, { model: "u2net" });
const options = vi.mocked(runPythonWithProgress).mock.calls[0][2];
expect(options.timeout).toBe(300000);
});
it("uses 600000ms base timeout for birefnet models", async () => {
await removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR, { model: "birefnet-general" });
const options = vi.mocked(runPythonWithProgress).mock.calls[0][2];
expect(options.timeout).toBeGreaterThanOrEqual(600000);
});
it("uses 600000ms base timeout for birefnet-massive model", async () => {
await removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR, { model: "birefnet-massive" });
const options = vi.mocked(runPythonWithProgress).mock.calls[0][2];
expect(options.timeout).toBeGreaterThanOrEqual(600000);
});
it("scales timeout with megapixels for large images", async () => {
vi.mocked(sharp).mockImplementation(
() =>
({
png: vi.fn().mockReturnThis(),
toBuffer: vi.fn().mockResolvedValue(Buffer.from("mock-png-data")),
metadata: vi.fn().mockResolvedValue({ width: 6000, height: 4000 }),
}) as unknown as ReturnType<typeof sharp>,
);
await removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR);
// 24 MP * 30 * 1000 = 720000 > 300000
const options = vi.mocked(runPythonWithProgress).mock.calls[0][2];
expect(options.timeout).toBe(720000);
});
it("uses default 300000ms base when model is not specified", async () => {
await removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR);
const options = vi.mocked(runPythonWithProgress).mock.calls[0][2];
expect(options.timeout).toBe(300000);
});
});
describe("temp file cleanup", () => {
it("cleans up both input and output temp files on success", async () => {
await removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR);
expect(unlink).toHaveBeenCalledTimes(2);
expect(unlink).toHaveBeenCalledWith(expect.stringContaining("rembg_in_"));
expect(unlink).toHaveBeenCalledWith(expect.stringContaining("rembg_out_"));
});
it("cleans up temp files when Python returns failure", async () => {
vi.mocked(parseStdoutJson).mockReturnValue({ success: false });
await expect(removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR)).rejects.toThrow();
expect(unlink).toHaveBeenCalledTimes(2);
});
it("cleans up temp files when bridge rejects", async () => {
vi.mocked(runPythonWithProgress).mockRejectedValue(new Error("crash"));
await expect(removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR)).rejects.toThrow();
expect(unlink).toHaveBeenCalledTimes(2);
});
it("does not throw if unlink fails (swallows cleanup errors)", async () => {
vi.mocked(unlink).mockRejectedValue(new Error("ENOENT"));
// Should not throw -- unlink errors are caught
await expect(removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR)).resolves.toBeDefined();
});
});
describe("onProgress forwarding", () => {
it("passes onProgress callback through to bridge", async () => {
const onProgress = vi.fn();
await removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR, {}, onProgress);
expect(runPythonWithProgress).toHaveBeenCalledWith(
"remove_bg.py",
expect.any(Array),
expect.objectContaining({ onProgress }),
);
});
it("passes undefined onProgress when not provided", async () => {
await removeBackground(FAKE_INPUT, FAKE_OUTPUT_DIR);
const options = vi.mocked(runPythonWithProgress).mock.calls[0][2];
expect(options.onProgress).toBeUndefined();
});
});
});