import json,sys
from pathlib import Path
root=Path(__file__).resolve().parent
expected=json.loads((root/'expected.json').read_text())
document=json.loads((root/'document.json').read_text())
chunks=json.loads(Path(sys.argv[1]).read_text() if len(sys.argv)>1 else (root/'chunks.json').read_text())
text=' '.join(c['text'] for c in chunks)
for s in expected['expected_texts']:assert s in text,s
headings={h for c in chunks for h in c.get('headings',[])}
for h in expected['expected_headings']:assert h in headings,'missing heading '+h
for c in chunks:
    items=c.get('items',[])
    assert items,'missing document refs'
    for item in items:
        ref=item['ref'].split('/')
        assert ref[0]=='#'
        target=document
        for part in ref[1:]:target=target[int(part)] if isinstance(target,list) else target[part]
        assert target['text'],item
        assert item['pages']==[1],item
print('Reviewed coverage, heading set and live document references PASS')
