import asyncio import io import json import struct import tempfile import unittest import uuid import wave from pathlib import Path import httpx from fastapi.testclient import TestClient from unittest.mock import patch from gateway import Dialogue, app from history import History from protocol import pack from tests_fake import FakeModel from test_gateway import Socket class SpeechModel(FakeModel): def infer(self, pcm, state): return (0.9 if pcm[0] else 0.0), state class HistoryTests(unittest.IsolatedAsyncioTestCase): async def asyncSetUp(self): self.temp = tempfile.TemporaryDirectory() self.h = History(Path(self.temp.name) / 'history.sqlite3') await self.h.initialize() self.cid = await self.h.conversation() async def asyncTearDown(self): self.temp.cleanup() async def test_restore_window_clear_and_partial(self): for i in range(12): rid = str(uuid.uuid4()) await self.h.begin(self.cid, rid, f'user{i}') await self.h.finish(rid, f'answer{i}', 'completed') await self.h.drained(rid) partial = str(uuid.uuid4()) await self.h.begin(self.cid, partial, 'partial user') await self.h.finish(partial, 'unfinished', 'interrupted') restarted = History(self.h.path); await restarted.initialize() context = await restarted.context(self.cid, 'current') self.assertEqual(len(context), 20) self.assertEqual(context[0]['content'], 'user2') self.assertFalse(any(x['content'] == 'unfinished' for x in context)) self.assertEqual(len(await restarted.rows(self.cid)), 26) other = await restarted.conversation() await restarted.begin(other, str(uuid.uuid4()), 'keep') await restarted.clear(self.cid) self.assertEqual(await restarted.rows(self.cid), []) self.assertEqual(len(await restarted.rows(other)), 2) self.assertEqual(self.h.path.stat().st_mode & 0o777, 0o600) with self.assertRaises(ValueError): await restarted.conversation('../bad') async def test_stale_finalize_and_playback_separate(self): rid = str(uuid.uuid4()) await self.h.begin(self.cid, rid, 'user') await self.h.interrupt(rid) await self.h.finish(rid, 'partial', 'interrupted') await self.h.finish(rid, 'old complete', 'completed') rows = await self.h.rows(self.cid) self.assertEqual(rows[-1]['text'], 'partial') self.assertEqual(rows[-1]['status'], 'interrupted') self.assertEqual(await self.h.context(self.cid, 'other'), []) rid2 = str(uuid.uuid4()) await self.h.begin(self.cid, rid2, 'user2') await self.h.finish(rid2, 'answer2', 'completed') self.assertEqual((await self.h.rows(self.cid))[-1]['playback'], 'awaiting_drain') self.assertEqual(await self.h.context(self.cid, 'next'), []) rid3 = str(uuid.uuid4()) await self.h.begin(self.cid, rid3, 'unheard user') await self.h.finish(rid3, 'unheard completed answer', 'completed') await self.h.interrupt(rid3) self.assertEqual(await self.h.context(self.cid, 'next'), []) self.assertEqual((await self.h.rows(self.cid))[-1]['status'], 'completed') await self.h.drained(rid2) rows = await self.h.rows(self.cid) self.assertEqual(next(r for r in rows if r['run_id'] == rid2 and r['role'] == 'assistant')['playback'], 'confirmed') self.assertEqual(len(await self.h.context(self.cid, 'next')), 2) async def test_duplex_running_and_awaiting_drain_no_loop(self): for running in (True, False): socket = Socket(); d = Dialogue(socket, SpeechModel(), None) await d.listen('auto') old = d.turn if running: d.task = asyncio.create_task(asyncio.sleep(10)) else: d.playback_waiting = True await d.input(pack(1, old, 1, struct.pack('