
from pathlib import Path
import json, subprocess, sys, zipfile, shutil
ROOT = Path(__file__).resolve().parents[1]

def run(cmd):
    return subprocess.check_output(cmd, cwd=ROOT, text=True)

def main():
    out = ROOT/'verification'/'selftest_single.docx'
    js = run([sys.executable,'-m','klt414.app','examples/works/ivanov_7b.txt','--ideal','examples/ideal_answer_ru.txt','--paid','--out',str(out),'--checker','Test','--email','test@example.com'])
    assert out.exists(), 'single docx was not created'
    batch_dir = ROOT/'verification'/'selftest_batch'
    if batch_dir.exists(): shutil.rmtree(batch_dir)
    js2 = run([sys.executable,'-m','klt414.app','--batch_archive','examples/batch_inputs_mixed.zip','--ideal','examples/ideal_answer_ru.txt','--paid','--out_dir',str(batch_dir),'--checker','Test','--email','test@example.com'])
    assert (batch_dir/'KLT_4_14_grade_register.docx').exists(), 'grade register was not created'
    demo = ROOT/'verification'/'demo.docx'
    profile = ROOT/'verification'/'demo_profile.json'
    if profile.exists(): profile.unlink()
    js3 = run([sys.executable,'-m','klt414.app','examples/works/petrova_7B.html','--ideal','examples/ideal_answer_ru.txt','--demo','--profile',str(profile),'--register_login','demo','--password','123','--full_name','Demo User','--phone','+70000000000','--user_email','demo@example.com','--out',str(demo)])
    assert not demo.exists(), 'demo mode must not export docx'
    assert demo.with_suffix('.demo_result.json').exists(), 'demo json result missing'
    plag = ROOT/'verification'/'plagiarism_sheet.docx'
    js4 = run([sys.executable,'-m','klt414.app','--compare_a','examples/works/ivanov_7b.txt','--compare_b','examples/works/petrova_7B.html','--paid','--out',str(plag)])
    assert plag.exists(), 'plagiarism sheet missing'
    print('KLT 4.14 selftest OK')
if __name__ == '__main__': main()
