chainlit / tests /test_app.py
Gustav2811's picture
Changed SRC structure
c00c6c4
"""
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"