Make the futures-vs-Elo relationship an explicit, configurable rule and fix futures odds failing to override stored Elo. Root causes addressed: - resolveSourceElos/resolveRatings used a hardcoded precedence (direct Elo -> projectedWins -> projectedTablePoints -> odds). The prior fix nulled sourceElo on futures entry but not projections, which still outranked odds. - The override relied on a destructive null-on-save hack that also wiped manually entered Elo. - Blended simulators buried their Elo/odds weight in module constants. - Futures odds were not surfaced on the /admin/simulators inventory. Changes: - Add a configurable source policy: sourceEloPriority (ordered) and oddsWeight, parsed/clamped in getSimulatorInputPolicy. resolveSourceElos/resolveRatings now resolve each participant by the configured priority instead of a fixed order. Futures-centric simulators (ncaam, ncaaw, world_cup, ncaa_football, college_hockey) default to a futures-override priority. - Drop the destructive nulling in batchSaveFuturesOddsForSimulator and batchSaveSourceOdds; override is now governed by policy, preserving stored Elo. - Thread an optional SimulationContext (oddsWeight) through the Simulator interface so blended sims (UCL, World Cup, NCAA FB, MLB) read the blend weight from the season policy; defaults preserve prior calibration when no context is passed. - Add a "Futures vs. Elo" strategy control and Odds Blend Weight input to the Simulator Setup input-policy card, persisted via save-input-policy. - Surface futures on /admin/simulators: a source badge and a Futures Odds quick link; extend listSportsSeasonSimulatorSummaries with odds source info. - Tests: configurable priority override (Elo/projections/rating), oddsWeight parsing/clamping, prefersFuturesOdds, manifest defaults, an NCAA Football context-blend behavioral test, and an updated non-destructive save test. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QhNeB7gN6VKene7sdbBVQT
294 lines
13 KiB
TypeScript
294 lines
13 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
getSimulatorInputPolicy,
|
|
prefersFuturesOdds,
|
|
resolveRatings,
|
|
resolveSourceElos,
|
|
ELO_FIRST_SOURCE_PRIORITY,
|
|
FUTURES_FIRST_SOURCE_PRIORITY,
|
|
} from "../input-policy";
|
|
import type { SimulatorManifestProfile } from "../manifest";
|
|
|
|
const profile = {
|
|
derivableInputs: { sourceElo: ["projectedWins", "sourceOdds"] },
|
|
} as Pick<SimulatorManifestProfile, "derivableInputs">;
|
|
|
|
const tablePointsProfile = {
|
|
derivableInputs: { sourceElo: ["projectedTablePoints"] },
|
|
} as Pick<SimulatorManifestProfile, "derivableInputs">;
|
|
|
|
describe("simulator input policy", () => {
|
|
it("keeps direct Elo ahead of derived values", () => {
|
|
const resolved = resolveSourceElos(
|
|
[{ participantId: "team-1", sourceElo: 1600, rating: null, sourceOdds: 2000, projectedWins: 20, projectedTablePoints: null }],
|
|
profile,
|
|
{ seasonGames: 82 }
|
|
);
|
|
|
|
expect(resolved.get("team-1")).toMatchObject({ sourceElo: 1600, method: "direct" });
|
|
});
|
|
|
|
it("derives Elo from projected wins when Elo is missing", () => {
|
|
const resolved = resolveSourceElos(
|
|
[{ participantId: "team-1", sourceElo: null, rating: null, sourceOdds: null, projectedWins: 60, projectedTablePoints: null }],
|
|
profile,
|
|
{ seasonGames: 82, parityFactor: 400 }
|
|
);
|
|
|
|
expect(resolved.get("team-1")?.method).toBe("projectedWins");
|
|
expect(resolved.get("team-1")?.sourceElo).toBeGreaterThan(1500);
|
|
});
|
|
|
|
it("uses parityFactor as the projected table points to Elo spread", () => {
|
|
const input = {
|
|
participantId: "team-1",
|
|
sourceElo: null,
|
|
rating: null,
|
|
sourceOdds: null,
|
|
projectedWins: null,
|
|
projectedTablePoints: 76,
|
|
};
|
|
|
|
const compressed = resolveSourceElos([input], tablePointsProfile, {
|
|
seasonGames: 38,
|
|
parityFactor: 250,
|
|
});
|
|
const wider = resolveSourceElos([input], tablePointsProfile, {
|
|
seasonGames: 38,
|
|
parityFactor: 1000,
|
|
});
|
|
|
|
expect(compressed.get("team-1")?.sourceElo).toBeLessThan(wider.get("team-1")?.sourceElo ?? 0);
|
|
});
|
|
|
|
it("blocks missing Elo unless a fallback strategy is configured", () => {
|
|
const inputs = [{ participantId: "team-1", sourceElo: null, rating: null, sourceOdds: null, projectedWins: null, projectedTablePoints: null }];
|
|
|
|
expect(resolveSourceElos(inputs, profile, {}).has("team-1")).toBe(false);
|
|
expect(resolveSourceElos(inputs, profile, {
|
|
inputPolicy: { missingEloStrategy: "fallbackElo", fallbackElo: 1375 },
|
|
}).get("team-1")).toMatchObject({ sourceElo: 1375, method: "fallbackElo" });
|
|
});
|
|
|
|
it("supports worst-known-minus tail fallback", () => {
|
|
const resolved = resolveSourceElos(
|
|
[
|
|
{ participantId: "known-1", sourceElo: 1500, rating: null, sourceOdds: null, projectedWins: null, projectedTablePoints: null },
|
|
{ participantId: "known-2", sourceElo: 1430, rating: null, sourceOdds: null, projectedWins: null, projectedTablePoints: null },
|
|
{ participantId: "tail", sourceElo: null, rating: null, sourceOdds: null, projectedWins: null, projectedTablePoints: null },
|
|
],
|
|
profile,
|
|
{ inputPolicy: { missingEloStrategy: "worstKnownMinus", fallbackEloDelta: 30 } }
|
|
);
|
|
|
|
expect(resolved.get("tail")).toMatchObject({ sourceElo: 1400, method: "worstKnownMinus" });
|
|
});
|
|
|
|
it("derives Elo from futures odds when no direct Elo is present", () => {
|
|
const resolved = resolveSourceElos(
|
|
[
|
|
{ participantId: "favorite", sourceElo: null, rating: null, sourceOdds: 300, projectedWins: null, projectedTablePoints: null },
|
|
{ participantId: "longshot", sourceElo: null, rating: null, sourceOdds: 20000, projectedWins: null, projectedTablePoints: null },
|
|
],
|
|
profile,
|
|
{}
|
|
);
|
|
|
|
expect(resolved.get("favorite")?.method).toBe("sourceOdds");
|
|
expect(resolved.get("longshot")?.method).toBe("sourceOdds");
|
|
expect(resolved.get("favorite")?.sourceElo).toBeGreaterThan(resolved.get("longshot")?.sourceElo ?? Infinity);
|
|
});
|
|
|
|
it("ignores a lone sourceOdds participant rather than assigning a flat Elo", () => {
|
|
const inputs = [
|
|
{ participantId: "only", sourceElo: null, rating: null, sourceOdds: 300, projectedWins: null, projectedTablePoints: null },
|
|
];
|
|
|
|
// Needs at least 2 odds to derive a spread; with block strategy it stays unresolved.
|
|
expect(resolveSourceElos(inputs, profile, {}).has("only")).toBe(false);
|
|
|
|
// With a fallback strategy it resolves via that method, not "sourceOdds".
|
|
expect(
|
|
resolveSourceElos(inputs, profile, {
|
|
inputPolicy: { missingEloStrategy: "fallbackElo", fallbackElo: 1400 },
|
|
}).get("only")
|
|
).toMatchObject({ sourceElo: 1400, method: "fallbackElo" });
|
|
});
|
|
|
|
it("derives Elo from odds once a prior direct Elo has been cleared", () => {
|
|
// Regression: after entering futures odds the simulator inputs row keeps
|
|
// sourceOdds but has its sourceElo nulled. The resolver must pick "sourceOdds",
|
|
// not resurrect a "direct" Elo.
|
|
const resolved = resolveSourceElos(
|
|
[
|
|
{ participantId: "favorite", sourceElo: null, rating: null, sourceOdds: 300, projectedWins: null, projectedTablePoints: null },
|
|
{ participantId: "longshot", sourceElo: null, rating: null, sourceOdds: 20000, projectedWins: null, projectedTablePoints: null },
|
|
],
|
|
profile,
|
|
{}
|
|
);
|
|
|
|
expect(resolved.get("favorite")?.method).toBe("sourceOdds");
|
|
expect(resolved.get("longshot")?.method).toBe("sourceOdds");
|
|
});
|
|
|
|
it("derives ratings from futures odds when the profile allows it", () => {
|
|
const resolved = resolveRatings(
|
|
[
|
|
{ participantId: "favorite", sourceElo: null, rating: null, sourceOdds: 300, projectedWins: null, projectedTablePoints: null },
|
|
{ participantId: "longshot", sourceElo: null, rating: null, sourceOdds: 20000, projectedWins: null, projectedTablePoints: null },
|
|
],
|
|
{ derivableInputs: { rating: ["sourceOdds"] } },
|
|
{ inputPolicy: { ratingMin: -10, ratingMax: 35 } }
|
|
);
|
|
|
|
expect(resolved.get("favorite")?.method).toBe("sourceOdds");
|
|
expect(resolved.get("longshot")?.method).toBe("sourceOdds");
|
|
expect(resolved.get("favorite")?.rating).toBeGreaterThan(resolved.get("longshot")?.rating ?? 999);
|
|
});
|
|
|
|
it("blocks missing ratings unless a rating fallback strategy is configured", () => {
|
|
const inputs = [
|
|
{ participantId: "known", sourceElo: null, rating: null, sourceOdds: 300, projectedWins: null, projectedTablePoints: null },
|
|
{ participantId: "longshot", sourceElo: null, rating: null, sourceOdds: 20000, projectedWins: null, projectedTablePoints: null },
|
|
{ participantId: "tail", sourceElo: null, rating: null, sourceOdds: null, projectedWins: null, projectedTablePoints: null },
|
|
];
|
|
const ratingProfile = { derivableInputs: { rating: ["sourceOdds"] } } as Pick<SimulatorManifestProfile, "derivableInputs">;
|
|
|
|
expect(resolveRatings(inputs, ratingProfile, {
|
|
inputPolicy: { ratingMin: -10, ratingMax: 35 },
|
|
}).has("tail")).toBe(false);
|
|
|
|
expect(resolveRatings(inputs, ratingProfile, {
|
|
inputPolicy: {
|
|
missingRatingStrategy: "fallbackRating",
|
|
fallbackRating: -4,
|
|
ratingMin: -10,
|
|
ratingMax: 35,
|
|
},
|
|
}).get("tail")).toMatchObject({ rating: -4, method: "fallbackRating" });
|
|
});
|
|
|
|
it("supports worst-known-minus rating fallback with clamping", () => {
|
|
const resolved = resolveRatings(
|
|
[
|
|
{ participantId: "favorite", sourceElo: null, rating: 30, sourceOdds: null, projectedWins: null, projectedTablePoints: null },
|
|
{ participantId: "known-tail", sourceElo: null, rating: -8, sourceOdds: null, projectedWins: null, projectedTablePoints: null },
|
|
{ participantId: "missing-tail", sourceElo: null, rating: null, sourceOdds: null, projectedWins: null, projectedTablePoints: null },
|
|
],
|
|
{ derivableInputs: { rating: ["sourceOdds"] } },
|
|
{
|
|
inputPolicy: {
|
|
missingRatingStrategy: "worstKnownMinus",
|
|
fallbackRatingDelta: 5,
|
|
ratingMin: -10,
|
|
ratingMax: 35,
|
|
},
|
|
}
|
|
);
|
|
|
|
expect(resolved.get("missing-tail")).toMatchObject({ rating: -10, method: "worstKnownMinus" });
|
|
});
|
|
|
|
it("lets a futures-first priority override a stored direct Elo", () => {
|
|
const inputs = [
|
|
{ participantId: "favorite", sourceElo: 1800, rating: null, sourceOdds: 300, projectedWins: null, projectedTablePoints: null },
|
|
{ participantId: "longshot", sourceElo: 1200, rating: null, sourceOdds: 20000, projectedWins: null, projectedTablePoints: null },
|
|
];
|
|
|
|
// Default (Elo-first): the stored direct Elo wins.
|
|
const eloFirst = resolveSourceElos(inputs, profile, {});
|
|
expect(eloFirst.get("favorite")).toMatchObject({ sourceElo: 1800, method: "direct" });
|
|
|
|
// Futures-first: odds win even though a direct Elo is present.
|
|
const futuresFirst = resolveSourceElos(inputs, profile, {
|
|
inputPolicy: { sourceEloPriority: FUTURES_FIRST_SOURCE_PRIORITY },
|
|
});
|
|
expect(futuresFirst.get("favorite")?.method).toBe("sourceOdds");
|
|
expect(futuresFirst.get("longshot")?.method).toBe("sourceOdds");
|
|
});
|
|
|
|
it("lets a futures-first priority override stored projected wins", () => {
|
|
const inputs = [
|
|
{ participantId: "team-1", sourceElo: null, rating: null, sourceOdds: 300, projectedWins: 60, projectedTablePoints: null },
|
|
{ participantId: "team-2", sourceElo: null, rating: null, sourceOdds: 20000, projectedWins: 20, projectedTablePoints: null },
|
|
];
|
|
|
|
// Default (Elo-first): projectedWins outranks odds.
|
|
expect(resolveSourceElos(inputs, profile, { seasonGames: 82 }).get("team-1")?.method).toBe("projectedWins");
|
|
|
|
// Futures-first: odds win over projections.
|
|
const futuresFirst = resolveSourceElos(inputs, profile, {
|
|
seasonGames: 82,
|
|
inputPolicy: { sourceEloPriority: FUTURES_FIRST_SOURCE_PRIORITY },
|
|
});
|
|
expect(futuresFirst.get("team-1")?.method).toBe("sourceOdds");
|
|
});
|
|
|
|
it("lets a futures-first priority override a stored direct rating", () => {
|
|
const ratingProfile = { derivableInputs: { rating: ["sourceOdds"] } } as Pick<SimulatorManifestProfile, "derivableInputs">;
|
|
const inputs = [
|
|
{ participantId: "favorite", sourceElo: null, rating: 30, sourceOdds: 300, projectedWins: null, projectedTablePoints: null },
|
|
{ participantId: "longshot", sourceElo: null, rating: -5, sourceOdds: 20000, projectedWins: null, projectedTablePoints: null },
|
|
];
|
|
|
|
// Default: direct rating wins.
|
|
expect(resolveRatings(inputs, ratingProfile, { inputPolicy: { ratingMin: -10, ratingMax: 35 } }).get("favorite"))
|
|
.toMatchObject({ rating: 30, method: "direct" });
|
|
|
|
// Futures-first: odds win.
|
|
expect(
|
|
resolveRatings(inputs, ratingProfile, {
|
|
inputPolicy: { ratingMin: -10, ratingMax: 35, sourceEloPriority: FUTURES_FIRST_SOURCE_PRIORITY },
|
|
}).get("favorite")?.method
|
|
).toBe("sourceOdds");
|
|
});
|
|
|
|
it("parses and clamps the input policy source priority and odds weight", () => {
|
|
const defaults = getSimulatorInputPolicy({});
|
|
expect(defaults.sourceEloPriority).toEqual(ELO_FIRST_SOURCE_PRIORITY);
|
|
expect(defaults.oddsWeight).toBe(0.3);
|
|
|
|
// oddsWeight can be sourced from the top-level config or the input policy,
|
|
// and is clamped to [0, 1]; junk priority entries are dropped.
|
|
expect(getSimulatorInputPolicy({ oddsWeight: 0.6 }).oddsWeight).toBe(0.6);
|
|
expect(getSimulatorInputPolicy({ inputPolicy: { oddsWeight: 5 } }).oddsWeight).toBe(1);
|
|
expect(getSimulatorInputPolicy({ inputPolicy: { oddsWeight: -2 } }).oddsWeight).toBe(0);
|
|
expect(
|
|
getSimulatorInputPolicy({ inputPolicy: { sourceEloPriority: ["sourceOdds", "bogus", "sourceElo"] } }).sourceEloPriority
|
|
).toEqual(["sourceOdds", "sourceElo"]);
|
|
// An empty/all-invalid priority falls back to the default.
|
|
expect(getSimulatorInputPolicy({ inputPolicy: { sourceEloPriority: ["nope"] } }).sourceEloPriority).toEqual(
|
|
ELO_FIRST_SOURCE_PRIORITY
|
|
);
|
|
});
|
|
|
|
it("detects whether a priority prefers futures odds", () => {
|
|
expect(prefersFuturesOdds(ELO_FIRST_SOURCE_PRIORITY)).toBe(false);
|
|
expect(prefersFuturesOdds(FUTURES_FIRST_SOURCE_PRIORITY)).toBe(true);
|
|
expect(prefersFuturesOdds(["projectedWins", "sourceElo"])).toBe(false);
|
|
expect(prefersFuturesOdds(["sourceOdds"])).toBe(true);
|
|
});
|
|
|
|
it("uses NCAAW-style rating scale when deriving and falling back", () => {
|
|
const resolved = resolveRatings(
|
|
[
|
|
{ participantId: "favorite", sourceElo: null, rating: null, sourceOdds: 300, projectedWins: null, projectedTablePoints: null },
|
|
{ participantId: "longshot", sourceElo: null, rating: null, sourceOdds: 20000, projectedWins: null, projectedTablePoints: null },
|
|
{ participantId: "tail", sourceElo: null, rating: null, sourceOdds: null, projectedWins: null, projectedTablePoints: null },
|
|
],
|
|
{ derivableInputs: { rating: ["sourceOdds"] } },
|
|
{
|
|
inputPolicy: {
|
|
missingRatingStrategy: "worstKnownMinus",
|
|
fallbackRatingDelta: 0.05,
|
|
ratingMin: 0.15,
|
|
ratingMax: 0.99,
|
|
},
|
|
}
|
|
);
|
|
|
|
expect(resolved.get("favorite")?.rating).toBeLessThanOrEqual(0.99);
|
|
expect(resolved.get("tail")).toMatchObject({ rating: 0.15, method: "worstKnownMinus" });
|
|
});
|
|
});
|