Add model settings and Markdown chat export

This commit is contained in:
Alvin-Zilverstand committed 2026-11-13 09:56:58 +01:00
1 parent 12f2a1655f
commit e6db824792
5 files changed
+317 -36

No files matched your search

+49
View File
@@ -4,6 +4,8 @@ import unittest
import contextlib
import io
import os
import tempfile
from pathlib import Path
from unittest.mock import patch
from rich.console import Console
@@ -12,6 +14,7 @@ from rich.markdown import Markdown
import httpx
from ai_chat import AIChatApp, AIConfig, MessageBubble, ChatMessage, ControlsHelp, ScreenshotOptions, main
from settings import load_settings, save_settings, store_model, model_from_settings
class ChatTests(unittest.IsolatedAsyncioTestCase):
@@ -328,8 +331,54 @@ class ChatTests(unittest.IsolatedAsyncioTestCase):
self.assertIsInstance(app.screen, ScreenshotOptions)
await pilot.press('escape')
async def test_text_transcript_preserves_message_order_and_markdown(self):
app = await self.make_app(lambda request: httpx.Response(500))
try:
app.messages = [
ChatMessage('user', 'Discuss **creativity**', 'System'),
ChatMessage('assistant', 'First point\n\n- detail', 'AI 1'),
ChatMessage('assistant', 'A counterpoint', 'AI 2'),
]
exported = app.export_conversation_text()
self.assertLess(exported.index('## Initial prompt'), exported.index('## AI 1'))
self.assertLess(exported.index('## AI 1'), exported.index('## AI 2'))
self.assertIn('Discuss **creativity**', exported)
self.assertIn('First point\n\n- detail', exported)
self.assertNotIn('test-key', exported)
finally:
await app.client.aclose()
class CommandLineTests(unittest.TestCase):
def test_env_only_starts_without_picker_or_json_import(self):
variables = {
'AI1_BASE_URL': 'http://one.test/v1', 'AI1_API_KEY': 'one-key',
'AI2_BASE_URL': 'http://two.test/v1', 'AI2_API_KEY': 'two-key',
'INITIAL_PROMPT': 'Discuss this', 'MAX_TURNS': '2',
}
with tempfile.TemporaryDirectory() as directory, patch.dict(os.environ, variables), \
patch('sys.argv', ['ai_chat.py']), patch('ai_chat.AIChatApp.run', autospec=True) as run:
original = os.getcwd()
try:
os.chdir(directory)
main()
self.assertFalse(Path('sloppy.json').exists())
finally:
os.chdir(original)
self.assertFalse(run.call_args.args[0].choose_models)
def test_model_settings_round_trip(self):
with tempfile.TemporaryDirectory() as directory:
path = Path(directory) / 'sloppy.json'
settings = load_settings(path)
store_model(settings, 'local', AIConfig('Local', 'http://localhost:11434/v1',
'env:LOCAL_KEY', 'llama3'))
settings['selected'] = {'ai1': 'local', 'ai2': 'local'}
save_settings(settings, path)
loaded = load_settings(path)
self.assertEqual(loaded['selected']['ai2'], 'local')
self.assertEqual(model_from_settings(loaded, 'local')['api_key'], 'env:LOCAL_KEY')
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: