"""合成离线样本检查。与同名 JSON 及文中列出的附件放在同一目录运行。"""
from pathlib import Path, PurePosixPath
import base64, hashlib, io, inspect, json, math, re, subprocess, sys, tempfile, wave, zipfile
from collections import Counter, defaultdict
from datetime import date, datetime
from decimal import Decimal
sys.dont_write_bytecode=True

def sha(b): return hashlib.sha256(b).hexdigest()
def rejected(fn):
    try: fn()
    except (ValueError,TypeError,KeyError,UnicodeError,OverflowError): return True
    return False

def git(root,*args,input=None,check=True):
    r=subprocess.run(['git','-c','core.autocrlf=false',*args],cwd=root,input=input,capture_output=True,check=check)
    return r

def verify(d,base):
    def budget(s):
        if len(s.encode('utf-8'))>d['max_bytes']:return 'byte_budget'
        depth=0;inside=False;escaped=False
        for ch in s:
            if inside:
                if escaped:escaped=False
                elif ch==chr(92):escaped=True
                elif ch=='"':inside=False
            elif ch=='"':inside=True
            elif ch in '[{':
                depth+=1
                if depth>d['max_depth']:return 'depth_budget'
            elif ch in ']}':depth-=1
        json.loads(s)
        return 'accepted'
    out=[budget(s) for s in d['fixtures']]
    assert out==d['expected'];assert rejected(lambda:budget('{bad}'))
    return {'results':out,'max_bytes':d['max_bytes'],'max_depth':d['max_depth']}

if __name__=='__main__':
    base=Path(__file__).resolve().parent
    data=json.loads((base/'a017.json').read_text('utf-8'))
    result=verify(data,base)
    print(json.dumps({'id':'A017','passed':True,'result':result},ensure_ascii=True))
