#!/usr/bin/env python3
"""Operator and bounded natural dictionary nine read keys, no account writes."""
import importlib.util
from pathlib import Path
import re
import sys
import integrate_20261010 as tool
from integrate_20261011_batches40_42 import manifest, private_files, run, sha
from integrate_20261011_batches46_48 import item

BASE=tool.AI+'parallel-ai-operator-dictionary-read9-20261011/'
def verify():
    root=tool.ROOT/BASE
    records={}
    for line in (root/'MANIFEST.sha256').read_text().splitlines():
        digest,name=line.split('  ',1);p=Path(name)
        assert re.fullmatch('[0-9a-f]{64}',digest) and not p.is_absolute() and '..' not in p.parts
        records[name]=digest
    assert len(records)==42
    manifest(BASE,records);m=tool.load(BASE+'manifest.json')
    sys.path.insert(0,str(root));modules={}
    try:
        for name in ['compare','sql_audit']:
            spec=importlib.util.spec_from_file_location(name,root/(name+'.py'))
            module=importlib.util.module_from_spec(spec);sys.modules[name]=module;spec.loader.exec_module(module);modules[name]=module
        result=modules['compare'].verify()
        assert result['HTTP']==result['complete_source_DTO_passes']==26 and result['business_reds']==0
        assert len(result['BC_own_and_boundary_full_body_pairs'])==10 and len(result['natural_positive_per_engine_not_BC_equal'])==6
        sql=modules['sql_audit'].audit();assert sql==m['SQL']
    finally:
        sys.path.pop(0)
        for name in ['compare','sql_audit']:sys.modules.pop(name,None)
    run(BASE,sys.executable,'-m','unittest','discover','-p','test_compare.py')
    assert m['interfaces']==9 and m['native_assertions']==104 and m['business_fix_files']=={}
    for lane in ['B','C']:
        assert len(m['raw'][lane])==7
        private_files(root/'private-evidence'/lane,m['raw'][lane])
        counts=sql[lane]['counts']
        assert counts['HTTP']==13 and counts['preparation_INSERT']==2 and counts['business_SELECT']==25
        assert counts['HTTP_DML']==counts['DDL']==counts['preparation_UPDATE']==counts['preparation_DELETE']==0
        assert sql[lane]['natural_credentials_values_consumed']==0 and sql[lane]['final_all29_and_natural4_exact']
        clean=m['cleanup'][lane]
        for key in ['port_closed','pid_gone','source_removed','private_cache_cli_header_native_removed',
                    'evidence_0600','evidence_dir_0700','preserved_master_original18102']:
            assert clean[key],(lane,key)
        assert clean['cache_files_before_cleanup']==0
    source=m['business_source_identity'];assert len(source['files'])==33
    for relative,digest in source['files'].items():
        assert sha(tool.ROOT.parent/'zhctproject/ai_api'/relative)==digest,relative
    manifest(BASE,records)

def targets():
    paths=[f'/{side}/store.User/{action}' for side in ['store','terminal'] for action in ['info','list','all']]
    paths+=['/admin/store.Api/index','/admin/store.Menu/index','/admin/store.Menu/info']
    return [item('ai_api','GET',path,BASE,
        risk='readonly-owned-non-login-operator-and-bounded-natural-dictionaries-no-permission-grants',
        parity='partial-own-full-body-and-independent-per-engine-natural-tree-types-values-not-fake-natural-BC-equality',
        boundary_empty_min_max='partial-positive-info-list-all-and-empty-filter-auth-missing-menu-empty-role-users-subbranches')
        for path in paths]

if __name__=='__main__':
    audit=Path(__file__).with_name('integration-20261011-batch60.json')
    assert not audit.exists(),'Preserve original transition audit'
    tool.main(targets(),audit,verify)
