mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
AI sidecar failures reached Sentry as 'Error: Error': the scrubber type-onlys plain Errors and the tool wrappers threw them from result.error. The bridge now exports toSidecarError(), wrapping the sidecar reason in a SafeError (memory-allocation text classifies as operational, the rest as bug); all 14 wrappers use it, plus the dispatcher crash/stdin/spawn rejection paths and parseStdoutJson. toBgRemovalError from #535 delegates to the shared helper. On the web side, DOMExceptions report their specific name via err.name, so the NATIVE_ERRORS allowlist dropped the whole family's browser-authored messages. It now carries the full WebIDL DOMException name table; messages still pass through url/path redaction. Bridge-mocking test files switched to importOriginal passthrough mocks.
83 lines
3.0 KiB
TypeScript
83 lines
3.0 KiB
TypeScript
import { tmpdir } from "node:os";
|
|
import sharp from "sharp";
|
|
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
// Mock the sidecar bridge by its resolved path so background-removal's
|
|
// `./bridge.js` import receives these stubs.
|
|
const runPythonWithProgress = vi.fn();
|
|
const parseStdoutJson = vi.fn();
|
|
vi.mock("../../../packages/ai/src/bridge.js", async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import("../../../packages/ai/src/bridge.js")>();
|
|
return {
|
|
...actual,
|
|
runPythonWithProgress: (...args: unknown[]) => runPythonWithProgress(...args),
|
|
parseStdoutJson: (...args: unknown[]) => parseStdoutJson(...args),
|
|
};
|
|
});
|
|
|
|
import { isSafeMessageError, SafeError } from "@snapotter/shared";
|
|
import { removeBackground } from "../../../packages/ai/src/background-removal.js";
|
|
|
|
let png: Buffer;
|
|
beforeAll(async () => {
|
|
png = await sharp({
|
|
create: { width: 4, height: 4, channels: 4, background: { r: 0, g: 0, b: 0, alpha: 0 } },
|
|
})
|
|
.png()
|
|
.toBuffer();
|
|
});
|
|
|
|
beforeEach(() => {
|
|
runPythonWithProgress.mockReset();
|
|
parseStdoutJson.mockReset();
|
|
});
|
|
|
|
describe("removeBackground error surfacing", () => {
|
|
it("throws a SafeError titled 'Background removal failed' with the sidecar reason in the cause", async () => {
|
|
runPythonWithProgress.mockResolvedValue({ stdout: "{}" });
|
|
parseStdoutJson.mockReturnValue({ success: false, error: "rembg model load failed" });
|
|
|
|
let caught: unknown;
|
|
try {
|
|
await removeBackground(png, tmpdir(), { model: "u2net" });
|
|
} catch (e) {
|
|
caught = e;
|
|
}
|
|
|
|
expect(isSafeMessageError(caught)).toBe(true);
|
|
// The specific sidecar reason is preserved as the message (and survives scrubbing).
|
|
expect((caught as SafeError).message).toBe("rembg model load failed");
|
|
expect((caught as SafeError).kind).toBe("bug");
|
|
expect(runPythonWithProgress).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("still retries with the lighter model on OOM, and wraps the fallback failure too", async () => {
|
|
parseStdoutJson
|
|
.mockReturnValueOnce({ success: false, error: "CUDA out of memory" })
|
|
.mockReturnValueOnce({ success: false, error: "still failing" });
|
|
runPythonWithProgress.mockResolvedValue({ stdout: "{}" });
|
|
|
|
let caught: unknown;
|
|
try {
|
|
await removeBackground(png, tmpdir(), { model: "isnet-general-use" });
|
|
} catch (e) {
|
|
caught = e;
|
|
}
|
|
|
|
// OOM detection still works: the fallback attempt fired (two sidecar calls).
|
|
expect(runPythonWithProgress).toHaveBeenCalledTimes(2);
|
|
expect(isSafeMessageError(caught)).toBe(true);
|
|
expect((caught as SafeError).message).toBe("still failing");
|
|
});
|
|
|
|
it("passes a bridge SafeError (e.g. timeout) through unchanged, not re-wrapped as a bug", async () => {
|
|
const timeout = new SafeError("Python script timed out", {
|
|
kind: "operational",
|
|
code: "timeout",
|
|
});
|
|
runPythonWithProgress.mockRejectedValue(timeout);
|
|
|
|
await expect(removeBackground(png, tmpdir(), { model: "u2net" })).rejects.toBe(timeout);
|
|
});
|
|
});
|