# 离线只读验证：不访问网络、不操作设备，只读取同目录合成输入。
import json, math, struct, statistics
from pathlib import Path
def assemble(seq):
 buf=None; out=[]; pings=0
 for op,fin,p in seq:
  if op==9:
   if not fin: raise ValueError('控制帧分片')
   pings+=1; continue
  if op==1:
   if buf is not None: raise ValueError('消息尚未结束')
   buf=p
  elif op==0:
   if buf is None: raise ValueError('缺首帧')
   buf+=p
  else: raise ValueError('未支持opcode')
  if fin: out.append(buf); buf=None
 return out,pings
def check(d):
 msgs,pings=assemble(d['frames'])
 try: assemble(d['bad']); err='未报错'
 except ValueError as e: err=str(e)
 return dict(messages=msgs,pings=pings,bad=err)

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))
