Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/anomalyDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ export class AnomalyDetector {
this.rapidCycleSeconds = options.rapidCycleSeconds ?? 300;
this.maxAmountVariance = options.maxAmountVariance ?? 0.8;
this.sensitivityThreshold = options.sensitivityThreshold ?? 0.8;
if (this.sensitivityThreshold <= 0 || this.sensitivityThreshold > 1) {
if (
!Number.isFinite(this.sensitivityThreshold) ||
this.sensitivityThreshold <= 0 ||
this.sensitivityThreshold > 1
) {
throw new RangeError("sensitivityThreshold must be in the range (0, 1]");
}
this.now = options.now ?? (() => Math.floor(Date.now() / 1000));
Expand Down
14 changes: 14 additions & 0 deletions test/anomalyDetector.nan.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, expect, it } from "vitest";
import { AnomalyDetector } from "../src/anomalyDetector.js";

describe("AnomalyDetector sensitivityThreshold validation", () => {
it("rejects NaN instead of silently disabling score alerts", () => {
expect(() => new AnomalyDetector({ sensitivityThreshold: Number.NaN })).toThrow(
RangeError
);
});

it("still accepts finite values at the upper boundary", () => {
expect(() => new AnomalyDetector({ sensitivityThreshold: 1 })).not.toThrow();
});
});