2026-05-07 16:07:34 -07:00
|
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
|
|
|
|
|
|
vi.mock("~/models/sport", () => ({
|
2026-05-08 21:19:09 -07:00
|
|
|
findAllSports: vi.fn(),
|
2026-05-07 16:07:34 -07:00
|
|
|
findSportById: vi.fn(),
|
2026-05-08 21:19:09 -07:00
|
|
|
isSportIconUrlUsed: vi.fn(),
|
2026-05-07 16:07:34 -07:00
|
|
|
updateSport: vi.fn(),
|
|
|
|
|
}));
|
|
|
|
|
vi.mock("~/lib/cloudinary.server", () => ({
|
|
|
|
|
deleteCloudinaryImageByUrl: vi.fn().mockResolvedValue(undefined),
|
|
|
|
|
}));
|
|
|
|
|
vi.mock("~/lib/logger", () => ({
|
|
|
|
|
logger: { error: vi.fn(), info: vi.fn(), warn: vi.fn() },
|
|
|
|
|
}));
|
|
|
|
|
vi.mock("~/services/simulations/registry", () => ({
|
|
|
|
|
SIMULATOR_TYPES: ["playoff_bracket"],
|
|
|
|
|
getSimulatorInfo: vi.fn((type: string) => ({ name: type })),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
import { action } from "../admin.sports.$id";
|
|
|
|
|
import { deleteCloudinaryImageByUrl } from "~/lib/cloudinary.server";
|
2026-05-08 21:19:09 -07:00
|
|
|
import { findSportById, isSportIconUrlUsed, updateSport } from "~/models/sport";
|
2026-05-07 16:07:34 -07:00
|
|
|
|
|
|
|
|
function makeRequest(iconUrl: string) {
|
2026-05-08 21:19:09 -07:00
|
|
|
const formData = new URLSearchParams();
|
2026-05-07 16:07:34 -07:00
|
|
|
formData.set("name", "NFL");
|
|
|
|
|
formData.set("type", "team");
|
|
|
|
|
formData.set("slug", "nfl");
|
|
|
|
|
formData.set("description", "Football");
|
|
|
|
|
formData.set("simulatorType", "playoff_bracket");
|
|
|
|
|
formData.set("iconUrl", iconUrl);
|
|
|
|
|
|
|
|
|
|
return new Request("http://test/admin/sports/sport-1", {
|
|
|
|
|
method: "POST",
|
2026-05-08 21:19:09 -07:00
|
|
|
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
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
|
|
|
body: formData.toString(),
|
2026-05-07 16:07:34 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
vi.clearAllMocks();
|
2026-05-08 21:19:09 -07:00
|
|
|
vi.mocked(isSportIconUrlUsed).mockResolvedValue(false);
|
2026-05-07 16:07:34 -07:00
|
|
|
vi.mocked(findSportById).mockResolvedValue({
|
|
|
|
|
id: "sport-1",
|
|
|
|
|
name: "Old NFL",
|
|
|
|
|
type: "team",
|
|
|
|
|
slug: "nfl",
|
|
|
|
|
description: null,
|
|
|
|
|
iconUrl: "https://res.cloudinary.com/demo/image/upload/v1/sports-icons/old.svg",
|
|
|
|
|
simulatorType: null,
|
|
|
|
|
createdAt: new Date(),
|
|
|
|
|
updatedAt: new Date(),
|
|
|
|
|
} as never);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("admin sport edit action", () => {
|
|
|
|
|
it("stores the submitted icon URL and deletes a replaced Cloudinary icon", async () => {
|
|
|
|
|
const nextIconUrl = "https://res.cloudinary.com/demo/image/upload/v1/sports-icons/new.svg";
|
|
|
|
|
vi.mocked(updateSport).mockResolvedValue({
|
|
|
|
|
iconUrl: nextIconUrl,
|
|
|
|
|
} as never);
|
|
|
|
|
|
|
|
|
|
await action({
|
|
|
|
|
request: makeRequest(nextIconUrl),
|
|
|
|
|
params: { id: "sport-1" },
|
|
|
|
|
context: {},
|
|
|
|
|
} as never);
|
|
|
|
|
|
|
|
|
|
expect(updateSport).toHaveBeenCalledWith("sport-1", expect.objectContaining({ iconUrl: nextIconUrl }));
|
|
|
|
|
expect(deleteCloudinaryImageByUrl).toHaveBeenCalledWith(
|
|
|
|
|
"https://res.cloudinary.com/demo/image/upload/v1/sports-icons/old.svg"
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("stores null when the icon is removed", async () => {
|
|
|
|
|
vi.mocked(updateSport).mockResolvedValue({
|
|
|
|
|
iconUrl: null,
|
|
|
|
|
} as never);
|
|
|
|
|
|
|
|
|
|
await action({
|
|
|
|
|
request: makeRequest(""),
|
|
|
|
|
params: { id: "sport-1" },
|
|
|
|
|
context: {},
|
|
|
|
|
} as never);
|
|
|
|
|
|
|
|
|
|
expect(updateSport).toHaveBeenCalledWith("sport-1", expect.objectContaining({ iconUrl: null }));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("rejects arbitrary icon URLs", async () => {
|
|
|
|
|
const response = await action({
|
|
|
|
|
request: makeRequest("https://example.com/icon.svg"),
|
|
|
|
|
params: { id: "sport-1" },
|
|
|
|
|
context: {},
|
|
|
|
|
} as never);
|
|
|
|
|
|
|
|
|
|
expect(response).toEqual({ error: "Sport icon must be an uploaded SVG icon." });
|
|
|
|
|
expect(updateSport).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("deletes a newly uploaded icon when update fails", async () => {
|
|
|
|
|
const nextIconUrl = "https://res.cloudinary.com/demo/image/upload/v1/sports-icons/new.svg";
|
|
|
|
|
vi.mocked(updateSport).mockRejectedValue(new Error("duplicate"));
|
|
|
|
|
|
|
|
|
|
await action({
|
|
|
|
|
request: makeRequest(nextIconUrl),
|
|
|
|
|
params: { id: "sport-1" },
|
|
|
|
|
context: {},
|
|
|
|
|
} as never);
|
|
|
|
|
|
|
|
|
|
expect(deleteCloudinaryImageByUrl).toHaveBeenCalledWith(nextIconUrl);
|
|
|
|
|
});
|
2026-05-08 21:19:09 -07:00
|
|
|
|
|
|
|
|
it("does not delete a reused existing icon when update fails", async () => {
|
|
|
|
|
const nextIconUrl = "https://res.cloudinary.com/demo/image/upload/v1/sports-icons/new.svg";
|
|
|
|
|
vi.mocked(updateSport).mockRejectedValue(new Error("duplicate"));
|
|
|
|
|
vi.mocked(isSportIconUrlUsed).mockResolvedValue(true);
|
|
|
|
|
|
|
|
|
|
await action({
|
|
|
|
|
request: makeRequest(nextIconUrl),
|
|
|
|
|
params: { id: "sport-1" },
|
|
|
|
|
context: {},
|
|
|
|
|
} as never);
|
|
|
|
|
|
|
|
|
|
expect(deleteCloudinaryImageByUrl).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("returns the update error when icon reuse lookup fails during cleanup", async () => {
|
|
|
|
|
const nextIconUrl = "https://res.cloudinary.com/demo/image/upload/v1/sports-icons/new.svg";
|
|
|
|
|
vi.mocked(updateSport).mockRejectedValue(new Error("database unavailable"));
|
|
|
|
|
vi.mocked(isSportIconUrlUsed).mockRejectedValue(new Error("database unavailable"));
|
|
|
|
|
|
|
|
|
|
const response = await action({
|
|
|
|
|
request: makeRequest(nextIconUrl),
|
|
|
|
|
params: { id: "sport-1" },
|
|
|
|
|
context: {},
|
|
|
|
|
} as never);
|
|
|
|
|
|
|
|
|
|
expect(response).toEqual({ error: "Failed to update sport: database unavailable" });
|
|
|
|
|
expect(deleteCloudinaryImageByUrl).not.toHaveBeenCalled();
|
|
|
|
|
});
|
2026-05-07 16:07:34 -07:00
|
|
|
});
|