integration: add tests/test_chat.py
Browse files- tests/test_chat.py +90 -0
tests/test_chat.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""DeepSeek V4 chat module tests — client, sessions, local echo, API surface."""
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import sys
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
import pytest
|
| 8 |
+
|
| 9 |
+
ROOT = Path(__file__).resolve().parent.parent
|
| 10 |
+
sys.path.insert(0, str(ROOT))
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@pytest.fixture(autouse=True)
|
| 14 |
+
def _clear_key(monkeypatch):
|
| 15 |
+
"""Force local mode in every test."""
|
| 16 |
+
monkeypatch.delenv("DEEPSEEK_API_KEY", raising=False)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def test_client_local_mode_defaults():
|
| 20 |
+
from chat.core import DeepSeekV4Client, DEFAULT_MODEL
|
| 21 |
+
|
| 22 |
+
client = DeepSeekV4Client()
|
| 23 |
+
assert client.local_mode is True
|
| 24 |
+
assert client.model == DEFAULT_MODEL
|
| 25 |
+
assert client.base_url == "https://api.deepseek.com/v1"
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def test_session_flow():
|
| 29 |
+
from chat.core import ChatSession, SOVEREIGN_SYSTEM_PROMPT
|
| 30 |
+
|
| 31 |
+
s = ChatSession()
|
| 32 |
+
assert s.session_id.startswith("ses_")
|
| 33 |
+
s.add("system", SOVEREIGN_SYSTEM_PROMPT)
|
| 34 |
+
s.add("user", "hello")
|
| 35 |
+
s.add("assistant", "hi")
|
| 36 |
+
api_msgs = s.to_api()
|
| 37 |
+
assert [m["role"] for m in api_msgs] == ["system", "user", "assistant"]
|
| 38 |
+
assert len(s.messages) == 3
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def test_session_history_bounded():
|
| 42 |
+
from chat.core import ChatSession
|
| 43 |
+
|
| 44 |
+
s = ChatSession(max_history=6)
|
| 45 |
+
for i in range(20):
|
| 46 |
+
s.add("user", f"msg {i}")
|
| 47 |
+
assert len(s.messages) <= 6
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def test_local_echo_completion():
|
| 51 |
+
from chat.core import ChatSession, DeepSeekV4Client
|
| 52 |
+
|
| 53 |
+
s = ChatSession()
|
| 54 |
+
s.add("user", "What modules exist?")
|
| 55 |
+
result = DeepSeekV4Client().complete(s)
|
| 56 |
+
assert result["mode"] == "local"
|
| 57 |
+
assert "Sovereign local echo" in result["content"]
|
| 58 |
+
assert "no DEEPSEEK_API_KEY" in result["content"]
|
| 59 |
+
assert result["model"] == "sovereign/deepseek-v4-sovereign"
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def test_client_env_config(monkeypatch):
|
| 63 |
+
from chat.core import DeepSeekV4Client
|
| 64 |
+
|
| 65 |
+
monkeypatch.setenv("DEEPSEEK_API_BASE", "https://example.com/v1")
|
| 66 |
+
monkeypatch.setenv("DEEPSEEK_MODEL", "custom-model")
|
| 67 |
+
client = DeepSeekV4Client()
|
| 68 |
+
assert client.base_url == "https://example.com/v1"
|
| 69 |
+
assert client.model == "custom-model"
|
| 70 |
+
assert client.local_mode is True # no key
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def test_list_models():
|
| 74 |
+
from chat.core import DeepSeekV4Client
|
| 75 |
+
|
| 76 |
+
models = DeepSeekV4Client().list_models()
|
| 77 |
+
ids = {m["id"] for m in models}
|
| 78 |
+
assert "sovereign/deepseek-v4-sovereign" in ids
|
| 79 |
+
assert "deepseek-v4-flash-0731" in ids
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def test_api_routes():
|
| 83 |
+
from chat.api.routes import router
|
| 84 |
+
|
| 85 |
+
paths = {route.path for route in router.routes}
|
| 86 |
+
assert "/api/v1/chat/sessions" in paths
|
| 87 |
+
assert "/api/v1/chat/sessions/{session_id}" in paths
|
| 88 |
+
assert "/api/v1/chat/send" in paths
|
| 89 |
+
assert "/api/v1/chat/models" in paths
|
| 90 |
+
assert "/api/v1/chat/status" in paths
|