Layer 3 Step 4b: public park base, source-pluggable, plus game-level capture
PHASE 0 — the settle path sees a player's game-log line, not the game. It knows date and teams, never venue or final totals. But the grain is far cheaper than per-prop or even per-game: ONE statsapi schedule call per game DATE returns every game that day with venue, linescore and scoring plays. Fifteen games, one call, verified live. PUBLIC BASE — the ingestion was already done. The static FanGraphs table from Session 15 is the public base; this converts its 100-indexed values into the multipliers the composable architecture wants (Coors 128 becomes 1.28) rather than ingesting a second copy of a number we already hold. It is labelled COMMODITY in the code, not just in a comment. Every resolution carries a provenance record, and the public one reads proprietary: false with the note "Commodity: a public number. Not a VYNDR derivation." The proprietary label exists but belongs only to the self-derived version, and only once it beats this base on the instrument. A surface rendering a park effect can state which it is rather than implying the flattering one. Honest-absent where even the PUBLIC number is thin: a relocated club in a temporary venue gets no factor, because a public number for a park with one season behind it is no more trustworthy than ours would be. SOURCE-PLUGGABLE is the architectural point. resolveParkBase() is the only accessor, public and derived return identical shapes, and callers never branch on source — so when self-derived factors clear their floor they swap into the same slot with nothing downstream to rewrite. A derived source with no factor available returns absent rather than silently falling back to public, because a silent fallback would make a proprietary claim out of a commodity number. GAME-LEVEL CAPTURE starts now because it cannot start retroactively. Game grain, deduped on game_id, never copied onto prop rows — a game's totals belong to the game, and duplicating them per prop is how one fact starts disagreeing with itself. Every field is tied to a named future derivation: venue for park factors, runs for the run environment, HR totals for HR factors. Nothing else is stored. Only Final games are captured, since an in-progress total is not a result, and a game with no scoring plays reports HR as absent rather than zero. HR totals come from scoring plays, which is complete because every home run scores at least the batter. The accrual target is stated rather than promised: 150 home games per venue at roughly 81 per season means about two seasons before a self-derived factor can be nominated, and accrualStatus() reports live progress per venue so the wait is measurable. Induced: Coors home runs +0.061 for the hitter and identically +0.061 for the pitcher's home-runs-allowed at the same park, mirrored on the under; San Francisco negative; Tampa flagged weather-N/A with its factor still applying; the Athletics' temporary venue absent; strikeouts untouched. A real 2025-07-19 capture produced 15 games across 15 venues, 12 with HR totals, zero duplicate game ids. Migration 035. Induce with POST /api/internal/gamectx/:date, progress at /gamectx/accrual. Tests 3688 passed / 298 suites, web build exit 0. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VCNgGSt5qvcLxaeQqa7Zpj
This commit is contained in:
@@ -431,6 +431,23 @@ router.post('/newsletter/send', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
/** POST /api/internal/gamectx/:date — induce a game-context capture on demand. */
|
||||
router.post('/gamectx/:date', async (req, res) => {
|
||||
try {
|
||||
const gc = require('../services/gameContext');
|
||||
const out = await gc.captureDate(req.params.date, { sport: req.query.sport || 'mlb' });
|
||||
return res.status(out.ok ? 200 : 500).json(out);
|
||||
} catch (err) { return res.status(500).json({ ok: false, error: err.message }); }
|
||||
});
|
||||
|
||||
/** GET /api/internal/gamectx/accrual — how far from a self-derived park factor. */
|
||||
router.get('/gamectx/accrual', async (req, res) => {
|
||||
try {
|
||||
const gc = require('../services/gameContext');
|
||||
return res.json(await gc.accrualStatus({ sport: req.query.sport || 'mlb' }));
|
||||
} catch (err) { return res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
/**
|
||||
* POST /api/internal/statcast/refresh — INDUCE the Layer-1 mechanism refresh.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,183 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* GAME-LEVEL CONTEXT CAPTURE (Layer 3, Step 4b).
|
||||
*
|
||||
* We store prop-level rows only, so park factors cannot be self-derived from
|
||||
* our own data. This starts the accrual that eventually can — and it is
|
||||
* FORWARD-ONLY: a day not captured is permanently lost, which is the entire
|
||||
* justification for storing anything before it is needed.
|
||||
*
|
||||
* ── WHY THIS IS JUSTIFIED STORAGE AND NOT HOARDING ───────────────────────
|
||||
* Three tests, all of which this passes and most speculative capture fails:
|
||||
* 1. NAMED NEED — every field maps to one specific future derivation
|
||||
* (venue → park factors, runs → run environment, HR → HR park factors).
|
||||
* A field with no named purpose is not stored.
|
||||
* 2. UNRECOVERABLE IF MISSED — statsapi serves historical schedules, but our
|
||||
* *joined* view of which games our props sat in is not reconstructable
|
||||
* cheaply, and the habit of capturing late is how the p_win gap happened.
|
||||
* 3. CHEAP — ONE schedule call per game DATE returns every game that day with
|
||||
* venue, linescore and scoring plays. Not per game, and certainly not per
|
||||
* prop.
|
||||
*
|
||||
* ── GRAIN ────────────────────────────────────────────────────────────────
|
||||
* One row per game, deduped on `game_id`. Never copied onto prop rows: a game's
|
||||
* totals belong to the game, and duplicating them per prop is how the same fact
|
||||
* starts disagreeing with itself.
|
||||
*
|
||||
* ── ACCRUAL TARGET (so "later" is a date, not a vibe) ─────────────────────
|
||||
* The derived park factor needs MIN_GAMES (150) home games per venue in the
|
||||
* current regime. At ~81 home games per park per season that is ~2 SEASONS of
|
||||
* capture before a self-derived factor can even be NOMINATED against the public
|
||||
* base. `accrualStatus()` reports live progress so the wait is measurable.
|
||||
*/
|
||||
|
||||
const { MIN_GAMES } = require('./parkFactors');
|
||||
|
||||
const HOME_GAMES_PER_SEASON = 81;
|
||||
|
||||
const num = (v) => {
|
||||
if (v == null || v === '') return null;
|
||||
const n = typeof v === 'number' ? v : Number(v);
|
||||
return Number.isFinite(n) ? n : null;
|
||||
};
|
||||
|
||||
/**
|
||||
* rowsFromSchedule(payload, opts) — PURE. statsapi schedule → game_context rows.
|
||||
*
|
||||
* Only FINAL games are stored: a suspended or in-progress game has totals that
|
||||
* are not the game's totals, and storing them would poison every factor derived
|
||||
* from them later.
|
||||
*/
|
||||
function rowsFromSchedule(payload, opts = {}) {
|
||||
const sport = opts.sport || 'mlb';
|
||||
const out = [];
|
||||
for (const dt of (payload && payload.dates) || []) {
|
||||
for (const g of dt.games || []) {
|
||||
const status = (g.status && g.status.detailedState) || null;
|
||||
if (status !== 'Final') continue; // in-progress totals are not results
|
||||
|
||||
const ls = g.linescore || {};
|
||||
const teams = ls.teams || {};
|
||||
const home = (g.teams && g.teams.home) || {};
|
||||
const away = (g.teams && g.teams.away) || {};
|
||||
|
||||
// Every home run scores at least the batter, so scoringPlays carries all
|
||||
// of them — no per-game boxscore fetch needed.
|
||||
const plays = g.scoringPlays || [];
|
||||
const hrPlays = plays.filter((p) => ((p.result || {}).event || '') === 'Home Run');
|
||||
const homeHr = hrPlays.filter((p) => (p.about || {}).halfInning === 'bottom').length;
|
||||
const awayHr = hrPlays.filter((p) => (p.about || {}).halfInning === 'top').length;
|
||||
|
||||
const homeRuns = num((teams.home || {}).runs);
|
||||
const awayRuns = num((teams.away || {}).runs);
|
||||
|
||||
out.push({
|
||||
game_id: `${sport}:${g.gamePk}`,
|
||||
sport,
|
||||
season: num(g.season) || (dt.date ? Number(String(dt.date).slice(0, 4)) : null),
|
||||
game_date: dt.date || (g.gameDate ? String(g.gameDate).slice(0, 10) : null),
|
||||
venue_id: num((g.venue || {}).id),
|
||||
venue_name: (g.venue || {}).name || null,
|
||||
home_team: ((home.team || {}).name) || null,
|
||||
away_team: ((away.team || {}).name) || null,
|
||||
home_runs: homeRuns,
|
||||
away_runs: awayRuns,
|
||||
total_runs: homeRuns == null || awayRuns == null ? null : homeRuns + awayRuns,
|
||||
home_hr: hrPlays.length ? homeHr : null,
|
||||
away_hr: hrPlays.length ? awayHr : null,
|
||||
total_hr: hrPlays.length ? hrPlays.length : null,
|
||||
status,
|
||||
});
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
const SCHEDULE_URL = (date) =>
|
||||
`https://statsapi.mlb.com/api/v1/schedule?sportId=1&startDate=${date}&endDate=${date}`
|
||||
+ '&gameType=R&hydrate=linescore,venue,scoringplays';
|
||||
|
||||
/**
|
||||
* captureDate(date, opts) — ONE call per game DATE, upserted on game_id.
|
||||
* Idempotent: re-running a date refreshes nothing that matters and never
|
||||
* duplicates.
|
||||
*/
|
||||
async function captureDate(date, opts = {}) {
|
||||
const sport = opts.sport || 'mlb';
|
||||
if (!date) return { ok: false, reason: 'no date' };
|
||||
|
||||
let payload;
|
||||
try {
|
||||
payload = opts.fetchJson
|
||||
? await opts.fetchJson(SCHEDULE_URL(date))
|
||||
: (await require('axios').get(SCHEDULE_URL(date), { timeout: 60_000 })).data;
|
||||
} catch (err) {
|
||||
return { ok: false, reason: `fetch failed: ${err.message}`, date };
|
||||
}
|
||||
|
||||
const rows = rowsFromSchedule(payload, { sport });
|
||||
if (!rows.length) return { ok: true, date, games: 0, written: 0, reason: 'no final games' };
|
||||
|
||||
const sb = opts.sb || getClient();
|
||||
if (!sb) return { ok: false, reason: 'supabase not configured', games: rows.length, written: 0 };
|
||||
|
||||
const { error } = await sb.from('game_context').upsert(rows, { onConflict: 'game_id' });
|
||||
if (error) return { ok: false, reason: `upsert failed: ${error.message}`, games: rows.length, written: 0 };
|
||||
|
||||
return {
|
||||
ok: true, date, games: rows.length, written: rows.length,
|
||||
venues: new Set(rows.map((r) => r.venue_id)).size,
|
||||
with_hr: rows.filter((r) => r.total_hr != null).length,
|
||||
};
|
||||
}
|
||||
|
||||
function getClient() {
|
||||
try { return require('../utils/supabase').getSupabaseServiceClient(); } catch { return null; }
|
||||
}
|
||||
|
||||
/**
|
||||
* accrualStatus(opts) — how far from a self-derived factor are we, per venue?
|
||||
* Makes "later" measurable instead of a promise.
|
||||
*/
|
||||
async function accrualStatus(opts = {}) {
|
||||
const sb = opts.sb || getClient();
|
||||
if (!sb) return { ok: false, reason: 'supabase not configured' };
|
||||
const { data, error } = await sb
|
||||
.from('game_context')
|
||||
.select('venue_id, venue_name')
|
||||
.eq('sport', opts.sport || 'mlb')
|
||||
.limit(20000);
|
||||
if (error) return { ok: false, reason: error.message };
|
||||
|
||||
const byVenue = new Map();
|
||||
for (const r of data || []) {
|
||||
if (r.venue_id == null) continue;
|
||||
const v = byVenue.get(r.venue_id) || { venue: r.venue_name, games: 0 };
|
||||
v.games += 1;
|
||||
byVenue.set(r.venue_id, v);
|
||||
}
|
||||
const venues = [...byVenue.entries()].map(([id, v]) => ({
|
||||
venue_id: id, venue: v.venue, games: v.games,
|
||||
ready: v.games >= MIN_GAMES,
|
||||
remaining: Math.max(0, MIN_GAMES - v.games),
|
||||
})).sort((a, b) => b.games - a.games);
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
total_games: (data || []).length,
|
||||
venues_tracked: venues.length,
|
||||
venues_ready: venues.filter((v) => v.ready).length,
|
||||
min_games_per_venue: MIN_GAMES,
|
||||
seasons_needed_estimate: Math.round((MIN_GAMES / HOME_GAMES_PER_SEASON) * 10) / 10,
|
||||
venues: venues.slice(0, 5),
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
captureDate,
|
||||
rowsFromSchedule,
|
||||
accrualStatus,
|
||||
SCHEDULE_URL,
|
||||
HOME_GAMES_PER_SEASON,
|
||||
};
|
||||
@@ -0,0 +1,143 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* PARK BASE — the SOURCE-PLUGGABLE environment coefficient (Layer 3, Step 4b).
|
||||
*
|
||||
* One slot, two possible sources, identical output shape:
|
||||
*
|
||||
* 'public' — the static FanGraphs table (src/data/parkFactors.js, S15).
|
||||
* COMMODITY. A stable public number anyone can look up.
|
||||
* 'derived' — our own multi-season derivation (src/services/parkFactors.js,
|
||||
* S73). Not yet nominated: it needs the game-level accrual that
|
||||
* `gameContext` starts collecting this session.
|
||||
*
|
||||
* The pluggability is the point. When the self-derived factors clear their
|
||||
* sample floor and beat the public base on the instrument, they swap into this
|
||||
* same slot — no rearchitecting downstream, because everything above consumes
|
||||
* `resolveParkBase()` and never a source directly.
|
||||
*
|
||||
* ── THE HONESTY LINE ─────────────────────────────────────────────────────
|
||||
* The public base is COMMODITY. It must never be marketed or described as a
|
||||
* proprietary park factor — it is a number anyone can read off FanGraphs. The
|
||||
* proprietary claim is reserved for the self-derived, mechanism-conditioned
|
||||
* version, and only once it beats this base on the settled ledger.
|
||||
* `PROVENANCE` below carries that label so any surface rendering a park effect
|
||||
* can state which it is.
|
||||
*/
|
||||
|
||||
const publicTable = require('../data/parkFactors');
|
||||
|
||||
const PROVENANCE = Object.freeze({
|
||||
public: {
|
||||
source: 'fangraphs_public',
|
||||
label: 'PUBLIC PARK FACTOR',
|
||||
proprietary: false,
|
||||
note: 'Commodity: a public number. Not a VYNDR derivation.',
|
||||
},
|
||||
derived: {
|
||||
source: 'vyndr_derived',
|
||||
label: 'VYNDR PARK FACTOR',
|
||||
proprietary: true,
|
||||
note: 'Self-derived from captured game context. Nominated only after it beats the public base.',
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* Parks whose PUBLIC factor is itself thin or unstable — new builds, relocated
|
||||
* clubs, temporary venues. A public number for a park with one season behind it
|
||||
* is no more trustworthy than our own would be, so it is HONEST-ABSENT rather
|
||||
* than emitted with false confidence.
|
||||
*
|
||||
* ATH: the Athletics' temporary Sacramento venue (2025–). SAC/LV as the club
|
||||
* moves. Extend this list when a park opens or a club relocates — the cost of a
|
||||
* missing entry is a confident factor for a stadium nobody has data on.
|
||||
*/
|
||||
const UNSTABLE_PUBLIC = Object.freeze(new Set(['ATH', 'OAK', 'SAC', 'LV']));
|
||||
|
||||
/** Domes + retractable roofs — reused from the derived service so the two
|
||||
* sources agree on which venues weather cannot touch. */
|
||||
const { DOME_VENUES } = require('./parkFactors');
|
||||
const DOME_TEAMS = Object.freeze(new Set(['TB', 'TOR', 'ARI', 'HOU', 'MIL', 'TEX', 'MIA', 'SEA']));
|
||||
|
||||
const num = (v) => {
|
||||
if (v == null || v === '') return null;
|
||||
const n = typeof v === 'number' ? v : Number(v);
|
||||
return Number.isFinite(n) ? n : null;
|
||||
};
|
||||
|
||||
/** The public table is indexed to 100. The composable architecture wants a
|
||||
* multiplier around 1.0, so 128 → 1.28. */
|
||||
const toMultiplier = (indexed) => {
|
||||
const n = num(indexed);
|
||||
return n == null || n <= 0 ? null : Math.round((n / 100) * 1000) / 1000;
|
||||
};
|
||||
|
||||
/**
|
||||
* resolveParkBase({ teamAbbr, venueName, source, derived }) — the ONE accessor.
|
||||
*
|
||||
* Returns the same record shape whichever source answers, so callers never
|
||||
* branch on provenance:
|
||||
* { state, hr_base, run_base, venue, weather_na, provenance, source }
|
||||
* with `state` one of 'present' | 'absent' — and `weather_na` orthogonal to
|
||||
* both, exactly as the S73 derivation defined it (a dome's park factor still
|
||||
* applies; the flag only tells the weather arc to stand down).
|
||||
*/
|
||||
function resolveParkBase({ teamAbbr, venueName, source = 'public', derived = null } = {}) {
|
||||
const abbr = String(teamAbbr || '').toUpperCase();
|
||||
const weatherNa = DOME_TEAMS.has(abbr) || (venueName ? DOME_VENUES.has(venueName) : false);
|
||||
|
||||
const absent = (reason, prov) => ({
|
||||
state: 'absent', hr_base: null, run_base: null,
|
||||
venue: venueName || abbr || null, weather_na: weatherNa,
|
||||
provenance: prov, source: prov.source, reason,
|
||||
});
|
||||
|
||||
// ── self-derived, when it has been nominated ──────────────────────────
|
||||
if (source === 'derived') {
|
||||
const prov = PROVENANCE.derived;
|
||||
if (!derived || derived.state !== 'present') {
|
||||
return absent('self-derived factor not available for this venue', prov);
|
||||
}
|
||||
return {
|
||||
state: 'present',
|
||||
hr_base: num(derived.hr_base),
|
||||
run_base: num(derived.run_base),
|
||||
venue: derived.venue || venueName || null,
|
||||
weather_na: derived.weather_na ?? weatherNa,
|
||||
provenance: prov, source: prov.source,
|
||||
games: derived.games ?? null, seasons: derived.seasons ?? null,
|
||||
regime_start: derived.regime_start ?? null,
|
||||
reason: null,
|
||||
};
|
||||
}
|
||||
|
||||
// ── public (commodity) base ───────────────────────────────────────────
|
||||
const prov = PROVENANCE.public;
|
||||
if (!abbr) return absent('no team/venue to resolve', prov);
|
||||
if (UNSTABLE_PUBLIC.has(abbr)) {
|
||||
// Even the PUBLIC number is thin here — a new or temporary park. Emitting
|
||||
// it would dress a shaky number as a settled one.
|
||||
return absent('public factor is itself thin/unstable for this venue', prov);
|
||||
}
|
||||
const row = publicTable.getParkFactor(abbr);
|
||||
if (!row) return absent('no public factor for this team', prov);
|
||||
|
||||
const hr = toMultiplier(row.hr);
|
||||
const run = toMultiplier(row.r);
|
||||
if (hr == null && run == null) return absent('public factor unparseable', prov);
|
||||
|
||||
return {
|
||||
state: 'present',
|
||||
hr_base: hr, run_base: run,
|
||||
venue: venueName || abbr, weather_na: weatherNa,
|
||||
provenance: prov, source: prov.source, reason: null,
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
resolveParkBase,
|
||||
PROVENANCE,
|
||||
UNSTABLE_PUBLIC,
|
||||
DOME_TEAMS,
|
||||
__internals: { toMultiplier, num },
|
||||
};
|
||||
@@ -286,6 +286,19 @@ function startSnapshotScheduler(opts = {}) {
|
||||
}
|
||||
} catch (e) { console.warn('[harness] nightly run failed:', e.message); }
|
||||
|
||||
// Session 74 — GAME-LEVEL CONTEXT. One schedule call per game DATE (not per
|
||||
// game, not per prop) capturing venue + final runs + HR totals for the day
|
||||
// that just completed. Forward-only: a day not captured is permanently
|
||||
// lost, which is why this runs beside the settle pass rather than waiting
|
||||
// until a self-derived park factor is wanted.
|
||||
try {
|
||||
const gc = opts.gameContext || require('./services/gameContext');
|
||||
const d = new Date(now().getTime() - 24 * 3600 * 1000);
|
||||
const ymd = d.toISOString().slice(0, 10);
|
||||
const r = await gc.captureDate(ymd, { sport: 'mlb' });
|
||||
if (r && r.games) console.log(`[gamectx] ${ymd} — ${r.written}/${r.games} games, ${r.venues} venues, ${r.with_hr} with HR totals`);
|
||||
} catch (e) { console.warn('[gamectx] capture failed:', e.message); }
|
||||
|
||||
// Session 70 — THE MEASUREMENT INSTRUMENT. Attach the de-vigged CLOSING
|
||||
// probability to locked rows from the append-only closing_captures, so
|
||||
// p_win, the close, the archetype vector and the outcome all land on ONE
|
||||
|
||||
@@ -0,0 +1,178 @@
|
||||
/* ============================================================
|
||||
Session 74 — PUBLIC PARK BASE (source-pluggable) + GAME-CONTEXT CAPTURE.
|
||||
============================================================ */
|
||||
|
||||
const pb = require('../../src/services/parkBase');
|
||||
const pf = require('../../src/services/parkFactors');
|
||||
const gc = require('../../src/services/gameContext');
|
||||
const ch = require('../../src/services/challengerProjection');
|
||||
|
||||
describe('public base — commodity, and labelled as such', () => {
|
||||
it('converts the 100-indexed public table to a multiplier', () => {
|
||||
const col = pb.resolveParkBase({ teamAbbr: 'COL' });
|
||||
expect(col.state).toBe('present');
|
||||
expect(col.hr_base).toBe(1.28); // 128 → 1.28
|
||||
expect(col.run_base).toBe(1.2);
|
||||
});
|
||||
|
||||
it('NEVER claims the public number is proprietary', () => {
|
||||
const col = pb.resolveParkBase({ teamAbbr: 'COL' });
|
||||
expect(col.provenance.proprietary).toBe(false);
|
||||
expect(col.provenance.label).toBe('PUBLIC PARK FACTOR');
|
||||
expect(col.provenance.note).toMatch(/Not a VYNDR derivation/);
|
||||
});
|
||||
|
||||
it('a pitcher park reads below 1', () => {
|
||||
expect(pb.resolveParkBase({ teamAbbr: 'SF' }).hr_base).toBeLessThan(1);
|
||||
});
|
||||
|
||||
it('HONEST-ABSENT where even the PUBLIC factor is thin/unstable', () => {
|
||||
// A relocated club in a temporary venue: a public number here is no more
|
||||
// trustworthy than ours would be.
|
||||
for (const t of ['ATH', 'SAC']) {
|
||||
const r = pb.resolveParkBase({ teamAbbr: t });
|
||||
expect(r.state).toBe('absent');
|
||||
expect(r.hr_base).toBeNull();
|
||||
expect(r.reason).toMatch(/thin\/unstable/);
|
||||
}
|
||||
});
|
||||
|
||||
it('unknown team → absent, never a neutral guess', () => {
|
||||
expect(pb.resolveParkBase({ teamAbbr: 'ZZZ' }).state).toBe('absent');
|
||||
expect(pb.resolveParkBase({}).state).toBe('absent');
|
||||
});
|
||||
|
||||
it('flags domes weather_na while the park factor STILL applies', () => {
|
||||
const tb = pb.resolveParkBase({ teamAbbr: 'TB' });
|
||||
expect(tb.weather_na).toBe(true);
|
||||
expect(tb.state).toBe('present'); // N/A is not absent
|
||||
});
|
||||
});
|
||||
|
||||
describe('SOURCE-PLUGGABLE — same slot, no rearchitecting', () => {
|
||||
const derived = { state: 'present', hr_base: 1.11, run_base: 1.05, venue: 'Coors Field', weather_na: false, games: 162, seasons: 4 };
|
||||
|
||||
it('self-derived swaps into the same shape and IS proprietary', () => {
|
||||
const d = pb.resolveParkBase({ teamAbbr: 'COL', source: 'derived', derived });
|
||||
expect(d.state).toBe('present');
|
||||
expect(d.hr_base).toBe(1.11);
|
||||
expect(d.provenance.proprietary).toBe(true);
|
||||
expect(d.provenance.label).toBe('VYNDR PARK FACTOR');
|
||||
// identical keys to the public answer → callers never branch on source
|
||||
const p = pb.resolveParkBase({ teamAbbr: 'COL' });
|
||||
expect(Object.keys(d)).toEqual(expect.arrayContaining(Object.keys(p)));
|
||||
});
|
||||
|
||||
it('derived-but-unavailable is absent, never silently falls back', () => {
|
||||
const r = pb.resolveParkBase({ teamAbbr: 'COL', source: 'derived', derived: null });
|
||||
expect(r.state).toBe('absent');
|
||||
expect(r.reason).toMatch(/self-derived factor not available/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('composes into the challenger, directional by prop-owner', () => {
|
||||
const env = (b, stat) => ({ multiplier: pf.parkMultiplier({ factor: b, statType: stat }), label: b.provenance.label, venue: b.venue, weather_na: b.weather_na });
|
||||
const col = pb.resolveParkBase({ teamAbbr: 'COL' });
|
||||
|
||||
it('hitter HR and pitcher HR-allowed move the SAME way at the same park', () => {
|
||||
const h = ch.adjust({ pWin: 0.5, direction: 'over', statType: 'home_runs', environment: env(col, 'home_runs') });
|
||||
const p = ch.adjust({ pWin: 0.5, direction: 'over', statType: 'home_runs_allowed', environment: env(col, 'home_runs_allowed') });
|
||||
expect(h.delta).toBeGreaterThan(0);
|
||||
expect(p.delta).toBe(h.delta);
|
||||
});
|
||||
|
||||
it('mirrors on the under and does nothing for an unrelated stat', () => {
|
||||
const over = ch.adjust({ pWin: 0.5, direction: 'over', statType: 'home_runs', environment: env(col, 'home_runs') });
|
||||
const under = ch.adjust({ pWin: 0.5, direction: 'under', statType: 'home_runs', environment: env(col, 'home_runs') });
|
||||
expect(under.delta).toBeCloseTo(-over.delta, 3);
|
||||
expect(ch.adjust({ pWin: 0.5, direction: 'over', statType: 'strikeouts', environment: env(col, 'strikeouts') }).delta).toBe(0);
|
||||
});
|
||||
|
||||
it('an absent park adjusts nothing', () => {
|
||||
const ath = pb.resolveParkBase({ teamAbbr: 'ATH' });
|
||||
expect(ch.adjust({ pWin: 0.5, direction: 'over', statType: 'home_runs', environment: env(ath, 'home_runs') }).delta).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('game-context capture — game grain, named purpose, finals only', () => {
|
||||
const payload = {
|
||||
dates: [{
|
||||
date: '2025-07-19',
|
||||
games: [
|
||||
{ gamePk: 777094, season: '2025', status: { detailedState: 'Final' },
|
||||
venue: { id: 14, name: 'Rogers Centre' },
|
||||
teams: { home: { team: { name: 'Toronto Blue Jays' } }, away: { team: { name: 'New York Yankees' } } },
|
||||
linescore: { teams: { home: { runs: 5 }, away: { runs: 4 } } },
|
||||
scoringPlays: [
|
||||
{ result: { event: 'Home Run' }, about: { halfInning: 'bottom' } },
|
||||
{ result: { event: 'Home Run' }, about: { halfInning: 'top' } },
|
||||
{ result: { event: 'Single' }, about: { halfInning: 'top' } },
|
||||
] },
|
||||
{ gamePk: 777095, season: '2025', status: { detailedState: 'In Progress' },
|
||||
venue: { id: 15, name: 'Other Park' }, linescore: { teams: { home: { runs: 2 }, away: { runs: 1 } } } },
|
||||
],
|
||||
}],
|
||||
};
|
||||
|
||||
it('one row per game, keyed for dedupe', () => {
|
||||
const rows = gc.rowsFromSchedule(payload);
|
||||
expect(rows).toHaveLength(1); // the in-progress game is excluded
|
||||
expect(rows[0].game_id).toBe('mlb:777094');
|
||||
expect(new Set(rows.map((r) => r.game_id)).size).toBe(rows.length);
|
||||
});
|
||||
|
||||
it('EXCLUDES non-final games — in-progress totals are not results', () => {
|
||||
expect(gc.rowsFromSchedule(payload).every((r) => r.status === 'Final')).toBe(true);
|
||||
});
|
||||
|
||||
it('captures exactly the NAMED-PURPOSE fields', () => {
|
||||
const r = gc.rowsFromSchedule(payload)[0];
|
||||
expect(r.venue_id).toBe(14); // → self-derived park factors
|
||||
expect(r.total_runs).toBe(9); // → run-environment factors
|
||||
expect(r.total_hr).toBe(2); // → HR park factors
|
||||
expect(r.home_hr).toBe(1);
|
||||
expect(r.away_hr).toBe(1);
|
||||
// No speculative fields hoarded "just in case".
|
||||
expect(Object.keys(r).sort()).toEqual([
|
||||
'away_hr', 'away_runs', 'away_team', 'game_date', 'game_id', 'home_hr',
|
||||
'home_runs', 'home_team', 'season', 'sport', 'status', 'total_hr',
|
||||
'total_runs', 'venue_id', 'venue_name',
|
||||
]);
|
||||
});
|
||||
|
||||
it('HR totals come from scoring plays — every HR scores, so none are missed', () => {
|
||||
const r = gc.rowsFromSchedule(payload)[0];
|
||||
expect(r.total_hr).toBe(2); // the Single is not counted
|
||||
});
|
||||
|
||||
it('a game with no scoring plays reports HR as ABSENT, not zero', () => {
|
||||
const noPlays = { dates: [{ date: '2025-07-19', games: [{ gamePk: 1, status: { detailedState: 'Final' }, venue: { id: 1 }, linescore: { teams: { home: { runs: 0 }, away: { runs: 0 } } } }] }] };
|
||||
expect(gc.rowsFromSchedule(noPlays)[0].total_hr).toBeNull();
|
||||
});
|
||||
|
||||
it('upserts on game_id — capture is idempotent per date', async () => {
|
||||
let opts = null;
|
||||
const sb = { from: () => ({ upsert: async (_r, o) => { opts = o; return { error: null }; } }) };
|
||||
await gc.captureDate('2025-07-19', { sb, fetchJson: async () => payload });
|
||||
expect(opts.onConflict).toBe('game_id');
|
||||
});
|
||||
|
||||
it('one call per DATE — not per game, not per prop', () => {
|
||||
expect(gc.SCHEDULE_URL('2025-07-19')).toMatch(/startDate=2025-07-19&endDate=2025-07-19/);
|
||||
expect(gc.SCHEDULE_URL('2025-07-19')).toMatch(/hydrate=linescore,venue,scoringplays/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('accrual target — "later" is measurable', () => {
|
||||
it('states the per-venue floor and the season estimate', async () => {
|
||||
const sb = { from: () => ({ select: () => ({ eq: () => ({ limit: async () => ({ data: [
|
||||
...Array(120).fill({ venue_id: 14, venue_name: 'Rogers Centre' }),
|
||||
...Array(160).fill({ venue_id: 19, venue_name: 'Coors Field' }),
|
||||
], error: null }) }) }) }) };
|
||||
const s = await gc.accrualStatus({ sb });
|
||||
expect(s.min_games_per_venue).toBe(pf.MIN_GAMES);
|
||||
expect(s.venues_ready).toBe(1); // Coors has 160 ≥ 150
|
||||
expect(s.seasons_needed_estimate).toBeGreaterThan(1); // ~2 seasons
|
||||
expect(s.venues.find((v) => v.venue_id === 14).remaining).toBe(30);
|
||||
});
|
||||
});
|
||||
+1
-1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user