Session 23: All-day intelligence layer — schedule, game lines, streaks, hot lists, stat filtering, ParlayAPI dead (1567 tests)

This commit is contained in:
Kev
2026-06-12 11:16:58 -04:00
parent 6ab49d4c37
commit 0538205fab
32 changed files with 2276 additions and 2 deletions
+98
View File
@@ -0,0 +1,98 @@
// Integration: /api/gamelines/:sport (Session 23).
//
// The Tank01 adapters are mocked — we assert the route normalizes the
// book-by-book body, parses teams from the gameID, handles the missing
// API-key path gracefully, and never 500s on adapter failure.
const express = require('express');
const request = require('supertest');
jest.mock('../../src/services/adapters/tank01NbaAdapter', () => ({
getNBABettingOdds: jest.fn(),
hasApiKey: jest.fn(() => true),
}));
jest.mock('../../src/services/adapters/tank01MlbAdapter', () => ({
getMLBBettingOdds: jest.fn(),
hasApiKey: jest.fn(() => true),
}));
jest.mock('../../src/services/scheduleService', () => ({
todayET: () => '2026-06-12',
}));
const nbaAdapter = require('../../src/services/adapters/tank01NbaAdapter');
const mlbAdapter = require('../../src/services/adapters/tank01MlbAdapter');
function mountApp() {
delete require.cache[require.resolve('../../src/routes/gameLines')];
const routes = require('../../src/routes/gameLines');
const app = express();
app.use('/api/gamelines', routes);
return app;
}
const MLB_BODY = {
'20260612_ARI@CIN': {
gameID: '20260612_ARI@CIN',
sportsBooks: [
{ sportsBook: 'bet365', odds: { homeTeamMLOdds: '-110', awayTeamMLOdds: '+100', totalOver: '9.5', totalOverOdds: '-105', totalUnderOdds: '-115' } },
{ sportsBook: 'betmgm', odds: { homeTeamMLOdds: '-115', awayTeamMLOdds: '-105', totalOver: '9' } },
],
},
};
beforeEach(() => {
jest.clearAllMocks();
// clearAllMocks wipes call history but not custom implementations set via
// mockReturnValue in a prior test — restore the configured-key default.
nbaAdapter.hasApiKey.mockReturnValue(true);
mlbAdapter.hasApiKey.mockReturnValue(true);
});
describe('GET /api/gamelines/:sport', () => {
test('mlb returns book-by-book odds with teams parsed from gameID', async () => {
mlbAdapter.getMLBBettingOdds.mockResolvedValue(MLB_BODY);
const res = await request(mountApp()).get('/api/gamelines/mlb');
expect(res.status).toBe(200);
expect(res.body.source).toBe('tank01');
const game = res.body.games['20260612_ARI@CIN'];
expect(game.homeTeam).toBe('CIN');
expect(game.awayTeam).toBe('ARI');
expect(game.books.bet365.homeML).toBe('-110');
expect(game.books.bet365.total).toBe('9.5');
expect(game.books.betmgm.homeML).toBe('-115');
});
test('nba returns empty games object off-season (not an error)', async () => {
nbaAdapter.getNBABettingOdds.mockResolvedValue({});
const res = await request(mountApp()).get('/api/gamelines/nba');
expect(res.status).toBe(200);
expect(res.body.games).toEqual({});
});
test('missing RAPID_API_KEY → graceful, configured:false, not a crash', async () => {
mlbAdapter.hasApiKey.mockReturnValue(false);
const res = await request(mountApp()).get('/api/gamelines/mlb');
expect(res.status).toBe(200);
expect(res.body.configured).toBe(false);
expect(mlbAdapter.getMLBBettingOdds).not.toHaveBeenCalled();
});
test('adapter throwing → empty games, 200 not 500', async () => {
mlbAdapter.getMLBBettingOdds.mockRejectedValue(new Error('rapidapi 429'));
const res = await request(mountApp()).get('/api/gamelines/mlb');
expect(res.status).toBe(200);
expect(res.body.games).toEqual({});
});
test('unsupported sport → 404', async () => {
const res = await request(mountApp()).get('/api/gamelines/soccer');
expect(res.status).toBe(404);
});
test('cache works — adapter called once per request (adapter owns TTL)', async () => {
mlbAdapter.getMLBBettingOdds.mockResolvedValue(MLB_BODY);
const app = mountApp();
await request(app).get('/api/gamelines/mlb');
expect(mlbAdapter.getMLBBettingOdds).toHaveBeenCalledTimes(1);
});
});
+127
View File
@@ -0,0 +1,127 @@
// Integration: /api/schedule/:sport (Session 23).
//
// The schedule service is mocked at the redis layer so we exercise the
// route + service normalization + flag enrichment without hitting ESPN
// or a live Redis. ESPN itself is stubbed via axios.
const express = require('express');
const request = require('supertest');
jest.mock('axios');
const axios = require('axios');
// In-memory redis stand-in. cacheGet returns whatever we seed; cacheSet
// records writes so we can assert the cache-aside warm path.
const store = {};
jest.mock('../../src/utils/redis', () => ({
cacheGet: jest.fn(async (k) => (k in store ? store[k] : null)),
cacheSet: jest.fn(async (k, v) => { store[k] = v; return true; }),
}));
function mountApp() {
delete require.cache[require.resolve('../../src/routes/schedule')];
delete require.cache[require.resolve('../../src/services/scheduleService')];
const scheduleRoutes = require('../../src/routes/schedule');
const app = express();
app.use(express.json());
app.use('/api/schedule', scheduleRoutes);
return app;
}
const ESPN_NBA = {
data: {
events: [
{
id: '401234567',
date: '2026-06-14T00:40:00Z',
status: { type: { state: 'pre' } },
competitions: [{
venue: { fullName: 'Frost Bank Center' },
broadcasts: [{ names: ['ABC'] }],
competitors: [
{ homeAway: 'home', score: '0', team: { displayName: 'San Antonio Spurs', abbreviation: 'SA' } },
{ homeAway: 'away', score: '0', team: { displayName: 'New York Knicks', abbreviation: 'NYK' } },
],
}],
},
],
},
};
beforeEach(() => {
for (const k of Object.keys(store)) delete store[k];
jest.clearAllMocks();
});
describe('GET /api/schedule/:sport', () => {
test('returns normalized games from a fresh ESPN fetch (cache miss)', async () => {
axios.get.mockResolvedValue(ESPN_NBA);
const res = await request(mountApp()).get('/api/schedule/nba?date=2026-06-12');
expect(res.status).toBe(200);
expect(res.body.sport).toBe('nba');
expect(res.body.source).toBe('espn');
expect(res.body.games).toHaveLength(1);
const g = res.body.games[0];
expect(g.homeTeam.abbreviation).toBe('SA');
expect(g.awayTeam.name).toBe('New York Knicks');
expect(g.status).toBe('pre');
expect(g.venue).toBe('Frost Bank Center');
expect(g.broadcast).toBe('ABC');
expect(g.score).toBeNull(); // pre-game → no score
expect(g.hasOdds).toBe(false);
expect(g.hasGameLines).toBe(false);
});
test('warms the cache, second call does not re-fetch ESPN', async () => {
axios.get.mockResolvedValue(ESPN_NBA);
const app = mountApp();
await request(app).get('/api/schedule/nba?date=2026-06-12');
const callsAfterFirst = axios.get.mock.calls.length;
await request(app).get('/api/schedule/nba?date=2026-06-12');
expect(axios.get.mock.calls.length).toBe(callsAfterFirst); // served from cache
});
test('returns empty array (not error) when no games today', async () => {
axios.get.mockResolvedValue({ data: { events: [] } });
const res = await request(mountApp()).get('/api/schedule/mlb?date=2026-06-12');
expect(res.status).toBe(200);
expect(res.body.games).toEqual([]);
});
test('hasOdds true when odds-api cache has props for the slate', async () => {
store['odds:nba:2026-06-12'] = { updated_at: 'x', props: [{ player: 'Wemby' }] };
axios.get.mockResolvedValue(ESPN_NBA);
const res = await request(mountApp()).get('/api/schedule/nba?date=2026-06-12');
expect(res.body.games[0].hasOdds).toBe(true);
expect(res.body.games[0].hasGameLines).toBe(false);
});
test('hasGameLines true when Tank01 odds cache has data', async () => {
store['tank01:nba:odds:20260612'] = { body: { '20260612_NYK@SA': { homeML: '-110' } } };
axios.get.mockResolvedValue(ESPN_NBA);
const res = await request(mountApp()).get('/api/schedule/nba?date=2026-06-12');
expect(res.body.games[0].hasGameLines).toBe(true);
});
test('scores appear once a game is in/post', async () => {
axios.get.mockResolvedValue({
data: { events: [{
id: '9', date: '2026-06-12T20:00:00Z',
status: { type: { state: 'in' } },
competitions: [{
competitors: [
{ homeAway: 'home', score: '54', team: { displayName: 'A', abbreviation: 'A' } },
{ homeAway: 'away', score: '49', team: { displayName: 'B', abbreviation: 'B' } },
],
}],
}] },
});
const res = await request(mountApp()).get('/api/schedule/nba?date=2026-06-12');
expect(res.body.games[0].score).toEqual({ home: 54, away: 49 });
});
test('unknown sport returns 404', async () => {
const res = await request(mountApp()).get('/api/schedule/cricket');
expect(res.status).toBe(404);
});
});
@@ -0,0 +1,90 @@
// Integration: /api/streaks/:sport and /api/hotlist/:sport (Session 23).
// rosterLogs is mocked so we exercise route → engine without Redis.
const express = require('express');
const request = require('supertest');
jest.mock('../../src/services/rosterLogs', () => ({
loadRosterLogs: jest.fn(),
}));
const { loadRosterLogs } = require('../../src/services/rosterLogs');
function mountStreaks() {
delete require.cache[require.resolve('../../src/routes/streaks')];
const app = express();
app.use('/api/streaks', require('../../src/routes/streaks'));
return app;
}
function mountHotlist() {
delete require.cache[require.resolve('../../src/routes/hotlist')];
const app = express();
app.use('/api/hotlist', require('../../src/routes/hotlist'));
return app;
}
beforeEach(() => jest.clearAllMocks());
describe('GET /api/streaks/:sport', () => {
test('returns computed streaks from cached logs', async () => {
loadRosterLogs.mockResolvedValue([
{ name: 'Wemby', team: 'SA', games: [{ points: 30 }, { points: 28 }, { points: 26 }] },
]);
const res = await request(mountStreaks()).get('/api/streaks/nba');
expect(res.status).toBe(200);
expect(res.body.source).toBe('computed');
expect(res.body.streaks[0].player).toBe('Wemby');
expect(res.body.streaks[0].currentStreak).toBe(3);
});
test('stat filter narrows the response', async () => {
loadRosterLogs.mockResolvedValue([
{ name: 'A', games: [{ points: 30 }, { points: 30 }] },
{ name: 'B', games: [{ assists: 9 }, { assists: 8 }] },
]);
const res = await request(mountStreaks()).get('/api/streaks/nba?stat=points');
expect(res.body.stat).toBe('points');
expect(res.body.streaks.every((s) => s.category === 'points')).toBe(true);
});
test('empty roster → empty streaks, not an error', async () => {
loadRosterLogs.mockResolvedValue([]);
const res = await request(mountStreaks()).get('/api/streaks/mlb');
expect(res.status).toBe(200);
expect(res.body.streaks).toEqual([]);
});
test('loader throwing → 200 with empty streaks (platform never down)', async () => {
loadRosterLogs.mockRejectedValue(new Error('redis exploded'));
const res = await request(mountStreaks()).get('/api/streaks/nba');
expect(res.status).toBe(200);
expect(res.body.streaks).toEqual([]);
});
test('unsupported sport → 404', async () => {
const res = await request(mountStreaks()).get('/api/streaks/cricket');
expect(res.status).toBe(404);
});
});
describe('GET /api/hotlist/:sport', () => {
test('returns ranked hot players', async () => {
loadRosterLogs.mockResolvedValue([
{ name: 'Riser', seasonAvg: { points: 18 }, games: [{ points: 28 }, { points: 30 }] },
]);
const res = await request(mountHotlist()).get('/api/hotlist/nba?stat=points');
expect(res.status).toBe(200);
expect(res.body.players[0].name).toBe('Riser');
expect(res.body.players[0].rank).toBe(1);
});
test('empty roster → empty players', async () => {
loadRosterLogs.mockResolvedValue([]);
const res = await request(mountHotlist()).get('/api/hotlist/mlb');
expect(res.body.players).toEqual([]);
});
test('unsupported sport → 404', async () => {
const res = await request(mountHotlist()).get('/api/hotlist/nfl');
expect(res.status).toBe(404);
});
});
+82
View File
@@ -0,0 +1,82 @@
// Unit: hot-list engine (Session 23). Pure function.
const { computeHotList } = require('../../src/services/hotListService');
describe('hotListService', () => {
test('"hot" means above personal average, not just high raw numbers', () => {
const players = [
// 20-PPG player erupting for 28/31/25 → HOT
{ name: 'Riser', games: [{ points: 28 }, { points: 31 }, { points: 25 }, { points: 20 }, { points: 19 }, { points: 21 }, { points: 20 }, { points: 18 }, { points: 22 }, { points: 20 }] },
// 30-PPG star who dropped 28 recently → NOT hot (below own baseline)
{ name: 'Star', games: [{ points: 28 }, { points: 27 }, { points: 26 }, { points: 31 }, { points: 33 }, { points: 30 }, { points: 32 }, { points: 31 }, { points: 30 }, { points: 33 }] },
];
const list = computeHotList(players, 'nba', { stat: 'points', window: 3 });
expect(list.map((p) => p.name)).toContain('Riser');
expect(list.map((p) => p.name)).not.toContain('Star');
});
test('returns a ranked list with rank field', () => {
const players = [
{ name: 'A', games: [{ points: 40 }, { points: 40 }, { points: 10 }, { points: 10 }] },
{ name: 'B', games: [{ points: 22 }, { points: 22 }, { points: 18 }, { points: 18 }] },
];
const list = computeHotList(players, 'nba', { stat: 'points', window: 2 });
expect(list[0].rank).toBe(1);
expect(list[0].name).toBe('A'); // biggest jump above baseline
expect(list[0].delta).toBeGreaterThan(list[1].delta);
});
test('uses explicit seasonAvg as baseline when present', () => {
const players = [
{ name: 'C', seasonAvg: { points: 15 }, games: [{ points: 25 }, { points: 25 }] },
];
const list = computeHotList(players, 'nba', { stat: 'points', window: 2 });
expect(list[0].baseline).toBe(15);
expect(list[0].delta).toBe(10);
});
test('7-day window filters by date when dates + now supplied', () => {
const now = new Date('2026-06-12T12:00:00Z').getTime();
const day = 86_400_000;
const players = [
{ name: 'D', games: [
{ date: '2026-06-11', hits: 3 }, // in window
{ date: '2026-06-10', hits: 2 }, // in window
{ date: '2026-06-01', hits: 0 }, // outside 7d → baseline
{ date: '2026-05-30', hits: 0 },
] },
];
const list = computeHotList(players, 'mlb', { stat: 'hits', windowDays: 7, now });
expect(list).toHaveLength(1);
expect(list[0].window).toBe(2); // only the 2 recent games
expect(list[0].recentAvg).toBe(2.5);
});
test('player with no baseline to trend against is excluded', () => {
const players = [
{ name: 'E', games: [{ points: 30 }, { points: 30 }] }, // window 7 → no rest, no seasonAvg
];
const list = computeHotList(players, 'nba', { stat: 'points' });
expect(list).toHaveLength(0);
});
test('tie on delta broken by raw recent average', () => {
const players = [
{ name: 'Low', games: [{ points: 15 }, { points: 15 }, { points: 10 }, { points: 10 }] }, // +5, recent 15
{ name: 'High', games: [{ points: 25 }, { points: 25 }, { points: 20 }, { points: 20 }] }, // +5, recent 25
];
const list = computeHotList(players, 'nba', { stat: 'points', window: 2 });
expect(list[0].name).toBe('High');
});
test('empty input → empty list', () => {
expect(computeHotList([], 'nba')).toEqual([]);
expect(computeHotList(null, 'nba')).toEqual([]);
});
test('all/default stat resolves to the sport headline stat', () => {
const players = [{ name: 'F', games: [{ hits: 3 }, { hits: 3 }, { hits: 0 }, { hits: 0 }] }];
const list = computeHotList(players, 'mlb', { stat: 'all', window: 2 });
expect(list[0].stat).toBe('hits');
});
});
+54
View File
@@ -0,0 +1,54 @@
// Unit: provider registry dead-provider handling (Session 23).
//
// ParlayAPI was marked `status: 'dead'` after Chrome Claude confirmed its
// host no longer resolves. It must be excluded from every fallback chain
// and the configured-providers list, while still resolving via getProvider
// (the adapter + its mocked tests still reference the config).
const {
getProvider, getFallbackChain, getConfiguredProviders, isDeadProvider,
} = require('../../src/config/providers');
describe('provider registry — dead providers', () => {
const saved = {};
beforeAll(() => {
// Configure keys so the chain/list filters are exercised on presence.
for (const k of ['PARLAYAPI_KEY', 'ODDS_API_KEY', 'ODDSPAPI_KEY']) {
saved[k] = process.env[k];
process.env[k] = 'test-key';
}
});
afterAll(() => {
for (const [k, v] of Object.entries(saved)) {
if (v === undefined) delete process.env[k];
else process.env[k] = v;
}
});
test('parlayapi is flagged dead', () => {
expect(isDeadProvider('parlayapi')).toBe(true);
expect(getProvider('parlayapi')).not.toBeNull(); // config still resolves
expect(getProvider('parlayapi').status).toBe('dead');
});
test('dead provider is excluded from fallback chains', () => {
const chain = getFallbackChain('historical_props', 'nba', null);
expect(chain).not.toContain('parlayapi');
});
test('dead provider is excluded from configured providers', () => {
const ids = getConfiguredProviders().map((p) => p.id);
expect(ids).not.toContain('parlayapi');
});
test('live providers still appear in fallback chains', () => {
const chain = getFallbackChain('closing_lines', 'nba', null);
expect(chain).toContain('oddspapi');
});
test('non-dead providers report isDeadProvider false', () => {
expect(isDeadProvider('odds-api')).toBe(false);
expect(isDeadProvider('tank01')).toBe(false);
expect(isDeadProvider('nonexistent')).toBe(false);
});
});
+60
View File
@@ -0,0 +1,60 @@
// Unit: rosterLogs loader (Session 23). Redis-only; no network.
const store = {};
const mockScan = jest.fn();
jest.mock('../../src/utils/redis', () => ({
cacheGet: jest.fn(async (k) => (k in store ? store[k] : null)),
getRedisClient: () => ({ scan: mockScan }),
isDegraded: () => false,
}));
const { loadRosterLogs, __internals } = require('../../src/services/rosterLogs');
beforeEach(() => {
for (const k of Object.keys(store)) delete store[k];
mockScan.mockReset();
});
describe('rosterLogs', () => {
test('fast path: returns a prefetched roster blob without scanning', async () => {
store['rosterlogs:nba'] = [{ name: 'Wemby', games: [{ points: 30 }] }];
const roster = await loadRosterLogs('nba');
expect(roster).toHaveLength(1);
expect(mockScan).not.toHaveBeenCalled();
});
test('scan path: assembles roster from per-player gamelogs keys', async () => {
store['gamelogs:nba:Wembanyama:20'] = [{ points: 30, team: 'SA', playerId: 'W1' }];
store['gamelogs:nba:Brunson:20'] = [{ points: 24, team: 'NYK' }];
mockScan
.mockResolvedValueOnce(['0', ['gamelogs:nba:Wembanyama:20', 'gamelogs:nba:Brunson:20']]);
const roster = await loadRosterLogs('nba');
expect(roster.map((p) => p.name).sort()).toEqual(['Brunson', 'Wembanyama']);
const wemby = roster.find((p) => p.name === 'Wembanyama');
expect(wemby.team).toBe('SA');
expect(wemby.playerId).toBe('W1');
});
test('dedupes by highest game-count variant', async () => {
store['gamelogs:nba:Star:10'] = [{ points: 1 }, { points: 2 }];
store['gamelogs:nba:Star:20'] = [{ points: 1 }, { points: 2 }, { points: 3 }];
mockScan.mockResolvedValueOnce(['0', ['gamelogs:nba:Star:10', 'gamelogs:nba:Star:20']]);
const roster = await loadRosterLogs('nba');
expect(roster).toHaveLength(1);
expect(roster[0].games).toHaveLength(3); // the :20 variant wins
});
test('empty cache → empty roster, never throws', async () => {
mockScan.mockResolvedValueOnce(['0', []]);
expect(await loadRosterLogs('nba')).toEqual([]);
});
test('scan failure → returns what it had, no throw', async () => {
mockScan.mockRejectedValueOnce(new Error('redis down'));
expect(await loadRosterLogs('nba')).toEqual([]);
});
test('playerFromKey parses the player name out of the key', () => {
expect(__internals.playerFromKey('gamelogs:nba:LeBron James:20', 'nba')).toBe('LeBron James');
});
});
+51
View File
@@ -0,0 +1,51 @@
// Unit: stat-filter config + /api/stats/filters/:sport (Session 23).
const express = require('express');
const request = require('supertest');
const { STAT_FILTERS, getStatFilters, isValidStat } = require('../../src/config/statFilters');
describe('statFilters config', () => {
test('every sport list starts with "all"', () => {
for (const list of Object.values(STAT_FILTERS)) {
expect(list[0]).toBe('all');
}
});
test('NBA categories include the headline stats', () => {
expect(getStatFilters('nba')).toEqual(
expect.arrayContaining(['points', 'rebounds', 'assists', 'threes', 'pra']),
);
});
test('MLB categories include home_runs and total_bases', () => {
expect(getStatFilters('mlb')).toEqual(
expect.arrayContaining(['home_runs', 'total_bases', 'on_base']),
);
});
test('unknown sport falls back to ["all"]', () => {
expect(getStatFilters('quidditch')).toEqual(['all']);
});
test('isValidStat — all is always valid, unknown stat is not', () => {
expect(isValidStat('nba', 'all')).toBe(true);
expect(isValidStat('nba', 'points')).toBe(true);
expect(isValidStat('nba', 'home_runs')).toBe(false);
expect(isValidStat('mlb', 'home_runs')).toBe(true);
});
});
describe('GET /api/stats/filters/:sport', () => {
function app() {
const a = express();
a.use('/api/stats', require('../../src/routes/stats'));
return a;
}
test('returns the category list for a sport', async () => {
const res = await request(app()).get('/api/stats/filters/nba');
expect(res.status).toBe(200);
expect(res.body.sport).toBe('nba');
expect(res.body.filters).toContain('points');
});
});
+130
View File
@@ -0,0 +1,130 @@
// Unit: streaks engine (Session 23). Pure function — no mocks needed.
const { computeStreaks, computePlayerStreaks } = require('../../src/services/streaksService');
// Most-recent-first game logs.
function nbaGames(...rows) { return rows; }
describe('streaksService — NBA', () => {
test('detects a consecutive 25+ points streak from the latest games', () => {
const player = {
name: 'Wembanyama', playerId: 'W1', team: 'SA',
games: [{ points: 31 }, { points: 28 }, { points: 33 }, { points: 22 }, { points: 40 }],
};
const streaks = computePlayerStreaks(player, 'nba');
const pts = streaks.find((s) => s.category === 'points');
expect(pts).toBeDefined();
// 31,28,33 meet 25+, then 22 breaks → run of 3.
expect(pts.currentStreak).toBe(3);
expect(pts.description).toBe('3-game 25+ pts streak');
});
test('streak breaks immediately when the latest game misses the threshold', () => {
const player = { name: 'X', games: [{ points: 10 }, { points: 30 }, { points: 30 }] };
const streaks = computePlayerStreaks(player, 'nba');
expect(streaks.find((s) => s.category === 'points')).toBeUndefined(); // run of 0 < MIN_STREAK
});
test('only the strongest streak per category surfaces', () => {
// 20+ and 25+ both qualify; keep the 25+ (higher run not guaranteed, but
// one points entry only).
const player = { name: 'Y', games: [{ points: 26 }, { points: 27 }, { points: 21 }] };
const streaks = computePlayerStreaks(player, 'nba');
const ptsEntries = streaks.filter((s) => s.category === 'points');
expect(ptsEntries).toHaveLength(1);
});
test('double-double streak counts categories >= 10', () => {
const player = {
name: 'Jokic', games: [
{ points: 20, rebounds: 12, assists: 11 },
{ points: 15, rebounds: 10, assists: 9 },
{ points: 8, rebounds: 11, assists: 3 },
],
};
const streaks = computePlayerStreaks(player, 'nba');
const dd = streaks.find((s) => s.type === 'double_double');
expect(dd).toBeDefined();
// g0: 3 doubles, g1: 2 doubles, g2: 1 double → dd (>=2) run breaks at g2.
expect(dd.currentStreak).toBe(2);
expect(dd.description).toBe('2-game double-double streak');
});
test('empty game logs → empty streaks (not error)', () => {
expect(computePlayerStreaks({ name: 'Z', games: [] }, 'nba')).toEqual([]);
expect(computeStreaks([], 'nba')).toEqual([]);
});
test('chronological input is reversed before counting', () => {
const player = { name: 'C', games: [{ points: 22 }, { points: 30 }, { points: 31 }] };
// As chronological (oldest first) the latest is 31,30 → run 2.
const streaks = computePlayerStreaks(player, 'nba', { chronological: true });
const pts = streaks.find((s) => s.category === 'points');
expect(pts.currentStreak).toBe(2);
});
});
describe('streaksService — MLB', () => {
test('classic hit streak counts consecutive games with a hit', () => {
const player = {
name: 'Acuna', team: 'ATL',
games: [{ hits: 2 }, { hits: 1 }, { hits: 3 }, { hits: 0 }, { hits: 1 }],
};
const streaks = computePlayerStreaks(player, 'mlb');
const hit = streaks.find((s) => s.type === 'hit_streak');
expect(hit.currentStreak).toBe(3);
expect(hit.description).toBe('3-game hit streak');
});
test('quality-start streak uses IP + ER', () => {
const pitcher = {
name: 'Strider',
games: [
{ inningsPitched: 7, earnedRuns: 2 },
{ inningsPitched: 6, earnedRuns: 3 },
{ inningsPitched: 5, earnedRuns: 1 }, // < 6 IP breaks it
],
};
const streaks = computePlayerStreaks(pitcher, 'mlb');
const qs = streaks.find((s) => s.type === 'qs_streak');
expect(qs.currentStreak).toBe(2);
});
test('MLB specs do not produce NBA streak types', () => {
const player = { name: 'P', games: [{ hits: 2 }, { hits: 2 }] };
const streaks = computePlayerStreaks(player, 'mlb');
expect(streaks.every((s) => s.type !== 'points_25')).toBe(true);
});
});
describe('streaksService — fan-out + filtering', () => {
const roster = [
{ name: 'A', games: [{ points: 30 }, { points: 30 }, { points: 30 }] },
{ name: 'B', games: [{ assists: 9 }, { assists: 8 }] },
{ name: 'C', games: [{ points: 26 }, { points: 27 }] },
];
test('sorts by streak length descending', () => {
const streaks = computeStreaks(roster, 'nba');
expect(streaks[0].player).toBe('A'); // 3-game points streak leads
expect(streaks[0].currentStreak).toBe(3);
});
test('stat filter narrows to a single category', () => {
const streaks = computeStreaks(roster, 'nba', { stat: 'points' });
expect(streaks.every((s) => s.category === 'points')).toBe(true);
expect(streaks.map((s) => s.player).sort()).toEqual(['A', 'C']);
});
test('limit caps the result', () => {
const streaks = computeStreaks(roster, 'nba', { limit: 1 });
expect(streaks).toHaveLength(1);
});
test('all filter (default) returns every category', () => {
const streaks = computeStreaks(roster, 'nba', { stat: 'all' });
const cats = new Set(streaks.map((s) => s.category));
expect(cats.has('points')).toBe(true);
expect(cats.has('assists')).toBe(true);
});
});
+23
View File
@@ -142,5 +142,28 @@ describe('tank01MlbAdapter', () => {
const games = await adapter.getMLBDailyScoreboard('20260611');
expect(games[0].gameId).toBe('STALE');
});
// Session 23 — game-level book-by-book betting odds.
test('getMLBBettingOdds returns the raw body and caches at the odds TTL', async () => {
mockAxiosGet.mockResolvedValueOnce({
data: { body: { '20260612_ARI@CIN': { sportsBooks: [{ sportsBook: 'bet365', odds: {} }] } } },
});
const body = await adapter.getMLBBettingOdds('2026-06-12');
expect(body['20260612_ARI@CIN']).toBeDefined();
const [url] = mockAxiosGet.mock.calls[0];
expect(url).toMatch(/getMLBBettingOdds\?gameDate=20260612/);
expect(mockCacheTtls.get('tank01:mlb:odds:20260612')).toBe(adapter.__internals.TTL.odds);
});
test('getMLBBettingOdds second call within TTL does not hit Tank01', async () => {
mockAxiosGet.mockResolvedValueOnce({ data: { body: { g: {} } } });
await adapter.getMLBBettingOdds('2026-06-12');
await adapter.getMLBBettingOdds('2026-06-12');
expect(mockAxiosGet).toHaveBeenCalledTimes(1);
});
test('getMLBBettingOdds null date returns null without axios', async () => {
expect(await adapter.getMLBBettingOdds(null)).toBeNull();
});
});
});