#!/usr/bin/env python3
"""Run local deterministic research tests. Never modifies a portfolio or website."""
import copy
import hashlib
import json
from pathlib import Path
from roster_fixture import rows, errors

cases=[]
def record(name, mutate, intended_rejection):
    items=copy.deepcopy(rows)
    mutate(items)
    result=errors(items)
    cases.append({"test":name,"historical_expected_rejection":intended_rejection,
        "legacy_errors":result,"legacy_rejected":bool(result),
        "same_ordered_records_as_baseline":items==rows})

record('unaltered historical baseline',lambda r:None,False)
record('duplicate insertion',lambda r:r.append(dict(r[0])),True)
record('active changed to parked',lambda r:r[0].__setitem__('status','P'),True)
record('designated Concept silently reclassified',lambda r:next(x for x in r if x['title']=='Court of Fifty').__setitem__('category','Investigation'),True)
record('unresolved category silently decided',lambda r:next(x for x in r if x['title']=='Before You Get My Vote').__setitem__('category','Concept'),True)
record('unsupported active topic inserted',lambda r:r.append({'title':'Shadow docket and emergency orders','category':'Investigation','status':'A'}),True)
def swap_status(r,a,b):
    x=next(i for i in r if i['title']==a);y=next(i for i in r if i['title']==b)
    x['status'],y['status']=y['status'],x['status']
record('active and retired swap with totals preserved',lambda r:swap_status(r,'Who Pays for Government','Florida Medical Marijuana'),True)
record('parked topic renamed',lambda r:r[2].__setitem__('title','Unapproved parked topic'),None)
record('ordinary active category changed',lambda r:r[0].__setitem__('category','Concept'),None)
record('parked and retired swapped',lambda r:swap_status(r,'What Did America Actually Build?','Meta Kids and Social Media'),None)
record('record order reversed',lambda r:r.reverse(),None)
assert all(c['legacy_rejected']==c['historical_expected_rejection'] for c in cases[:7])
assert all(not c['legacy_rejected'] and not c['same_ordered_records_as_baseline'] for c in cases[7:])
print(json.dumps({
 'title':'Frozen RT roster checker: historical cases and new boundary tests',
 'test_design_date':'2026-11-06',
 'fixture_sha256':hashlib.sha256(Path(__file__).with_name('roster_fixture.py').read_bytes()).hexdigest(),
 'baseline':{'historical_date':'2026-10-17','rows':len(rows),'active':12,'parked':13,'retired':8},
 'cases':cases,
 'interpretation':'The original seven cases behave as recorded. The legacy checker accepts three added content corruptions and one order change. Exact ordered comparison detects differences but is not a deployed fix.',
 'limits':['Constructed deterministic tests, not random observations or an error-rate estimate.','Historical fixture, not the current Demo portfolio. Authorized changes require a new approved baseline.','No fresh-model sessions, original-chat recovery, physical backup restore, human validation or production deployment.']
},indent=2))
