Files
sloppy/test_ai_chat.py
T

236 lines
11 KiB
Python
Raw Normal View History

2026-10-06 15:46:37 +02:00
import asyncio
import json
import unittest
import contextlib
import io
import os
from unittest.mock import patch
2026-10-06 16:17:58 +02:00
from rich.console import Console
from rich.markdown import Markdown
2026-10-06 15:46:37 +02:00
import httpx
2026-10-15 01:05:01 +02:00
from ai_chat import AIChatApp, AIConfig, MessageBubble, ChatMessage, ControlsHelp, ScreenshotOptions, main
2026-10-06 15:46:37 +02:00
class ChatTests(unittest.IsolatedAsyncioTestCase):
async def make_app(self, handler, turns=1):
config = AIConfig('Test', 'https://example.test/v1', 'test-key', 'test-model')
app = AIChatApp(config, config, 'Start here', turns)
await app.client.aclose()
app.client = httpx.AsyncClient(transport=httpx.MockTransport(handler))
return app
async def wait_finished(self, app, pilot):
for _ in range(100):
if not app._is_running:
return
await pilot.pause(0.02)
self.fail('Conversation did not finish')
async def test_stream_usage_and_speaker_roles(self):
requests = []
def handler(request):
requests.append(json.loads(request.content))
return httpx.Response(200, text='data:{"choices":[{"delta":{"content":"[bold]hello[/bold]"}}]}\n\ndata: {"choices":[],"usage":{"total_tokens":1}}\n\ndata: [DONE]\n\n')
app = await self.make_app(handler, turns=3)
async with app.run_test() as pilot:
await self.wait_finished(app, pilot)
self.assertEqual(app.current_turn, 3)
self.assertEqual(len(app.messages), 4)
self.assertEqual([m['role'] for m in requests[1]['messages']], ['system', 'user', 'user'])
self.assertEqual([m['role'] for m in requests[2]['messages']], ['system', 'user', 'assistant', 'user'])
self.assertEqual(len(app.query(MessageBubble)), 4)
2026-10-06 16:17:58 +02:00
self.assertIsInstance(app.query(MessageBubble).last()._content_widget.content, Markdown)
self.assertEqual(app.messages[-1].content, '[bold]hello[/bold]')
2026-10-06 15:46:37 +02:00
self.assertTrue(app.client.is_closed)
async def test_partial_stream_fallback_reuses_bubble(self):
def handler(request):
if json.loads(request.content).get('stream'):
return httpx.Response(200, text='data: {"choices":[{"delta":{"content":"partial"}}]}\n\ndata: {"error":"failed"}\n\n')
return httpx.Response(200, json={'choices': [{'message': {'content': 'complete'}}]})
app = await self.make_app(handler)
async with app.run_test() as pilot:
await self.wait_finished(app, pilot)
self.assertEqual([m.content for m in app.messages], ['Start here', 'complete'])
self.assertEqual(len(app.query(MessageBubble)), 2)
self.assertFalse(app.query(MessageBubble).last()._streaming)
async def test_empty_stream_uses_fallback(self):
calls = []
def handler(request):
calls.append(json.loads(request.content))
if calls[-1].get('stream'):
return httpx.Response(200, text='data: [DONE]\n\n')
return httpx.Response(200, json={'choices': [{'message': {'content': 'fallback'}}]})
app = await self.make_app(handler)
async with app.run_test() as pilot:
await self.wait_finished(app, pilot)
self.assertEqual(len(calls), 2)
self.assertEqual(app.messages[-1].content, 'fallback')
async def test_error_is_preserved(self):
app = await self.make_app(lambda request: httpx.Response(500, text='unavailable'))
async with app.run_test() as pilot:
await self.wait_finished(app, pilot)
self.assertIn('Error:', str(app.status_bar.render()))
self.assertNotIn('Conversation ended', str(app.status_bar.render()))
self.assertEqual(app.current_turn, 0)
self.assertEqual(app.messages[-1].role, 'system')
async def test_quit_while_request_is_pending(self):
started = asyncio.Event()
cancelled = asyncio.Event()
async def handler(request):
started.set()
try:
await asyncio.Event().wait()
except asyncio.CancelledError:
cancelled.set()
raise
app = await self.make_app(handler)
async with app.run_test() as pilot:
await asyncio.wait_for(started.wait(), 2)
await pilot.press('ctrl+c')
self.assertTrue(cancelled.is_set())
self.assertTrue(app.client.is_closed)
async def test_empty_fallback_reports_error(self):
def handler(request):
if json.loads(request.content).get('stream'):
return httpx.Response(200, text='data: [DONE]\n\n')
return httpx.Response(200, json={'choices': [{'message': {'content': None}}]})
app = await self.make_app(handler)
async with app.run_test() as pilot:
await self.wait_finished(app, pilot)
self.assertIn('Empty response', str(app.status_bar.render()))
self.assertEqual(app.current_turn, 0)
2026-10-06 16:17:58 +02:00
async def test_scrolling_preserves_reading_position_and_resumes(self):
app = await self.make_app(lambda request: httpx.Response(500))
async def idle():
pass
app.start_conversation = idle
async with app.run_test(size=(100, 30)) as pilot:
chat = app.chat_container
bubble = chat.add_message(ChatMessage("assistant", "line\n\n" * 100, "AI 1"))
await pilot.pause()
self.assertGreater(chat.max_scroll_y, 0)
self.assertEqual(chat.scroll_y, chat.max_scroll_y)
await pilot.press('pageup')
position = chat.scroll_y
self.assertLess(position, chat.max_scroll_y)
bubble.append_content("new line\n\n" * 30)
await pilot.pause()
self.assertEqual(chat.scroll_y, position)
chat.add_message(ChatMessage("assistant", "next reply\n\n" * 30, "AI 2"))
await pilot.pause()
self.assertEqual(chat.scroll_y, position)
await pilot.press('home')
self.assertEqual(chat.scroll_y, 0)
await pilot.press('down')
self.assertGreater(chat.scroll_y, 0)
await pilot.press('end')
self.assertEqual(chat.scroll_y, chat.max_scroll_y)
chat.update_last_message("more lines\n\n" * 80)
await pilot.pause()
self.assertEqual(chat.scroll_y, chat.max_scroll_y)
await pilot.resize_terminal(70, 24)
await pilot.pause()
self.assertEqual(chat.scroll_y, chat.max_scroll_y)
await pilot.press('f1')
self.assertIsInstance(app.screen, ControlsHelp)
await pilot.press('escape')
self.assertNotIsInstance(app.screen, ControlsHelp)
async def test_markdown_and_waiting_during_stream(self):
app = await self.make_app(lambda request: httpx.Response(500))
first_chunk = asyncio.Event()
finish = asyncio.Event()
source = "# Heading\n\n**bold** and `code`\n\n- item\n\n```python\nprint(1)\n```"
async def stream(ai, history):
await first_chunk.wait()
yield source
await finish.wait()
app.stream_ai = stream
async with app.run_test() as pilot:
bubble = app.query(MessageBubble).last()
self.assertTrue(bubble._waiting_widget.display)
self.assertFalse(bubble._content_widget.display)
first_chunk.set()
await pilot.pause()
self.assertFalse(bubble._waiting_widget.display)
self.assertTrue(bubble._content_widget.display)
output = io.StringIO()
Console(file=output, width=80, force_terminal=False).print(bubble._content_widget.content)
rendered = output.getvalue()
self.assertIn('Heading', rendered)
self.assertIn('print(1)', rendered)
self.assertNotIn('**bold**', rendered)
self.assertNotIn('```', rendered)
self.assertEqual(bubble.message.content, source)
finish.set()
await self.wait_finished(app, pilot)
self.assertFalse(bubble._waiting_widget.display)
async def test_waiting_during_fallback_and_error(self):
fallback_started = asyncio.Event()
finish = asyncio.Event()
async def handler(request):
if json.loads(request.content).get('stream'):
return httpx.Response(200, text='data: [DONE]\n\n')
fallback_started.set()
await finish.wait()
return httpx.Response(500, text='failed')
app = await self.make_app(handler)
async with app.run_test() as pilot:
await asyncio.wait_for(fallback_started.wait(), 2)
bubble = app.query(MessageBubble).last()
self.assertTrue(bubble._waiting_widget.display)
finish.set()
await self.wait_finished(app, pilot)
self.assertFalse(bubble._waiting_widget.display)
self.assertEqual(app.messages[-1].role, 'system')
2026-10-15 01:05:01 +02:00
async def test_screenshot_menu_stays_open_when_turn_finishes(self):
app = await self.make_app(lambda request: httpx.Response(500))
finish = asyncio.Event()
async def stream(ai, history):
await finish.wait()
yield "Complete"
app.stream_ai = stream
async with app.run_test() as pilot:
await pilot.press('s')
self.assertIsInstance(app.screen, ScreenshotOptions)
self.assertGreater(app.screen.query_one('#full').region.height, 0)
finish.set()
await self.wait_finished(app, pilot)
self.assertEqual(app.current_turn, 1)
self.assertEqual(app.turn_counter.content, 'Turns completed: 1 / 1')
self.assertIsInstance(app.screen, ScreenshotOptions)
await pilot.press('escape', 'f2')
self.assertIsInstance(app.screen, ScreenshotOptions)
await pilot.press('escape')
2026-10-06 15:46:37 +02:00
class CommandLineTests(unittest.TestCase):
def test_invalid_environment_turns_reports_usage_error(self):
with patch.dict(os.environ, {"MAX_TURNS": "invalid"}), patch('sys.argv', ['ai_chat.py']):
with contextlib.redirect_stderr(io.StringIO()) as stderr:
with self.assertRaises(SystemExit) as error:
main()
self.assertEqual(error.exception.code, 2)
self.assertIn('invalid int value', stderr.getvalue())
def test_help_works_with_invalid_environment_turns(self):
with patch.dict(os.environ, {"MAX_TURNS": "invalid"}), patch('sys.argv', ['run_example.py', '--help']):
with contextlib.redirect_stdout(io.StringIO()):
with self.assertRaises(SystemExit) as error:
main()
self.assertEqual(error.exception.code, 0)
if __name__ == '__main__':
unittest.main()