59 lines
3.6 KiB
Python
59 lines
3.6 KiB
Python
import asyncio
|
|
import json
|
|
import tempfile
|
|
import unittest
|
|
import uuid
|
|
import wave
|
|
from pathlib import Path
|
|
from fastapi.testclient import TestClient
|
|
from gateway import app
|
|
from tts_debug import DebugStore
|
|
|
|
|
|
class DebugTests(unittest.TestCase):
|
|
def test_complete_partial_permissions_and_retention(self):
|
|
with tempfile.TemporaryDirectory() as root:
|
|
store=DebugStore(root,max_runs=2)
|
|
cid=str(uuid.uuid4())
|
|
for status in ['completed','interrupted','failed']:
|
|
c=store.create({'conversation_id':cid,'run_id':str(uuid.uuid4()),'tts_requests':[{'input':'公开测试'}]})
|
|
c.append(bytes(960),10,1);c.finalize(status)
|
|
with wave.open(str(c.path/'audio.wav')) as w:self.assertEqual(w.getnframes(),480)
|
|
self.assertEqual((c.path/'metadata.json').stat().st_mode&511,0o600)
|
|
rows=store.list(cid);self.assertEqual(len(rows),2)
|
|
store.mark(rows[0]['debug_id'],cid)
|
|
self.assertTrue(store.list(cid)[0]['marked_abnormal'])
|
|
with self.assertRaises(ValueError):store.authorized('../escape',cid,'wav')
|
|
with self.assertRaises(ValueError):store.authorized(rows[0]['debug_id'],str(uuid.uuid4()),'wav')
|
|
text=store.authorized(rows[0]['debug_id'],cid,'metadata').read_text()
|
|
for secret in ['LAN_GATEWAY_TOKEN','Authorization','reference_wav_path']:self.assertNotIn(secret,text)
|
|
capped=store.create({'conversation_id':cid});capped.append(bytes(48000*31*2),0,1);capped.finalize('interrupted')
|
|
with wave.open(str(capped.path/'audio.wav')) as w:self.assertLessEqual(w.getnframes(),48000*30)
|
|
|
|
def test_download_requires_auth_origin_and_scoped_id(self):
|
|
with tempfile.TemporaryDirectory() as root:
|
|
app.state.token='x'*40;app.state.debug=DebugStore(root);cid=str(uuid.uuid4());c=app.state.debug.create({'conversation_id':cid});c.append(bytes(960),0,1);c.finalize('completed')
|
|
client=TestClient(app)
|
|
payload={'conversation_id':cid,'debug_id':c.id,'kind':'wav'}
|
|
self.assertEqual(client.post('/debug/download',json=payload).status_code,403)
|
|
headers={'Authorization':'Bearer '+'x'*40,'Origin':'https://192.168.31.61:5011'}
|
|
self.assertEqual(client.post('/debug/download',json=payload,headers=headers).status_code,200)
|
|
payload['debug_id']='../bad'
|
|
self.assertEqual(client.post('/debug/download',json=payload,headers=headers).status_code,404)
|
|
|
|
def test_report_accepts_old_and_finite_new_diagnostics(self):
|
|
from unittest.mock import patch
|
|
app.state.lan_open=True;app.state.remote_reports=[]
|
|
old={'type':'diagnostic_report','app_version':'0.5.0','logs':[]}
|
|
new={**old,'app_version':'0.5.1','version_code':9,'camera':{'granted':False,'status':'off','permission_pending':False,'face_sharing':False},
|
|
'role_playing':False,'role_active':None,'role_previous':{'turn_id':2,'seq':7,'emotion':'neutral','intensity':0,'source':'fallback','reason':'non_json'}}
|
|
with patch('gateway.GuestSessions.lan',return_value=True):
|
|
client=TestClient(app)
|
|
for payload in (old,new):self.assertEqual(client.post('/debug/report',json=payload).status_code,200)
|
|
self.assertEqual(len(app.state.remote_reports),2)
|
|
|
|
def test_lore_constraints_bounded(self):
|
|
d=json.loads(Path('personas/lvbu.lorebook.json').read_text());text=d['rules']+'\n'+'\n'.join(e['text'] for e in d['entries'])
|
|
self.assertLess(len(text),1400)
|
|
for word in ['AI','安全','Markdown','日常','现代']:self.assertIn(word,text)
|