"""Deterministic synthetic scorer tests; these are never speech-quality evidence.""" from copy import deepcopy import hashlib import json from pathlib import Path import pytest from app.score_speech import tokens,word_errors,percentile,score def test_conservative_normalization(): assert tokens('عربي، ۱۲۳ كیف؟')==tokens('عربی 123 کیف') assert tokens('می\u200cروم')==['می','روم'] assert tokens('AC-42 1.5 1405/07')!=tokens('AC42 15 140507') assert tokens('-5 15%')!=tokens('5 15') assert word_errors('نه نه 42','نه 43')['errors']==2 assert word_errors('a b','a b b')['insertions']==1 assert word_errors('a b','')['deletions']==2 with pytest.raises(ValueError):word_errors('','anything') assert percentile([10,20,30],.5)==20 assert percentile([], .95) is None def fixture(tmp_path): root=Path(__file__).resolve().parents[2]/'docs/evaluation' manifest=json.loads((root/'openai-persian-speech-manifest.json').read_text()) labels=json.loads((root/'openai-persian-speech-annotations.json').read_text()) recording=tmp_path/'synthetic-fixture.bin';recording.write_bytes(b'synthetic-test-only') profile={'provider':'openai','model':'fixture-only','voice':'fixture-only','audio_input':{}} digest=hashlib.sha256(json.dumps(profile,sort_keys=True,separators=(',',':')).encode()).hexdigest() observations={'effective_profile':profile,'effective_profile_sha256':digest,'observations':[]} for sample,label in zip(manifest['samples'],labels['annotations']): sample.update(speaker_id='synthetic-'+str(sample['planned_speaker_slot']),native_iranian_persian=True, consent_reference='test-only',recording_path=recording.name,recording_sha256=hashlib.sha256(recording.read_bytes()).hexdigest(),duration_seconds=1) label.update(reviewed_by='synthetic-test-only',review_status='approved',reference_text='fee 42', critical_facts=[{'fact_id':'fee','kind':'number','literal':'42'}]) observations['observations'].append(dict(sample_id=sample['sample_id'],effective_profile_sha256=digest, session_reference='test-only',final_transcript='fee 42',critical_fact_results={'fee':True}, critical_reviewed_by='synthetic-test-only',completion_delay_ms=100,response_latency_ms=200, segmentation_errors=0,truncated_transcripts=0)) return manifest,labels,observations def test_weighted_scores_missing_data_and_critical_gate(tmp_path): m,a,o=fixture(tmp_path) result=score(m,a,o,root=tmp_path) assert result['scored_samples']==10 and result['asr_accuracy_status']=='passed' assert result['phase_acceptance_status']=='not_run' assert result['groups']['quiet']['wer']==0 held=next(s for s in m['samples'] if s['split']=='held_out' and s['condition']=='quiet') obs=next(r for r in o['observations'] if r['sample_id']==held['sample_id']) obs['critical_fact_results']['fee']=False assert score(m,a,o,root=tmp_path)['asr_accuracy_status']=='failed' obs['critical_fact_results']['fee']=True o['observations'].remove(obs) result=score(m,a,o,root=tmp_path) assert result['asr_accuracy_status']=='blocked' and held['sample_id'] in result['missing_samples'] @pytest.mark.parametrize('problem',['digest','reference','critical','recording','duplicate']) def test_refuses_invalid_evidence(tmp_path,problem): m,a,o=fixture(tmp_path) held=next(s for s in m['samples'] if s['split']=='held_out') obs=next(r for r in o['observations'] if r['sample_id']==held['sample_id']) label=next(r for r in a['annotations'] if r['sample_id']==held['sample_id']) if problem=='digest':o['effective_profile_sha256']='wrong' if problem=='reference':label['reference_text']='' if problem=='critical':obs['critical_fact_results']={} if problem=='recording':held['recording_sha256']='wrong' if problem=='duplicate':o['observations'].append(deepcopy(obs)) with pytest.raises(ValueError):score(m,a,o,root=tmp_path) def test_blank_assets_cannot_pass(): root=Path(__file__).resolve().parents[2]/'docs/evaluation' m=json.loads((root/'openai-persian-speech-manifest.json').read_text()) a=json.loads((root/'openai-persian-speech-annotations.json').read_text()) o={'effective_profile':{},'effective_profile_sha256':hashlib.sha256(b'{}').hexdigest(),'observations':[]} result=score(m,a,o,root=root) assert result['asr_accuracy_status']=='blocked' and result['scored_samples']==0