"""Check the frozen DOCX package and disclosed denominators; not Word rendering or schema validation."""
import hashlib
import json
from pathlib import Path
import xml.etree.ElementTree as ET
import zipfile

root = Path(__file__).resolve().parent
results = json.loads((root / 'results.json').read_text())
corpus = json.loads((root / 'corpus.json').read_text())
assert len(corpus['cases']) == len(results['rows']) == 42
assert len({c['id'] for c in corpus['cases']}) == 42
assert sum(r['parseable'] for r in results['rows']) == 36
valid = [r for r in results['rows'] if r['inputClass'] == 'valid']
invalid = [r for r in results['rows'] if r['inputClass'] == 'invalid']
assert len(valid) == 32 and all(r['structuralChecksMet'] for r in valid)
assert len(invalid) == 6 and all(not r['parseable'] for r in invalid)
docx = root / 'structural-examples.docx'
assert hashlib.sha256(docx.read_bytes()).hexdigest() == results['docxSha256']
with zipfile.ZipFile(docx) as archive:
    assert archive.testzip() is None
    xml = archive.read('word/document.xml')
    assert xml == (root / 'document.xml').read_bytes()
    document = ET.fromstring(xml)
    equations = document.findall('.//{http://schemas.openxmlformats.org/officeDocument/2006/math}oMath')
    assert len(equations) == results['docxEquationCount'] == 32
print(json.dumps({'packageIntegrity': True, 'nativeEquationCount': len(equations),
                  'validStructuralChecks': len(valid), 'invalidParserRejections': len(invalid),
                  'actualWordClientTest': False}))
