"""合成离线样本检查。与同名 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 parse(chunks):
        line='';data=[];out=[];skip_lf=False
        def consume(line):
            if line=='':
                if data:out.append('\n'.join(data));data.clear()
            elif not line.startswith(':'):
                field,sep,value=line.partition(':')
                if value.startswith(' '):value=value[1:]
                if field=='data':data.append(value)
        for chunk in chunks:
            for ch in chunk:
                if skip_lf:
                    skip_lf=False
                    if ch=='\n':continue
                if ch=='\r':consume(line);line='';skip_lf=True
                elif ch=='\n':consume(line);line=''
                else:line+=ch
        return out
    runs=0
    for c in d['fixtures']:
        s=c['stream'];assert parse([s])==c['expected']
        for i in range(len(s)+1):assert parse([s[:i],s[i:]])==c['expected'];runs+=1
    assert json.loads(d['fixtures'][0]['expected'][0])=={'a':1}
    return {'fixture_count':len(d['fixtures']),'chunk_splits_verified':runs,'unfinished_dispatched':False}

if __name__=='__main__':
    base=Path(__file__).resolve().parent
    data=json.loads((base/'a022.json').read_text('utf-8'))
    result=verify(data,base)
    print(json.dumps({'id':'A022','passed':True,'result':result},ensure_ascii=True))
