Formalize simulator system with manifest, input-policy, runner, and admin UI (#409)
Introduces three new schema tables (simulator_profiles,
sports_season_simulator_configs, season_participant_simulator_inputs),
a central model layer (app/models/simulator.ts), and a single runner
entry point so every simulator run follows the same prepare → simulate
→ persist → snapshot → recalculate flow.
Key additions:
- manifest.ts: per-simulator display names, default configs, required/
optional inputs, derivable-input declarations, and setup sections
- input-policy.ts: resolves sourceElo from projectedWins,
projectedTablePoints, or sourceOdds; resolves ratings from sourceOdds;
supports block / fallbackElo / averageKnown / worstKnownMinus strategies
- runner.ts: single entry point for admin simulation runs; materialises
derived inputs, normalises result columns, zeroes omitted participants,
snapshots EVs, and recalculates linked fantasy standings
- /admin/simulators: inventory page with per-season readiness and bulk run
- /admin/sports-seasons/:id/simulator: per-season setup page with readiness
summary, input-policy editor, raw JSON config override, and CSV bulk input
- NCAAM/NCAAW simulators now read ratings from season_participant_simulator_inputs,
falling back to the hardcoded name-keyed maps while DB data is being populated
- Clone flow copies simulator config by default; volatile inputs (odds, Elo)
only copied when explicitly requested
Code-review fixes included in this commit:
- source field in compatibility bridge checked with !== null instead of !== undefined
- sourceEloRequirementLabel no longer appends "configured fallback" when the
participant is already excluded from all resolved sources
- Duplicate inline label maps in input-policy.ts replaced with simulatorInputLabel
- save-config preserves existing inputPolicy when the submitted JSON omits it
- Input table truncation label added (Showing 20 of N)
- CSV description notes values must not contain commas
- N+1 comment added to listSportsSeasonSimulatorSummaries
- assertRegistrySchemaDriftFree called in manifest tests
- Runner test suite added covering happy path, already-running guard,
readiness failure, empty results, and error recovery with status reset
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-11 21:09:53 -07:00
|
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
|
|
|
|
|
|
vi.mock("~/models/simulator", () => ({
|
|
|
|
|
listSportsSeasonSimulatorSummaries: vi.fn(),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("~/services/simulations/runner", () => ({
|
|
|
|
|
runSportsSeasonSimulation: vi.fn(),
|
|
|
|
|
}));
|
|
|
|
|
|
2026-05-12 16:52:26 -07:00
|
|
|
import { action, loader } from "../admin.simulators";
|
|
|
|
|
import { listSportsSeasonSimulatorSummaries } from "~/models/simulator";
|
Formalize simulator system with manifest, input-policy, runner, and admin UI (#409)
Introduces three new schema tables (simulator_profiles,
sports_season_simulator_configs, season_participant_simulator_inputs),
a central model layer (app/models/simulator.ts), and a single runner
entry point so every simulator run follows the same prepare → simulate
→ persist → snapshot → recalculate flow.
Key additions:
- manifest.ts: per-simulator display names, default configs, required/
optional inputs, derivable-input declarations, and setup sections
- input-policy.ts: resolves sourceElo from projectedWins,
projectedTablePoints, or sourceOdds; resolves ratings from sourceOdds;
supports block / fallbackElo / averageKnown / worstKnownMinus strategies
- runner.ts: single entry point for admin simulation runs; materialises
derived inputs, normalises result columns, zeroes omitted participants,
snapshots EVs, and recalculates linked fantasy standings
- /admin/simulators: inventory page with per-season readiness and bulk run
- /admin/sports-seasons/:id/simulator: per-season setup page with readiness
summary, input-policy editor, raw JSON config override, and CSV bulk input
- NCAAM/NCAAW simulators now read ratings from season_participant_simulator_inputs,
falling back to the hardcoded name-keyed maps while DB data is being populated
- Clone flow copies simulator config by default; volatile inputs (odds, Elo)
only copied when explicitly requested
Code-review fixes included in this commit:
- source field in compatibility bridge checked with !== null instead of !== undefined
- sourceEloRequirementLabel no longer appends "configured fallback" when the
participant is already excluded from all resolved sources
- Duplicate inline label maps in input-policy.ts replaced with simulatorInputLabel
- save-config preserves existing inputPolicy when the submitted JSON omits it
- Input table truncation label added (Showing 20 of N)
- CSV description notes values must not contain commas
- N+1 comment added to listSportsSeasonSimulatorSummaries
- assertRegistrySchemaDriftFree called in manifest tests
- Runner test suite added covering happy path, already-running guard,
readiness failure, empty results, and error recovery with status reset
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-11 21:09:53 -07:00
|
|
|
import { runSportsSeasonSimulation } from "~/services/simulations/runner";
|
|
|
|
|
|
|
|
|
|
function postForm(entries: Record<string, string | string[]>) {
|
|
|
|
|
const body = new URLSearchParams();
|
|
|
|
|
for (const [key, value] of Object.entries(entries)) {
|
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
|
for (const item of value) body.append(key, item);
|
|
|
|
|
} else {
|
|
|
|
|
body.set(key, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return new Request("http://test/admin/simulators", {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
|
|
|
body: body.toString(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
vi.clearAllMocks();
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-12 16:52:26 -07:00
|
|
|
describe("admin simulators loader", () => {
|
|
|
|
|
it("hides completed seasons", async () => {
|
|
|
|
|
vi.mocked(listSportsSeasonSimulatorSummaries).mockResolvedValue([
|
|
|
|
|
{
|
|
|
|
|
sportsSeasonId: "season-upcoming",
|
|
|
|
|
seasonName: "2026",
|
|
|
|
|
year: 2026,
|
|
|
|
|
seasonStatus: "upcoming",
|
|
|
|
|
simulationStatus: "idle",
|
|
|
|
|
fantasySeasonId: null,
|
|
|
|
|
fantasySeasonName: null,
|
|
|
|
|
leagueName: null,
|
|
|
|
|
sportName: "NBA",
|
|
|
|
|
sportSlug: "nba",
|
|
|
|
|
simulatorType: "nba_bracket",
|
|
|
|
|
simulatorName: "NBA Bracket",
|
|
|
|
|
participantCount: 16,
|
|
|
|
|
participantInputCount: 16,
|
|
|
|
|
lastSimulatedDate: null,
|
|
|
|
|
readiness: {
|
|
|
|
|
status: "ready",
|
|
|
|
|
canRun: true,
|
|
|
|
|
participantInputCount: 16,
|
|
|
|
|
participantCount: 16,
|
|
|
|
|
missingInputs: [],
|
|
|
|
|
warnings: [],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
sportsSeasonId: "season-completed",
|
|
|
|
|
seasonName: "2025",
|
|
|
|
|
year: 2025,
|
|
|
|
|
seasonStatus: "completed",
|
|
|
|
|
simulationStatus: "idle",
|
|
|
|
|
fantasySeasonId: null,
|
|
|
|
|
fantasySeasonName: null,
|
|
|
|
|
leagueName: null,
|
|
|
|
|
sportName: "NFL",
|
|
|
|
|
sportSlug: "nfl",
|
|
|
|
|
simulatorType: "nfl_bracket",
|
|
|
|
|
simulatorName: "NFL Bracket",
|
|
|
|
|
participantCount: 14,
|
|
|
|
|
participantInputCount: 14,
|
|
|
|
|
lastSimulatedDate: "2026-05-11",
|
|
|
|
|
readiness: {
|
|
|
|
|
status: "ready",
|
|
|
|
|
canRun: true,
|
|
|
|
|
participantInputCount: 14,
|
|
|
|
|
participantCount: 14,
|
|
|
|
|
missingInputs: [],
|
|
|
|
|
warnings: [],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
] as never);
|
|
|
|
|
|
|
|
|
|
const response = await loader();
|
|
|
|
|
|
|
|
|
|
expect(response).toEqual({
|
|
|
|
|
simulators: [
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
sportsSeasonId: "season-upcoming",
|
|
|
|
|
seasonStatus: "upcoming",
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
sports: ["NBA"],
|
|
|
|
|
simulatorTypes: ["nba_bracket"],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
Formalize simulator system with manifest, input-policy, runner, and admin UI (#409)
Introduces three new schema tables (simulator_profiles,
sports_season_simulator_configs, season_participant_simulator_inputs),
a central model layer (app/models/simulator.ts), and a single runner
entry point so every simulator run follows the same prepare → simulate
→ persist → snapshot → recalculate flow.
Key additions:
- manifest.ts: per-simulator display names, default configs, required/
optional inputs, derivable-input declarations, and setup sections
- input-policy.ts: resolves sourceElo from projectedWins,
projectedTablePoints, or sourceOdds; resolves ratings from sourceOdds;
supports block / fallbackElo / averageKnown / worstKnownMinus strategies
- runner.ts: single entry point for admin simulation runs; materialises
derived inputs, normalises result columns, zeroes omitted participants,
snapshots EVs, and recalculates linked fantasy standings
- /admin/simulators: inventory page with per-season readiness and bulk run
- /admin/sports-seasons/:id/simulator: per-season setup page with readiness
summary, input-policy editor, raw JSON config override, and CSV bulk input
- NCAAM/NCAAW simulators now read ratings from season_participant_simulator_inputs,
falling back to the hardcoded name-keyed maps while DB data is being populated
- Clone flow copies simulator config by default; volatile inputs (odds, Elo)
only copied when explicitly requested
Code-review fixes included in this commit:
- source field in compatibility bridge checked with !== null instead of !== undefined
- sourceEloRequirementLabel no longer appends "configured fallback" when the
participant is already excluded from all resolved sources
- Duplicate inline label maps in input-policy.ts replaced with simulatorInputLabel
- save-config preserves existing inputPolicy when the submitted JSON omits it
- Input table truncation label added (Showing 20 of N)
- CSV description notes values must not contain commas
- N+1 comment added to listSportsSeasonSimulatorSummaries
- assertRegistrySchemaDriftFree called in manifest tests
- Runner test suite added covering happy path, already-running guard,
readiness failure, empty results, and error recovery with status reset
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-11 21:09:53 -07:00
|
|
|
describe("admin simulators action", () => {
|
|
|
|
|
it("runs one simulator", async () => {
|
|
|
|
|
vi.mocked(runSportsSeasonSimulation).mockResolvedValue({
|
|
|
|
|
sportsSeasonId: "season-1",
|
|
|
|
|
simulatorType: "nba_bracket",
|
|
|
|
|
simulatedParticipants: 30,
|
|
|
|
|
zeroedParticipants: 0,
|
|
|
|
|
snapshotDate: "2026-05-11",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const response = await action({
|
|
|
|
|
request: postForm({ intent: "run-one", sportsSeasonId: "season-1" }),
|
|
|
|
|
params: {},
|
|
|
|
|
context: {},
|
|
|
|
|
} as never);
|
|
|
|
|
|
|
|
|
|
expect(runSportsSeasonSimulation).toHaveBeenCalledWith("season-1");
|
|
|
|
|
expect(response).toEqual({
|
|
|
|
|
success: true,
|
|
|
|
|
message: "Simulation run complete: 1/1 succeeded.",
|
|
|
|
|
results: [{ sportsSeasonId: "season-1", ok: true, message: "Simulated 30 participant(s)." }],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("runs selected simulators sequentially and reports failures", async () => {
|
|
|
|
|
vi.mocked(runSportsSeasonSimulation)
|
|
|
|
|
.mockResolvedValueOnce({
|
|
|
|
|
sportsSeasonId: "season-1",
|
|
|
|
|
simulatorType: "nba_bracket",
|
|
|
|
|
simulatedParticipants: 30,
|
|
|
|
|
zeroedParticipants: 0,
|
|
|
|
|
snapshotDate: "2026-05-11",
|
|
|
|
|
})
|
|
|
|
|
.mockRejectedValueOnce(new Error("Simulator is not ready: source odds."));
|
|
|
|
|
|
|
|
|
|
const response = await action({
|
|
|
|
|
request: postForm({ intent: "run-selected", sportsSeasonId: ["season-1", "season-2"] }),
|
|
|
|
|
params: {},
|
|
|
|
|
context: {},
|
|
|
|
|
} as never);
|
|
|
|
|
|
|
|
|
|
expect(runSportsSeasonSimulation).toHaveBeenNthCalledWith(1, "season-1");
|
|
|
|
|
expect(runSportsSeasonSimulation).toHaveBeenNthCalledWith(2, "season-2");
|
|
|
|
|
expect(response).toEqual({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "Simulation run complete: 1/2 succeeded.",
|
|
|
|
|
results: [
|
|
|
|
|
{ sportsSeasonId: "season-1", ok: true, message: "Simulated 30 participant(s)." },
|
|
|
|
|
{ sportsSeasonId: "season-2", ok: false, message: "Simulator is not ready: source odds." },
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|