#!/usr/bin/env python3
"""Weight history reads; tool-type failures preserved, not relabelled CLI green."""
import hashlib
import json
from pathlib import Path
import subprocess
import integrate_20261010 as tool

BASE=tool.STORE+'parallel-store-weight-history-readonly-20261011/'

def verify():
    m=tool.load(BASE+'manifest.json')
    root=tool.ROOT/BASE
    for item in m['control_files']:
        assert hashlib.sha256((root/item['path']).read_bytes()).hexdigest()==item['sha256']
    private=Path(m['private_terminal_root'])
    assert str(private)=='/private/tmp/cdx-weight-private.2D6krm'
    assert private.is_dir() and not private.is_symlink() and private.stat().st_mode & 0o777==0o700
    for item in m['private_files']:
        p=Path(item['path'])
        assert p.parent==private and not p.is_symlink() and p.stat().st_mode & 0o777==0o600
        assert len(p.read_bytes())==item['bytes'] and hashlib.sha256(p.read_bytes()).hexdigest()==item['sha256']
    result=subprocess.run(['node',str(root/'compare-private.js'),str(private)],check=True,capture_output=True,text=True)
    r=json.loads(result.stdout)
    assert r['passed'] and r['gates']==212 and not r['failed']
    assert r['mutation_gates']=={'total':57,'missed':[],'passed':True}
    assert r['HTTP']=={'B':6,'C':6} and r['original_CLI_tool_type_errors_not_replayed']
    assert r['original_C_show_time_difference_preserved']
    assert m['total_HTTP']==12 and m['total_assertions']==42 and m['failed_assertions']==10
    assert m['fixture_PDO_INSERTs']==8 and m['API_writes']==m['DDL']==0
    p=tool.ROOT.parent/'zhctproject/store/application/p/logic/Weight.php'
    assert hashlib.sha256(p.read_bytes()).hexdigest()==m['final_source_lock']['p/logic/Weight.php']
    resource=tool.load(BASE+'resource-cleanup.json')
    assert resource['B']['stopped'] and resource['C']['stopped']
    assert resource['B']['port_18321_closed'] and resource['C']['port_18322_closed']
    assert resource['runtime_Auth_token_config_cache_source_original_CLI_removed']
    assert resource['B_natural_original_rows']=={'member_weight':104,'member_weight_status':104,'full_value_hash_unchanged':True}
    assert resource['C_natural_original_rows']=={'member_weight':0,'member_weight_status':0,'full_value_hash_unchanged':True}
    assert resource['reference_Staff_User_actor_full_rows_unchanged']

if __name__=='__main__':
    audit=Path(__file__).with_name('integration-20261011-batch45.json')
    assert not audit.exists(),'Preserve original transition audit'
    items=[dict(repo='store',method='POST',path='/p/weight/'+action,module='体重历史读取',terminal='current-PC',
        role='normal-active-actor-existing-marked-staff-user',risk='readonly-plus-approved-4-fixture-INSERT-per-lane',
        scope='store-primary-two-marked-weight-and-status-rows-natural-104-stable',evidence=BASE+'REPORT.md',
        request_type='current-PC-form',parity='partial-evidence-complete-original-native-types-DM-show-seconds-only',
        cleanup='4-marked-fixtures-per-lane-retained-natural-and-identity-stable-workers-auth-released',
        boundary_empty_min_max='partial-evidence-nonempty-zero-float-CHAR-two-row-sort-page2-empty-missing-staff-chart-page-ignored')
        for action in ['show','chart']]
    tool.main(items,audit,verify)
