brackt/scripts/__tests__/backfill-canonical-layer.test.ts
Chris Parsons 2848231235
Canonical tournament layer: schema + backfill (1/2) (#365)
* refactor(schema): rename per-window tables to season_* prefix

Renames participants, participant_expected_values, participant_qualifying_totals,
participant_results, participant_surface_elos to season_* prefixed names.
Renames event_results.participant_id to season_participant_id.
Phase 1a of canonical tournament layer migration.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* refactor: rename participant.ts model file to season-participant.ts

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* refactor(models): update model layer to use renamed schema exports

Updated all model files to use the renamed schema exports from Task 1:
- participants → seasonParticipants
- participantExpectedValues → seasonParticipantExpectedValues
- participantQualifyingTotals → seasonParticipantQualifyingTotals
- participantResults → seasonParticipantResults
- participantSurfaceElos → seasonParticipantSurfaceElos
- eventResults.participantId → eventResults.seasonParticipantId
- db.query relation accessors updated
- Relation field .participant → .seasonParticipant where applicable
- Import paths updated: ./participant → ./season-participant

Files updated (14 model files + 3 test files):
- draft-pick.ts
- draft-utils.ts
- event-result.ts
- group-stage-match.ts
- participant-result.ts
- qualifying-points.ts
- scoring-calculator.ts
- scoring-event.ts
- sports-season.ts
- surface-elo.ts
- team-score-events.ts
- cs2-major-stage.ts
- golf-skills.ts
- participant-expected-value.ts
- __tests__/sports-season.clone.test.ts
- __tests__/auto-pick.test.ts
- __tests__/executeAutoPick.timer.test.ts

Typecheck errors decreased: 779 → 499 (280 fewer)
All model file errors related to renamed schemas resolved.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* refactor(routes): update route layer to use renamed schema exports

- Update model import from ~/models/participant to ~/models/season-participant
- Rename schema.participants to schema.seasonParticipants
- Rename schema.participantResults to schema.seasonParticipantResults
- Rename db.query.participants to db.query.seasonParticipants
- Update 9 route files and 1 test file

Affected files:
- admin.sports-seasons.$id.events.$eventId.bracket.server.ts
- admin.sports-seasons.$id.participants.tsx
- api/draft.force-manual-pick.ts
- api/draft.make-pick.ts
- api/draft.replace-pick.ts
- api/seasons.$seasonId.draft.ts
- leagues/$leagueId.draft-board.$seasonId.tsx
- leagues/$leagueId.sports-seasons.$sportsSeasonId.server.ts
- admin/__tests__/sports-seasons-participants.test.ts

Error count reduced from 499 to 453 (46 errors fixed).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* refactor(routes): update route files for schema rename

Update route imports from ~/models/participant to ~/models/season-participant
and fix references to .participant/.participantId on event results to use
.seasonParticipant/.seasonParticipantId after schema rename.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* refactor(services): update simulators and services for renamed schema

Update all simulators, services, and server files to use renamed schema tables:
- participants → seasonParticipants
- participantExpectedValues → seasonParticipantExpectedValues
- participantResults → seasonParticipantResults
- eventResults.participantId → eventResults.seasonParticipantId

Files updated:
- 20 sport simulators (NBA, NHL, NFL, MLB, etc.)
- probability-updater.ts
- standings-sync/index.ts
- sports-data-sync.server.ts
- server/socket.ts

Typecheck errors reduced from 365 to 0.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* migration: rename per-window tables to season_* prefix

* fix(tests): update mock query keys after participants table rename

Change mock db.query.participants to db.query.seasonParticipants in test
files to match the schema rename from commit 66145a9. This fixes
"Cannot read properties of undefined (reading 'findFirst'/'findMany')"
errors that occurred when production code queries db.query.seasonParticipants
but test mocks only defined the old participants key.

Files updated:
- app/services/simulations/__tests__/world-cup-simulator.test.ts
- app/routes/api/__tests__/draft.force-manual-pick.test.ts
- app/routes/api/__tests__/draft.force-manual-pick.timer-mode.test.ts
- app/routes/api/__tests__/draft.make-pick.timer-mode.test.ts
- server/__tests__/timer-autodraft.test.ts
- app/models/__tests__/team-score-events.test.ts

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(tests): update remaining mock paths and keys after schema rename

* fix(tests): final two mock stragglers after schema rename

- draft-pick.test.ts: assertion on db.query.participantQualifyingTotals
- process-match-result.test.ts: mock key participants → seasonParticipants

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* chore: add post-phase1a baseline capture (temp, for diff verification)

* chore: capture pre-migration baselines

* chore: remove post-phase1a capture helper after verification

* schema: add canonical tournament & participant tables

Adds tournaments, participants (canonical), tournament_results, and
participant_surface_elos (canonical). Adds nullable tournament_id to
scoring_events and nullable participant_id to season_participants.
Phase 1b of canonical tournament layer migration.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* feat(models): add canonical tournament, participant, result, surface-elo models

Adds CRUD modules for the canonical tables created in commit 775b905.
Each module mirrors existing app/models conventions (database() from
~/database/context, schema from ~/database/schema, mock-based tests).

Key implementation notes:
- participant.ts exports use "Canonical" prefix (CanonicalParticipant,
  createCanonicalParticipant, etc.) to avoid collision with existing
  season-participant.ts exports
- All four models include comprehensive unit tests following the
  audit-log.test.ts pattern
- Tests use mocked db responses (no real database access)
- Upsert functions use onConflictDoUpdate for appropriate unique constraints

Part of Phase 1b of canonical tournament layer migration.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* migration: create canonical tables, add nullable FKs

* scripts: add extractTournamentIdentity helper for backfill

Pure function that derives canonical (name, year) identity from a
scoring_events row, stripping trailing 4-digit years from the name or
falling back to eventDate. Used by the Phase 2 backfill to group
per-window events into canonical tournaments.

* scripts: add backfill orchestrator for canonical layer

Populates canonical tournaments, participants, tournament_results, and
participant_surface_elos from per-window data for qualifying-points
sports. Skips already-linked rows, is idempotent, and supports dry-run
mode.

Critical invariants enforced by the implementation:
- qualifying_points_awarded is never copied to tournament_results
- season_participant_qualifying_totals is never touched
- conflicting surface-Elo values between windows raise a loud error
  (recorded in report.errors) rather than overwriting

* scripts: add backfill CLI with dry-run default

Wires backfill-canonical-layer.ts to a CLI entry point exposed as
`npm run backfill:canonical`. Defaults to --dry-run; requires --apply
to actually write. Supports --sport=<uuid> to limit to a single sport.
Exits 2 if the backfill reports errors (e.g., surface-Elo conflicts).

* fix(backfill-cli): wrap runBackfill in DatabaseContext.run

The orchestrator uses database() from ~/database/context, which requires
AsyncLocalStorage to be populated. Wrap the CLI invocation with
DatabaseContext.run(db, ...) using server/db's cached connection pool.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(backfill-cli): exit 0 on success so pg pool doesn't block

The cached postgres connection pool keeps the Node event loop open after
main() returns. Explicit process.exit(0) on success mirrors the pattern
in scripts/capture-baseline.ts.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Chris Parsons <chrisp@extrahop.com>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-01 20:13:18 -07:00

450 lines
15 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
vi.mock("~/database/context", () => ({
database: vi.fn(),
}));
import { runBackfill } from "../backfill-canonical-layer";
import { database } from "~/database/context";
import * as schema from "~/database/schema";
/**
* Fixture rows used across multiple tests.
*/
const SPORT_ID = "sport-golf";
const SEASON_ID = "season-golf-2026";
const GOLF_SEASON = {
id: SEASON_ID,
sportId: SPORT_ID,
scoringPattern: "qualifying_points",
} as const;
function makeEvent(overrides: Partial<Record<string, unknown>> = {}) {
return {
id: "event-1",
sportsSeasonId: SEASON_ID,
tournamentId: null,
name: "Masters Tournament",
eventDate: "2026-04-09",
eventType: "tournament",
...overrides,
};
}
function makeSeasonParticipant(
overrides: Partial<Record<string, unknown>> = {},
) {
return {
id: "sp-1",
sportsSeasonId: SEASON_ID,
participantId: null,
name: "Scottie Scheffler",
...overrides,
};
}
/**
* Builds a fake drizzle db that routes select/insert/update calls based
* on the target table. The caller supplies per-table select-result
* arrays (one per call to that table's select()). Inserts return the
* value they were given, extended with a stub id. Updates are recorded
* but otherwise no-op.
*/
interface TableState {
/** Queue of results to return for successive select() calls on this table. */
selects?: unknown[][];
/** Rows that insert().values().returning() should yield. */
insertReturns?: unknown[];
/** Incremented on each update() call. */
updates?: { count: number };
}
interface FakeDbOptions {
tables: Map<unknown, TableState>;
/** Records every insert call: tableRef → rows seen. */
inserts?: Map<unknown, unknown[]>;
/** Records every update call: tableRef → count. */
updateCounts?: Map<unknown, number>;
}
function makeFakeDb(opts: FakeDbOptions) {
const { tables, inserts, updateCounts } = opts;
// Each select() starts a new chain that ultimately resolves to the
// next queued selects[] entry for the passed table. The chain is a
// thenable via .limit/.where resolution.
const db = {
select: vi.fn().mockImplementation(() => {
let boundTable: unknown;
const chain: Record<string, unknown> = {
from: vi.fn().mockImplementation((table: unknown) => {
boundTable = table;
return chain;
}),
where: vi.fn().mockImplementation(() => chain),
limit: vi.fn().mockImplementation(() => chain),
// Terminal: make chain thenable so `await chain` yields the queued result.
then: (resolve: (v: unknown) => unknown) => {
const state = tables.get(boundTable);
const queue = state?.selects ?? [];
const next = queue.shift() ?? [];
return Promise.resolve(next).then(resolve);
},
};
return chain;
}),
insert: vi.fn().mockImplementation((table: unknown) => {
return {
values: vi.fn().mockImplementation((row: unknown) => {
if (inserts) {
const seen = inserts.get(table) ?? [];
seen.push(row);
inserts.set(table, seen);
}
const state = tables.get(table);
const returnRows = state?.insertReturns ?? [
{ ...(row as object), id: `generated-${Math.random()}` },
];
const afterValues = {
returning: vi.fn().mockResolvedValue(returnRows),
// If the caller doesn't chain .returning(), make it awaitable anyway.
then: (resolve: (v: unknown) => unknown) =>
Promise.resolve(returnRows).then(resolve),
};
return afterValues;
}),
};
}),
update: vi.fn().mockImplementation((table: unknown) => {
if (updateCounts) {
updateCounts.set(table, (updateCounts.get(table) ?? 0) + 1);
}
return {
set: vi.fn().mockReturnValue({
where: vi.fn().mockResolvedValue(undefined),
}),
};
}),
};
return db;
}
beforeEach(() => {
vi.clearAllMocks();
});
// ─── 1. empty DB no-op ─────────────────────────────────────────────────────
describe("runBackfill - empty DB", () => {
it("returns zero counts when no qualifying-points seasons exist", async () => {
const tables = new Map<unknown, TableState>([
[schema.sportsSeasons, { selects: [[]] }],
]);
vi.mocked(database).mockReturnValue(makeFakeDb({ tables }) as never);
const report = await runBackfill({ dryRun: false });
expect(report).toMatchObject({
tournamentsCreated: 0,
tournamentsLinked: 0,
participantsCreated: 0,
participantsLinked: 0,
tournamentResultsCreated: 0,
surfaceElosCreated: 0,
errors: [],
});
});
});
// ─── 2. golf window with 4 events creates 4 tournaments ───────────────────
describe("runBackfill - golf tournaments", () => {
it("creates 4 canonical tournaments for a golf season with 4 events", async () => {
const events = [
makeEvent({ id: "ev-1", name: "Masters Tournament", eventDate: "2026-04-09" }),
makeEvent({ id: "ev-2", name: "PGA Championship", eventDate: "2026-05-14" }),
makeEvent({ id: "ev-3", name: "U.S. Open", eventDate: "2026-06-18" }),
makeEvent({ id: "ev-4", name: "The Open Championship", eventDate: "2026-07-16" }),
];
const inserts = new Map<unknown, unknown[]>();
// Build per-event insertReturns so each new tournament gets its id.
const tables = new Map<unknown, TableState>([
[schema.sportsSeasons, { selects: [[GOLF_SEASON]] }],
[
schema.scoringEvents,
{
// 1st select: unlinked events; 2nd: refetch for results loop.
selects: [events, events.map((e, i) => ({ ...e, tournamentId: `t-${i + 1}` }))],
},
],
[
schema.tournaments,
{
// One select-miss per event, all return [] (no existing row).
selects: [[], [], [], []],
// One insert per event; return sequential ids.
insertReturns: [{ id: "t-1" }],
},
],
[schema.seasonParticipants, { selects: [[]] }],
[schema.eventResults, { selects: [[], [], [], []] }],
[schema.seasonParticipantSurfaceElos, { selects: [[]] }],
]);
// Because our insertReturns is consumed once per test, re-queue per event
// by overriding insert behaviour via the fake db options.
const tournamentIds = ["t-1", "t-2", "t-3", "t-4"];
let insertIdx = 0;
const db: ReturnType<typeof makeFakeDb> = makeFakeDb({ tables, inserts });
// Override insert for tournaments specifically to return sequential ids.
db.insert.mockImplementation((table: unknown) => ({
values: vi.fn().mockImplementation((row: unknown) => {
const seen = inserts.get(table) ?? [];
seen.push(row);
inserts.set(table, seen);
let returnRows: unknown[];
if (table === schema.tournaments) {
returnRows = [{ ...(row as object), id: tournamentIds[insertIdx++] }];
} else {
returnRows = [{ ...(row as object), id: "x" }];
}
return {
returning: vi.fn().mockResolvedValue(returnRows),
then: (resolve: (v: unknown) => unknown) =>
Promise.resolve(returnRows).then(resolve),
};
}),
}));
vi.mocked(database).mockReturnValue(db as never);
const report = await runBackfill({ dryRun: false });
expect(report.tournamentsCreated).toBe(4);
expect(report.tournamentsLinked).toBe(4);
expect(inserts.get(schema.tournaments)).toHaveLength(4);
expect(report.errors).toEqual([]);
});
});
// ─── 3. re-running doesn't duplicate ──────────────────────────────────────
describe("runBackfill - idempotence", () => {
it("does not create new tournaments when they already exist", async () => {
const events = [
makeEvent({ id: "ev-1", name: "Masters Tournament", eventDate: "2026-04-09" }),
];
const existingTournament = {
id: "t-existing",
sportId: SPORT_ID,
name: "Masters Tournament",
year: 2026,
};
const inserts = new Map<unknown, unknown[]>();
const updateCounts = new Map<unknown, number>();
const tables = new Map<unknown, TableState>([
[schema.sportsSeasons, { selects: [[GOLF_SEASON]] }],
[
schema.scoringEvents,
{
selects: [events, [{ ...events[0], tournamentId: "t-existing" }]],
},
],
[schema.tournaments, { selects: [[existingTournament]] }],
[schema.seasonParticipants, { selects: [[]] }],
[schema.eventResults, { selects: [[]] }],
[schema.seasonParticipantSurfaceElos, { selects: [[]] }],
]);
vi.mocked(database).mockReturnValue(
makeFakeDb({ tables, inserts, updateCounts }) as never,
);
const report = await runBackfill({ dryRun: false });
expect(report.tournamentsCreated).toBe(0);
expect(report.tournamentsLinked).toBe(1);
expect(inserts.get(schema.tournaments)).toBeUndefined();
});
});
// ─── 4. participant backfill ───────────────────────────────────────────────
describe("runBackfill - participants", () => {
it("creates canonical participants and links season_participants", async () => {
const sps = [
makeSeasonParticipant({ id: "sp-1", name: "Scottie Scheffler" }),
makeSeasonParticipant({ id: "sp-2", name: "Rory McIlroy" }),
];
const inserts = new Map<unknown, unknown[]>();
const updateCounts = new Map<unknown, number>();
const pIds = ["p-1", "p-2"];
let pIdx = 0;
const tables = new Map<unknown, TableState>([
[schema.sportsSeasons, { selects: [[GOLF_SEASON]] }],
[schema.scoringEvents, { selects: [[], []] }],
[schema.seasonParticipants, { selects: [sps] }],
[schema.participants, { selects: [[], []] }],
[schema.eventResults, { selects: [] }],
[schema.seasonParticipantSurfaceElos, { selects: [[]] }],
]);
const db = makeFakeDb({ tables, inserts, updateCounts });
db.insert.mockImplementation((table: unknown) => ({
values: vi.fn().mockImplementation((row: unknown) => {
const seen = inserts.get(table) ?? [];
seen.push(row);
inserts.set(table, seen);
let returnRows: unknown[];
if (table === schema.participants) {
returnRows = [{ ...(row as object), id: pIds[pIdx++] }];
} else {
returnRows = [{ ...(row as object), id: "x" }];
}
return {
returning: vi.fn().mockResolvedValue(returnRows),
then: (resolve: (v: unknown) => unknown) =>
Promise.resolve(returnRows).then(resolve),
};
}),
}));
vi.mocked(database).mockReturnValue(db as never);
const report = await runBackfill({ dryRun: false });
expect(report.participantsCreated).toBe(2);
expect(report.participantsLinked).toBe(2);
expect(inserts.get(schema.participants)).toHaveLength(2);
expect(updateCounts.get(schema.seasonParticipants)).toBe(2);
});
});
// ─── 5. Masters 2026 round-trip ───────────────────────────────────────────
describe("runBackfill - tournament_results from event_results", () => {
it("copies placement and rawScore but NOT qualifying_points_awarded", async () => {
const events = [
makeEvent({
id: "ev-masters",
tournamentId: "t-masters",
name: "Masters Tournament",
eventDate: "2026-04-09",
}),
];
const seasonParticipant = {
id: "sp-1",
sportsSeasonId: SEASON_ID,
participantId: "p-scottie",
name: "Scottie Scheffler",
};
const eventResult = {
id: "er-1",
scoringEventId: "ev-masters",
seasonParticipantId: "sp-1",
placement: 1,
rawScore: "-12.00",
qualifyingPointsAwarded: "100.00",
};
const inserts = new Map<unknown, unknown[]>();
const tables = new Map<unknown, TableState>([
[schema.sportsSeasons, { selects: [[GOLF_SEASON]] }],
[
schema.scoringEvents,
{
// 1st: no unlinked events (tournamentId already set)
// 2nd: refetch — events linked to tournament
selects: [[], events],
},
],
[schema.tournaments, { selects: [] }],
[schema.seasonParticipants, { selects: [[], [seasonParticipant]] }],
[schema.participants, { selects: [] }],
[schema.eventResults, { selects: [[eventResult]] }],
[schema.tournamentResults, { selects: [[]] }],
[schema.seasonParticipantSurfaceElos, { selects: [[]] }],
]);
vi.mocked(database).mockReturnValue(
makeFakeDb({ tables, inserts }) as never,
);
const report = await runBackfill({ dryRun: false });
expect(report.tournamentResultsCreated).toBe(1);
const trInserts = inserts.get(schema.tournamentResults) ?? [];
expect(trInserts).toHaveLength(1);
const inserted = trInserts[0] as Record<string, unknown>;
expect(inserted.tournamentId).toBe("t-masters");
expect(inserted.participantId).toBe("p-scottie");
expect(inserted.placement).toBe(1);
expect(inserted.rawScore).toBe("-12.00");
// CRITICAL: qualifying_points_awarded must NOT be copied.
expect(inserted).not.toHaveProperty("qualifyingPointsAwarded");
});
});
// ─── 6. Surface elo conflict detection ────────────────────────────────────
describe("runBackfill - surface elo conflicts", () => {
it("records a conflict error when two windows disagree on eloHard", async () => {
const sp = {
id: "sp-1",
sportsSeasonId: SEASON_ID,
participantId: "p-tennis-1",
name: "Carlos Alcaraz",
};
const windowElo = {
id: "se-1",
participantId: "sp-1",
sportsSeasonId: SEASON_ID,
eloHard: 2050,
eloClay: 2100,
eloGrass: 2000,
worldRanking: 2,
};
const existingCanonicalElo = {
id: "cpe-1",
participantId: "p-tennis-1",
eloHard: 2000, // differs!
eloClay: 2100,
eloGrass: 2000,
worldRanking: 2,
};
const inserts = new Map<unknown, unknown[]>();
const tables = new Map<unknown, TableState>([
[schema.sportsSeasons, { selects: [[GOLF_SEASON]] }],
[schema.scoringEvents, { selects: [[], []] }],
[schema.seasonParticipants, { selects: [[], [sp]] }],
[schema.participants, { selects: [] }],
[schema.eventResults, { selects: [] }],
[schema.seasonParticipantSurfaceElos, { selects: [[windowElo]] }],
[schema.participantSurfaceElos, { selects: [[existingCanonicalElo]] }],
]);
vi.mocked(database).mockReturnValue(
makeFakeDb({ tables, inserts }) as never,
);
const report = await runBackfill({ dryRun: false });
expect(report.surfaceElosCreated).toBe(0);
expect(report.errors).toHaveLength(1);
expect(report.errors[0]).toMatch(/conflict for participant p-tennis-1/);
expect(report.errors[0]).toMatch(/eloHard/);
// Must not have inserted a conflicting row.
expect(inserts.get(schema.participantSurfaceElos)).toBeUndefined();
});
});