Skip to content
Merged
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
11 changes: 11 additions & 0 deletions src/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ export interface CompareResult {
cases: CaseDelta[];
}

/** Raised when compare options fail validation. */
export class CompareValidationError extends Error {
constructor(message: string) {
super(`[evalgate] invalid compare options: ${message}`);
this.name = "CompareValidationError";
}
}

/** Options for {@link compareRuns}. */
export interface CompareOptions {
/**
Expand Down Expand Up @@ -65,6 +73,9 @@ export function compareRuns(
options: CompareOptions = {},
): CompareResult {
const tolerance = options.tolerance ?? 0;
if (!Number.isFinite(tolerance) || tolerance < 0) {
throw new CompareValidationError("tolerance must be a non-negative finite number");
}
const failOnPassFlip = options.failOnPassFlip !== false;

const baseMap = byId(baseline.cases);
Expand Down
12 changes: 12 additions & 0 deletions tests/compare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,16 @@ describe("compareRuns", () => {
expect(changes.a).toBe("removed");
expect(changes.b).toBe("added");
});

it("rejects negative tolerance", () => {
const base = run([caseResult("a", 1)]);
const head = run([caseResult("a", 0.5)]);
expect(() => compareRuns(base, head, { tolerance: -0.5 })).toThrow(/tolerance/);
});

it("rejects NaN tolerance", () => {
const base = run([caseResult("a", 1)]);
const head = run([caseResult("a", 0.5)]);
expect(() => compareRuns(base, head, { tolerance: NaN })).toThrow(/tolerance/);
});
});
Loading