#!/usr/bin/env python3
"""Four real Syllabus writes, no replay of successful MySQL writes."""
import hashlib
import json
from pathlib import Path
import subprocess
import integrate_20261010 as tool

BASE=tool.STORE+'parallel-store-syllabus-evaluate-crud-20261010/'

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-sylcrud-private.DnTqXW'
    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']==758 and not r['failed']
    assert r['mutation_gates']=={'total':140,'missed':[],'passed':True}
    assert r['requests']=={'B':23,'C':24}
    assert m['total_HTTP']==47 and m['total_assertions']==141 and m['failed_assertions']==2
    assert m['accepted_requests']==46 and m['accepted_assertions']==138
    final=m['final_source_lock']['p/logic/SyllabusEvaluate.php']
    p=tool.ROOT.parent/'zhctproject/store/application/p/logic/SyllabusEvaluate.php'
    assert hashlib.sha256(p.read_bytes()).hexdigest()==final
    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['Auth_token_config_cache_runtime_source_and_original_CLI_removed']
    assert resource['old_ext_original_content_restored_via_same_API']
    assert resource['natural_tables_exact_PK_excluded_unchanged'] and resource['staff_user_actor_full_original_rows_unchanged']
    assert all(x['deleted_by_approved_API'] and x['remaining']==0 for x in resource['new_API_created_rows'].values())
    assert resource['fixture_PDO_writes']==resource['DDL']==resource['Staff_user_permission_mutations']==0

if __name__=='__main__':
    audit=Path(__file__).with_name('integration-20261011-batch43.json')
    assert not audit.exists(),'Preserve original transition audit'
    targets=[dict(repo='store',method='POST',path='/p/syllabusEvaluate/'+action,module='大纲考核真实写入',
        terminal='current-PC',role='normal-active-type0-actor-existing-marked-staff',risk='owned-syllabus-CRUD-or-ext-update',
        scope='store-primary-own-generated-syllabus-id3-and-restore-original-marked-ext-content',
        evidence=BASE+'REPORT.md',request_type='current-PC-form',
        parity='partial-evidence-full-original-snapshot-native-wire-types-and-DM-create-by32-storage-copy',
        cleanup='API-delete-own-new-row-original-three-sentinels-and-identities-unchanged-ext-restored-workers-released',
        duplicate_retry='partial-evidence-same-update-save-and-repeat-delete-rejection',
        boundary_empty_min_max='partial-evidence-required-fields-invalid-date-over20-achievement-missing-uuid-see-report')
        for action in ['add','update','delete','save']]
    tool.main(targets,audit,verify)
