Files
SnapOtter/tests/unit/api/error-report.test.ts
T
SnapOtterandGitHub ae6a4c8b7c fix: error-only Sentry telemetry, storm-proof capture, and crash fixes (#476)
Removes Sentry tracing entirely (BullMQ idle polling burned 4.8M transactions in 2 days at the baked 0.1 rate), decouples PostHog sampling, and replaces the type-only error scrub with a vetted-field sanitizer plus SafeError/ToolInputError contracts. One classified capture path with per-signature throttles and a per-process ceiling makes storms impossible (NODE-1E was 4,541 events from one 30s loop). Browser errors move to a dedicated web Sentry project with their own source maps. Adds the SNAPOTTER_TELEMETRY runtime kill switch and silences test fleets.

Crash fixes: remote 204/304 SSRF process kill (NODE-20), conversion-preset boot crash loop (NODE-21), Redis version preflight + unhandled subscribe rejection (NODE-1T), Sign PDF on plain-http origins (NODE-1K/1M), wavesurfer/pdf.js teardown rejections (NODE-1P/1N), bundle-import ZlibError to 400 (NODE-1Z), chart-maker input errors declassified (NODE-1H/1J), asset requests skip the session DB lookup (NODE-1D).
2026-07-10 21:41:49 +08:00

87 lines
3.9 KiB
TypeScript

import { SafeError, ToolInputError } from "@snapotter/shared";
import { beforeEach, describe, expect, it } from "vitest";
import {
classifyError,
errorSignature,
resetThrottleForTests,
shouldReport,
} from "../../../apps/api/src/lib/error-report.js";
describe("classifyError", () => {
it("expected: tool input, aborts, worker cancel/timeout strings, zod, upload validation", () => {
expect(classifyError(new ToolInputError("bad csv"))).toBe("expected");
expect(classifyError(Object.assign(new Error("aborted"), { code: "ECONNRESET" }))).toBe(
"expected",
);
expect(classifyError(new Error("Canceled"))).toBe("expected");
expect(classifyError(new Error("Timed out after 120s"))).toBe("expected");
expect(
classifyError(Object.assign(new Error("zodish"), { name: "ZodError", issues: [] })),
).toBe("expected");
expect(
classifyError(Object.assign(new Error("bad png"), { name: "InputValidationError" })),
).toBe("expected");
});
it("operational: connectivity, disk, perms, operational SafeError, marker-copied SafeError without kind", () => {
const pg = Object.assign(new Error("Failed query: q"), {
cause: Object.assign(new Error("57P01"), { code: "57P01" }),
});
expect(classifyError(pg)).toBe("operational");
expect(classifyError(Object.assign(new Error("full"), { code: "ENOSPC" }))).toBe("operational");
expect(classifyError(new SafeError("AI dispatcher exited", { kind: "operational" }))).toBe(
"operational",
);
expect(classifyError(Object.assign(new Error("copied"), { isSafeMessage: true }))).toBe(
"operational",
);
});
it("bug: everything else, including bug-kind SafeError and ReplyError", () => {
expect(classifyError(new Error("undefined is not a function"))).toBe("bug");
expect(classifyError(new SafeError("Impossible state", { kind: "bug" }))).toBe("bug");
expect(classifyError(Object.assign(new Error("ERR bad cmd"), { name: "ReplyError" }))).toBe(
"bug",
);
});
it("worker source: zod is a bug (schema drift) and bare resets are operational", () => {
const zod = Object.assign(new Error("z"), { name: "ZodError", issues: [] });
expect(classifyError(zod, "worker")).toBe("bug");
expect(classifyError(zod, "http")).toBe("expected");
const reset = Object.assign(new Error("read ECONNRESET"), { code: "ECONNRESET" });
expect(classifyError(reset, "worker")).toBe("operational");
expect(classifyError(reset, "http")).toBe("expected");
});
});
describe("throttle", () => {
beforeEach(() => resetThrottleForTests());
it("operational: 1 per signature per hour; bug: 10", () => {
expect(shouldReport("operational", "sig-a")).toBe(true);
expect(shouldReport("operational", "sig-a")).toBe(false);
expect(shouldReport("operational", "sig-b")).toBe(true);
for (let i = 0; i < 10; i++) expect(shouldReport("bug", "sig-c")).toBe(true);
expect(shouldReport("bug", "sig-c")).toBe(false);
});
it("window resets after an hour", () => {
expect(shouldReport("operational", "sig", 1_000)).toBe(true);
expect(shouldReport("operational", "sig", 2_000)).toBe(false);
expect(shouldReport("operational", "sig", 1_000 + 3_600_001)).toBe(true);
});
it("same signature under different classes throttles independently", () => {
expect(shouldReport("operational", "sig-x")).toBe(true);
expect(shouldReport("bug", "sig-x")).toBe(true);
});
});
describe("errorSignature", () => {
it("combines name, code, and first in-repo frame", () => {
const err = Object.assign(new Error("x"), { code: "EACCES" });
err.stack =
"Error: x\n at mkdir (node:fs:1)\n at startCleanupCron (/app/apps/api/src/lib/cleanup.ts:48:3)";
expect(errorSignature(err)).toBe("Error:EACCES:cleanup.ts:48");
});
it("degrades gracefully without stack or code", () => {
expect(errorSignature(new TypeError("t"))).toMatch(/^TypeError:-:/);
expect(errorSignature(null)).toBe("Unknown:-:-");
});
});