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
3 changes: 2 additions & 1 deletion src/scorers/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export function clamp01(n: number): number {
/** Read the weight from a spec, defaulting to 1 and rejecting negatives. */
export function specWeight(spec: ScorerSpec): number {
const w = typeof spec.weight === "number" ? spec.weight : 1;
return w < 0 ? 0 : w;
if (!Number.isFinite(w) || w < 0) return 0;
return w;
}

/** Convenience builder that fills in the boilerplate of a {@link ScoreResult}. */
Expand Down
4 changes: 2 additions & 2 deletions src/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ function validateScorer(data: unknown, caseId: string, index: number): ScorerSpe
);
if (s.weight !== undefined) {
assert(
typeof s.weight === "number" && s.weight >= 0,
`case "${caseId}" scorers[${index}].weight must be a non-negative number`,
typeof s.weight === "number" && Number.isFinite(s.weight) && s.weight >= 0,
`case "${caseId}" scorers[${index}].weight must be a finite non-negative number`,
);
}
return s as ScorerSpec;
Expand Down
18 changes: 18 additions & 0 deletions tests/suite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@ cases:
).toThrow(/at least one scorer/);
});

it.each([Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.NaN])(
"rejects non-finite scorer weight (%s)",
(weight) => {
expect(() =>
validateSuite({
name: "d",
cases: [
{
id: "x",
input: { prompt: "a" },
scorers: [{ type: "regex", weight }],
},
],
}),
).toThrow(/scorers\[0\]\.weight/);
},
);

it("rejects an out-of-range threshold", () => {
expect(() =>
validateSuite({
Expand Down
Loading