"""The scoring paths must never see a result that was inferred from prices.

This exists because they did. On 2026-08-15 a market-derived fallback was put
straight into `ftr` so the dashboard would stop showing finished matches as
open. Every scoring path in the project defines "settled" as `ftr is not None`,
so the inferences were promoted into the forward test unnoticed and moved the
xlow OOS ROI from -1.22% to -0.01% -- a losing rule rendered break-even, in the
flattering direction, on the tile sitting next to real account cash.

The fix was to stop overloading `ftr`. These tests hold that line.
"""
import sys
sys.path.insert(0, r'E:\soccer\research')
sys.path.insert(0, r'E:\soccer\main')
import forward_test_unique_book as F

fails = []


def check(label, cond, extra=''):
    print(f'  [{"pass" if cond else "FAIL"}] {label}{("  " + extra) if extra else ""}')
    if not cond:
        fails.append(label)


print('=== load_fixtures contract ===')
scored = F.load_fixtures(require_result=True)
disp = F.load_fixtures(require_result=False)

check('every scored fixture has a real result',
      all(f['ftr'] in ('H', 'D', 'A') for f in scored), f'n={len(scored)}')
check('scored fixtures are all api-sourced',
      {f.get('ftr_src') for f in scored} == {'api'},
      f'{ {f.get("ftr_src") for f in scored} }')
check('ftr is NEVER a market inference, even on the display load',
      all(f['ftr'] is None or f.get('ftr_src') == 'api' for f in disp))
check('market results live only in ftr_display',
      all(f['ftr'] is None for f in disp if f.get('ftr_src') == 'market'),
      f'{sum(1 for f in disp if f.get("ftr_src") == "market")} inferred')

print('\n=== the invariant the dashboard actually relies on ===')
# dashboard.forward_test() does exactly this, then scores it
prepped = [fx for fx in disp if fx['ftr'] is not None]
check('filtering on `ftr is not None` yields ONLY confirmed results',
      all(f.get('ftr_src') == 'api' for f in prepped), f'n={len(prepped)}')
check('and matches the require_result=True set exactly',
      len(prepped) == len(scored), f'{len(prepped)} vs {len(scored)}')

print('\n=== scoring is unchanged by the display fallback ===')
oos_clean = [f for f in scored if f['gd'] > F.FREEZE_DATE]
oos_disp = [f for f in prepped if f['gd'] > F.FREEZE_DATE]
a = F.stat(F.classify_xlow(oos_clean))
b = F.stat(F.classify_xlow(oos_disp))
check('xlow n identical', a['n'] == b['n'], f'{a["n"]} vs {b["n"]}')
check('xlow roi identical', abs(a['roi'] - b['roi']) < 1e-9,
      f'{a["roi"]:+.4f}% vs {b["roi"]:+.4f}%')

print('\n=== fire_xhigh_detail keeps `won` on the confirmed result ===')
det = [d for d in (F.fire_xhigh_detail(f) for f in disp) if d]
bad = [d for d in det
       if d.get('ftr') is None and d.get('won') is True]
check('no detail row claims a win without a confirmed result',
      not bad, f'{len(det)} detail rows checked')
# Written first as "some row has ftr_display without ftr, OR every row has ftr",
# which failed the moment the API backfill caught up and left only genuinely
# unplayed fixtures carrying neither. That is the normal state, not a defect --
# the test was asserting that inferences EXIST rather than that they are
# labelled. State the real invariant instead: wherever a display-only result
# appears, it is tagged as inferred and has not been written into `ftr`.
inferred = [d for d in det if d.get('ftr_display') and not d.get('ftr')]
check('every display-only result is tagged as inferred',
      all(d.get('ftr_src') == 'market' for d in inferred),
      f'{len(inferred)} inferred of {len(det)} rows'
      + (' (none right now -- API has caught up)' if not inferred else ''))
check('no row carries a result in ftr that is flagged inferred',
      not [d for d in det if d.get('ftr') and d.get('ftr_src') == 'market'])

print('\n' + ('ALL PASSED' if not fails else f'FAILURES: {fails}'))
sys.exit(1 if fails else 0)
