This commit is contained in:
Alvin-Zilverstand committed 2026-11-13 10:02:13 +01:00
1 parent e6db824792
commit ed76261303
5 files changed
+170 -9

No files matched your search

+60
View File
@@ -5,6 +5,7 @@ import contextlib
import io
import os
import tempfile
import stat
from pathlib import Path
from unittest.mock import patch
@@ -175,6 +176,29 @@ class ChatTests(unittest.IsolatedAsyncioTestCase):
self.assertEqual(len(app.query(MessageBubble)), 2)
self.assertFalse(app.query(MessageBubble).last()._streaming)
async def test_missing_done_clears_partial_before_fallback_and_history(self):
requests = []
fallback_started = asyncio.Event()
release_fallback = asyncio.Event()
async def handler(request):
payload = json.loads(request.content)
requests.append(payload)
if payload.get('stream'):
if len(requests) == 1:
return httpx.Response(200, text='data: {"choices":[{"delta":{"content":"partial secret"}}]}\n\ndata: {"choices":[],"usage":{"completion_tokens":2}}\n\n')
return httpx.Response(200, text='data: {"choices":[{"delta":{"content":"second reply"}}]}\n\ndata: [DONE]\n\n')
fallback_started.set()
await release_fallback.wait()
return httpx.Response(200, json={'choices': [{'message': {'content': 'complete reply'}}]})
app = await self.make_app(handler, turns=2)
async with app.run_test() as pilot:
await asyncio.wait_for(fallback_started.wait(), 2)
self.assertEqual(app.query(MessageBubble).last().message.content, '')
release_fallback.set()
await self.wait_finished(app, pilot)
self.assertEqual([m.content for m in app.messages], ['Start here', 'complete reply', 'second reply'])
self.assertNotIn('partial secret', json.dumps(requests[2]))
async def test_empty_stream_uses_fallback(self):
calls = []
def handler(request):
@@ -350,6 +374,42 @@ class ChatTests(unittest.IsolatedAsyncioTestCase):
class CommandLineTests(unittest.TestCase):
def test_settings_validation_identifies_invalid_fields(self):
with tempfile.TemporaryDirectory() as directory:
path = Path(directory) / 'sloppy.json'
for data, field in [
({'models': {}, 'selected': None}, 'selected'),
({'models': {'x': None}}, 'models.x'),
({'models': {'x': {'api_key': None}}}, 'models.x.api_key'),
({'models': {}, 'selected': {'ai1': None}}, 'selected.ai1'),
]:
path.write_text(json.dumps(data), encoding='utf-8')
with self.subTest(field=field), self.assertRaisesRegex(ValueError, field.replace('.', r'\.')):
load_settings(path)
path.write_text(json.dumps({'models': {'x': {'model': 'm'}}}), encoding='utf-8')
self.assertEqual(load_settings(path)['selected'], {'ai1': '', 'ai2': ''})
@unittest.skipIf(os.name == 'nt', 'Unix file modes only')
def test_settings_temp_is_private_and_failure_cleans_up(self):
with tempfile.TemporaryDirectory() as directory:
path = Path(directory) / 'sloppy.json'
for existing in (False, True):
if existing:
path.write_text('old', encoding='utf-8')
path.chmod(0o644)
original_replace = os.replace
def inspect_replace(source, destination):
self.assertEqual(stat.S_IMODE(source.stat().st_mode), 0o600)
self.assertIn('secret', source.read_text(encoding='utf-8'))
original_replace(source, destination)
with patch('settings.os.replace', side_effect=inspect_replace):
save_settings({'api_key': 'secret'}, path)
self.assertEqual(stat.S_IMODE(path.stat().st_mode), 0o600)
with patch('settings.os.replace', side_effect=OSError('replace failed')):
with self.assertRaisesRegex(OSError, 'replace failed'):
save_settings({'api_key': 'secret'}, path)
self.assertFalse(path.with_name('sloppy.json.tmp').exists())
def test_env_only_starts_without_picker_or_json_import(self):
variables = {
'AI1_BASE_URL': 'http://one.test/v1', 'AI1_API_KEY': 'one-key',