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
181 lines
7 KiB
TypeScript
181 lines
7 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
vi.mock("~/models/sports-season", () => ({
|
|
findSportsSeasonById: vi.fn(),
|
|
updateSportsSeason: vi.fn(),
|
|
}));
|
|
vi.mock("~/models/simulator", () => ({
|
|
getSportsSeasonSimulatorConfig: vi.fn(),
|
|
validateSimulatorReadiness: vi.fn(),
|
|
prepareSimulatorInputsForRun: vi.fn(),
|
|
}));
|
|
vi.mock("~/models/season-participant", () => ({
|
|
findParticipantsBySportsSeasonId: vi.fn(),
|
|
}));
|
|
vi.mock("~/models/participant-expected-value", () => ({
|
|
batchUpsertParticipantEVs: vi.fn(),
|
|
}));
|
|
vi.mock("~/models/ev-snapshot", () => ({
|
|
batchUpsertParticipantEvSnapshots: vi.fn(),
|
|
}));
|
|
vi.mock("~/models/scoring-calculator", () => ({
|
|
recalculateStandings: vi.fn(),
|
|
}));
|
|
vi.mock("~/services/simulations/registry", async (importOriginal) => {
|
|
// Keep the real SIMULATOR_TYPES / getSimulatorInfo so the manifest (pulled in
|
|
// transitively via input-policy) can build; only stub getSimulator.
|
|
const actual = await importOriginal<typeof import("~/services/simulations/registry")>();
|
|
return { ...actual, getSimulator: vi.fn() };
|
|
});
|
|
vi.mock("~/services/simulations/simulation-probabilities", () => ({
|
|
normalizeSimulationResultColumns: vi.fn(),
|
|
}));
|
|
vi.mock("~/database/context", () => ({
|
|
database: vi.fn(() => ({
|
|
query: {
|
|
seasonSports: { findMany: vi.fn().mockResolvedValue([]) },
|
|
seasons: { findFirst: vi.fn() },
|
|
},
|
|
})),
|
|
}));
|
|
|
|
import { runSportsSeasonSimulation } from "../runner";
|
|
import { findSportsSeasonById, updateSportsSeason } from "~/models/sports-season";
|
|
import {
|
|
getSportsSeasonSimulatorConfig,
|
|
validateSimulatorReadiness,
|
|
prepareSimulatorInputsForRun,
|
|
} from "~/models/simulator";
|
|
import { findParticipantsBySportsSeasonId } from "~/models/season-participant";
|
|
import { batchUpsertParticipantEVs } from "~/models/participant-expected-value";
|
|
import { batchUpsertParticipantEvSnapshots } from "~/models/ev-snapshot";
|
|
import { getSimulator } from "~/services/simulations/registry";
|
|
import { normalizeSimulationResultColumns } from "~/services/simulations/simulation-probabilities";
|
|
|
|
const SEASON = {
|
|
id: "season-1",
|
|
status: "active",
|
|
simulationStatus: "idle",
|
|
fantasySeasonId: null,
|
|
};
|
|
|
|
const CONFIG = {
|
|
sportsSeasonId: "season-1",
|
|
simulatorType: "nba_bracket" as const,
|
|
config: {},
|
|
profile: { displayName: "NBA Bracket", requiredInputs: [], optionalInputs: [], setupSections: [] },
|
|
};
|
|
|
|
const READY = { canRun: true, missingInputs: [], status: "ready" as const };
|
|
|
|
const PARTICIPANTS = [{ id: "p1" }, { id: "p2" }];
|
|
|
|
const RESULTS = [
|
|
{
|
|
participantId: "p1",
|
|
probabilities: {
|
|
probFirst: 0.5, probSecond: 0.2, probThird: 0.1, probFourth: 0.1,
|
|
probFifth: 0.05, probSixth: 0.03, probSeventh: 0.01, probEighth: 0.01,
|
|
},
|
|
source: "elo_simulation" as const,
|
|
},
|
|
{
|
|
participantId: "p2",
|
|
probabilities: {
|
|
probFirst: 0.5, probSecond: 0.2, probThird: 0.1, probFourth: 0.1,
|
|
probFifth: 0.05, probSixth: 0.03, probSeventh: 0.01, probEighth: 0.01,
|
|
},
|
|
source: "elo_simulation" as const,
|
|
},
|
|
];
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
vi.mocked(findSportsSeasonById).mockResolvedValue(SEASON as never);
|
|
vi.mocked(getSportsSeasonSimulatorConfig).mockResolvedValue(CONFIG as never);
|
|
vi.mocked(validateSimulatorReadiness).mockResolvedValue(READY as never);
|
|
vi.mocked(prepareSimulatorInputsForRun).mockResolvedValue(undefined);
|
|
vi.mocked(updateSportsSeason).mockResolvedValue(undefined as never);
|
|
vi.mocked(findParticipantsBySportsSeasonId).mockResolvedValue(PARTICIPANTS as never);
|
|
vi.mocked(batchUpsertParticipantEVs).mockResolvedValue([] as never);
|
|
vi.mocked(batchUpsertParticipantEvSnapshots).mockResolvedValue(undefined as never);
|
|
vi.mocked(getSimulator).mockReturnValue({ simulate: vi.fn().mockResolvedValue(RESULTS) } as never);
|
|
vi.mocked(normalizeSimulationResultColumns).mockImplementation(() => undefined);
|
|
});
|
|
|
|
describe("runSportsSeasonSimulation", () => {
|
|
it("runs and returns a summary with zeroed participants for those not in results", async () => {
|
|
vi.mocked(findParticipantsBySportsSeasonId).mockResolvedValue([
|
|
...PARTICIPANTS,
|
|
{ id: "p3" },
|
|
] as never);
|
|
|
|
const result = await runSportsSeasonSimulation("season-1");
|
|
|
|
expect(result.simulatedParticipants).toBe(2);
|
|
expect(result.zeroedParticipants).toBe(1);
|
|
expect(result.simulatorType).toBe("nba_bracket");
|
|
expect(result.snapshotDate).toMatch(/^\d{4}-\d{2}-\d{2}$/);
|
|
});
|
|
|
|
it("sets simulationStatus to running before simulate and back to idle after", async () => {
|
|
await runSportsSeasonSimulation("season-1");
|
|
|
|
expect(vi.mocked(updateSportsSeason).mock.calls[0]).toEqual(["season-1", { simulationStatus: "running" }]);
|
|
expect(vi.mocked(updateSportsSeason).mock.calls[1]).toEqual(["season-1", { simulationStatus: "idle" }]);
|
|
});
|
|
|
|
it("throws when the sports season is not found", async () => {
|
|
vi.mocked(findSportsSeasonById).mockResolvedValue(undefined);
|
|
|
|
await expect(runSportsSeasonSimulation("missing")).rejects.toThrow("Sports season not found");
|
|
});
|
|
|
|
it("throws when no simulator config is set", async () => {
|
|
vi.mocked(getSportsSeasonSimulatorConfig).mockResolvedValue(null);
|
|
|
|
await expect(runSportsSeasonSimulation("season-1")).rejects.toThrow("no simulator type configured");
|
|
});
|
|
|
|
it("throws when a simulation is already running", async () => {
|
|
vi.mocked(findSportsSeasonById).mockResolvedValue({ ...SEASON, simulationStatus: "running" } as never);
|
|
|
|
await expect(runSportsSeasonSimulation("season-1")).rejects.toThrow("already running");
|
|
});
|
|
|
|
it("throws when the sports season is completed", async () => {
|
|
vi.mocked(findSportsSeasonById).mockResolvedValue({ ...SEASON, status: "completed" } as never);
|
|
|
|
await expect(runSportsSeasonSimulation("season-1")).rejects.toThrow("cannot be simulated");
|
|
expect(prepareSimulatorInputsForRun).not.toHaveBeenCalled();
|
|
expect(updateSportsSeason).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("throws when readiness check fails", async () => {
|
|
vi.mocked(validateSimulatorReadiness).mockResolvedValue({
|
|
canRun: false,
|
|
missingInputs: ["Elo rating for 3 participant(s)"],
|
|
status: "not_ready",
|
|
} as never);
|
|
|
|
await expect(runSportsSeasonSimulation("season-1")).rejects.toThrow("not ready");
|
|
});
|
|
|
|
it("sets simulationStatus to failed and re-throws when the simulator errors", async () => {
|
|
vi.mocked(getSimulator).mockReturnValue({
|
|
simulate: vi.fn().mockRejectedValue(new Error("bracket data missing")),
|
|
} as never);
|
|
|
|
await expect(runSportsSeasonSimulation("season-1")).rejects.toThrow("bracket data missing");
|
|
expect(vi.mocked(updateSportsSeason).mock.calls.at(-1)).toEqual(["season-1", { simulationStatus: "failed" }]);
|
|
});
|
|
|
|
it("sets simulationStatus to failed and re-throws when simulate returns no results", async () => {
|
|
vi.mocked(getSimulator).mockReturnValue({
|
|
simulate: vi.fn().mockResolvedValue([]),
|
|
} as never);
|
|
|
|
await expect(runSportsSeasonSimulation("season-1")).rejects.toThrow("no results");
|
|
expect(vi.mocked(updateSportsSeason).mock.calls.at(-1)).toEqual(["season-1", { simulationStatus: "failed" }]);
|
|
});
|
|
});
|