#!/usr/bin/env python3
import copy,json,pathlib,sys,unittest
from VerifyReports import verify_loaded

root=pathlib.Path(sys.argv[1])
original={n:json.loads((root/(n+'.json')).read_text()) for n in ('core','native','passthrough','recovery')}

def setpath(obj,path,value):
    cursor=obj
    for key in path[:-1]:cursor=cursor[key]
    cursor[path[-1]]=value

class GateTests(unittest.TestCase):
    def test_real_reports_are_limited_not_integration_pass(self):
        result=verify_loaded(copy.deepcopy(original))
        self.assertEqual('synthetic_supported_apis_pass_not_integration_ready',result['status'])
        self.assertEqual(0,result['http_pass_increment']);self.assertIs(False,result['global_precision_p0_closed'])
        self.assertIn('foreach_bypasses_restore_and_blocks_integration',result['limitations'])

mutations={
    'missing_runtime':(['recovery','runs'],[original['recovery']['runs'][0]]),
    'native_exit_failure':(['native','runs',0,'exit_code'],139),
    'stderr_warning':(['recovery','runs',0,'stderr_bytes'],1),
    'stage_failed':(['recovery','runs',0,'report','status'],'fail'),
    'pure_db_request':(['core','runs',0,'report','database_requests'],1),
    'candidate_option':(['recovery','runs',0,'report','identity','extra_decimal_defined'],True),
    'core_dump_enabled':(['recovery','runs',0,'report','identity','core_soft'],1024),
    'missing_maps':(['native','runs',0,'report','identity','mapped'],{}),
    'wrong_schema':(['recovery','runs',0,'report','target','schema_matches'],False),
    'socket_not_proven':(['recovery','runs',0,'report','target','owned_established_loopback_5238_sockets'],0),
    'unknown_falsely_confirmed':(['recovery','runs',0,'report','attributes','17','effective'],'getter-confirmed'),
    'native_red_missing':(['native','runs',0,'report','native_red','irreversible_native_mismatch'],False),
    'business_write':(['recovery','runs',0,'report','dml'],1),
    'real_orm_claim':(['recovery','runs',0,'report','real_orm_getter_tested'],True),
    'missing_case':(['recovery','runs',0,'report','cases'],original['recovery']['runs'][0]['report']['cases'][:-1]),
    'empty_assertions':(['recovery','runs',0,'report','cases',0,'assertions'],0),
    'missing_plan':(['recovery','runs',0,'report','plan_sql_sha256'],{}),
    'forged_plan_hash':(['recovery','runs',0,'report','plan_sql_sha256','decimal30_2'],'0'*64),
    'no_real_execution':(['recovery','runs',0,'report','execute_calls'],0),
    'foreach_false_supported':(['recovery','runs',0,'report','iterator','status'],'supported_observed'),
    'foreach_not_executed':(['recovery','runs',0,'report','iterator','rows'],[]),
    'foreach_float_coerced_to_string':(['recovery','runs',0,'report','iterator','rows',0,'v'],'9007199254740994'),
    'foreach_float_coerced_to_integer':(['recovery','runs',0,'report','iterator','rows',0,'v'],9007199254740994),
}
for name,(path,value) in mutations.items():
    def test(self,path=path,value=value):
        obj=copy.deepcopy(original);setpath(obj,path,value)
        with self.assertRaises((AssertionError,KeyError,TypeError,ValueError)):verify_loaded(obj)
    setattr(GateTests,'test_reject_'+name,test)

if __name__=='__main__':unittest.main(argv=[sys.argv[0]],verbosity=2)
