File size: 2,635 Bytes
91da766
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""DeepSeek V4 chat module tests — client, sessions, local echo, API surface."""

import os
import sys
from pathlib import Path

import pytest

ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))


@pytest.fixture(autouse=True)
def _clear_key(monkeypatch):
    """Force local mode in every test."""
    monkeypatch.delenv("DEEPSEEK_API_KEY", raising=False)


def test_client_local_mode_defaults():
    from chat.core import DeepSeekV4Client, DEFAULT_MODEL

    client = DeepSeekV4Client()
    assert client.local_mode is True
    assert client.model == DEFAULT_MODEL
    assert client.base_url == "https://api.deepseek.com/v1"


def test_session_flow():
    from chat.core import ChatSession, SOVEREIGN_SYSTEM_PROMPT

    s = ChatSession()
    assert s.session_id.startswith("ses_")
    s.add("system", SOVEREIGN_SYSTEM_PROMPT)
    s.add("user", "hello")
    s.add("assistant", "hi")
    api_msgs = s.to_api()
    assert [m["role"] for m in api_msgs] == ["system", "user", "assistant"]
    assert len(s.messages) == 3


def test_session_history_bounded():
    from chat.core import ChatSession

    s = ChatSession(max_history=6)
    for i in range(20):
        s.add("user", f"msg {i}")
    assert len(s.messages) <= 6


def test_local_echo_completion():
    from chat.core import ChatSession, DeepSeekV4Client

    s = ChatSession()
    s.add("user", "What modules exist?")
    result = DeepSeekV4Client().complete(s)
    assert result["mode"] == "local"
    assert "Sovereign local echo" in result["content"]
    assert "no DEEPSEEK_API_KEY" in result["content"]
    assert result["model"] == "sovereign/deepseek-v4-sovereign"


def test_client_env_config(monkeypatch):
    from chat.core import DeepSeekV4Client

    monkeypatch.setenv("DEEPSEEK_API_BASE", "https://example.com/v1")
    monkeypatch.setenv("DEEPSEEK_MODEL", "custom-model")
    client = DeepSeekV4Client()
    assert client.base_url == "https://example.com/v1"
    assert client.model == "custom-model"
    assert client.local_mode is True  # no key


def test_list_models():
    from chat.core import DeepSeekV4Client

    models = DeepSeekV4Client().list_models()
    ids = {m["id"] for m in models}
    assert "sovereign/deepseek-v4-sovereign" in ids
    assert "deepseek-v4-flash-0731" in ids


def test_api_routes():
    from chat.api.routes import router

    paths = {route.path for route in router.routes}
    assert "/api/v1/chat/sessions" in paths
    assert "/api/v1/chat/sessions/{session_id}" in paths
    assert "/api/v1/chat/send" in paths
    assert "/api/v1/chat/models" in paths
    assert "/api/v1/chat/status" in paths