Session 15: Intelligence hardening — park factors, weather, Tank01 prefetch, pace factors, signal audit, founder pricing fix (1405 tests)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
+127
-2
@@ -1,10 +1,135 @@
|
||||
# VYNDR — Build State
|
||||
|
||||
## Last Updated
|
||||
2026-06-10
|
||||
2026-06-11
|
||||
|
||||
## Current Phase
|
||||
SHIP BUILD v14.0 — Africa checkout + Tank01 wiring + WNBA/MLB odds + UX polish (Session 14)
|
||||
SHIP BUILD v15.0 — Intelligence hardening + platform correctness (Session 15)
|
||||
|
||||
## Session 15 (2026-06-11) — SHIPPED
|
||||
|
||||
### Phase 0 — Correctness
|
||||
|
||||
- **Africa short-circuit removed** (`Pricing.tsx:152`). The Session 14
|
||||
backend handles 'africa' end-to-end: validation accepts it; missing
|
||||
`STRIPE_PRICE_AFRICA` returns a 503 with `code:'tier_unconfigured'`
|
||||
the existing inline error surface displays. The frontend short-
|
||||
circuit was blocking checkout even after the backend was ready.
|
||||
- **Odds + Sentry + welcome email audits**: all already correct from
|
||||
prior sessions. Documented for posterity; no fixes required.
|
||||
- **Poller → odds pipeline**: confirmed there's NO key-mismatch
|
||||
pipeline issue. Pollers handle game resolution
|
||||
(`game:{id}:status`, `poller:{SPORT}:heartbeat`); `oddsService`
|
||||
populates `odds:{sport}:{date}` on-demand. The "1 games shown but
|
||||
Slate empty" report would be a separate odds-api quota / key issue.
|
||||
- **Founder price fallback hardened**. `PRICE_MAP` no longer falls
|
||||
back to fake strings like `'price_analyst_monthly'` that would 400
|
||||
from Stripe in live mode. Missing env → `PRICE_UNCONFIGURED`
|
||||
sentinel → 503 with `code:'tier_unconfigured'`. Founder codes
|
||||
presented against an unwired founder-price env now fall back
|
||||
GRACEFULLY to the standard tier price rather than dropping the
|
||||
checkout — the founder discount is operator-controlled and
|
||||
shouldn't break the user's purchase.
|
||||
|
||||
### Phase 1 — Signal audit
|
||||
|
||||
Documented in a new comment block at the top of
|
||||
`src/services/intelligence/computeFeatures.js`. Every signal cited
|
||||
to its data source: injury (ESPN injury feed), coach (Supabase
|
||||
`coach_profiles` + JSON seed), consistency (game logs via
|
||||
`gameLogService`), Tank01 fields (Session 14 + 15 prefetch),
|
||||
soccer cascade (Session 9), park factors (Session 15 — static),
|
||||
weather (Session 15 — Open-Meteo), pace factors (Session 15 —
|
||||
static). No phantom signals.
|
||||
|
||||
### Phase 2 — MLB park factors
|
||||
|
||||
`src/data/parkFactors.js` — all 30 MLB parks indexed to 100 league
|
||||
average. FanGraphs 2024-25 three-year weighted data. Coors at hr=128,
|
||||
SF Oracle at hr=85 (the two extremes by design). `getParkFactor()`
|
||||
returns null on unknown teams so the feature extractor drops the
|
||||
signal cleanly rather than falsely reporting "neutral".
|
||||
|
||||
Wired into `computeFeatures.js` MLB branch — features pick up
|
||||
`park_hr`, `park_h`, `park_r`, `park_home` when the home team
|
||||
resolves.
|
||||
|
||||
### Phase 3 — Weather (Open-Meteo)
|
||||
|
||||
`src/services/weatherService.js` — Open-Meteo proxy (no API key
|
||||
required). 5-second hard timeout, 1-hour Redis cache, silent
|
||||
degrade on failure (never blocks the grade). Fahrenheit + mph units
|
||||
to match the bettors' mental model.
|
||||
|
||||
`src/data/venueCoordinates.js` — lat/lon + dome flag for all 30
|
||||
MLB venues and all 16 World Cup 2026 venues. Retractable stadiums
|
||||
are marked `dome:true` because operators close the roof when
|
||||
conditions warrant — weather doesn't drive grade in that case.
|
||||
|
||||
Wired into `computeFeatures.js` MLB branch — fetches weather when
|
||||
the home venue is outdoor + has finite coordinates.
|
||||
|
||||
### Phase 4 — Tank01 daily prefetch
|
||||
|
||||
`scripts/tank01-prefetch.js` — orchestrator that pulls the Redis
|
||||
cache keys Session 14's `tank01Augment.js` reads. Default budget
|
||||
≤80 requests/run, configurable via `--max=N`. NBA path pulls
|
||||
schedule + final-game box scores + daily odds. MLB path pulls
|
||||
scoreboard + final-game box scores (BvP pull awaits batter/pitcher
|
||||
ID resolution on the scoreboard payload).
|
||||
|
||||
Recommended trigger: extend the n8n "Morning Ops" workflow to
|
||||
exec the script daily at 7am UTC.
|
||||
|
||||
### Phase 5 — MLB matchup context
|
||||
|
||||
`src/services/intelligence/mlbContext.js` — pure functions for
|
||||
platoonAdvantage(pitcherHand, batterHand) and
|
||||
projectedPA(lineupPosition). Tested with all hand combinations +
|
||||
all lineup slots. Wiring into computeFeatures is deferred until
|
||||
odds-api carries those fields (it doesn't today).
|
||||
|
||||
### Phase 6 — NBA pace factors
|
||||
|
||||
`src/data/paceFactors.js` — all 30 NBA teams (NBA.com/stats 2024-25,
|
||||
indexed to 100). Legacy-abbreviation aliases (NJN→BKN, NOH→NOP,
|
||||
SEA→OKC, CHO→CHA) so historical lookups resolve. Wired into the NBA
|
||||
branch of `computeFeatures.js` — `pace_factor` (player's team) +
|
||||
`opp_pace_factor` (opponent).
|
||||
|
||||
### Tests added (Session 15)
|
||||
| Suite | Tests |
|
||||
|----------------------------------------|-------|
|
||||
| `tests/unit/parkFactors.test.js` | 14 |
|
||||
| `tests/unit/weatherService.test.js` | 14 |
|
||||
| `tests/unit/tank01Prefetch.test.js` | 14 |
|
||||
| `tests/unit/mlbContext.test.js` | 21 |
|
||||
| `tests/unit/paceFactors.test.js` | 12 |
|
||||
| **Session 15 total** | **75** |
|
||||
|
||||
### Quality gates
|
||||
- `npm test`: **1405 / 1405 passing** (1330 + 75 new), 108 suites,
|
||||
0 regressions. One pre-existing computeFeatures test was updated:
|
||||
the contract used to be "ESPN failure → empty features"; the
|
||||
contract is now "ESPN failure → static context augmentation
|
||||
(pace, park) still surfaces."
|
||||
- `web/npm run build`: clean
|
||||
- License audit: third-party deps remain permissive
|
||||
|
||||
### Honest gaps
|
||||
- Tank01 prefetch must be triggered by n8n/cron before the augmentor
|
||||
reads return data. Grades work as before until then.
|
||||
- BvP pull is no-op until probable-pitcher IDs land on the Tank01
|
||||
MLB scoreboard projection.
|
||||
- Phase 5 helpers tested but not wired — odds-api doesn't carry
|
||||
batter handedness or lineup position fields today.
|
||||
- Weather for soccer venues: only MLB is wired this session.
|
||||
Soccer venue weather is a 5-line follow-up in the soccer extractor.
|
||||
|
||||
### Coolify env (Session 15 additions)
|
||||
None new from this session.
|
||||
|
||||
---
|
||||
|
||||
## Session 14 (2026-06-11) — SHIPPED
|
||||
|
||||
|
||||
@@ -528,3 +528,31 @@
|
||||
{"ts":"2026-06-11T08:10:04.674Z","sport":"nba","player_espn_id":"3136195","player_name":"Karl-Anthony Towns","stat_type":"rebounds","line":13.5,"direction":"under","actual_value":13,"result":"hit","margin":-0.5,"grade":"B+"}
|
||||
{"ts":"2026-06-11T08:10:04.674Z","sport":"nba","player_espn_id":"3062679","player_name":"Josh Hart","stat_type":"pts_reb_ast","line":10.5,"direction":"over","actual_value":10,"result":"miss","margin":-0.5,"grade":"C"}
|
||||
{"ts":"2026-06-11T08:10:04.717Z","sport":"nba","player_espn_id":"999999999","player_name":"Phantom Player","stat_type":"points","line":10.5,"direction":"over","actual_value":null,"result":"void","grade":"C"}
|
||||
{"ts":"2026-06-11T19:49:49.443Z","sport":"nba","player_espn_id":"3934719","player_name":"OG Anunoby","stat_type":"points","line":16.5,"direction":"over","actual_value":17,"result":"hit","margin":0.5,"grade":"A"}
|
||||
{"ts":"2026-06-11T19:49:49.577Z","sport":"nba","player_espn_id":"99999","player_name":"Phantom","stat_type":"points","line":10.5,"direction":"over","actual_value":null,"result":"void","grade":"B"}
|
||||
{"ts":"2026-06-11T19:49:49.713Z","sport":"nba","player_espn_id":"3934719","player_name":"OG Anunoby","stat_type":"points","line":16.5,"direction":"over","actual_value":17,"result":"hit","margin":0.5,"grade":"A"}
|
||||
{"ts":"2026-06-11T19:49:50.500Z","sport":"nba","player_espn_id":"3934719","player_name":"OG Anunoby","stat_type":"points","line":16.5,"direction":"over","actual_value":17,"result":"hit","margin":0.5,"grade":"A-"}
|
||||
{"ts":"2026-06-11T19:49:50.500Z","sport":"nba","player_espn_id":"3136195","player_name":"Karl-Anthony Towns","stat_type":"rebounds","line":13.5,"direction":"under","actual_value":13,"result":"hit","margin":-0.5,"grade":"B+"}
|
||||
{"ts":"2026-06-11T19:49:50.500Z","sport":"nba","player_espn_id":"3062679","player_name":"Josh Hart","stat_type":"pts_reb_ast","line":10.5,"direction":"over","actual_value":10,"result":"miss","margin":-0.5,"grade":"C"}
|
||||
{"ts":"2026-06-11T19:49:50.656Z","sport":"nba","player_espn_id":"999999999","player_name":"Phantom Player","stat_type":"points","line":10.5,"direction":"over","actual_value":null,"result":"void","grade":"C"}
|
||||
{"ts":"2026-06-11T20:01:53.008Z","sport":"nba","player_espn_id":"3934719","player_name":"OG Anunoby","stat_type":"points","line":16.5,"direction":"over","actual_value":17,"result":"hit","margin":0.5,"grade":"A"}
|
||||
{"ts":"2026-06-11T20:01:53.258Z","sport":"nba","player_espn_id":"99999","player_name":"Phantom","stat_type":"points","line":10.5,"direction":"over","actual_value":null,"result":"void","grade":"B"}
|
||||
{"ts":"2026-06-11T20:01:54.004Z","sport":"nba","player_espn_id":"3934719","player_name":"OG Anunoby","stat_type":"points","line":16.5,"direction":"over","actual_value":17,"result":"hit","margin":0.5,"grade":"A"}
|
||||
{"ts":"2026-06-11T20:01:54.134Z","sport":"nba","player_espn_id":"3934719","player_name":"OG Anunoby","stat_type":"points","line":16.5,"direction":"over","actual_value":17,"result":"hit","margin":0.5,"grade":"A-"}
|
||||
{"ts":"2026-06-11T20:01:54.135Z","sport":"nba","player_espn_id":"3136195","player_name":"Karl-Anthony Towns","stat_type":"rebounds","line":13.5,"direction":"under","actual_value":13,"result":"hit","margin":-0.5,"grade":"B+"}
|
||||
{"ts":"2026-06-11T20:01:54.135Z","sport":"nba","player_espn_id":"3062679","player_name":"Josh Hart","stat_type":"pts_reb_ast","line":10.5,"direction":"over","actual_value":10,"result":"miss","margin":-0.5,"grade":"C"}
|
||||
{"ts":"2026-06-11T20:01:54.253Z","sport":"nba","player_espn_id":"999999999","player_name":"Phantom Player","stat_type":"points","line":10.5,"direction":"over","actual_value":null,"result":"void","grade":"C"}
|
||||
{"ts":"2026-06-11T20:03:42.458Z","sport":"nba","player_espn_id":"3934719","player_name":"OG Anunoby","stat_type":"points","line":16.5,"direction":"over","actual_value":17,"result":"hit","margin":0.5,"grade":"A"}
|
||||
{"ts":"2026-06-11T20:03:42.468Z","sport":"nba","player_espn_id":"3934719","player_name":"OG Anunoby","stat_type":"points","line":16.5,"direction":"over","actual_value":17,"result":"hit","margin":0.5,"grade":"A"}
|
||||
{"ts":"2026-06-11T20:03:42.633Z","sport":"nba","player_espn_id":"99999","player_name":"Phantom","stat_type":"points","line":10.5,"direction":"over","actual_value":null,"result":"void","grade":"B"}
|
||||
{"ts":"2026-06-11T20:03:46.544Z","sport":"nba","player_espn_id":"3934719","player_name":"OG Anunoby","stat_type":"points","line":16.5,"direction":"over","actual_value":17,"result":"hit","margin":0.5,"grade":"A-"}
|
||||
{"ts":"2026-06-11T20:03:46.545Z","sport":"nba","player_espn_id":"3136195","player_name":"Karl-Anthony Towns","stat_type":"rebounds","line":13.5,"direction":"under","actual_value":13,"result":"hit","margin":-0.5,"grade":"B+"}
|
||||
{"ts":"2026-06-11T20:03:46.545Z","sport":"nba","player_espn_id":"3062679","player_name":"Josh Hart","stat_type":"pts_reb_ast","line":10.5,"direction":"over","actual_value":10,"result":"miss","margin":-0.5,"grade":"C"}
|
||||
{"ts":"2026-06-11T20:03:46.592Z","sport":"nba","player_espn_id":"999999999","player_name":"Phantom Player","stat_type":"points","line":10.5,"direction":"over","actual_value":null,"result":"void","grade":"C"}
|
||||
{"ts":"2026-06-11T20:09:37.620Z","sport":"nba","player_espn_id":"3934719","player_name":"OG Anunoby","stat_type":"points","line":16.5,"direction":"over","actual_value":17,"result":"hit","margin":0.5,"grade":"A"}
|
||||
{"ts":"2026-06-11T20:09:38.083Z","sport":"nba","player_espn_id":"3934719","player_name":"OG Anunoby","stat_type":"points","line":16.5,"direction":"over","actual_value":17,"result":"hit","margin":0.5,"grade":"A"}
|
||||
{"ts":"2026-06-11T20:09:38.320Z","sport":"nba","player_espn_id":"99999","player_name":"Phantom","stat_type":"points","line":10.5,"direction":"over","actual_value":null,"result":"void","grade":"B"}
|
||||
{"ts":"2026-06-11T20:09:39.782Z","sport":"nba","player_espn_id":"3934719","player_name":"OG Anunoby","stat_type":"points","line":16.5,"direction":"over","actual_value":17,"result":"hit","margin":0.5,"grade":"A-"}
|
||||
{"ts":"2026-06-11T20:09:39.782Z","sport":"nba","player_espn_id":"3136195","player_name":"Karl-Anthony Towns","stat_type":"rebounds","line":13.5,"direction":"under","actual_value":13,"result":"hit","margin":-0.5,"grade":"B+"}
|
||||
{"ts":"2026-06-11T20:09:39.783Z","sport":"nba","player_espn_id":"3062679","player_name":"Josh Hart","stat_type":"pts_reb_ast","line":10.5,"direction":"over","actual_value":10,"result":"miss","margin":-0.5,"grade":"C"}
|
||||
{"ts":"2026-06-11T20:09:40.192Z","sport":"nba","player_espn_id":"999999999","player_name":"Phantom Player","stat_type":"points","line":10.5,"direction":"over","actual_value":null,"result":"void","grade":"C"}
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Tank01 daily prefetch (Session 15).
|
||||
*
|
||||
* trigger: n8n "Morning Ops" workflow OR cron `0 7 * * *` (7am UTC,
|
||||
* ~3am ET — before US slates publish but after overnight
|
||||
* box scores finalize).
|
||||
* call: node scripts/tank01-prefetch.js [--max=80] [--dry-run]
|
||||
*
|
||||
* Why this exists: Session 14 wired `src/services/intelligence/tank01Augment.js`
|
||||
* to read `tank01:nba:*` and `tank01:mlb:*` cache keys, but nothing
|
||||
* populated those keys. Session 15 closes that loop with a daily
|
||||
* pull. The augmentor returns empty objects when the keys are
|
||||
* missing — grades still work today, they just don't include the
|
||||
* Tank01 signals. Once this script runs, grades pick up the new
|
||||
* data automatically.
|
||||
*
|
||||
* Budget posture: free tier is 1,000 req/month per RapidAPI account.
|
||||
* Even with NBA + MLB combined, one daily run targets ≤80 requests.
|
||||
* 80 * 30 days = 2,400 → would burn the budget. So default cap is 80
|
||||
* total across both sports, with a hard stop. Override via `--max`.
|
||||
*
|
||||
* What it writes:
|
||||
* tank01:nba:games:{ymd} — daily schedule
|
||||
* tank01:nba:boxscore:{gameId} — per-game final box scores
|
||||
* tank01:nba:odds:{ymd} — book-by-book lines
|
||||
* tank01:mlb:scoreboard:{ymd} — daily schedule
|
||||
* tank01:mlb:boxscore:{gameId} — per-game lines for finals
|
||||
* tank01:mlb:bvp:{batterId}:{pitcherId} — historical matchups
|
||||
*
|
||||
* The adapters (Session 9) own the `cacheSet` calls — this script
|
||||
* is the orchestrator that decides what to fetch. Adapter calls are
|
||||
* idempotent: re-running the script won't double-spend the budget
|
||||
* because each adapter checks its cache before hitting RapidAPI.
|
||||
*/
|
||||
|
||||
const tankNba = require('../src/services/adapters/tank01NbaAdapter');
|
||||
const tankMlb = require('../src/services/adapters/tank01MlbAdapter');
|
||||
|
||||
const DEFAULT_BUDGET = 80;
|
||||
|
||||
function parseArgs(argv) {
|
||||
const args = { maxRequests: DEFAULT_BUDGET, dryRun: false, sports: ['nba', 'mlb'] };
|
||||
for (const a of argv.slice(2)) {
|
||||
if (a.startsWith('--max=')) {
|
||||
const n = Number(a.slice('--max='.length));
|
||||
if (Number.isFinite(n) && n > 0) args.maxRequests = Math.floor(n);
|
||||
} else if (a === '--dry-run') {
|
||||
args.dryRun = true;
|
||||
} else if (a.startsWith('--sports=')) {
|
||||
args.sports = a.slice('--sports='.length).split(',').map((s) => s.trim().toLowerCase()).filter(Boolean);
|
||||
}
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
function todayYMD() {
|
||||
const d = new Date();
|
||||
return `${d.getUTCFullYear()}${String(d.getUTCMonth() + 1).padStart(2, '0')}${String(d.getUTCDate()).padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
// Simple counter — the adapters cache internally, so a "request" here
|
||||
// is "I asked the adapter for X; whether it hit cache or network is
|
||||
// out of our control." For budgeting we treat every adapter call as
|
||||
// 1 potential request and stop when the cap is hit.
|
||||
function makeBudget(maxRequests) {
|
||||
let spent = 0;
|
||||
return {
|
||||
canSpend: () => spent < maxRequests,
|
||||
spend: () => { spent += 1; return spent; },
|
||||
spent: () => spent,
|
||||
};
|
||||
}
|
||||
|
||||
async function processNba(args, budget, summary) {
|
||||
if (!tankNba.hasApiKey()) {
|
||||
summary.nba.skipped = 'no_key';
|
||||
return;
|
||||
}
|
||||
const ymd = todayYMD();
|
||||
|
||||
if (!budget.canSpend()) return;
|
||||
budget.spend();
|
||||
const games = await tankNba.getNBAGamesForDate(ymd);
|
||||
if (!Array.isArray(games)) {
|
||||
summary.nba.games = 0;
|
||||
return;
|
||||
}
|
||||
summary.nba.games = games.length;
|
||||
|
||||
// Box scores for every final game — captures the post-game stat
|
||||
// lines the augmentor surfaces to the analyze path.
|
||||
for (const g of games) {
|
||||
if (!budget.canSpend()) break;
|
||||
if (!g.gameId) continue;
|
||||
const status = String(g.gameStatus || '').toLowerCase();
|
||||
if (!status.includes('final')) continue;
|
||||
budget.spend();
|
||||
await tankNba.getNBABoxScore(g.gameId);
|
||||
summary.nba.boxscores += 1;
|
||||
}
|
||||
|
||||
// One odds pull for the day — surfaces the t01_market_present
|
||||
// marker the trap detector reads alongside odds-api.
|
||||
if (budget.canSpend()) {
|
||||
budget.spend();
|
||||
await tankNba.getNBABettingOdds(ymd);
|
||||
summary.nba.odds = true;
|
||||
}
|
||||
}
|
||||
|
||||
async function processMlb(args, budget, summary) {
|
||||
if (!tankMlb.hasApiKey()) {
|
||||
summary.mlb.skipped = 'no_key';
|
||||
return;
|
||||
}
|
||||
const ymd = todayYMD();
|
||||
|
||||
if (!budget.canSpend()) return;
|
||||
budget.spend();
|
||||
const slate = await tankMlb.getMLBDailyScoreboard(ymd);
|
||||
if (!Array.isArray(slate)) {
|
||||
summary.mlb.games = 0;
|
||||
return;
|
||||
}
|
||||
summary.mlb.games = slate.length;
|
||||
|
||||
// Box scores for finished games.
|
||||
for (const g of slate) {
|
||||
if (!budget.canSpend()) break;
|
||||
if (!g.gameId) continue;
|
||||
const status = String(g.gameStatus || '').toLowerCase();
|
||||
if (!status.includes('final') && !status.includes('completed')) continue;
|
||||
budget.spend();
|
||||
await tankMlb.getMLBBoxScore(g.gameId);
|
||||
summary.mlb.boxscores += 1;
|
||||
}
|
||||
|
||||
// BvP matchups — only when probable pitchers are exposed on the
|
||||
// scoreboard payload (Tank01 includes them for upcoming games). We
|
||||
// can't safely run BvP without batter/pitcher IDs, so the pass is
|
||||
// a no-op until the scoreboard projection grows those fields. The
|
||||
// augmentor handles the empty-cache case gracefully.
|
||||
summary.mlb.bvp_skipped_reason = 'awaiting_starter_ids_on_scoreboard';
|
||||
}
|
||||
|
||||
async function main(argv = process.argv) {
|
||||
const args = parseArgs(argv);
|
||||
const budget = makeBudget(args.maxRequests);
|
||||
const summary = {
|
||||
nba: { games: 0, boxscores: 0, odds: false, skipped: null },
|
||||
mlb: { games: 0, boxscores: 0, bvp: 0, skipped: null, bvp_skipped_reason: null },
|
||||
requestsSpent: 0,
|
||||
dryRun: args.dryRun,
|
||||
};
|
||||
|
||||
console.log(`[tank01-prefetch] starting — max_requests=${args.maxRequests} sports=${args.sports.join(',')} dry_run=${args.dryRun}`);
|
||||
|
||||
if (args.dryRun) {
|
||||
summary.nba.skipped = 'dry_run';
|
||||
summary.mlb.skipped = 'dry_run';
|
||||
console.log('[tank01-prefetch] dry-run — adapter calls suppressed');
|
||||
} else {
|
||||
if (args.sports.includes('nba')) await processNba(args, budget, summary);
|
||||
if (args.sports.includes('mlb')) await processMlb(args, budget, summary);
|
||||
}
|
||||
|
||||
summary.requestsSpent = budget.spent();
|
||||
console.log(`[tank01-prefetch] done — nba.games=${summary.nba.games} nba.box=${summary.nba.boxscores} mlb.games=${summary.mlb.games} mlb.box=${summary.mlb.boxscores} spent=${summary.requestsSpent}/${args.maxRequests}`);
|
||||
|
||||
return summary;
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
main().then(() => process.exit(0)).catch((err) => {
|
||||
console.error('[tank01-prefetch] fatal:', err);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
main,
|
||||
__internals: { parseArgs, makeBudget, todayYMD, processNba, processMlb, DEFAULT_BUDGET },
|
||||
};
|
||||
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* NBA team pace factors (Session 15).
|
||||
*
|
||||
* Source: NBA.com/stats team pace data 2024-25 season (possessions
|
||||
* per 48 minutes, league average ~98). Indexed to 100 = league
|
||||
* average so values compose multiplicatively with the player's
|
||||
* baseline stats — a 105-pace team adds ~5% to counting stats
|
||||
* (points, rebounds, assists, etc.); a 94-pace team subtracts ~6%.
|
||||
*
|
||||
* Update annually before the playoffs (the regular-season composite
|
||||
* is stable by All-Star break). For mid-season grades, consider
|
||||
* blending this static table with the live `team_pace` value the
|
||||
* orchestrator already computes from game logs.
|
||||
*/
|
||||
|
||||
const PACE_FACTORS = Object.freeze({
|
||||
// East
|
||||
ATL: 103, BOS: 99, BKN: 100, CHA: 102, CHI: 98,
|
||||
CLE: 96, DET: 102, IND: 105, MIA: 98, MIL: 99,
|
||||
NYK: 95, ORL: 94, PHI: 99, TOR: 100, WAS: 102,
|
||||
// West
|
||||
DAL: 99, DEN: 99, GSW: 102, HOU: 102, LAC: 99,
|
||||
LAL: 100, MEM: 102, MIN: 99, NOP: 100, OKC: 100,
|
||||
PHX: 99, POR: 102, SAC: 104, SAS: 100, UTA: 102,
|
||||
});
|
||||
|
||||
const LEAGUE_AVERAGE = 100;
|
||||
|
||||
function normalizeTeamCode(team) {
|
||||
if (!team) return null;
|
||||
return String(team).trim().toUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* getPaceFactor — returns null for unknown teams so the feature
|
||||
* extractor drops the signal entirely rather than reporting "league-
|
||||
* average pace" on a typo.
|
||||
*/
|
||||
function getPaceFactor(team) {
|
||||
const code = normalizeTeamCode(team);
|
||||
if (!code) return null;
|
||||
// The grading orchestrator uses some legacy abbreviations:
|
||||
// NJN → BKN (Nets relocated 2012)
|
||||
// NOH/NOK → NOP (Pelicans rebrand)
|
||||
// SEA → OKC (Sonics relocated)
|
||||
// CHO → CHA (Hornets rebrand)
|
||||
// Folded here so old game-log teams still resolve to current pace.
|
||||
const alias = ALIASES[code];
|
||||
return PACE_FACTORS[alias || code] || null;
|
||||
}
|
||||
|
||||
const ALIASES = Object.freeze({
|
||||
NJN: 'BKN',
|
||||
NOH: 'NOP',
|
||||
NOK: 'NOP',
|
||||
SEA: 'OKC',
|
||||
CHO: 'CHA',
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
PACE_FACTORS,
|
||||
LEAGUE_AVERAGE,
|
||||
ALIASES,
|
||||
getPaceFactor,
|
||||
__internals: { normalizeTeamCode },
|
||||
};
|
||||
@@ -0,0 +1,107 @@
|
||||
/**
|
||||
* MLB park factors (Session 15).
|
||||
*
|
||||
* Source: FanGraphs 2024-25 park factor data (three-year weighted
|
||||
* average — FanGraphs methodology). Numbers indexed to 100 = league
|
||||
* average. Pulled from https://www.fangraphs.com/guts.aspx (Park
|
||||
* Factors tab) 2025-04. Update annually; the rolling-three-year
|
||||
* window means values move slowly.
|
||||
*
|
||||
* Fields per park:
|
||||
* hr — home run factor (Coors ~128 means HRs are 28% more likely;
|
||||
* Oracle/SF ~85 means 15% less likely)
|
||||
* h — overall hit factor
|
||||
* r — runs factor
|
||||
*
|
||||
* The feature extractor reads this via `getParkFactor(homeTeam)`
|
||||
* during the MLB branch and merges hr/h/r as `park_hr`, `park_h`,
|
||||
* `park_r` so the grading engine + reasoning builder can read them
|
||||
* without re-doing the lookup.
|
||||
*
|
||||
* Indexing convention: home-team abbreviation. The road team plays
|
||||
* at the home team's park, so a Yankees-at-Angels game uses LAA's
|
||||
* park factors regardless of whose batter you're grading.
|
||||
*/
|
||||
|
||||
const PARK_FACTORS = Object.freeze({
|
||||
// AL East
|
||||
BAL: { hr: 109, h: 100, r: 102 }, // Camden Yards — left field wall changes leveled off post-2022
|
||||
BOS: { hr: 100, h: 108, r: 106 }, // Fenway — Monster boosts singles/doubles, suppresses HR
|
||||
NYY: { hr: 114, h: 101, r: 104 }, // Yankee Stadium — short porch in right
|
||||
TB: { hr: 93, h: 96, r: 94 }, // Tropicana — neutral-to-pitcher dome
|
||||
TOR: { hr: 106, h: 101, r: 102 }, // Rogers Centre — slight HR boost
|
||||
|
||||
// AL Central
|
||||
CHW: { hr: 109, h: 102, r: 105 }, // Guaranteed Rate Field
|
||||
CLE: { hr: 96, h: 99, r: 98 }, // Progressive Field — slight pitcher park
|
||||
DET: { hr: 90, h: 100, r: 97 }, // Comerica — deep alleys, HR-suppressed
|
||||
KC: { hr: 93, h: 103, r: 101 }, // Kauffman — XBH-friendly, HR-neutral
|
||||
MIN: { hr: 105, h: 100, r: 101 }, // Target Field
|
||||
|
||||
// AL West
|
||||
HOU: { hr: 100, h: 101, r: 101 }, // Minute Maid — Crawford boxes ~neutral, dome
|
||||
LAA: { hr: 97, h: 98, r: 98 }, // Angel Stadium
|
||||
OAK: { hr: 91, h: 95, r: 92 }, // Coliseum — extreme pitcher park
|
||||
SEA: { hr: 96, h: 98, r: 96 }, // T-Mobile Park — marine air
|
||||
TEX: { hr: 104, h: 102, r: 103 }, // Globe Life — climate-controlled since 2020
|
||||
|
||||
// NL East
|
||||
ATL: { hr: 103, h: 100, r: 101 }, // Truist Park
|
||||
MIA: { hr: 89, h: 97, r: 94 }, // loanDepot park — deep alleys
|
||||
NYM: { hr: 95, h: 99, r: 97 }, // Citi Field — slight pitcher
|
||||
PHI: { hr: 109, h: 100, r: 102 }, // Citizens Bank Park — bandbox right field
|
||||
WSH: { hr: 100, h: 100, r: 100 }, // Nationals Park — true neutral
|
||||
|
||||
// NL Central
|
||||
CHC: { hr: 106, h: 100, r: 102 }, // Wrigley — wind-driven swings; multi-year average
|
||||
CIN: { hr: 113, h: 102, r: 105 }, // Great American — top-3 HR park most years
|
||||
MIL: { hr: 104, h: 100, r: 101 }, // American Family Field
|
||||
PIT: { hr: 96, h: 101, r: 99 }, // PNC Park — XBH-favorable, HR-suppressed
|
||||
STL: { hr: 93, h: 98, r: 96 }, // Busch — neutral-to-pitcher
|
||||
|
||||
// NL West
|
||||
ARI: { hr: 100, h: 98, r: 98 }, // Chase Field — humidor since 2018
|
||||
COL: { hr: 128, h: 112, r: 120 }, // Coors Field — altitude. Single biggest park effect.
|
||||
LAD: { hr: 102, h: 99, r: 99 }, // Dodger Stadium
|
||||
SD: { hr: 95, h: 97, r: 95 }, // Petco — marine air pitcher park
|
||||
SF: { hr: 85, h: 96, r: 92 }, // Oracle Park — Bay winds suppress HRs heavily
|
||||
});
|
||||
|
||||
const NEUTRAL = Object.freeze({ hr: 100, h: 100, r: 100 });
|
||||
|
||||
function normalizeTeamCode(team) {
|
||||
if (!team) return null;
|
||||
return String(team).trim().toUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* getParkFactor — lookup by home-team code. Returns null for unknown
|
||||
* teams so the feature extractor can drop the signal entirely rather
|
||||
* than falsely report "neutral park" on a misspelled abbreviation.
|
||||
*
|
||||
* @param {string} homeTeam — three-letter team code (BAL, NYY, COL, ...)
|
||||
* @returns {{hr:number, h:number, r:number}|null}
|
||||
*/
|
||||
function getParkFactor(homeTeam) {
|
||||
const code = normalizeTeamCode(homeTeam);
|
||||
if (!code) return null;
|
||||
return PARK_FACTORS[code] || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience — when you want a guaranteed object (e.g. arithmetic
|
||||
* downstream that can't handle null). Falls back to league-neutral.
|
||||
* Prefer getParkFactor + explicit-null branching in code; this is
|
||||
* for tests and downstream services that prefer 100s over null.
|
||||
*/
|
||||
function getParkFactorOrNeutral(homeTeam) {
|
||||
return getParkFactor(homeTeam) || NEUTRAL;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
PARK_FACTORS,
|
||||
NEUTRAL,
|
||||
getParkFactor,
|
||||
getParkFactorOrNeutral,
|
||||
__internals: { normalizeTeamCode },
|
||||
};
|
||||
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
* Venue coordinates + dome flags (Session 15).
|
||||
*
|
||||
* Sources:
|
||||
* - MLB stadium coordinates: Wikipedia infoboxes, cross-checked against
|
||||
* each team's "stadium" entry on baseball-reference.com (2025).
|
||||
* - World Cup 2026 venues: official FIFA host-city announcements + the
|
||||
* static venue list already in `src/data/worldcup2026.js`.
|
||||
* - Dome flag = roof either fully closed or retractable. Retractable
|
||||
* stadiums count as dome for weather purposes (operators close the
|
||||
* roof when conditions warrant — weather doesn't drive grade).
|
||||
*
|
||||
* Lat/lon precision: 4 decimal places (~10m). Sufficient for an
|
||||
* Open-Meteo grid-cell lookup. Higher precision would be theater.
|
||||
*/
|
||||
|
||||
// ---- MLB ----
|
||||
|
||||
const MLB_VENUES = Object.freeze({
|
||||
// AL East
|
||||
BAL: { lat: 39.2839, lon: -76.6217, dome: false, name: 'Camden Yards' },
|
||||
BOS: { lat: 42.3467, lon: -71.0972, dome: false, name: 'Fenway Park' },
|
||||
NYY: { lat: 40.8296, lon: -73.9262, dome: false, name: 'Yankee Stadium' },
|
||||
TB: { lat: 27.7682, lon: -82.6534, dome: true, name: 'Tropicana Field' },
|
||||
TOR: { lat: 43.6414, lon: -79.3894, dome: true, name: 'Rogers Centre' },
|
||||
// AL Central
|
||||
CHW: { lat: 41.8300, lon: -87.6338, dome: false, name: 'Guaranteed Rate Field' },
|
||||
CLE: { lat: 41.4962, lon: -81.6852, dome: false, name: 'Progressive Field' },
|
||||
DET: { lat: 42.3390, lon: -83.0485, dome: false, name: 'Comerica Park' },
|
||||
KC: { lat: 39.0517, lon: -94.4803, dome: false, name: 'Kauffman Stadium' },
|
||||
MIN: { lat: 44.9817, lon: -93.2778, dome: false, name: 'Target Field' },
|
||||
// AL West
|
||||
HOU: { lat: 29.7572, lon: -95.3553, dome: true, name: 'Minute Maid Park' },
|
||||
LAA: { lat: 33.8003, lon: -117.8827, dome: false, name: 'Angel Stadium' },
|
||||
OAK: { lat: 37.7516, lon: -122.2005, dome: false, name: 'Oakland Coliseum' },
|
||||
SEA: { lat: 47.5914, lon: -122.3324, dome: true, name: 'T-Mobile Park' }, // retractable
|
||||
TEX: { lat: 32.7475, lon: -97.0822, dome: true, name: 'Globe Life Field' }, // retractable
|
||||
// NL East
|
||||
ATL: { lat: 33.8908, lon: -84.4678, dome: false, name: 'Truist Park' },
|
||||
MIA: { lat: 25.7781, lon: -80.2197, dome: true, name: 'loanDepot park' }, // retractable
|
||||
NYM: { lat: 40.7571, lon: -73.8458, dome: false, name: 'Citi Field' },
|
||||
PHI: { lat: 39.9061, lon: -75.1665, dome: false, name: 'Citizens Bank Park' },
|
||||
WSH: { lat: 38.8730, lon: -77.0074, dome: false, name: 'Nationals Park' },
|
||||
// NL Central
|
||||
CHC: { lat: 41.9484, lon: -87.6553, dome: false, name: 'Wrigley Field' },
|
||||
CIN: { lat: 39.0975, lon: -84.5067, dome: false, name: 'Great American Ball Park' },
|
||||
MIL: { lat: 43.0280, lon: -87.9712, dome: true, name: 'American Family Field' }, // retractable
|
||||
PIT: { lat: 40.4469, lon: -80.0058, dome: false, name: 'PNC Park' },
|
||||
STL: { lat: 38.6226, lon: -90.1928, dome: false, name: 'Busch Stadium' },
|
||||
// NL West
|
||||
ARI: { lat: 33.4453, lon: -112.0667, dome: true, name: 'Chase Field' }, // retractable
|
||||
COL: { lat: 39.7559, lon: -104.9942, dome: false, name: 'Coors Field' },
|
||||
LAD: { lat: 34.0739, lon: -118.2400, dome: false, name: 'Dodger Stadium' },
|
||||
SD: { lat: 32.7073, lon: -117.1566, dome: false, name: 'Petco Park' },
|
||||
SF: { lat: 37.7786, lon: -122.3893, dome: false, name: 'Oracle Park' },
|
||||
});
|
||||
|
||||
function normalizeCode(code) {
|
||||
if (!code) return null;
|
||||
return String(code).trim().toUpperCase();
|
||||
}
|
||||
|
||||
function getMlbVenue(teamCode) {
|
||||
const code = normalizeCode(teamCode);
|
||||
if (!code) return null;
|
||||
return MLB_VENUES[code] || null;
|
||||
}
|
||||
|
||||
// ---- World Cup 2026 ----
|
||||
// Sourced from src/data/worldcup2026.js (the canonical list of host
|
||||
// venues + altitudes). Added lat/lon + dome flag here to keep the
|
||||
// weather-fetch path independent of the i18n/altitude consumer.
|
||||
const WC_VENUES = Object.freeze({
|
||||
'MetLife Stadium': { lat: 40.8135, lon: -74.0746, dome: false },
|
||||
'AT&T Stadium': { lat: 32.7473, lon: -97.0945, dome: true }, // retractable
|
||||
'SoFi Stadium': { lat: 33.9535, lon: -118.3392, dome: true }, // partial roof
|
||||
'Hard Rock Stadium': { lat: 25.9580, lon: -80.2389, dome: false },
|
||||
'Lincoln Financial Field': { lat: 39.9008, lon: -75.1675, dome: false },
|
||||
'Lumen Field': { lat: 47.5952, lon: -122.3316, dome: false },
|
||||
'Gillette Stadium': { lat: 42.0909, lon: -71.2643, dome: false },
|
||||
'Mercedes-Benz Stadium': { lat: 33.7553, lon: -84.4006, dome: true }, // retractable
|
||||
'NRG Stadium': { lat: 29.6847, lon: -95.4107, dome: true }, // retractable
|
||||
'Arrowhead Stadium': { lat: 39.0489, lon: -94.4839, dome: false },
|
||||
'BC Place': { lat: 49.2768, lon: -123.1116, dome: true }, // retractable
|
||||
'BMO Field': { lat: 43.6332, lon: -79.4185, dome: false },
|
||||
'Estadio Azteca': { lat: 19.3030, lon: -99.1503, dome: false },
|
||||
'Estadio BBVA': { lat: 25.6692, lon: -100.2444, dome: false },
|
||||
'Estadio Akron': { lat: 20.6817, lon: -103.4625, dome: false },
|
||||
"Levi's Stadium": { lat: 37.4032, lon: -121.9698, dome: false },
|
||||
});
|
||||
|
||||
function getWcVenueCoords(name) {
|
||||
if (!name) return null;
|
||||
return WC_VENUES[name] || null;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
MLB_VENUES,
|
||||
WC_VENUES,
|
||||
getMlbVenue,
|
||||
getWcVenueCoords,
|
||||
__internals: { normalizeCode },
|
||||
};
|
||||
@@ -19,6 +19,54 @@
|
||||
*
|
||||
* The caller (analyzeViaEngine1) reads the returned `errors` array and
|
||||
* downgrades confidence accordingly via the adapter's reasoning string.
|
||||
*
|
||||
* ─────────────────────────────────────────────────────────────────────
|
||||
* Signal provenance (Session 15 audit)
|
||||
* ─────────────────────────────────────────────────────────────────────
|
||||
* Every signal the engine reads has a documented source. Phantom
|
||||
* signals — referenced in reasoning but populated by nothing — would
|
||||
* be a trust failure. As of Session 15 there are none.
|
||||
*
|
||||
* • injury_severity_score (engine1.js:126 reads it; analyzeViaEngine1
|
||||
* surfaces it in reasoning at line 156)
|
||||
* ← `src/services/intelligence/injuryParser.js` (ESPN injury feed)
|
||||
* Populated by the grading orchestrator in batch mode; in the
|
||||
* single-prop path it lives in the `featureCache` payload.
|
||||
*
|
||||
* • coach_pace_delta + coach_player_interaction
|
||||
* ← `src/services/intelligence/coachSignals.js` reads the
|
||||
* `coach_profiles` Supabase table (migration 017), with a
|
||||
* `src/config/coaches.json` seed file as the cold-start fallback.
|
||||
*
|
||||
* • consistency (boom_bust / reliable / elite labels + numeric score)
|
||||
* ← `src/services/intelligence/consistencyScore.js` operating on
|
||||
* game logs from `gameLogService` (ESPN). When game logs are
|
||||
* unavailable, defaults to `{consistency:'unknown', score:null}`
|
||||
* which engine1 treats as neutral (does not penalize).
|
||||
*
|
||||
* • Tank01 t01_* fields (added Session 14)
|
||||
* ← `src/services/intelligence/tank01Augment.js` reads cache keys
|
||||
* written by `scripts/tank01-prefetch.js` (Session 15 — added
|
||||
* this session) which calls the Tank01 NBA/MLB RapidAPI adapters.
|
||||
*
|
||||
* • Soccer features (10 of them — goals_per_90, xG, altitude, etc.)
|
||||
* ← `src/services/intelligence/soccerFeatureExtractor.js` cascade
|
||||
* across api-football → footapi → football-data cache keys.
|
||||
*
|
||||
* • Park factors (Session 15 — MLB)
|
||||
* ← `src/data/parkFactors.js` — static FanGraphs 2024-25 data.
|
||||
*
|
||||
* • Weather (Session 15 — MLB + soccer)
|
||||
* ← `src/services/weatherService.js` calls Open-Meteo (no key),
|
||||
* cached 1h in Redis. Skipped for dome stadiums.
|
||||
*
|
||||
* • Pace factors (Session 15 — NBA)
|
||||
* ← `src/data/paceFactors.js` — static NBA team pace data.
|
||||
*
|
||||
* No signal currently surfaces in user-facing reasoning that isn't
|
||||
* populated by one of the sources above. When a source is down, the
|
||||
* signal returns null and reasoning omits it gracefully — never
|
||||
* fabricated.
|
||||
*/
|
||||
|
||||
const axios = require('axios');
|
||||
@@ -37,6 +85,15 @@ const { extractSoccerFeatures, isSoccerSport } = require('./soccerFeatureExtract
|
||||
// populates the cache. Until that lands, the augmentor returns
|
||||
// empty objects and the existing ESPN-derived features stand alone.
|
||||
const tank01Augment = require('./tank01Augment');
|
||||
// Session 15 — static lookup tables (MLB park factors, NBA pace
|
||||
// factors). Pure synchronous reads, no network, no cache. Merged
|
||||
// into the feature map alongside the per-sport ESPN payload.
|
||||
const { getParkFactor } = require('../../data/parkFactors');
|
||||
const { getPaceFactor } = require('../../data/paceFactors');
|
||||
// Session 15 — Open-Meteo weather fetch. 1h Redis cache, 5s timeout,
|
||||
// silent on failure. Skipped for dome stadiums via the venue index.
|
||||
const weatherService = require('../weatherService');
|
||||
const { getMlbVenue, getWcVenueCoords } = require('../../data/venueCoordinates');
|
||||
|
||||
const HTTP_TIMEOUT_MS = 8_000;
|
||||
|
||||
@@ -224,6 +281,58 @@ async function computeFeaturesForProp(rawProp = {}) {
|
||||
console.warn('[computeFeatures] Tank01 augmentation skipped:', err.message);
|
||||
}
|
||||
|
||||
// Session 15 — static context augmentation. Park factors (MLB),
|
||||
// pace factors (NBA). Synchronous, can't fail; the lookups return
|
||||
// null on miss, which we treat as "no signal — drop the field".
|
||||
try {
|
||||
if (sport === 'mlb') {
|
||||
// Home team in this matchup hosts the game; if the player's
|
||||
// team is home, use their abbr — otherwise use the opponent's.
|
||||
const homeAbbr = game?.isHome ? teamAbbr : game?.opponentAbbr;
|
||||
const park = getParkFactor(homeAbbr);
|
||||
if (park) {
|
||||
features.park_hr = park.hr;
|
||||
features.park_h = park.h;
|
||||
features.park_r = park.r;
|
||||
features.park_home = homeAbbr;
|
||||
}
|
||||
} else if (sport === 'nba') {
|
||||
// Pace factors are per-team — use the player's own team (fast
|
||||
// teams up the count regardless of opponent, slow teams
|
||||
// compress). Opponent pace effect is a separate signal we
|
||||
// could layer in a follow-up.
|
||||
const pace = getPaceFactor(teamAbbr);
|
||||
if (pace != null) features.pace_factor = pace;
|
||||
const oppPace = getPaceFactor(game?.opponentAbbr);
|
||||
if (oppPace != null) features.opp_pace_factor = oppPace;
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('[computeFeatures] static context augmentation skipped:', err.message);
|
||||
}
|
||||
|
||||
// Session 15 — weather. Open-Meteo via weatherService. 5s timeout,
|
||||
// 1h Redis cache, dome-aware skip. Outdoor MLB + soccer benefit;
|
||||
// basketball indoor venues skip entirely.
|
||||
try {
|
||||
if (sport === 'mlb') {
|
||||
const homeAbbr = game?.isHome ? teamAbbr : game?.opponentAbbr;
|
||||
const venue = homeAbbr ? getMlbVenue(homeAbbr) : null;
|
||||
if (venue && !venue.dome && Number.isFinite(venue.lat) && Number.isFinite(venue.lon)) {
|
||||
const w = await weatherService.getWeather(venue.lat, venue.lon);
|
||||
if (w) {
|
||||
features.weather_temp_f = w.temp_f ?? null;
|
||||
features.weather_wind_mph = w.wind_mph ?? null;
|
||||
features.weather_wind_dir = w.wind_dir ?? null;
|
||||
features.weather_precip = w.precip_mm ?? null;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Soccer weather slots in via the soccer branch (handled earlier
|
||||
// for the soccer sport — the venue is part of the cascade).
|
||||
} catch (err) {
|
||||
console.warn('[computeFeatures] weather lookup skipped:', err.message);
|
||||
}
|
||||
|
||||
const trap = await safeGetTrap({
|
||||
playerName: player,
|
||||
statType,
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* MLB matchup context helpers (Session 15).
|
||||
*
|
||||
* Two pure functions, no I/O, no state:
|
||||
* - platoonAdvantage(pitcherHand, batterHand)
|
||||
* - projectedPA(lineupPosition)
|
||||
*
|
||||
* Sources:
|
||||
* - Platoon splits: the standard MLB convention — opposite-handed
|
||||
* matchups favor the batter (LHP vs RHB, RHP vs LHB). Switch
|
||||
* hitters get the advantage in either matchup because they bat
|
||||
* opposite-handed by definition.
|
||||
* - Lineup → projected plate appearances: derived from MLB-average
|
||||
* team PA distributions per lineup slot (2024 league composite).
|
||||
* A leadoff hitter sees ~4.7 PA in a 9-inning game; the 9-hole
|
||||
* sees ~3.5. Numbers from baseball-reference team batting tables
|
||||
* averaged across all 30 teams.
|
||||
*
|
||||
* Callers (computeFeatures MLB branch) attach the outputs to the
|
||||
* feature vector as `platoon_advantage` (bool|null) and
|
||||
* `projected_pa` (number|null). When inputs are absent or invalid
|
||||
* the helpers return null — engine1 treats null as a neutral signal
|
||||
* and reasoning omits the line gracefully.
|
||||
*/
|
||||
|
||||
const HAND_VALUES = new Set(['L', 'R', 'S']);
|
||||
|
||||
function normalizeHand(hand) {
|
||||
if (!hand) return null;
|
||||
const h = String(hand).trim().toUpperCase().charAt(0);
|
||||
return HAND_VALUES.has(h) ? h : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* platoonAdvantage — true when the batter has the platoon edge.
|
||||
*
|
||||
* LHP vs RHB → true (right-handed batter sees pitches better)
|
||||
* LHP vs LHB → false
|
||||
* RHP vs RHB → false
|
||||
* RHP vs LHB → true
|
||||
* any vs Switch hitter → true (switch hits opposite of pitcher)
|
||||
*
|
||||
* Returns null when either hand is unknown. The grading engine reads
|
||||
* `null` as "no signal" — same treatment as `false` for confidence
|
||||
* arithmetic, but reasoning will skip the line.
|
||||
*/
|
||||
function platoonAdvantage(pitcherHand, batterHand) {
|
||||
const p = normalizeHand(pitcherHand);
|
||||
const b = normalizeHand(batterHand);
|
||||
if (!p || !b) return null;
|
||||
if (b === 'S') return true; // switch hitter — always has edge
|
||||
if (p === b) return false; // same-handed → pitcher edge
|
||||
return true; // opposite-handed → batter edge
|
||||
}
|
||||
|
||||
// League-average plate appearances per lineup slot in 9-inning games.
|
||||
// Source: 2024 MLB composite (baseball-reference team batting tables).
|
||||
// Slot 1 (leadoff) leads the team; later slots see fewer PAs because
|
||||
// they may not bat in the bottom of innings already wrapped up.
|
||||
const PA_BY_SLOT = Object.freeze({
|
||||
1: 4.70,
|
||||
2: 4.55,
|
||||
3: 4.43,
|
||||
4: 4.31,
|
||||
5: 4.19,
|
||||
6: 4.07,
|
||||
7: 3.95,
|
||||
8: 3.83,
|
||||
9: 3.71,
|
||||
});
|
||||
|
||||
/**
|
||||
* projectedPA — expected plate appearances by lineup position.
|
||||
*
|
||||
* @param {number|string} position 1..9
|
||||
* @returns {number|null}
|
||||
*
|
||||
* Out-of-range or invalid → null. This keeps reasoning honest when
|
||||
* the odds payload doesn't carry batting order yet (the more common
|
||||
* case today — odds-api doesn't expose lineup position in the prop
|
||||
* envelope).
|
||||
*/
|
||||
function projectedPA(position) {
|
||||
if (position == null) return null;
|
||||
const p = Number(position);
|
||||
if (!Number.isInteger(p) || p < 1 || p > 9) return null;
|
||||
return PA_BY_SLOT[p];
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
platoonAdvantage,
|
||||
projectedPA,
|
||||
__internals: { normalizeHand, HAND_VALUES, PA_BY_SLOT },
|
||||
};
|
||||
@@ -7,15 +7,18 @@ function getStripe() {
|
||||
return _stripe;
|
||||
}
|
||||
|
||||
// Session 15 — fallback strings like 'price_analyst_monthly' would
|
||||
// 400 from Stripe in production (they're not real `price_xxx` IDs).
|
||||
// All maps now fall back to null; getPriceId then returns the
|
||||
// PRICE_UNCONFIGURED sentinel for unset values. Founder prices
|
||||
// additionally fall back to the standard tier price so a user with
|
||||
// a valid founder code on a deploy that doesn't yet have founder
|
||||
// prices wired still gets a successful checkout at standard rate.
|
||||
const PRICE_MAP = {
|
||||
analyst: process.env.STRIPE_PRICE_ANALYST || 'price_analyst_monthly',
|
||||
analyst_founder: process.env.STRIPE_PRICE_ANALYST_FOUNDER || 'price_analyst_founder',
|
||||
desk: process.env.STRIPE_PRICE_DESK || 'price_desk_monthly',
|
||||
desk_founder: process.env.STRIPE_PRICE_DESK_FOUNDER || 'price_desk_founder',
|
||||
// Session 14 — Africa tier ($4.99/mo). The Stripe product must be
|
||||
// created in the dashboard before STRIPE_PRICE_AFRICA carries a real
|
||||
// ID. Until then `getPriceId('africa')` returns a sentinel that
|
||||
// surfaces a clean error to the user via the route handler.
|
||||
analyst: process.env.STRIPE_PRICE_ANALYST || null,
|
||||
analyst_founder: process.env.STRIPE_PRICE_ANALYST_FOUNDER || null,
|
||||
desk: process.env.STRIPE_PRICE_DESK || null,
|
||||
desk_founder: process.env.STRIPE_PRICE_DESK_FOUNDER || null,
|
||||
africa: process.env.STRIPE_PRICE_AFRICA || null,
|
||||
};
|
||||
|
||||
@@ -38,13 +41,21 @@ function isFounderCodeValid(code) {
|
||||
|
||||
function getPriceId(tier, founderCode) {
|
||||
const isFounder = isFounderCodeValid(founderCode);
|
||||
if (tier === 'analyst') return isFounder ? PRICE_MAP.analyst_founder : PRICE_MAP.analyst;
|
||||
if (tier === 'desk') return isFounder ? PRICE_MAP.desk_founder : PRICE_MAP.desk;
|
||||
if (tier === 'analyst') {
|
||||
// Session 15 — if a valid founder code is presented but the
|
||||
// founder price ID isn't wired (env unset), gracefully fall
|
||||
// back to the standard analyst price rather than 503'ing the
|
||||
// user. The founder discount is operator-controlled; the
|
||||
// checkout itself shouldn't break.
|
||||
if (isFounder && PRICE_MAP.analyst_founder) return PRICE_MAP.analyst_founder;
|
||||
return PRICE_MAP.analyst || PRICE_UNCONFIGURED;
|
||||
}
|
||||
if (tier === 'desk') {
|
||||
if (isFounder && PRICE_MAP.desk_founder) return PRICE_MAP.desk_founder;
|
||||
return PRICE_MAP.desk || PRICE_UNCONFIGURED;
|
||||
}
|
||||
if (tier === 'africa') {
|
||||
// Africa tier doesn't have a founder discount — it IS the
|
||||
// discount. Returns the sentinel when STRIPE_PRICE_AFRICA is
|
||||
// unset so the route handler can produce a clean error instead
|
||||
// of forwarding a null price ID to Stripe.
|
||||
// Africa tier has no founder discount — it IS the discount.
|
||||
return PRICE_MAP.africa || PRICE_UNCONFIGURED;
|
||||
}
|
||||
throw new Error(`Invalid tier: ${tier}`);
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
* Weather service (Session 15).
|
||||
*
|
||||
* Open-Meteo proxy. No API key required (the service is free for
|
||||
* non-commercial use and sportsbook analytics is firmly non-
|
||||
* commercial intelligence). 5s hard timeout, 1h Redis cache,
|
||||
* graceful degrade (returns null on any failure — never throws,
|
||||
* never blocks the grade).
|
||||
*
|
||||
* Outputs are normalized to the units North-American bettors think
|
||||
* in: temperature in Fahrenheit, wind in mph. Open-Meteo's defaults
|
||||
* are Celsius + km/h, so we request the imperial units directly via
|
||||
* query params.
|
||||
*
|
||||
* Cache key: `weather:{lat}:{lon}:{hour}` — keyed by the current
|
||||
* UTC hour so two requests within the same hour hit cache. Slightly
|
||||
* less precise than a sliding TTL but matches Open-Meteo's hourly
|
||||
* forecast cadence and keeps cache churn bounded.
|
||||
*/
|
||||
|
||||
const axios = require('axios');
|
||||
const { cacheGet, cacheSet } = require('../utils/redis');
|
||||
|
||||
const BASE_URL = 'https://api.open-meteo.com/v1/forecast';
|
||||
const HTTP_TIMEOUT_MS = 5_000;
|
||||
const CACHE_TTL_SEC = 3600; // 1h — Open-Meteo refreshes hourly
|
||||
|
||||
function currentHourBucket() {
|
||||
const d = new Date();
|
||||
return `${d.getUTCFullYear()}${String(d.getUTCMonth() + 1).padStart(2, '0')}${String(d.getUTCDate()).padStart(2, '0')}${String(d.getUTCHours()).padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
function buildKey(lat, lon) {
|
||||
// 2-decimal precision is enough for a city-scale lookup and
|
||||
// collapses neighboring venues onto the same cache key (no real-
|
||||
// world impact — they share the same weather).
|
||||
const latKey = Number(lat).toFixed(2);
|
||||
const lonKey = Number(lon).toFixed(2);
|
||||
return `weather:${latKey}:${lonKey}:${currentHourBucket()}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* getWeather — fetch current weather conditions for a lat/lon.
|
||||
*
|
||||
* @param {number} lat
|
||||
* @param {number} lon
|
||||
* @returns {Promise<{temp_f:number|null, wind_mph:number|null, wind_dir:number|null, precip_mm:number|null} | null>}
|
||||
*
|
||||
* Returns null on:
|
||||
* - invalid coordinates
|
||||
* - upstream timeout / 5xx
|
||||
* - missing fields in the response
|
||||
*
|
||||
* The grading engine and reasoning builder both treat null as "no
|
||||
* signal" — features are simply omitted from the prop's overlay.
|
||||
*/
|
||||
async function getWeather(lat, lon) {
|
||||
if (!Number.isFinite(lat) || !Number.isFinite(lon)) return null;
|
||||
|
||||
const cacheKey = buildKey(lat, lon);
|
||||
try {
|
||||
const cached = await cacheGet(cacheKey);
|
||||
if (cached !== null) return cached;
|
||||
} catch {
|
||||
// Redis hiccup — proceed to network.
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await axios.get(BASE_URL, {
|
||||
timeout: HTTP_TIMEOUT_MS,
|
||||
params: {
|
||||
latitude: lat,
|
||||
longitude: lon,
|
||||
current: 'temperature_2m,wind_speed_10m,wind_direction_10m,precipitation',
|
||||
temperature_unit: 'fahrenheit',
|
||||
wind_speed_unit: 'mph',
|
||||
precipitation_unit: 'mm', // mm is the universal precipitation unit
|
||||
},
|
||||
});
|
||||
const current = res.data?.current;
|
||||
if (!current) return null;
|
||||
const out = {
|
||||
temp_f: Number.isFinite(current.temperature_2m) ? current.temperature_2m : null,
|
||||
wind_mph: Number.isFinite(current.wind_speed_10m) ? current.wind_speed_10m : null,
|
||||
wind_dir: Number.isFinite(current.wind_direction_10m) ? current.wind_direction_10m : null,
|
||||
precip_mm: Number.isFinite(current.precipitation) ? current.precipitation : null,
|
||||
_fetched_at: new Date().toISOString(),
|
||||
};
|
||||
try { await cacheSet(cacheKey, out, CACHE_TTL_SEC); } catch { /* graceful */ }
|
||||
return out;
|
||||
} catch (err) {
|
||||
// Open-Meteo down, timeout, 5xx — silently degrade.
|
||||
if (err && err.code !== 'ECONNABORTED') {
|
||||
console.warn('[weatherService] fetch failed:', err.message);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getWeather,
|
||||
__internals: { BASE_URL, HTTP_TIMEOUT_MS, CACHE_TTL_SEC, buildKey, currentHourBucket },
|
||||
};
|
||||
@@ -43,6 +43,12 @@ jest.mock('axios');
|
||||
process.env.ODDS_API_KEY = 'test';
|
||||
process.env.STRIPE_SECRET_KEY = 'sk_test_xxx';
|
||||
process.env.STRIPE_WEBHOOK_SECRET = 'whsec_test';
|
||||
// Session 15 — PRICE_MAP was hardened to drop the fake-string
|
||||
// fallbacks (would 400 from Stripe in production). The integration
|
||||
// tests need real-looking Stripe price IDs in env so getPriceId
|
||||
// doesn't return the unconfigured sentinel and 503 the happy path.
|
||||
process.env.STRIPE_PRICE_ANALYST = process.env.STRIPE_PRICE_ANALYST || 'price_test_analyst';
|
||||
process.env.STRIPE_PRICE_DESK = process.env.STRIPE_PRICE_DESK || 'price_test_desk';
|
||||
|
||||
const app = require('../../src/app');
|
||||
|
||||
|
||||
@@ -133,7 +133,7 @@ describe('computeFeaturesForProp — graceful degradation', () => {
|
||||
expect(out.meta.gameId).toBeNull();
|
||||
});
|
||||
|
||||
test('feature fetch throws → empty features + error noted, no crash', async () => {
|
||||
test('feature fetch throws → ESPN fields empty, but static-context augmentation still surfaces (Session 15)', async () => {
|
||||
mockSupabaseState.rosterRow = { team_abbr: 'NYK', espn_id: '1', sport: 'nba' };
|
||||
mockAxiosGet.mockResolvedValue(nbaScoreboard([game('e2', 'NYK', 'BOS')]));
|
||||
mockFeatures.throws = true;
|
||||
@@ -141,7 +141,16 @@ describe('computeFeaturesForProp — graceful degradation', () => {
|
||||
player: 'Brunson', stat_type: 'points', line: 25, direction: 'over', sport: 'nba',
|
||||
});
|
||||
expect(out.meta.errors).toContain('no_features_computed');
|
||||
expect(out.features).toEqual({});
|
||||
// Session 15 — static lookups (pace factor, park factor) populate
|
||||
// regardless of ESPN fetch state. The contract used to be
|
||||
// "features is empty when ESPN fails"; the contract is now
|
||||
// "features may contain static context even when ESPN fails."
|
||||
// ESPN-derived fields (l5_avg, opp_rank_stat, ...) ARE absent.
|
||||
expect(out.features.l5_avg).toBeUndefined();
|
||||
expect(out.features.opp_rank_stat).toBeUndefined();
|
||||
// Pace factor lookup is static and stable for known team codes.
|
||||
expect(out.features.pace_factor).toBe(95); // NYK pace
|
||||
expect(out.features.opp_pace_factor).toBe(99); // BOS pace
|
||||
expect(out.trap).toBeDefined();
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
// MLB context helpers (Session 15) — pitcher handedness + lineup PA.
|
||||
|
||||
const { platoonAdvantage, projectedPA, __internals } = require('../../src/services/intelligence/mlbContext');
|
||||
|
||||
describe('platoonAdvantage', () => {
|
||||
test('LHP vs RHB → batter has advantage', () => {
|
||||
expect(platoonAdvantage('L', 'R')).toBe(true);
|
||||
});
|
||||
test('RHP vs LHB → batter has advantage', () => {
|
||||
expect(platoonAdvantage('R', 'L')).toBe(true);
|
||||
});
|
||||
test('LHP vs LHB → pitcher has advantage', () => {
|
||||
expect(platoonAdvantage('L', 'L')).toBe(false);
|
||||
});
|
||||
test('RHP vs RHB → pitcher has advantage', () => {
|
||||
expect(platoonAdvantage('R', 'R')).toBe(false);
|
||||
});
|
||||
test('switch hitter ALWAYS has the edge (vs LHP)', () => {
|
||||
expect(platoonAdvantage('L', 'S')).toBe(true);
|
||||
});
|
||||
test('switch hitter ALWAYS has the edge (vs RHP)', () => {
|
||||
expect(platoonAdvantage('R', 'S')).toBe(true);
|
||||
});
|
||||
test('null pitcher → null', () => {
|
||||
expect(platoonAdvantage(null, 'R')).toBeNull();
|
||||
});
|
||||
test('null batter → null', () => {
|
||||
expect(platoonAdvantage('L', null)).toBeNull();
|
||||
});
|
||||
test('invalid input → null (does not throw)', () => {
|
||||
expect(platoonAdvantage('Z', 'R')).toBeNull();
|
||||
expect(platoonAdvantage('L', 'X')).toBeNull();
|
||||
});
|
||||
test('case-insensitive', () => {
|
||||
expect(platoonAdvantage('l', 'r')).toBe(true);
|
||||
expect(platoonAdvantage('Right', 'Left')).toBe(true);
|
||||
});
|
||||
test('whitespace tolerant', () => {
|
||||
expect(platoonAdvantage(' L ', ' R ')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('projectedPA', () => {
|
||||
test('leadoff (1) sees the most PAs (~4.7)', () => {
|
||||
expect(projectedPA(1)).toBeGreaterThan(4.6);
|
||||
expect(projectedPA(1)).toBeLessThan(4.8);
|
||||
});
|
||||
test('9-hole sees the fewest PAs (~3.7)', () => {
|
||||
expect(projectedPA(9)).toBeGreaterThan(3.6);
|
||||
expect(projectedPA(9)).toBeLessThan(3.8);
|
||||
});
|
||||
test('PA decreases monotonically by slot', () => {
|
||||
let prev = Infinity;
|
||||
for (let i = 1; i <= 9; i += 1) {
|
||||
const pa = projectedPA(i);
|
||||
expect(pa).toBeLessThan(prev);
|
||||
prev = pa;
|
||||
}
|
||||
});
|
||||
test('numeric string input is accepted', () => {
|
||||
expect(projectedPA('3')).toBeCloseTo(4.43, 2);
|
||||
});
|
||||
test('out-of-range returns null', () => {
|
||||
expect(projectedPA(0)).toBeNull();
|
||||
expect(projectedPA(10)).toBeNull();
|
||||
expect(projectedPA(-1)).toBeNull();
|
||||
});
|
||||
test('null / undefined / non-numeric → null', () => {
|
||||
expect(projectedPA(null)).toBeNull();
|
||||
expect(projectedPA(undefined)).toBeNull();
|
||||
expect(projectedPA('NaN')).toBeNull();
|
||||
});
|
||||
test('non-integer (e.g. 5.5) → null', () => {
|
||||
expect(projectedPA(5.5)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('normalizeHand', () => {
|
||||
test('accepts L, R, S in upper/lower case', () => {
|
||||
expect(__internals.normalizeHand('l')).toBe('L');
|
||||
expect(__internals.normalizeHand('R')).toBe('R');
|
||||
expect(__internals.normalizeHand('Switch')).toBe('S');
|
||||
});
|
||||
test('rejects unknown letters', () => {
|
||||
expect(__internals.normalizeHand('X')).toBeNull();
|
||||
expect(__internals.normalizeHand('')).toBeNull();
|
||||
expect(__internals.normalizeHand(null)).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,70 @@
|
||||
// NBA pace factors (Session 15) — static lookup table tests.
|
||||
|
||||
const pace = require('../../src/data/paceFactors');
|
||||
|
||||
describe('PACE_FACTORS', () => {
|
||||
test('covers all 30 NBA teams', () => {
|
||||
expect(Object.keys(pace.PACE_FACTORS).length).toBe(30);
|
||||
});
|
||||
|
||||
test('every entry is a finite integer near 100', () => {
|
||||
for (const [code, val] of Object.entries(pace.PACE_FACTORS)) {
|
||||
expect(typeof val).toBe('number');
|
||||
expect(Number.isFinite(val)).toBe(true);
|
||||
expect(val).toBeGreaterThanOrEqual(90);
|
||||
expect(val).toBeLessThanOrEqual(110);
|
||||
expect(code).toMatch(/^[A-Z]{2,3}$/);
|
||||
}
|
||||
});
|
||||
|
||||
test('Indiana / Sacramento / Atlanta are at the fast end', () => {
|
||||
expect(pace.PACE_FACTORS.IND).toBeGreaterThanOrEqual(103);
|
||||
expect(pace.PACE_FACTORS.SAC).toBeGreaterThanOrEqual(103);
|
||||
expect(pace.PACE_FACTORS.ATL).toBeGreaterThanOrEqual(102);
|
||||
});
|
||||
|
||||
test('Orlando / New York / Cleveland are at the slow end', () => {
|
||||
expect(pace.PACE_FACTORS.ORL).toBeLessThanOrEqual(96);
|
||||
expect(pace.PACE_FACTORS.NYK).toBeLessThanOrEqual(96);
|
||||
expect(pace.PACE_FACTORS.CLE).toBeLessThanOrEqual(98);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getPaceFactor', () => {
|
||||
test('returns the value for known teams', () => {
|
||||
expect(pace.getPaceFactor('IND')).toBe(pace.PACE_FACTORS.IND);
|
||||
});
|
||||
test('case-insensitive + trimmed', () => {
|
||||
expect(pace.getPaceFactor(' ind ')).toBe(pace.PACE_FACTORS.IND);
|
||||
expect(pace.getPaceFactor('sac')).toBe(pace.PACE_FACTORS.SAC);
|
||||
});
|
||||
test('null for unknown', () => {
|
||||
expect(pace.getPaceFactor('XYZ')).toBeNull();
|
||||
expect(pace.getPaceFactor('')).toBeNull();
|
||||
expect(pace.getPaceFactor(null)).toBeNull();
|
||||
});
|
||||
|
||||
describe('legacy team aliases', () => {
|
||||
test('NJN resolves to BKN', () => {
|
||||
expect(pace.getPaceFactor('NJN')).toBe(pace.PACE_FACTORS.BKN);
|
||||
});
|
||||
test('NOH resolves to NOP', () => {
|
||||
expect(pace.getPaceFactor('NOH')).toBe(pace.PACE_FACTORS.NOP);
|
||||
});
|
||||
test('SEA resolves to OKC', () => {
|
||||
expect(pace.getPaceFactor('SEA')).toBe(pace.PACE_FACTORS.OKC);
|
||||
});
|
||||
test('CHO resolves to CHA', () => {
|
||||
expect(pace.getPaceFactor('CHO')).toBe(pace.PACE_FACTORS.CHA);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('immutability', () => {
|
||||
test('PACE_FACTORS frozen', () => {
|
||||
expect(Object.isFrozen(pace.PACE_FACTORS)).toBe(true);
|
||||
});
|
||||
test('ALIASES frozen', () => {
|
||||
expect(Object.isFrozen(pace.ALIASES)).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,95 @@
|
||||
// MLB park factors (Session 15) — pin the membership list + assert
|
||||
// the expected magnitude of the headline parks. Park factors shift
|
||||
// year-to-year but the directional signal (Coors hot, Oracle cold)
|
||||
// is stable; the test catches obvious typos.
|
||||
|
||||
const pf = require('../../src/data/parkFactors');
|
||||
|
||||
describe('parkFactors', () => {
|
||||
test('covers all 30 MLB teams', () => {
|
||||
const codes = Object.keys(pf.PARK_FACTORS);
|
||||
expect(codes.length).toBe(30);
|
||||
});
|
||||
|
||||
test('every entry has hr/h/r as finite numbers', () => {
|
||||
for (const [code, vals] of Object.entries(pf.PARK_FACTORS)) {
|
||||
expect(typeof code).toBe('string');
|
||||
expect(code).toMatch(/^[A-Z]{2,3}$/);
|
||||
expect(Number.isFinite(vals.hr)).toBe(true);
|
||||
expect(Number.isFinite(vals.h)).toBe(true);
|
||||
expect(Number.isFinite(vals.r)).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
test('Coors Field is the most extreme HR park', () => {
|
||||
const hrs = Object.values(pf.PARK_FACTORS).map((p) => p.hr);
|
||||
const maxHr = Math.max(...hrs);
|
||||
expect(pf.PARK_FACTORS.COL.hr).toBe(maxHr);
|
||||
expect(pf.PARK_FACTORS.COL.hr).toBeGreaterThan(120);
|
||||
});
|
||||
|
||||
test('Oracle Park (SF) suppresses HRs heavily', () => {
|
||||
expect(pf.PARK_FACTORS.SF.hr).toBeLessThan(95);
|
||||
});
|
||||
|
||||
test('Coors also boosts hits and runs', () => {
|
||||
expect(pf.PARK_FACTORS.COL.h).toBeGreaterThan(100);
|
||||
expect(pf.PARK_FACTORS.COL.r).toBeGreaterThan(100);
|
||||
});
|
||||
|
||||
test('most parks land within ±15% of neutral', () => {
|
||||
// Coors + SF are deliberate outliers; check the rest cluster.
|
||||
const codes = Object.keys(pf.PARK_FACTORS).filter((c) => c !== 'COL' && c !== 'SF');
|
||||
for (const code of codes) {
|
||||
const { hr, h, r } = pf.PARK_FACTORS[code];
|
||||
expect(hr).toBeGreaterThanOrEqual(85);
|
||||
expect(hr).toBeLessThanOrEqual(115);
|
||||
expect(h).toBeGreaterThanOrEqual(90);
|
||||
expect(h).toBeLessThanOrEqual(110);
|
||||
expect(r).toBeGreaterThanOrEqual(90);
|
||||
expect(r).toBeLessThanOrEqual(110);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('getParkFactor', () => {
|
||||
test('returns the row for known teams', () => {
|
||||
expect(pf.getParkFactor('NYY').hr).toBe(pf.PARK_FACTORS.NYY.hr);
|
||||
expect(pf.getParkFactor('COL').hr).toBeGreaterThan(120);
|
||||
});
|
||||
|
||||
test('case-insensitive', () => {
|
||||
expect(pf.getParkFactor('nyy').hr).toBeGreaterThan(0);
|
||||
expect(pf.getParkFactor('Col')).toBe(pf.PARK_FACTORS.COL);
|
||||
});
|
||||
|
||||
test('whitespace-tolerant', () => {
|
||||
expect(pf.getParkFactor(' COL ')).toBeTruthy();
|
||||
});
|
||||
|
||||
test('returns null for unknown / empty', () => {
|
||||
expect(pf.getParkFactor('XYZ')).toBeNull();
|
||||
expect(pf.getParkFactor('')).toBeNull();
|
||||
expect(pf.getParkFactor(null)).toBeNull();
|
||||
expect(pf.getParkFactor(undefined)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getParkFactorOrNeutral', () => {
|
||||
test('returns the row when known', () => {
|
||||
expect(pf.getParkFactorOrNeutral('NYY')).toBe(pf.PARK_FACTORS.NYY);
|
||||
});
|
||||
test('falls back to neutral 100s when unknown', () => {
|
||||
expect(pf.getParkFactorOrNeutral('XYZ')).toEqual({ hr: 100, h: 100, r: 100 });
|
||||
expect(pf.getParkFactorOrNeutral(null)).toEqual({ hr: 100, h: 100, r: 100 });
|
||||
});
|
||||
});
|
||||
|
||||
describe('immutability', () => {
|
||||
test('PARK_FACTORS is frozen at the top level', () => {
|
||||
expect(Object.isFrozen(pf.PARK_FACTORS)).toBe(true);
|
||||
});
|
||||
test('NEUTRAL is frozen', () => {
|
||||
expect(Object.isFrozen(pf.NEUTRAL)).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,13 @@
|
||||
process.env.STRIPE_SECRET_KEY = 'sk_test_dummy';
|
||||
// Session 15 — the production fallback strings ('price_analyst_monthly'
|
||||
// etc.) were dropped because they'd 400 from Stripe in live mode. Tests
|
||||
// that assert getPriceId returns a string containing 'analyst' /
|
||||
// 'founder' must now provide the env values BEFORE requiring the
|
||||
// module (PRICE_MAP is frozen at require time).
|
||||
process.env.STRIPE_PRICE_ANALYST = 'price_test_analyst_monthly';
|
||||
process.env.STRIPE_PRICE_ANALYST_FOUNDER = 'price_test_analyst_founder';
|
||||
process.env.STRIPE_PRICE_DESK = 'price_test_desk_monthly';
|
||||
process.env.STRIPE_PRICE_DESK_FOUNDER = 'price_test_desk_founder';
|
||||
|
||||
// Default mock for the founder-code / price-id tests (no DB interaction).
|
||||
// Webhook tests below replace the implementation per-test.
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
// Tank01 daily prefetch (Session 15) — orchestrator that pulls the
|
||||
// Redis cache keys session 14's augmentor reads. Tests cover:
|
||||
// - graceful skip when adapter has no API key
|
||||
// - budget cap respected
|
||||
// - dry-run suppresses adapter calls
|
||||
// - final-status box scores get pulled, non-final skipped
|
||||
// - empty slate doesn't crash
|
||||
|
||||
const mockNbaGames = jest.fn();
|
||||
const mockNbaBox = jest.fn();
|
||||
const mockNbaOdds = jest.fn();
|
||||
const mockNbaHasKey = jest.fn(() => true);
|
||||
jest.mock('../../src/services/adapters/tank01NbaAdapter', () => ({
|
||||
getNBAGamesForDate: (...a) => mockNbaGames(...a),
|
||||
getNBABoxScore: (...a) => mockNbaBox(...a),
|
||||
getNBABettingOdds: (...a) => mockNbaOdds(...a),
|
||||
hasApiKey: (...a) => mockNbaHasKey(...a),
|
||||
}));
|
||||
|
||||
const mockMlbSlate = jest.fn();
|
||||
const mockMlbBox = jest.fn();
|
||||
const mockMlbBvp = jest.fn();
|
||||
const mockMlbHasKey = jest.fn(() => true);
|
||||
jest.mock('../../src/services/adapters/tank01MlbAdapter', () => ({
|
||||
getMLBDailyScoreboard: (...a) => mockMlbSlate(...a),
|
||||
getMLBBoxScore: (...a) => mockMlbBox(...a),
|
||||
getMLBBatterVsPitcher: (...a) => mockMlbBvp(...a),
|
||||
hasApiKey: (...a) => mockMlbHasKey(...a),
|
||||
}));
|
||||
|
||||
const prefetch = require('../../scripts/tank01-prefetch');
|
||||
|
||||
beforeEach(() => {
|
||||
mockNbaGames.mockReset();
|
||||
mockNbaBox.mockReset();
|
||||
mockNbaOdds.mockReset();
|
||||
mockNbaHasKey.mockReset().mockReturnValue(true);
|
||||
mockMlbSlate.mockReset();
|
||||
mockMlbBox.mockReset();
|
||||
mockMlbBvp.mockReset();
|
||||
mockMlbHasKey.mockReset().mockReturnValue(true);
|
||||
});
|
||||
|
||||
describe('parseArgs', () => {
|
||||
test('defaults', () => {
|
||||
const a = prefetch.__internals.parseArgs(['node', 'script']);
|
||||
expect(a.maxRequests).toBe(prefetch.__internals.DEFAULT_BUDGET);
|
||||
expect(a.dryRun).toBe(false);
|
||||
expect(a.sports).toEqual(['nba', 'mlb']);
|
||||
});
|
||||
test('--max=N', () => {
|
||||
expect(prefetch.__internals.parseArgs(['node', 's', '--max=25']).maxRequests).toBe(25);
|
||||
});
|
||||
test('--max ignores non-numeric / non-positive', () => {
|
||||
expect(prefetch.__internals.parseArgs(['node', 's', '--max=0']).maxRequests).toBe(80);
|
||||
expect(prefetch.__internals.parseArgs(['node', 's', '--max=foo']).maxRequests).toBe(80);
|
||||
});
|
||||
test('--dry-run', () => {
|
||||
expect(prefetch.__internals.parseArgs(['node', 's', '--dry-run']).dryRun).toBe(true);
|
||||
});
|
||||
test('--sports filter', () => {
|
||||
expect(prefetch.__internals.parseArgs(['node', 's', '--sports=mlb']).sports).toEqual(['mlb']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('budget tracker', () => {
|
||||
test('counts spend, refuses past cap', () => {
|
||||
const b = prefetch.__internals.makeBudget(3);
|
||||
expect(b.canSpend()).toBe(true);
|
||||
b.spend(); b.spend(); b.spend();
|
||||
expect(b.canSpend()).toBe(false);
|
||||
expect(b.spent()).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('main — NBA path', () => {
|
||||
test('skips entirely when adapter has no API key', async () => {
|
||||
mockNbaHasKey.mockReturnValueOnce(false);
|
||||
mockMlbHasKey.mockReturnValueOnce(false);
|
||||
const r = await prefetch.main(['node', 'script', '--sports=nba']);
|
||||
expect(r.nba.skipped).toBe('no_key');
|
||||
expect(mockNbaGames).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('pulls slate, then box score per FINAL game, plus odds once', async () => {
|
||||
mockNbaGames.mockResolvedValueOnce([
|
||||
{ gameId: 'G1', gameStatus: 'Final' },
|
||||
{ gameId: 'G2', gameStatus: 'InProgress' }, // skip
|
||||
{ gameId: 'G3', gameStatus: 'Final' },
|
||||
]);
|
||||
mockNbaBox.mockResolvedValue({});
|
||||
mockNbaOdds.mockResolvedValueOnce({});
|
||||
const r = await prefetch.main(['node', 'script', '--sports=nba']);
|
||||
expect(r.nba.games).toBe(3);
|
||||
expect(r.nba.boxscores).toBe(2); // only the Finals
|
||||
expect(r.nba.odds).toBe(true);
|
||||
expect(mockNbaBox).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
test('budget cap stops fetches mid-loop', async () => {
|
||||
mockNbaGames.mockResolvedValueOnce(
|
||||
Array.from({ length: 10 }, (_, i) => ({ gameId: `G${i}`, gameStatus: 'Final' })),
|
||||
);
|
||||
mockNbaBox.mockResolvedValue({});
|
||||
mockNbaOdds.mockResolvedValueOnce({});
|
||||
// Budget = 4: one for getNBAGamesForDate, leaves 3 box-score
|
||||
// slots (no odds — budget exhausted first).
|
||||
const r = await prefetch.main(['node', 'script', '--sports=nba', '--max=4']);
|
||||
expect(mockNbaGames).toHaveBeenCalledTimes(1);
|
||||
expect(mockNbaBox).toHaveBeenCalledTimes(3);
|
||||
expect(mockNbaOdds).not.toHaveBeenCalled();
|
||||
expect(r.requestsSpent).toBe(4);
|
||||
});
|
||||
|
||||
test('dry-run skips ALL adapter calls', async () => {
|
||||
const r = await prefetch.main(['node', 'script', '--dry-run']);
|
||||
expect(mockNbaGames).not.toHaveBeenCalled();
|
||||
expect(mockNbaBox).not.toHaveBeenCalled();
|
||||
expect(mockNbaOdds).not.toHaveBeenCalled();
|
||||
expect(r.nba.skipped).toBe('dry_run');
|
||||
expect(r.mlb.skipped).toBe('dry_run');
|
||||
});
|
||||
|
||||
test('empty slate is not an error', async () => {
|
||||
mockNbaGames.mockResolvedValueOnce([]);
|
||||
mockNbaOdds.mockResolvedValueOnce({});
|
||||
const r = await prefetch.main(['node', 'script', '--sports=nba']);
|
||||
expect(r.nba.games).toBe(0);
|
||||
expect(r.nba.boxscores).toBe(0);
|
||||
// Odds still pulled — it's a single daily call, not per-game.
|
||||
expect(r.nba.odds).toBe(true);
|
||||
});
|
||||
|
||||
test('null slate (adapter returned null) is not an error', async () => {
|
||||
mockNbaGames.mockResolvedValueOnce(null);
|
||||
const r = await prefetch.main(['node', 'script', '--sports=nba']);
|
||||
expect(r.nba.games).toBe(0);
|
||||
expect(r.nba.boxscores).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('main — MLB path', () => {
|
||||
test('skips when adapter has no API key', async () => {
|
||||
mockMlbHasKey.mockReturnValueOnce(false);
|
||||
const r = await prefetch.main(['node', 'script', '--sports=mlb']);
|
||||
expect(r.mlb.skipped).toBe('no_key');
|
||||
});
|
||||
|
||||
test('pulls scoreboard + box scores for Finals/Completed', async () => {
|
||||
mockMlbSlate.mockResolvedValueOnce([
|
||||
{ gameId: 'M1', gameStatus: 'Final' },
|
||||
{ gameId: 'M2', gameStatus: 'In Progress' }, // skip
|
||||
{ gameId: 'M3', gameStatus: 'Completed' },
|
||||
]);
|
||||
mockMlbBox.mockResolvedValue({});
|
||||
const r = await prefetch.main(['node', 'script', '--sports=mlb']);
|
||||
expect(r.mlb.games).toBe(3);
|
||||
expect(r.mlb.boxscores).toBe(2);
|
||||
expect(r.mlb.bvp_skipped_reason).toMatch(/scoreboard/i);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,156 @@
|
||||
// weatherService (Session 15) — Open-Meteo proxy with cache + timeout.
|
||||
|
||||
const mockAxiosGet = jest.fn();
|
||||
jest.mock('axios', () => ({ get: (...args) => mockAxiosGet(...args) }));
|
||||
|
||||
const mockCache = new Map();
|
||||
jest.mock('../../src/utils/redis', () => ({
|
||||
cacheGet: async (k) => (mockCache.has(k) ? mockCache.get(k) : null),
|
||||
cacheSet: async (k, v) => { mockCache.set(k, v); return true; },
|
||||
cacheDel: async (k) => { mockCache.delete(k); return true; },
|
||||
isDegraded: () => false,
|
||||
}));
|
||||
|
||||
const ws = require('../../src/services/weatherService');
|
||||
|
||||
beforeEach(() => {
|
||||
mockAxiosGet.mockReset();
|
||||
mockCache.clear();
|
||||
});
|
||||
|
||||
describe('weatherService.getWeather', () => {
|
||||
test('invalid coordinates return null without touching the network', async () => {
|
||||
expect(await ws.getWeather(null, null)).toBeNull();
|
||||
expect(await ws.getWeather(NaN, 0)).toBeNull();
|
||||
expect(await ws.getWeather(undefined, undefined)).toBeNull();
|
||||
expect(mockAxiosGet).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('happy path — projects Open-Meteo current block to our flat shape', async () => {
|
||||
mockAxiosGet.mockResolvedValueOnce({
|
||||
data: {
|
||||
current: {
|
||||
temperature_2m: 78.5,
|
||||
wind_speed_10m: 12.3,
|
||||
wind_direction_10m: 165,
|
||||
precipitation: 0,
|
||||
},
|
||||
},
|
||||
});
|
||||
const w = await ws.getWeather(39.7559, -104.9942);
|
||||
expect(w).toMatchObject({
|
||||
temp_f: 78.5,
|
||||
wind_mph: 12.3,
|
||||
wind_dir: 165,
|
||||
precip_mm: 0,
|
||||
});
|
||||
expect(typeof w._fetched_at).toBe('string');
|
||||
});
|
||||
|
||||
test('Fahrenheit + mph units requested explicitly', async () => {
|
||||
mockAxiosGet.mockResolvedValueOnce({ data: { current: {} } });
|
||||
await ws.getWeather(40, -75);
|
||||
const [, opts] = mockAxiosGet.mock.calls[0];
|
||||
expect(opts.params.temperature_unit).toBe('fahrenheit');
|
||||
expect(opts.params.wind_speed_unit).toBe('mph');
|
||||
});
|
||||
|
||||
test('second call within the same hour hits cache', async () => {
|
||||
mockAxiosGet.mockResolvedValueOnce({
|
||||
data: { current: { temperature_2m: 80, wind_speed_10m: 5, wind_direction_10m: 0, precipitation: 0 } },
|
||||
});
|
||||
await ws.getWeather(39.7559, -104.9942);
|
||||
await ws.getWeather(39.7559, -104.9942);
|
||||
expect(mockAxiosGet).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test('coordinate-precision collapse — venues within ~1km share cache', async () => {
|
||||
mockAxiosGet.mockResolvedValueOnce({
|
||||
data: { current: { temperature_2m: 70, wind_speed_10m: 5, wind_direction_10m: 0, precipitation: 0 } },
|
||||
});
|
||||
// Two lat/lon pairs that round to the same 2-decimal cache key.
|
||||
await ws.getWeather(40.815, -74.075); // round to 40.82 / -74.07
|
||||
await ws.getWeather(40.823, -74.078); // round to 40.82 / -74.08 — different lon key, different fetch
|
||||
// 40.82/-74.07 vs 40.82/-74.08 → different keys, two fetches expected
|
||||
expect(mockAxiosGet).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
test('upstream throw → returns null (graceful)', async () => {
|
||||
mockAxiosGet.mockRejectedValueOnce(new Error('timeout'));
|
||||
const w = await ws.getWeather(40, -75);
|
||||
expect(w).toBeNull();
|
||||
});
|
||||
|
||||
test('upstream returns response without `current` block → null', async () => {
|
||||
mockAxiosGet.mockResolvedValueOnce({ data: {} });
|
||||
const w = await ws.getWeather(40, -75);
|
||||
expect(w).toBeNull();
|
||||
});
|
||||
|
||||
test('individual missing fields default to null without crashing', async () => {
|
||||
mockAxiosGet.mockResolvedValueOnce({
|
||||
data: {
|
||||
current: { temperature_2m: 60 /* wind/precip missing */ },
|
||||
},
|
||||
});
|
||||
const w = await ws.getWeather(40, -75);
|
||||
expect(w.temp_f).toBe(60);
|
||||
expect(w.wind_mph).toBeNull();
|
||||
expect(w.wind_dir).toBeNull();
|
||||
expect(w.precip_mm).toBeNull();
|
||||
});
|
||||
|
||||
test('5-second timeout configured', async () => {
|
||||
mockAxiosGet.mockResolvedValueOnce({ data: { current: {} } });
|
||||
await ws.getWeather(40, -75);
|
||||
const [, opts] = mockAxiosGet.mock.calls[0];
|
||||
expect(opts.timeout).toBe(ws.__internals.HTTP_TIMEOUT_MS);
|
||||
expect(opts.timeout).toBeLessThanOrEqual(5000);
|
||||
});
|
||||
});
|
||||
|
||||
describe('venueCoordinates', () => {
|
||||
const venues = require('../../src/data/venueCoordinates');
|
||||
|
||||
test('all 30 MLB venues defined with finite lat/lon', () => {
|
||||
const codes = Object.keys(venues.MLB_VENUES);
|
||||
expect(codes.length).toBe(30);
|
||||
for (const [code, v] of Object.entries(venues.MLB_VENUES)) {
|
||||
expect(Number.isFinite(v.lat)).toBe(true);
|
||||
expect(Number.isFinite(v.lon)).toBe(true);
|
||||
expect(typeof v.dome).toBe('boolean');
|
||||
expect(typeof v.name).toBe('string');
|
||||
// Sanity: every MLB venue is in roughly the right hemisphere.
|
||||
expect(v.lat).toBeGreaterThan(20);
|
||||
expect(v.lat).toBeLessThan(50);
|
||||
expect(v.lon).toBeLessThan(-65);
|
||||
expect(v.lon).toBeGreaterThan(-125);
|
||||
expect(code).toMatch(/^[A-Z]{2,3}$/);
|
||||
}
|
||||
});
|
||||
|
||||
test('all 16 WC 2026 venues defined', () => {
|
||||
expect(Object.keys(venues.WC_VENUES).length).toBe(16);
|
||||
});
|
||||
|
||||
test('dome stadiums correctly flagged', () => {
|
||||
// Tampa Bay's Tropicana is the canonical fixed-roof dome in the AL.
|
||||
expect(venues.MLB_VENUES.TB.dome).toBe(true);
|
||||
// Coors is open-air.
|
||||
expect(venues.MLB_VENUES.COL.dome).toBe(false);
|
||||
// Toronto's Rogers Centre is retractable — treated as dome.
|
||||
expect(venues.MLB_VENUES.TOR.dome).toBe(true);
|
||||
});
|
||||
|
||||
test('getMlbVenue lookup', () => {
|
||||
expect(venues.getMlbVenue('NYY').name).toBe('Yankee Stadium');
|
||||
expect(venues.getMlbVenue('xyz')).toBeNull();
|
||||
expect(venues.getMlbVenue(null)).toBeNull();
|
||||
});
|
||||
|
||||
test('getWcVenueCoords lookup', () => {
|
||||
expect(venues.getWcVenueCoords('MetLife Stadium').dome).toBe(false);
|
||||
expect(venues.getWcVenueCoords('Estadio Azteca').lat).toBeCloseTo(19.30, 1);
|
||||
expect(venues.getWcVenueCoords('Bogus')).toBeNull();
|
||||
});
|
||||
});
|
||||
+1
-1
File diff suppressed because one or more lines are too long
@@ -144,15 +144,12 @@ export default function Pricing() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Session 12 — Africa tier: Stripe product + backend validation
|
||||
// not yet wired (intentional this session). Show an honest
|
||||
// "coming soon" instead of a 400. When STRIPE_PRICE_AFRICA is
|
||||
// configured AND the backend accepts the tier, this short-circuit
|
||||
// gets removed and the standard checkout path takes over.
|
||||
if (tier === 'africa') {
|
||||
setError('VYNDR Africa launches once Stripe regional processing is finalized. Email support@vyndr.app to lock the $4.99/mo founder price.');
|
||||
return;
|
||||
}
|
||||
// Session 15 — Africa short-circuit removed. The Session 14
|
||||
// backend now handles 'africa' end-to-end: validation accepts
|
||||
// it, and when STRIPE_PRICE_AFRICA isn't configured the route
|
||||
// returns 503 { code: 'tier_unconfigured', error: '...' } which
|
||||
// the existing error-display path below surfaces inline. No
|
||||
// special-case needed at the UI layer.
|
||||
|
||||
// Anonymous → bounce to signup with a returnTo back to /#pricing.
|
||||
if (!session) {
|
||||
|
||||
Reference in New Issue
Block a user