#!/usr/bin/env python3
"""Role status positive; preserve two pre-business bootstrap failures separately."""
import hashlib
import json
import subprocess
from pathlib import Path
import integrate_20261010 as tool
from integrate_20261011_batches46_48 import item

HERE = Path(__file__).resolve().parent
PRIVATE = Path('/Users/wiscourper/Documents/workspace/local/dm8-store-purchase-read3-20261012.L5dTYR/stage3/role-status-pool-20261012')
BASE = 'work_store/apifox/2026-09-30-dm8-interface-acceptance/parallel-role-status-20261012/'


def verify():
    for name, digest in [
        ('REPORT.md', 'ad35e3f3468d570dd56c9e174659c0965822d0896a8c41ac9766a535bf56e2b8'),
        ('manifest.json', '261db990a0c52cb3c50104f32f9cad8ff8f67361d33bd7ac452d85f1f4487e2b'),
        ('verify.js', '902e89c65c00370e884888cd805cd04ae722b6b3aad94fff656c28b3e6b01ca4'),
    ]:
        assert hashlib.sha256((PRIVATE / name).read_bytes()).hexdigest() == digest
    r = json.loads(subprocess.check_output(['node', str(PRIVATE / 'verify.js'), '--readonly']))
    assert r['readonly'] and r['checks'] == 317
    for lane, count in [('B', 133), ('C', 165)]:
        s = r['sides'][lane]
        assert s['HTTP'] == 6 and s['new_read'] == count
        assert s['I'] == s['U'] == s['D_affected'] == 1
        assert s['owner_and_links_empty'] and s['protected_equal'] and s['actor_config_equal']
        old = json.loads((PRIVATE / lane / 'wire-ROLESTATUS12-S.json').read_text())
        new = json.loads((PRIVATE / lane / 'wire-ROLESTATUS12-S2.json').read_text())
        a = [n for n in old['native'] if n['SQL'].startswith('UPDATE')]
        b = [n for n in new['actual_native'] if n['SQL'].startswith('UPDATE')]
        assert len(a) == len(b) == 1 and a[0]['SQL'] == b[0]['SQL']
        assert a[0]['binds'] == b[0]['binds']


if __name__ == '__main__':
    audit = HERE / 'integration-20261012-batch140.json'
    assert not audit.exists()
    specs = [item('store', 'POST', '/p/role/status', BASE,
        from_repo='unreviewed', from_source='apifox-2026-05-08',
        risk='own-unassigned-role-status-only',
        readback='tested-pass-complete-seven-columns-status1-to0-links0-and-exact-cleanup',
        success_case='partial-evidence-canonical-string-zero-original-PDO-INT',
        parity='partial-evidence-original-response-and-business-state-equal',
        cleanup='owned-role-and-three-links-zero-natural-ACL-and-actor-config-unchanged')]
    # Tool errors before business SQL do not add successful coverage or imply DM defects.
    for action in ['dashboardOverview', 'operationsOverview']:
        specs.append(item('store', 'POST', '/p/mealOrder/' + action, BASE,
                          evidence_only=True, risk='readonly-report-bootstrap-failure-original-preserved'))
    tool.main(specs, audit, verify)
