Session 55: Self-learning loop + real-time layer (2274 tests)

Product overhaul core — the two transformative, differentiated systems:

Self-learning loop (Phase 2): outcomeService settles locked snapshot grades
against real MLB Stats API results → hit/miss/push, rolling accuracy by grade
tier (30d window). Idempotent, injectable, unit-tested. New GET /api/accuracy +
/api/ledger/accuracy + internal settle triggers + cron hook. AccuracyBadge
(dashboard/scan/landing) is honest — "LEARNING" below MIN_SAMPLE, never a fake
number. Settled HIT/MISS chips overlay the live slate.

Real-time layer (Phase 1): Slate silent 60s auto-refresh (no flash, no wipe on
transient blips) + "SIGNAL LIVE · UPDATED Xs ago" freshness strip; Ticker LIVE
badge that flashes on fresh events.

Landing (Phase 3): TopSignals shows tonight's real top-3 A-rated grades + live
accuracy — the product shown, not described.

Founder pricing: FOUNDER_CODE_EXPIRY default 2026-06-30 → 2026-12-31 (had
lapsed, disabling every founder code + the ClaimMeter pitch). That expiry — not
a tier change — was the real cause of the 4 stripe test failures.

Backend 2255 (4 failing) → 2274 (all green; +19 new, +4 fixed). Web build exit 0.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Kev
2026-07-10 15:39:13 -04:00
parent 8629021774
commit d09a06c054
27 changed files with 1285 additions and 17 deletions
+67
View File
@@ -0,0 +1,67 @@
'use strict';
// Session 55 — the self-learning loop's public read endpoints. Redis is mocked
// so these run offline; the store is seeded per-test via cacheGet.
const request = require('supertest');
let mockStore = {};
jest.mock('../../src/utils/redis', () => ({
getRedisClient: () => ({}),
cacheGet: async (k) => (k in mockStore ? mockStore[k] : null),
cacheSet: async (k, v) => { mockStore[k] = v; return true; },
cacheDel: async () => true,
isDegraded: () => false,
}));
const app = require('../../src/app');
beforeEach(() => { mockStore = {}; });
describe('GET /api/accuracy', () => {
test('cold cache → valid empty-safe shape', async () => {
const res = await request(app).get('/api/accuracy');
expect(res.status).toBe(200);
expect(res.body).toHaveProperty('overall');
expect(res.body).toHaveProperty('sports');
expect(res.body.min_sample).toBeGreaterThan(0);
});
test('returns the persisted record when present', async () => {
mockStore['accuracy:overall'] = {
sport: 'overall', updated_at: '2026-07-10T00:00:00Z', window_days: 30, sample: 20,
overall: { hits: 14, misses: 6, pushes: 0, total: 20, pct: 70 },
byGrade: { 'A': { hits: 8, misses: 2, pushes: 0, total: 10, pct: 80 } },
};
mockStore['accuracy:mlb'] = mockStore['accuracy:overall'];
const res = await request(app).get('/api/accuracy');
expect(res.body.overall.overall.pct).toBe(70);
expect(res.body.sports.mlb).toBeTruthy();
});
});
describe('GET /api/ledger/accuracy', () => {
test('returns grade-tier buckets from the accuracy record', async () => {
mockStore['accuracy:overall'] = {
sport: 'overall', updated_at: '2026-07-10T00:00:00Z', window_days: 30, sample: 15,
overall: { hits: 10, misses: 5, pushes: 0, total: 15, pct: 67 },
byGrade: {
'A+': { hits: 3, misses: 0, pushes: 0, total: 3, pct: 100 },
'A': { hits: 5, misses: 2, pushes: 0, total: 7, pct: 71 },
'B': { hits: 2, misses: 3, pushes: 0, total: 5, pct: 40 },
},
};
const res = await request(app).get('/api/ledger/accuracy');
expect(res.status).toBe(200);
expect(Array.isArray(res.body.buckets)).toBe(true);
const grades = res.body.buckets.map((b) => b.grade);
expect(grades).toContain('A+');
expect(grades).toContain('A');
});
test('cold cache → empty buckets, never 500', async () => {
const res = await request(app).get('/api/ledger/accuracy');
expect(res.status).toBe(200);
expect(res.body.buckets).toEqual([]);
});
});
+162
View File
@@ -0,0 +1,162 @@
'use strict';
const svc = require('../../src/services/outcomeService');
const { settleResult, gradeBucket, dateStrings, statValue, outcomeKey } = svc.__internals;
// A tiny in-memory Redis so settleSnapshot round-trips through cacheGet/cacheSet.
function memCache(seed = {}) {
const store = { ...seed };
return {
store,
cacheGet: async (k) => (k in store ? store[k] : null),
cacheSet: async (k, v) => { store[k] = v; return true; },
};
}
const ISO = '2026-07-10T02:00:00.000Z'; // ~10pm ET Jul 9 — exercises the ET rollover
function snapshot(grades) {
return { sport: 'mlb', updated_at: ISO, grades };
}
function grade(over = {}) {
return {
player: over.player || 'Aaron Judge',
stat_type: over.stat || 'hits',
line: over.line != null ? over.line : 1.5,
direction: over.side || 'over',
grade: over.grade || 'A',
gradedAt: { line: over.line != null ? over.line : 1.5, odds: -115, timestamp: ISO },
};
}
// Game-log rows keyed to the ET or UTC date of ISO.
function log(date, stat) { return [{ date, opponent: 'BOS', stat }]; }
describe('outcomeService — settlement math', () => {
test('over: actual above line = hit, below = miss, equal = push', () => {
expect(settleResult('over', 2, 1.5)).toBe('hit');
expect(settleResult('over', 1, 1.5)).toBe('miss');
expect(settleResult('over', 2, 2)).toBe('push');
});
test('under: actual below line = hit, above = miss', () => {
expect(settleResult('under', 1, 1.5)).toBe('hit');
expect(settleResult('under', 2, 1.5)).toBe('miss');
expect(settleResult('U', 3, 3)).toBe('push');
});
test('non-numeric actual/line → null (unsettleable)', () => {
expect(settleResult('over', null, 1.5)).toBeNull();
expect(settleResult('over', 2, undefined)).toBeNull();
});
test('gradeBucket tiers: A+ stands alone, letters collapse', () => {
expect(gradeBucket('A+')).toBe('A+');
expect(gradeBucket('A-')).toBe('A');
expect(gradeBucket('B+')).toBe('B');
expect(gradeBucket('C')).toBe('C');
expect(gradeBucket('')).toBeNull();
});
test('statValue maps VYNDR stat_type → game-log field', () => {
expect(statValue({ totalBases: 3 }, 'total_bases')).toBe(3);
expect(statValue({ homeRuns: 1 }, 'home_runs')).toBe(1);
expect(statValue({ strikeOuts: 7 }, 'strikeouts')).toBe(7);
expect(statValue({ hits: 2 }, 'unknown_stat')).toBeNull();
});
test('dateStrings yields both UTC and ET calendar dates', () => {
const ds = dateStrings(ISO);
expect(ds).toContain('2026-07-10'); // UTC
expect(ds).toContain('2026-07-09'); // ET (10pm prior day)
});
});
describe('outcomeService — settleSnapshot', () => {
const judgeStats = async (name) => {
if (name === 'Aaron Judge') return { found: true, last10: log('2026-07-09', { hits: 2, totalBases: 4 }) };
return { found: false };
};
test('settles a hit against the real game log', async () => {
const cache = memCache({ 'snapshot:mlb:latest': snapshot([grade({ stat: 'hits', line: 1.5, side: 'over', grade: 'A' })]) });
const res = await svc.settleSnapshot('mlb', { ...cache, getPlayerStats: judgeStats, now: () => '2026-07-10T15:00:00Z' });
expect(res.settled).toBe(1);
expect(res.log[0]).toMatchObject({ result: 'hit', actual: 2, grade: 'A', side: 'O' });
expect(cache.store['accuracy:mlb'].byGrade['A'].hits).toBe(1);
expect(cache.store['accuracy:mlb'].overall.pct).toBe(100);
});
test('settles a miss (under, actual above line)', async () => {
const cache = memCache({ 'snapshot:mlb:latest': snapshot([grade({ stat: 'hits', line: 1.5, side: 'under', grade: 'B+' })]) });
const res = await svc.settleSnapshot('mlb', { ...cache, getPlayerStats: judgeStats, now: () => '2026-07-10T15:00:00Z' });
expect(res.log[0].result).toBe('miss');
expect(cache.store['accuracy:mlb'].byGrade['B'].misses).toBe(1);
});
test('is idempotent — a second run does not double-count', async () => {
const cache = memCache({ 'snapshot:mlb:latest': snapshot([grade({ grade: 'A' })]) });
const deps = { ...cache, getPlayerStats: judgeStats, now: () => '2026-07-10T15:00:00Z' };
await svc.settleSnapshot('mlb', deps);
const res2 = await svc.settleSnapshot('mlb', deps);
expect(res2.settled).toBe(0);
expect(res2.log.length).toBe(1);
expect(cache.store['accuracy:mlb'].overall.total).toBe(1);
});
test('a prop with no matching game stays pending', async () => {
const noGame = async () => ({ found: true, last10: log('2026-01-01', { hits: 0 }) });
const cache = memCache({ 'snapshot:mlb:latest': snapshot([grade()]) });
const res = await svc.settleSnapshot('mlb', { ...cache, getPlayerStats: noGame, now: () => '2026-07-10T15:00:00Z' });
expect(res.settled).toBe(0);
expect(res.pending).toBe(1);
});
test('offline stats (found:false) → pending, never throws', async () => {
const cache = memCache({ 'snapshot:nba:latest': snapshot([grade()]) });
const res = await svc.settleSnapshot('nba', { ...cache, getPlayerStats: async () => ({ found: false }), now: () => '2026-07-10T15:00:00Z' });
expect(res.settled).toBe(0);
expect(res.pending).toBe(1);
});
});
describe('outcomeService — accuracy aggregation', () => {
test('pct excludes pushes and gates on the 30-day window', () => {
const nowIso = '2026-07-10T00:00:00Z';
const mk = (grade, result, date) => ({ grade, result, date, player: 'X', stat: 'hits', line: 1.5, side: 'O' });
const logRows = [
mk('A', 'hit', '2026-07-09'),
mk('A', 'hit', '2026-07-08'),
mk('A', 'miss', '2026-07-07'),
mk('A', 'push', '2026-07-06'),
mk('A', 'hit', '2026-01-01'), // outside 30d — excluded
];
const acc = svc.computeAccuracy('mlb', logRows, nowIso);
expect(acc.byGrade['A'].hits).toBe(2);
expect(acc.byGrade['A'].misses).toBe(1);
expect(acc.byGrade['A'].pushes).toBe(1);
expect(acc.byGrade['A'].pct).toBe(67); // 2/(2+1) rounded
expect(acc.sample).toBe(4); // 4 in-window, push counts toward sample
});
test('accuracyBuckets flattens only non-empty tiers', () => {
const acc = svc.computeAccuracy('mlb', [
{ grade: 'A', result: 'hit', date: '2026-07-09' },
{ grade: 'B', result: 'miss', date: '2026-07-09' },
], '2026-07-10T00:00:00Z');
const buckets = svc.accuracyBuckets(acc);
expect(buckets.map((b) => b.grade).sort()).toEqual(['A', 'B']);
});
test('getAccuracy is cold-cache safe', async () => {
const cache = memCache();
const out = await svc.getAccuracy(cache);
expect(out.overall.overall.total).toBe(0);
expect(out.sports).toEqual({});
});
test('recomputeOverall merges every sport log', async () => {
const cache = memCache({
'outcomes:mlb:log': [{ grade: 'A', result: 'hit', date: '2026-07-09' }],
'outcomes:nba:log': [{ grade: 'A', result: 'miss', date: '2026-07-09' }],
});
const acc = await svc.recomputeOverall({ ...cache, now: () => '2026-07-10T00:00:00Z' });
expect(acc.overall.total).toBe(2);
expect(acc.overall.pct).toBe(50);
expect(cache.store['accuracy:overall']).toBeTruthy();
});
});
+24
View File
@@ -33,6 +33,30 @@ describe('groupPropsByPlayer', () => {
});
});
describe('buildPlayerStripsFromProps — settled outcome passthrough (Session 55)', () => {
it('attaches the settled outcome from the snapshot grade onto the prop', () => {
const props = [{ player: 'Aaron Judge', stat_type: 'hits', line: 1.5 }];
const gradeIndex = adapter.indexGrades([
{ player: 'Aaron Judge', stat_type: 'hits', line: 1.5, direction: 'over', grade: 'A',
gradedAt: { line: 1.5, timestamp: '2026-07-10T02:00:00Z' },
outcome: { result: 'hit', actual: 2 } },
]);
const strips = adapter.buildPlayerStripsFromProps(props, gradeIndex, {});
expect(strips[0].props[0].outcome).toEqual({ result: 'hit', actual: 2 });
expect(strips[0].props[0].grade).toBe('A');
});
it('leaves outcome null when the grade has not settled', () => {
const props = [{ player: 'Aaron Judge', stat_type: 'hits', line: 1.5 }];
const gradeIndex = adapter.indexGrades([
{ player: 'Aaron Judge', stat_type: 'hits', line: 1.5, direction: 'over', grade: 'A',
gradedAt: { line: 1.5, timestamp: '2026-07-10T02:00:00Z' } },
]);
const strips = adapter.buildPlayerStripsFromProps(props, gradeIndex, {});
expect(strips[0].props[0].outcome).toBeNull();
});
});
describe('mapPitchers', () => {
it('maps MLB probable pitchers to the GameCard shape', () => {
const p = adapter.mapPitchers({