Book Comparison Phase 1-3(backend): fenced per-book store + honest gated crown

Per-book prices existed only transiently (odds cache, ~1h, raw names, grade-path
input); every grade-path persistence point collapses to one book. The
/api/books feature was built+mounted but non-functional (fed FLAT rows to a
GROUPED comparator -> always empty).

Phase 1: bookPriceStore captures per-book prices from `props` BEFORE dedupeProps,
keyed nameKey|stat, into bookprices:{sport} (SNAP_TTL) in snapshotService. Fenced:
reads props, writes its own key, read by nothing on the grade path. Grade proven
byte-identical (test + no-grade-path-reference grep test).

Phase 2: scripts/measure-book-spread.js reports same-line best-vs-worst spread
(cents + implied-prob pts), per sport, never pooled. Pre-registered crown
threshold: median >=8c OR >=2pp. Runs post-deploy on real data.

Phase 3 (backend): compareProp is honest-absent (single-book/flat -> no crown)
and the crown is gated (BOOK_CROWN_ENABLED, default OFF until Phase 2 clears).
/api/books repointed to the snapshot-locked store (fallback odds cache),
nameKey-matched; `source` field is the deploy fingerprint.

HELD unchanged: dedupeProps, snapshot dedup, selector, grade, champion,
challengers, ranking, edge_pct/ev_pct. UI routing of BookComparison + crown
treatment deferred to post-measurement (gated on Phase 2). Full suite 3834 green,
web build exit 0.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VsztNChZ7vEvSR61AuMhD1
This commit is contained in:
Kev
2026-07-27 00:45:01 -04:00
parent 914a057611
commit e81c9b8c51
9 changed files with 704 additions and 52 deletions
+43 -10
View File
@@ -75,22 +75,55 @@ describe('GET /api/lines/:sport/movers', () => {
describe('GET /api/books/:sport', () => {
const app = () => mount('/api/books', '../../src/routes/bookComparison');
test('returns best lines from cached props', async () => {
mockStore[`odds:nba:${new Date().toISOString().split('T')[0]}`] = {
// Book Comparison order: /api/books/:sport is a crown claim ("best lines
// tonight"), so it is gated OFF until Phase 2's spread measurement clears the
// bar — even with data it returns []. `source` is the deploy fingerprint.
test('reads the snapshot-locked bookprices store; makes no crown claim while gated off', async () => {
mockStore['bookprices:nba'] = {
props: [
{
player: 'Wemby', stat_type: 'points',
lines: [
{ book: 'dk', over_odds: -110 },
{ book: 'fd', over_odds: -105 },
],
},
{ player: 'Wemby', stat_type: 'points', books: [
{ book: 'draftkings', line: 28.5, over_odds: -110 },
{ book: 'fanduel', line: 28.5, over_odds: -105 },
] },
],
};
const res = await request(app()).get('/api/books/nba');
expect(res.status).toBe(200);
expect(res.body.source).toBe('bookprices'); // new store is serving
expect(res.body.bestLines).toEqual([]); // crown gated off → no claim
});
test('crown ENABLED → crowns the best price from the store', async () => {
process.env.BOOK_CROWN_ENABLED = '1';
mockStore['bookprices:nba'] = {
props: [
{ player: 'Wemby', stat_type: 'points', books: [
{ book: 'draftkings', line: 28.5, over_odds: -110 },
{ book: 'fanduel', line: 28.5, over_odds: -105 },
] },
],
};
const res = await request(app()).get('/api/books/nba');
delete process.env.BOOK_CROWN_ENABLED;
expect(res.status).toBe(200);
expect(res.body.bestLines).toHaveLength(1);
expect(res.body.bestLines[0].bestBook).toBe('fd');
expect(res.body.bestLines[0].bestBook).toBe('fanduel');
});
test('per-prop endpoint returns the honest grid (all books, no crown) even gated off', async () => {
mockStore['bookprices:nba'] = {
props: [
{ player: 'Wemby', stat_type: 'points', books: [
{ book: 'draftkings', line: 28.5, over_odds: -110 },
{ book: 'fanduel', line: 28.5, over_odds: -105 },
] },
],
};
const res = await request(app()).get('/api/books/nba/Wemby/points');
expect(res.status).toBe(200);
expect(res.body.books).toHaveLength(2);
expect(res.body.books.every((b) => b.isBest === false)).toBe(true);
expect(res.body.bestBook).toBeNull();
});
test('empty when no cached props', async () => {