From ad910d4f40fae6277e758b832b2246c7aca6abe5 Mon Sep 17 00:00:00 2001
From: chrisp
Date: Mon, 1 Jun 2026 06:47:30 +0000
Subject: [PATCH] Fix simulate route crash and surface actual simulation errors
(#64)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
## Problem
Running a simulation on MLB (and any sport where the readiness check fails) showed a confusing React Router 400 error instead of the real failure reason. The cause: the simulate route had only an \`action\` export. When the action **threw** instead of redirecting, React Router tried to render the route via GET to display the error — hit the missing loader — and replaced the real error with its own internal 400.
A secondary issue: the interim fix used \`?simulationError=\` query params, which had three problems identified in code review:
- Stale errors re-appeared when pressing browser Back after fixing the issue
- The param reflected arbitrary text into the UI (social engineering surface)
- The styling didn't match the established \`bg-destructive/15\` error pattern
## Changes
**\`admin.sports-seasons.\$id.simulate.tsx\`** — strip the action; keep a loader-only redirect stub so bookmarked URLs degrade gracefully to the season page.
**\`admin.sports-seasons.\$id.tsx\`** — add \`intent="simulate"\` to the action dispatcher (matching \`delete\`, \`rescore\`, \`sync-standings\`, \`finalize-standings\`). On success: redirect to expected-values (unchanged). On failure: return \`{ simulateError: message }\` displayed inline with the correct \`bg-destructive/15\` styling.
## Test plan
- [ ] Click Run Simulation on a season with a missing readiness input → error message appears inline below the description text, styled consistently with other errors on the page
- [ ] Click Run Simulation on a fully-configured season → redirects to expected-values as before
- [ ] Navigate directly to \`/admin/sports-seasons//simulate\` in browser → redirects to season page (no 400)
- [ ] Run simulation, see error, fix the issue, run again successfully → no stale error message persists
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Chris Parsons
Reviewed-on: https://forge.brackt.com/chrisp/brackt/pulls/64
---
.../admin.sports-seasons.$id.simulate.tsx | 21 +++++--------------
app/routes/admin.sports-seasons.$id.tsx | 20 +++++++++++++++++-
2 files changed, 24 insertions(+), 17 deletions(-)
diff --git a/app/routes/admin.sports-seasons.$id.simulate.tsx b/app/routes/admin.sports-seasons.$id.simulate.tsx
index 5dace5c..49dce26 100644
--- a/app/routes/admin.sports-seasons.$id.simulate.tsx
+++ b/app/routes/admin.sports-seasons.$id.simulate.tsx
@@ -1,24 +1,13 @@
/**
- * Admin: Run EV Simulation for a Sports Season
+ * Admin: Simulate action endpoint (stub)
*
- * Action-only route. POSTing here:
- * 1. Delegates validation + execution to the shared simulator runner
- * 6. Redirects back to the sports season admin page
+ * Simulation is now handled by intent="simulate" on the season admin page.
+ * This stub redirects any direct GET navigation to that page.
*/
import { redirect } from "react-router";
import type { Route } from "./+types/admin.sports-seasons.$id.simulate";
-import { runSportsSeasonSimulation } from "~/services/simulations/runner";
-export async function action({ params }: Route.ActionArgs) {
- const sportsSeasonId = params.id;
- try {
- await runSportsSeasonSimulation(sportsSeasonId);
- } catch (error) {
- throw new Response(error instanceof Error ? error.message : "Simulation failed", {
- status: error instanceof Error && error.message.includes("already running") ? 409 : 400,
- });
- }
-
- return redirect(`/admin/sports-seasons/${sportsSeasonId}/expected-values`);
+export async function loader({ params }: Route.LoaderArgs) {
+ return redirect(`/admin/sports-seasons/${params.id}`);
}
diff --git a/app/routes/admin.sports-seasons.$id.tsx b/app/routes/admin.sports-seasons.$id.tsx
index fb84899..43aab95 100644
--- a/app/routes/admin.sports-seasons.$id.tsx
+++ b/app/routes/admin.sports-seasons.$id.tsx
@@ -11,6 +11,7 @@ import { database } from "~/database/context";
import { participantEvSnapshots, seasonSports } from "~/database/schema";
import { eq, desc } from "drizzle-orm";
import { getSimulatorInfo, type SimulatorType } from "~/services/simulations/registry";
+import { runSportsSeasonSimulation } from "~/services/simulations/runner";
import { syncStandings } from "~/services/standings-sync/index";
import {
getPendingStandingsMappings,
@@ -280,6 +281,17 @@ export async function action(args: Route.ActionArgs) {
}
}
+ if (intent === "simulate") {
+ try {
+ await runSportsSeasonSimulation(params.id);
+ return redirect(`/admin/sports-seasons/${params.id}/expected-values`);
+ } catch (error) {
+ return {
+ simulateError: error instanceof Error ? error.message : "Simulation failed",
+ };
+ }
+ }
+
// Update
const name = formData.get("name");
const year = formData.get("year");
@@ -669,7 +681,8 @@ export default function EditSportsSeason({ loaderData, actionData }: Route.Compo
)}
{simulatorInfo && (
-
+ {"simulateError" in (actionData ?? {}) && actionData?.simulateError && (
+
+ {actionData.simulateError}
+
+ )}