little-wren / scripts /test_http.py
HirModel's picture
Upload 116 files
2790a3a verified
Raw History Blame Contribute Delete
6.66 kB
#!/usr/bin/env python3
"""Run against a local parent-continuity preview using real Gradio sessions and HTTP."""
import argparse,io,json,re,sys,zipfile
from pathlib import Path
sys.path.insert(0,str(Path(__file__).resolve().parents[1]))
import httpx
from gradio_client import Client
from little_wren import packets as P
# API-visible output indexes (Gradio state itself is hidden from the client)
STATUS=0;STATS=1;ENTRY=2;SOURCE=4;EVENT=7;CLAIM=9;REVIEW_FOCUS=11;QUESTIONS=12;REVIEWS=13;CONTINUITY=15;DISCLOSURE=17;AUDIT=18;DOWNLOADS=19;SNAPSHOT=20
def main():
ap=argparse.ArgumentParser();ap.add_argument('--base',default='http://127.0.0.1:7860');ap.add_argument('--out',type=Path);args=ap.parse_args()
b=args.base.rstrip('/');rows=[]
def check(name,condition):
rows.append({'check':name,'status':'PASS' if condition else 'FAIL'})
if not condition:raise AssertionError(name)
h=httpx.Client(base_url=b,timeout=20,trust_env=False)
check('root_responds',h.get('/').status_code==200)
health=h.get('/healthz').json();check('parent_continuity_version',health['version']=='0.1.1-parent-continuity-preview');check('synthetic_health_boundary',health['real_case_intake'] is False and health['model_inference'] is False)
config=h.get('/config').json();types={c['type'] for c in config['components']}
check('no_upload_input_component','file' not in types and 'uploadbutton' not in types)
check('generic_upload_route_blocked',h.post('/gradio_api/upload',files={'files':('probe.txt',b'NOT_A_FAMILY_RECORD')}).status_code==403)
check('generic_file_route_blocked',h.get('/gradio_api/file=/etc/passwd').status_code==403)
check('oversize_selection_request_blocked',h.post('/gradio_api/call/load_fictional_case',content=b' '*262145).status_code==413)
check('brand_asset_available',h.get('/brand/little-wren.webp').headers.get('content-type')=='image/webp')
a=Client(b,verbose=False,download_files=False);other=Client(b,verbose=False,download_files=False)
r=a.predict(api_name='/load_fictional_case');check('session_A_loads','earliest contemporaneous family record' in r[STATUS]);check('receipt_first_entry','STARTING RECEIPT' in r[ENTRY] and 'S02' in r[ENTRY]);check('parent_front_state','Earlier family message' in r[SOURCE])
q=other.predict(api_name='/load_fictional_case');check('session_B_loads','earliest contemporaneous family record' in q[STATUS])
r=a.predict('C03',api_name='/select_claim');check('review_repeats_exact_claim','Platform recommendation amplified' in r[REVIEW_FOCUS]);check('parent_language_unresolved','Cause not established' in r[CLAIM])
r=a.predict('REJECT_INTERPRETATION','CAUSE_UNRESOLVED',api_name='/record_review');check('rejection_recorded','do not accept' in r[REVIEWS].lower() or 'Reject interpretation' in r[REVIEWS]);check('rejection_keeps_scoped_cause','Cause not established' in r[CLAIM]);check('continuity_carries_review','REJECT' in r[CONTINUITY].upper() or 'Human reviews' in r[STATS])
q=other.predict('C03',api_name='/select_claim');check('session_reviews_isolated','No human disposition' in q[REVIEWS])
r=a.predict('SCHOOL_REVIEW',api_name='/select_disclosure');check('selective_disclosure_view','School-facing review view' in r[DISCLOSURE] and 'Screenshot transcript' not in r[DISCLOSURE])
r=a.predict(api_name='/correct_observation_time');check('observation_correction_applied','19:05' in r[EVENT]);check('event_time_still_unknown','Unknown' in r[EVENT])
r=a.predict('S04',api_name='/select_source');check('original_transcript_unchanged','19:00' in r[SOURCE])
r=a.predict(False,api_name='/build_packet');check('export_requires_explicit_permission','permission' in r[STATUS])
r=a.predict(True,api_name='/build_packet');check('continuity_export_after_rejection','built and verified' in r[STATUS]);check('continuity_panel_present','CONTINUITY RECORD' in r[CONTINUITY])
paths=re.findall(r'href="(/download/[^"]+)"',r[DOWNLOADS]);check('six_output_formats',len(paths)==6)
zipresponse=h.get(paths[0]);data=zipresponse.content
parsed=P.read_packet(data);w=parsed['workbench']
check('downloaded_capsule_verifies',parsed['status']=='BYTE_INTEGRITY_AND_DOCUMENTARY_STRUCTURE_PASS')
check('review_survives_export',w['reviews'][0]['disposition']=='REJECT_INTERPRETATION')
check('correction_survives_export',w['case']['events'][3]['times']['observed_raw']=='2026-01-08 19:05')
check('continuity_index_verifies',parsed['continuity_record']['projected_state_digest'])
with zipfile.ZipFile(io.BytesIO(data)) as z:names=z.namelist();check('continuity_members_present','CONTINUITY_RECORD.json' in names and 'DISCLOSURE_VIEWS.json' in names)
check('sidecar_matches_outer',h.get(paths[1]).text.split()[0]==parsed['sha256'])
check('download_cache_disabled','no-store' in zipresponse.headers.get('cache-control',''))
check('download_nosniff',zipresponse.headers.get('x-content-type-options')=='nosniff')
check('html_download_is_attachment','attachment' in h.get(paths[2]).headers.get('content-disposition',''))
token=r[SNAPSHOT]['value'];r=a.predict(token,api_name='/reopen_packet');check('continuity_capsule_reopens','lawful case state was restored' in r[STATUS])
try:
other_result=other.predict(token,api_name='/reopen_packet');denied='error' in other_result[STATUS]
except Exception:denied=True
check('other_session_cannot_reopen',denied)
r=a.predict('S02',api_name='/select_source');r=a.predict(True,api_name='/withdraw_source');check('source_withdrawal_recorded','withdrawn' in r[STATUS])
check('prior_links_revoked',all(h.get(p).status_code==404 for p in paths))
r=a.predict(True,api_name='/build_packet');newpaths=re.findall(r'href="(/download/[^"]+)"',r[DOWNLOADS]);data=h.get(newpaths[0]).content
with zipfile.ZipFile(io.BytesIO(data)) as z:names=z.namelist()
check('withdrawn_bytes_not_exported','sources/earlier_family_message.txt' not in names and 'sources/family_message_copy.txt' not in names)
check('withdrawn_capsule_verifies',P.read_packet(data)['status']=='BYTE_INTEGRITY_AND_DOCUMENTARY_STRUCTURE_PASS')
r=a.predict(api_name='/clear_session');check('clear_removes_case','No case loaded' in r[STATS]);check('clear_revokes_all_links',all(h.get(p).status_code==404 for p in newpaths))
check('other_session_not_cleared','No human disposition' in other.predict('C03',api_name='/select_claim')[REVIEWS])
other.predict(api_name='/clear_session')
result={'execution':'LOCAL_HTTP_AND_GRADIO_CLIENT','browser_interaction':'NOT_RUN_IN_THIS_SCRIPT','passed':len(rows),'failed':0,'checks':rows}
if args.out:args.out.parent.mkdir(parents=True,exist_ok=True);args.out.write_text(json.dumps(result,indent=2)+'\n')
print(json.dumps({'passed':len(rows),'failed':0}))
if __name__=='__main__':main()