#!/usr/bin/env python3
"""Record the actual text-export red without treating diagnostic HTTP as coverage."""
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 = tool.ROOT.parent / 'worktrees/dm8-compat-20261012/private-prepay-harness'
BASE = 'work_store/apifox/2026-09-30-dm8-interface-acceptance/parallel-body-export-charset-20261012/'


def verify():
    expected = [
        ('batch170-safe-read/verify_offline.php', '7d69fc144ae32c651a65ca53f9abebdcacdf708116272bcbe7393d5cf19a1866', 'original_count', 13),
        ('batch170-safe-read/verify_diagnostics_offline.php', 'c1ca9a462988943238777e5425f07ae5dd7aac60345a8e0bf49e7bf55bf5d484', 'diagnostic_original_count', 8),
        ('batch172-c-web-charset/verify_offline.php', '1249d24b02f112ab426c9d31246edfcdeeb80f9e073b5305f934cfa83c9ebe84', 'frozen_originals', 5),
    ]
    for name, sha, key, count in expected:
        assert hashlib.sha256((PRIVATE / name).read_bytes()).hexdigest() == sha
        result = json.loads(subprocess.check_output(['/opt/homebrew/opt/php@7.4/bin/php', str(PRIVATE / name)]))
        assert result['status'] == 'verified' and result[key] == count
    b = json.loads((PRIVATE / 'batch170-safe-read/B-bodycomp-http-receipt.json').read_text())
    c = json.loads((PRIVATE / 'batch170-safe-read/C-bodycomp-http-receipt.json').read_text())
    for lane, receipt in [('B', b), ('C', c)]:
        body = (PRIVATE / ('batch170-safe-read/' + lane + '-bodycomp.raw')).read_bytes()
        assert body.decode('gbk').split('|') == receipt['segments']
        assert receipt['before'] == receipt['after'] and receipt['actual_http_count'] == 1
    assert b['segments'][4] == '172.00' and c['segments'][4] == '172'


if __name__ == '__main__':
    audit = HERE / 'integration-20261012-batch170.json'
    assert not audit.exists()
    specs = [item('store', 'GET', '/p/staff/bodyCompositionTest', BASE,
        risk='owned-staff-GBK-text-export-read-cli-fixture-charset-and-height-type-drift',
        readback='tested-pass-exact-own-Staff-Actor-AIUser-rows-unchanged-business-readonly',
        success_case='partial-evidence-B-own-row-five-fields-C-HTTP200-with-contract-red',
        parity='tested-fail-height-fixed-scale-and-own-fixture-client-encoding-mismatch',
        cleanup='new-test-Staff-retained-old-red-frozen-no-natural-data-repair-no-external')]
    tool.main(specs, audit, verify)
