File size: 7,994 Bytes
5c03d80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"""Tests for settings API routes."""

import pytest
from fastapi.testclient import TestClient


class TestSettingsAPI:
    """Test settings API endpoints."""

    def test_get_settings_returns_default_config(self, client: TestClient) -> None:
        """Test GET /api/settings returns default configuration."""
        response = client.get("/api/settings")
        
        assert response.status_code == 200
        data = response.json()
        
        # Check response structure
        assert "api_keys_configured" in data
        assert "selected_model" in data
        assert "available_models" in data
        assert "plugins_installed" in data
        
        # Check default values
        assert data["selected_model"]["provider"] == "groq"
        assert data["selected_model"]["model"] == "gpt-oss-120b"
        assert isinstance(data["api_keys_configured"], dict)
        assert isinstance(data["available_models"], list)
        assert isinstance(data["plugins_installed"], list)

    def test_update_api_key_valid_provider(self, client: TestClient) -> None:
        """Test POST /api/settings/api-key with valid provider."""
        test_key = "test-key-12345"
        payload = {
            "provider": "openai",
            "api_key": test_key
        }
        
        response = client.post("/api/settings/api-key", json=payload)
        
        assert response.status_code == 200
        data = response.json()
        
        assert data["status"] == "success"
        assert data["provider"] == "openai"
        assert data["configured"] is True
        assert "set" in data["message"]

    def test_update_api_key_clear_key(self, client: TestClient) -> None:
        """Test POST /api/settings/api-key to clear API key."""
        payload = {
            "provider": "anthropic",
            "api_key": None
        }
        
        response = client.post("/api/settings/api-key", json=payload)
        
        assert response.status_code == 200
        data = response.json()
        
        assert data["status"] == "success"
        assert data["provider"] == "anthropic"
        assert data["configured"] is False
        assert "cleared" in data["message"]

    def test_update_api_key_invalid_provider(self, client: TestClient) -> None:
        """Test POST /api/settings/api-key with invalid provider."""
        payload = {
            "provider": "invalid_provider",
            "api_key": "test-key"
        }
        
        response = client.post("/api/settings/api-key", json=payload)
        
        assert response.status_code == 400
        data = response.json()
        assert "Unknown provider" in data["detail"]

    def test_select_model_valid_model(self, client: TestClient) -> None:
        """Test POST /api/settings/model with valid model."""
        payload = {
            "provider": "openai",
            "model": "gpt-4o"
        }
        
        response = client.post("/api/settings/model", json=payload)
        
        assert response.status_code == 200
        data = response.json()
        
        assert data["status"] == "success"
        assert data["selected_model"]["provider"] == "openai"
        assert data["selected_model"]["model"] == "gpt-4o"
        assert "Model set to" in data["message"]

    def test_select_model_invalid_model(self, client: TestClient) -> None:
        """Test POST /api/settings/model with invalid model."""
        payload = {
            "provider": "invalid_provider",
            "model": "invalid_model"
        }
        
        response = client.post("/api/settings/model", json=payload)
        
        assert response.status_code == 400
        data = response.json()
        assert "Invalid model" in data["detail"]

    def test_check_api_key_required_default(self, client: TestClient) -> None:
        """Test GET /api/settings/api-key/required returns default status."""
        response = client.get("/api/settings/api-key/required")
        
        assert response.status_code == 200
        data = response.json()
        
        assert "required" in data
        assert "provider" in data
        assert "model" in data
        assert isinstance(data["required"], bool)

    def test_settings_persistence_across_calls(self, client: TestClient) -> None:
        """Test that settings persist across multiple API calls."""
        # Set an API key
        api_key_payload = {
            "provider": "google",
            "api_key": "test-google-key"
        }
        response = client.post("/api/settings/api-key", json=api_key_payload)
        assert response.status_code == 200
        
        # Select a model
        model_payload = {
            "provider": "anthropic",
            "model": "claude-3-5-sonnet-20241022"
        }
        response = client.post("/api/settings/model", json=model_payload)
        assert response.status_code == 200
        
        # Check that both changes are reflected in settings
        response = client.get("/api/settings")
        assert response.status_code == 200
        data = response.json()
        
        # Model should be updated
        assert data["selected_model"]["provider"] == "anthropic"
        assert data["selected_model"]["model"] == "claude-3-5-sonnet-20241022"
        
        # API key should be configured (we don't expose actual keys)
        assert "google" in data["api_keys_configured"]

    def test_api_key_payload_validation(self, client: TestClient) -> None:
        """Test API key payload validation."""
        # Missing provider - should fail validation
        response = client.post("/api/settings/api-key", json={"api_key": "test"})
        assert response.status_code == 422

    def test_model_payload_validation(self, client: TestClient) -> None:
        """Test model selection payload validation."""
        # Missing provider
        response = client.post("/api/settings/model", json={"model": "gpt-4"})
        assert response.status_code == 422
        
        # Missing model
        response = client.post("/api/settings/model", json={"provider": "openai"})
        assert response.status_code == 422

    def test_all_providers_supported(self, client: TestClient) -> None:
        """Test that all expected providers are supported."""
        expected_providers = ["openai", "anthropic", "google", "groq"]
        
        for provider in expected_providers:
            payload = {
                "provider": provider,
                "api_key": f"test-{provider}-key"
            }
            response = client.post("/api/settings/api-key", json=payload)
            assert response.status_code == 200
            
            data = response.json()
            assert data["provider"] == provider
            assert data["status"] == "success"

    def test_available_models_structure(self, client: TestClient) -> None:
        """Test that available models have expected structure."""
        response = client.get("/api/settings")
        assert response.status_code == 200
        
        data = response.json()
        models = data["available_models"]
        
        assert isinstance(models, list)
        assert len(models) > 0
        
        # Check structure of first model
        first_model = models[0]
        assert "provider" in first_model
        assert "model" in first_model
        assert isinstance(first_model["provider"], str)
        assert isinstance(first_model["model"], str)

    def test_api_keys_configured_structure(self, client: TestClient) -> None:
        """Test api_keys_configured returns all expected providers."""
        response = client.get("/api/settings")
        assert response.status_code == 200
        
        data = response.json()
        api_keys = data["api_keys_configured"]
        
        expected_providers = ["openai", "anthropic", "google", "groq"]
        for provider in expected_providers:
            assert provider in api_keys
            assert isinstance(api_keys[provider], bool)