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_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)