Files
BoneQuest/tests/playtest.py
T

185 lines
12 KiB
Python

#!/usr/bin/env python3
"""Developer qualification using the installed app's isolated playtest helper.
Requires explicitly supplied private ROMs; does not operate the personal emulator.
"""
import argparse,ast,base64,hashlib,json,re,struct,subprocess,zlib
from pathlib import Path
from check_assets import read_views
ROOT=Path(__file__).resolve().parents[1]
def png(path):
b=Path(path).read_bytes();p=8;data=b''
while p<len(b):
n=int.from_bytes(b[p:p+4],'big');kind=b[p+4:p+8];v=b[p+8:p+8+n];p+=n+12
if kind==b'IHDR':w,h,depth,ct,*_=struct.unpack('>IIBBBBB',v)
if kind==b'IDAT':data+=v
assert depth==8 and ct in (2,6)
ch=3 if ct==2 else 4;stride=w*ch;raw=zlib.decompress(data);prior=bytearray(stride);rows=[];p=0
for _ in range(h):
f=raw[p];p+=1;row=bytearray(raw[p:p+stride]);p+=stride
for i in range(stride):
a=row[i-ch] if i>=ch else 0;b=prior[i];c=prior[i-ch] if i>=ch else 0
if f==1:pred=a
elif f==2:pred=b
elif f==3:pred=(a+b)//2
elif f==4:
v=a+b-c;pa=abs(v-a);pb=abs(v-b);pc=abs(v-c);pred=a if pa<=pb and pa<=pc else b if pb<=pc else c
else:pred=0
row[i]=(row[i]+pred)&255
rows.append(row);prior=row
return w,h,ch,rows
class Screen:
def __init__(self,path,font):self.w,self.h,self.ch,self.rows=png(path);self.font=font
def guest(self,x,y):return tuple(self.rows[16+y*2][x*self.ch:x*self.ch+3])
def text(self,x,y,n):
bg=self.guest(x+10,y);out=''
for j in range(n):
bits=[self.guest(x+j*12+c*2,y+r)!=bg for r in range(8) for c in range(5)]
scores=[]
for k in range(32,127):
expected=[not(self.font[(k-32)*8+r]&(128>>c)) for r in range(8) for c in range(5)]
scores.append((sum(a!=b for a,b in zip(bits,expected)),k))
d,k=min(scores);out+=chr(k) if d==0 else '?'
return out.rstrip()
def main():
p=argparse.ArgumentParser(description=__doc__)
p.add_argument('--app',type=Path,default=Path('/Applications/VCC Plus.app'))
p.add_argument('--rom',type=Path,required=True);p.add_argument('--disk-rom',type=Path,required=True)
p.add_argument('--evidence',type=Path,default=ROOT/'test-output/playtest')
p.add_argument('--cpu',choices=['6809','6309','both'],default='both');args=p.parse_args();out=args.evidence.resolve();out.mkdir(parents=True,exist_ok=True)
rows=json.loads((ROOT/'data/bones.json').read_text());count=len(rows);binary=ROOT/'build/BONEQ.bin';disk=ROOT/'build/BONEQ.dsk';sha=hashlib.sha256(disk.read_bytes()).hexdigest()
mp=binary.with_suffix('.map').read_text();symbols={n:int(a,16) for n,a in re.findall(r'Symbol: _(\w+) .*?= ([0-9A-Fa-f]+)',mp)}
blob=binary.read_bytes();memory={};i=0
while i<len(blob):
kind=blob[i];n,a=struct.unpack('>HH',blob[i+1:i+5]);i+=5
if kind==255:break
memory.update((a+j,b) for j,b in enumerate(blob[i:i+n]));i+=n
font=bytes(memory[symbols['font5x8']+i] for i in range(768))
views,arrays=read_views();colors={0:(0,0,0),1:(255,255,255),2:(255,255,0),3:(255,0,0)}
manifest=json.loads((ROOT/'data/geometry-manifest.json').read_text())
b64=lambda path:base64.b64encode(path.read_bytes()).decode();reports=[]
for cpu in (('6809','6309') if args.cpu=='both' else (args.cpu,)):
proc=subprocess.Popen([str(args.app/'Contents/Helpers/vcc-playtest')],stdin=subprocess.PIPE,stdout=subprocess.PIPE,text=True)
def call(op,**kw):
proc.stdin.write(json.dumps(dict(op=op,**kw))+'\n');proc.stdin.flush();r=json.loads(proc.stdout.readline());assert 'error' not in r,r;return r['result']
def read(name,n=1):return call('read_memory',address=symbols[name],count=n)['bytes']
def step(n=600):return call('input',steps=[dict(mask=0,fields=n)])
def press(mask,settle=600,hold=8):return call('input',steps=[dict(mask=mask,fields=hold)]+([dict(mask=0,fields=settle)] if settle else []))
def key(bit,settle=600,hold=8):return press(1<<bit,settle,hold)
def type_text(s):
steps=[]
for c in s:
bit=31 if c==' ' else 46 if c=='.' else 45 if c=='-' else ord(c.lower())-97+1
mask=(1<<bit)|((1<<55) if c.isupper() else 0)
steps.extend([dict(mask=mask,fields=8),dict(mask=0,fields=16)])
for i in range(0,len(steps),60):call('input',steps=steps[i:i+60])
def capture(name):
r=call('capture');path=out/(cpu+'-'+name+'.png');path.write_bytes(base64.b64decode(r.pop('png_base64')))
(out/(cpu+'-'+name+'.json')).write_text(json.dumps(r,indent=2)+'\n');return Screen(path,font)
def check_map(s,current,outcomes):
def check_view(view,ox,oy,scale=1,cx=0,cy=0,cw=None,ch=None):
w=view['width'];h=view['height'];cw=w if cw is None else cw;ch=h if ch is None else ch
expected=[None]*(w*h)
for bone,pixels in enumerate(view['layers']):
col=0 if bone==count+1 else 2 if bone==current else 3 if outcomes.get(bone) is False else 1
for pos in pixels:expected[pos]=colors[col]
checked=0
for y in range(cy,cy+ch):
for x in range(cx,cx+cw):
value=expected[y*w+x]
if value is None:continue
for yy in range(scale):
for xx in range(scale):
actual=s.guest(ox+(x-cx)*scale+xx,oy+(y-cy)*scale+yy)
assert actual==value,(cpu,current,view['name'],x,y,actual,value)
checked+=1
assert checked>0
check_view(views[0],8+(200-views[0]['width'])//2,30)
if current is None:return
detail=arrays['bone_detail_view'][current]
if detail:
view=views[detail];check_view(view,224+(400-view['width'])//2,34+(98-view['height'])//2)
else:
x,y,w,h=manifest['overviewBoxes'][rows[current]['code']]
if x>2:x-=2;w+=2
else:x=0
if y>2:y-=2;h+=2
else:y=0
w+=2;h+=2;w=min(w,views[0]['width']-x);h=min(h,views[0]['height']-y)
scale=max(1,min(4,98//h))
if w*scale>400:scale=400//w
check_view(views[0],224+(400-w*scale)//2,34+(98-h*scale)//2,scale,x,y,w,h)
def ready(phase,q=None):
for _ in range(120):
if read('phase')==[phase] and (q is None or read('question')==[q]):return
step(30)
raise AssertionError(('Screen did not finish',cpu,phase,q,read('phase'),read('question')))
def sound(name):
r=call('audio_capture',fields=120);wav=base64.b64decode(r['audio_wav_base64']);(out/(cpu+'-'+name+'.wav')).write_bytes(wav)
assert any(wav[44:]),'No guest audio samples'
call('init',system_rom=b64(args.rom),disk_rom=b64(args.disk_rom),disk=b64(disk),cpu=cpu,boot_keys=[1<<b for b in (4,15,19,48)])
step();ready(0);s=capture('title');check_map(s,None,{});assert s.text(224,35,34)==f'{count} STRUCTURES'
key(3);s=capture('credits');assert s.text(16,42,45)=='Skeleton: Mariana Ruiz Villarreal';key(31);key(33)
outcomes={};slots=[];visited=[]
for q in range(count):
ready(1,q);assert read('phase')==[1] and read('question')==[q]
bone=read('current_bone')[0];s=capture('mc-%02d-%s'%(q+1,rows[bone]['code']));check_map(s,bone,outcomes)
assert s.text(16,19,45)==f'Question {q+1}/{count} Score {sum(outcomes.values())}'
opts=[s.text(224,168+i*11,34)[2:] for i in range(4)];assert len(set(opts))==4 and rows[bone]['name'] in opts,(q,opts)
slot=opts.index(rows[bone]['name']);assert not slots or slot!=slots[-1];slots.append(slot);visited.append(bone)
right=q not in (0,5,10,15,20);selection=slot if right else (slot+1)%4
if q==0:
# Hold through feedback and the next question; only one answer.
press(1<<(33+selection),600,600);assert read('question')==[1];step(16)
else:key(33+selection)
outcomes[bone]=right
if q%5==0:print(cpu,'multiple choice',q+1,flush=True)
ready(4);assert len(set(visited))==count and read('phase')==[4]
s=capture('mc-results');check_map(s,None,outcomes);assert s.text(224,55,34)==f'{count-5} / {count} correct {(count-5)*100//count}%'
key(18);ready(6);s=capture('mc-review');assert s.text(16,39,45)==rows[visited[0]]['name'];key(14);capture('mc-review-next');key(2);key(16);ready(0);check_map(capture('replay-title'),None,{})
key(34);press((1<<55)|(1<<32),16);outcomes={};typed=[];corrections=0
for q in range(count):
ready(1,q);assert read('phase')==[1] and read('question')==[q]
bone=read('current_bone')[0];name=rows[bone]['name'];s=capture('typed-%02d-%s'%(q+1,rows[bone]['code']));check_map(s,bone,outcomes)
right=q not in (2,3,4,5,6)
if q==0:
typo=name[0]+name[2]+name[1]+name[3:];type_text(typo);key(48);assert read('phase')==[2],(name,typo,read('phase'))
capture('spelling-retry');key(18)
for _ in typo:key(29,16)
type_text(name);corrections=1
elif q==1:
type_text(rows[bone]['aliases'][0] if rows[bone]['aliases'] else name);capture('typed-alias')
elif q==3:
pass # Blank Enter now reveals; Break is a global quit key.
elif q==4:
type_text('X'*33);type_text('XXXXXX');step();s=capture('input-limit');assert s.text(224,168,34)=='X'*33+'_'
elif q==5:pass
elif q in (2,6):
other=rows[(bone+1)%count];type_text(other['aliases'][0] if q==6 and other['aliases'] else other['name'])
else:
text=' '.join(name.split());text=(' '+text.replace(' ',' ')+' ') if q%3==0 else text.lower() if q%3==1 else text.upper();type_text(text)
if q in (0,2):
key(48,0)
for _ in range(1200):
if read('phase')==[3]:break
step(1)
assert read('phase')==[3]
sound('correct-feedback' if right else 'incorrect-feedback');step()
else:key(48)
s=capture('typed-feedback-%02d'%(q+1));assert read('phase')==[3]
assert s.text(224,170,30)==('CORRECT!' if right else 'NOT THIS TIME.'),(q,name,s.text(224,170,30))
outcomes[bone]=right;typed.append(bone);assert read('score')==[sum(outcomes.values())]
key(31)
if q%5==0:print(cpu,'typed',q+1,flush=True)
ready(4);assert len(set(typed))==count and read('phase')==[4]
s=capture('typed-results');check_map(s,None,outcomes);assert s.text(224,55,34)==f'{count-5} / {count} correct {(count-5)*100//count}%';assert s.text(224,77,34)=='1 spelling corrections'
key(18);ready(6);s=capture('typed-review');assert s.text(16,39,45)==rows[typed[2]]['name'];key(50)
s=capture('exit');r=call('capture');assert 'THANKS FOR PLAYING BONE QUEST!' in r.get('screen_text','')
final=call('stop');final.pop('disk_base64',None);proc.wait(timeout=10)
assert final['disk_writes']==0 and final['disk_sha256']==sha
reports.append(dict(cpu=cpu,ramKB=512,multipleChoice=count,typed=count,score=count-5,spellingCorrections=1,mapPixelsChecked=True,detailPixelsChecked=True,feedbackAudio=True,noRepeatedCorrectSlots=True,heldKey=True,review=True,replay=True,exit=True,diskSHA256=sha,diskWrites=0))
(out/'report.json').write_text(json.dumps(reports,indent=2)+'\n');print('PASS',cpu,'complete quizzes, map/colors/choices/input/review/replay/exit',flush=True)
if __name__=='__main__':main()