05e173b293
Replace the WebView shell with SwiftUI screens, add account-scoped Wiki and TTS APIs with adaptive review and photo scan support, and keep web/iOS pages usable while data loads asynchronously. Co-authored-by: Cursor <cursoragent@cursor.com>
79 lines
3.0 KiB
Python
79 lines
3.0 KiB
Python
import json
|
|
import os
|
|
import unittest
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from services.llm_service import LlmServiceError, llm_service
|
|
|
|
|
|
class _FakeResponse:
|
|
def __init__(self, payload: dict):
|
|
self._payload = json.dumps(payload).encode("utf-8")
|
|
|
|
def read(self):
|
|
return self._payload
|
|
|
|
def __enter__(self):
|
|
return self
|
|
|
|
def __exit__(self, exc_type, exc, tb):
|
|
return False
|
|
|
|
|
|
class LlmServiceTest(unittest.TestCase):
|
|
def tearDown(self):
|
|
get_settings = __import__("config", fromlist=["get_settings"]).get_settings
|
|
get_settings.cache_clear()
|
|
|
|
def test_not_configured_without_api_key(self):
|
|
with patch.dict(os.environ, {"DEEPSEEK_API_KEY": ""}, clear=False):
|
|
__import__("config", fromlist=["get_settings"]).get_settings.cache_clear()
|
|
self.assertFalse(llm_service.is_configured())
|
|
|
|
@patch("services.llm_service.urllib.request.urlopen")
|
|
def test_enhance_wiki_page_returns_model_content(self, urlopen_mock: MagicMock):
|
|
with patch.dict(os.environ, {"DEEPSEEK_API_KEY": "test-key"}, clear=False):
|
|
__import__("config", fromlist=["get_settings"]).get_settings.cache_clear()
|
|
urlopen_mock.return_value = _FakeResponse(
|
|
{
|
|
"choices": [
|
|
{"message": {"content": "# demo\n\n整理后的知识页。"}},
|
|
]
|
|
}
|
|
)
|
|
content = llm_service.enhance_wiki_page(title="demo", draft_markdown="# draft")
|
|
self.assertIn("整理后的知识页", content)
|
|
|
|
def test_missing_api_key_raises(self):
|
|
with patch.dict(os.environ, {"DEEPSEEK_API_KEY": ""}, clear=False):
|
|
__import__("config", fromlist=["get_settings"]).get_settings.cache_clear()
|
|
with self.assertRaises(LlmServiceError):
|
|
llm_service.enhance_wiki_page(title="demo", draft_markdown="# draft")
|
|
|
|
@patch("services.llm_service.urllib.request.urlopen")
|
|
def test_parse_word_pairs_from_text(self, urlopen_mock: MagicMock):
|
|
with patch.dict(os.environ, {"DEEPSEEK_API_KEY": "test-key"}, clear=False):
|
|
__import__("config", fromlist=["get_settings"]).get_settings.cache_clear()
|
|
urlopen_mock.return_value = _FakeResponse(
|
|
{
|
|
"choices": [
|
|
{
|
|
"message": {
|
|
"content": (
|
|
'{"items":[{"en":"apple","zh":"苹果"},'
|
|
'{"en":"banana","zh":"香蕉"}]}'
|
|
)
|
|
}
|
|
},
|
|
]
|
|
}
|
|
)
|
|
pairs = llm_service.parse_word_pairs_from_text("apple 苹果\nbanana 香蕉")
|
|
self.assertEqual(len(pairs), 2)
|
|
self.assertEqual(pairs[0]["en"], "apple")
|
|
self.assertEqual(pairs[0]["zh"], "苹果")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|