# 离线只读验证：不访问网络、不操作设备，只读取同目录合成输入。
import json, math, struct, statistics
from pathlib import Path
def check(d):
 import codecs
 decoder=codecs.getincrementaldecoder('utf-8')('strict')
 text=''.join(decoder.decode(bytes.fromhex(v),final=i==len(d['chunks'])-1) for i,v in enumerate(d['chunks']))
 try: bytes.fromhex(d['invalid']).decode('utf-8','strict'); bad='未报错'
 except UnicodeDecodeError: bad='UTF8错误'
 return dict(stream=text,invalid=bad,binary='交业务二进制解析器' if d['binary_opcode']==2 else '文本')

if __name__=='__main__':
 fixture=json.loads(Path(__file__).with_name('fixture.json').read_text(encoding='utf-8'))
 actual=check(fixture['input'])
 assert actual==fixture['expected'], {'实际':actual,'预期':fixture['expected']}
 print(json.dumps({'状态':'通过','实际':actual,'边界':'仅离线合成样例，现场未测'},ensure_ascii=False))
