s
This commit is contained in:
1 parent
33b5e3b598
commit
2ad5d1711c
3 files changed
+233
-12
No files matched your search
+92
-2
@@ -6,9 +6,12 @@ import io
|
||||
import os
|
||||
from unittest.mock import patch
|
||||
|
||||
from rich.console import Console
|
||||
from rich.markdown import Markdown
|
||||
|
||||
import httpx
|
||||
|
||||
from ai_chat import AIChatApp, AIConfig, MessageBubble, main
|
||||
from ai_chat import AIChatApp, AIConfig, MessageBubble, ChatMessage, ControlsHelp, main
|
||||
|
||||
|
||||
class ChatTests(unittest.IsolatedAsyncioTestCase):
|
||||
@@ -39,7 +42,8 @@ class ChatTests(unittest.IsolatedAsyncioTestCase):
|
||||
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)
|
||||
self.assertEqual(str(app.query(MessageBubble).last()._content_widget.render()), '[bold]hello[/bold]')
|
||||
self.assertIsInstance(app.query(MessageBubble).last()._content_widget.content, Markdown)
|
||||
self.assertEqual(app.messages[-1].content, '[bold]hello[/bold]')
|
||||
self.assertTrue(app.client.is_closed)
|
||||
|
||||
async def test_partial_stream_fallback_reuses_bubble(self):
|
||||
@@ -104,6 +108,92 @@ class ChatTests(unittest.IsolatedAsyncioTestCase):
|
||||
self.assertIn('Empty response', str(app.status_bar.render()))
|
||||
self.assertEqual(app.current_turn, 0)
|
||||
|
||||
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')
|
||||
|
||||
|
||||
class CommandLineTests(unittest.TestCase):
|
||||
def test_invalid_environment_turns_reports_usage_error(self):
|
||||
|
||||
Reference in new issue
Block a user