File size: 3,368 Bytes
971a10e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c00c6c4
971a10e
 
 
 
 
 
 
 
c00c6c4
971a10e
 
 
 
 
 
 
 
 
 
 
c00c6c4
971a10e
 
 
 
 
 
 
 
 
 
 
 
 
 
c00c6c4
971a10e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c00c6c4
971a10e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Tests for the main Chainlit application.
"""

import os
import sys
from unittest.mock import patch

import pytest

# Add the src directory to the path so we can import our modules
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))

from naked_chat import __version__


def test_version():
    """Test that the version is properly defined."""
    assert __version__ == "0.1.0"


def test_environment_variables():
    """Test that environment variables are properly handled."""
    # Test with missing environment variables
    with patch.dict(os.environ, {}, clear=True):
        from app import DEFAULT_MODEL, GOOGLE_API_KEY, OPENAI_API_KEY

        assert DEFAULT_MODEL == "gpt-4o"
        assert OPENAI_API_KEY is None
        assert GOOGLE_API_KEY is None


def test_openai_model_validation():
    """Test OpenAI model validation."""
    from app import call_openai_model

    # Test with no API key configured
    with patch("naked_chat.app.openai_client", None):
        with pytest.raises(ValueError, match="OpenAI API key not configured"):
            import asyncio

            asyncio.run(call_openai_model("gpt-3.5-turbo", [], 0.7, 100))


def test_gemini_model_validation():
    """Test Gemini model validation."""
    from app import call_gemini_model

    # Test with no API key configured
    with patch("naked_chat.app.GOOGLE_API_KEY", None):
        with pytest.raises(ValueError, match="Google API key not configured"):
            import asyncio

            asyncio.run(call_gemini_model("gemini-pro", [], 0.7, 100))


class TestAppConstants:
    """Test class for application constants and configuration."""

    def test_default_model_is_valid(self):
        """Test that the default model is a valid option."""
        from app import DEFAULT_MODEL

        valid_models = [
            # OpenAI Flagship Models
            "gpt-4.1",
            "gpt-4o",
            # OpenAI Reasoning Models
            "o4-mini",
            "o3",
            "o3-pro",
            "o3-mini",
            # Google Gemini Models
            "gemini-2.5-pro",
            "gemini-2.5-flash",
        ]
        assert DEFAULT_MODEL in valid_models

    def test_import_structure(self):
        """Test that all required modules can be imported."""
        try:
            import naked_chat
            import app

            assert True
        except ImportError as e:
            pytest.fail(f"Import failed: {e}")


# Example of integration test structure (would need mocking for actual API calls)
class TestChatIntegration:
    """Integration tests for chat functionality."""

    @pytest.mark.asyncio
    async def test_message_history_management(self):
        """Test that message history is properly managed."""
        # This would be a more complex test with actual Chainlit session mocking
        # For now, just test the concept
        message_history = []

        # Simulate adding messages
        for i in range(15):
            message_history.append({"role": "user", "content": f"Message {i}"})

        # Test that history is truncated to last 10 messages
        if len(message_history) > 10:
            message_history = message_history[-10:]

        assert len(message_history) == 10
        assert message_history[0]["content"] == "Message 5"
        assert message_history[-1]["content"] == "Message 14"