File size: 2,922 Bytes
d806877
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""Independent state-count/history and canonical-UTF8 checksum oracle."""
import collections,hashlib,json,random,subprocess
from pathlib import Path
rng=random.Random(481903);cases=[]
for c in range(40):
 labels=['bug','问题','例子'];items=[{'id':'__proto__'if i==0 else f'item-{i}','group':None if i%3==0 else 'g😀','text':f'Raw {i} 😀\u2028café\x00\u0085\ufeff'}for i in range(rng.randint(1,25))]
 actions=[]
 for _ in range(100):
  if rng.randrange(5)==0:actions.append({'type':'undo'})
  else:
   status=rng.choice(['unlabelled','labelled','abstained']);actions.append({'type':'decide','id':rng.choice(items)['id'],'status':status,'label':rng.choice(labels)if status=='labelled'else None})
 cases.append({'inventory':{'taxonomy':labels,'items':items},'actions':actions})
results=json.loads(subprocess.run(['node','action_driver.mjs'],input=json.dumps(cases,ensure_ascii=False),capture_output=True,text=True,check=True,cwd=Path(__file__).parent).stdout)
def checksum(value):return hashlib.sha256(json.dumps(value,ensure_ascii=False,separators=(',',':')).encode()).hexdigest()
for case,result in zip(cases,results):
 inv=case['inventory'];states={r['id']:{'id':r['id'],'status':'unlabelled','label':None}for r in inv['items']};history=[];revision=0
 for action,actual in zip(case['actions'],result['trace']):
  if action['type']=='undo':
   if history:
    old=history.pop();states[old['id']]=old;revision+=1
  else:
   target={'id':action['id'],'status':action['status'],'label':action['label']}
   if target!=states[action['id']]:
    history.append(states[action['id']].copy());history=history[-20:];states[action['id']]=target;revision+=1
  count=collections.Counter(r['status']for r in states.values());labelcounts=collections.Counter(r['label']for r in states.values()if r['status']=='labelled');n=len(states)
  expected={'total':n,'unlabelled':count['unlabelled'],'labelled':count['labelled'],'abstained':count['abstained'],'reviewed':count['labelled']+count['abstained'],'reviewed_fraction':(count['labelled']+count['abstained'])/n,'unknown_group_records':sum(x['group']is None for x in inv['items']),'labels':[{'label':l,'count':labelcounts[l]}for l in inv['taxonomy']]}
  assert actual['summary']==expected;assert actual['revision']==revision;assert actual['undo_count']==len(history)
 payload={'schema_version':1,'tool_version':'0.1.0','inventory':inv,'inventory_sha256':checksum(inv),'annotations':[states[x['id']]for x in inv['items']],'revision':revision}
 assert result['checkpoint']=={**payload,'content_sha256':checksum(payload)};assert result['restored_payload']==payload and result['restored_undo_count']==0
print(json.dumps({'status':'PASS','inventories':40,'independent_action_states':4000,'canonical_payload_hashes':40,'restored_full_inventories':40}))
Path(__file__).with_name('reference-checkpoint.json').write_text(json.dumps(results[0]['checkpoint'],ensure_ascii=False,indent=2)+'\n')