bielik_app_service / test_simplified.py
Patryk Studzinski
feat: Add main backup and simplified service implementations with API endpoints
9222e8a
"""
Unit tests for simplified Bielik service
Tests the API structure without running actual models
"""
import os
import json
from unittest.mock import Mock, AsyncMock, patch
# Skip llama-cpp installation during testing
os.environ["SKIP_LLAMA_INSTALL"] = "1"
# Mock the registry before importing main
mock_registry = Mock()
mock_registry.get_available_model_names.return_value = ["bielik-1.5b-transformer", "bielik-11b-transformer"]
mock_registry.get_model_info.return_value = {"type": "transformers", "device": "cuda:0"}
@patch("app.main.registry", mock_registry)
def test_app_structure():
"""Test that simplified app has correct endpoints"""
from app.main import app
# Get all routes
routes = {route.path: route.methods for route in app.routes}
# Check required endpoints exist
assert "/" in routes, "Root endpoint missing"
assert "/health" in routes, "Health endpoint missing"
assert "/models" in routes, "Models endpoint missing"
assert "/chat" in routes, "Chat endpoint missing"
assert "/generate" in routes, "Generate endpoint missing"
# Check methods
assert "GET" in routes["/health"], "Health should be GET"
assert "GET" in routes["/models"], "Models should be GET"
assert "POST" in routes["/chat"], "Chat should be POST"
assert "POST" in routes["/generate"], "Generate should be POST"
print("βœ… App structure correct")
print(f" Routes: {list(routes.keys())}")
@patch("app.main.registry", mock_registry)
def test_no_business_logic():
"""Verify no domain/infill endpoints exist"""
from app.main import app
routes = {route.path for route in app.routes}
# These should NOT exist
forbidden_routes = ["/enhance", "/compare", "/infill", "/compare-infill", "/user/me"]
for route in forbidden_routes:
assert route not in routes, f"Business logic endpoint {route} should not exist"
print("βœ… No business logic endpoints found")
@patch("app.main.registry", mock_registry)
def test_request_schemas():
"""Test request/response schemas are valid"""
from app.main import ChatRequest, GenerateRequest, ChatResponse, GenerateResponse
from app.main import Message, HealthResponse, ModelsResponse
# Test ChatRequest
chat_req = ChatRequest(
model="bielik-1.5b-transformer",
messages=[Message(role="user", content="Hello")]
)
assert chat_req.model == "bielik-1.5b-transformer"
assert len(chat_req.messages) == 1
print("βœ… ChatRequest schema valid")
# Test GenerateRequest
gen_req = GenerateRequest(
model="bielik-1.5b-transformer",
prompt="Hello world"
)
assert gen_req.model == "bielik-1.5b-transformer"
assert gen_req.prompt == "Hello world"
print("βœ… GenerateRequest schema valid")
# Test HealthResponse
health = HealthResponse(
status="ok",
gpu_available=True,
models_available=2
)
assert health.status == "ok"
print("βœ… HealthResponse schema valid")
# Test ModelsResponse
models_resp = ModelsResponse(models=[])
assert isinstance(models_resp.models, list)
print("βœ… ModelsResponse schema valid")
@patch("app.main.registry", mock_registry)
def test_default_values():
"""Test that requests have sensible defaults"""
from app.main import ChatRequest, GenerateRequest, Message
# Chat with minimal fields
chat = ChatRequest(
model="test",
messages=[Message(role="user", content="test")]
)
assert chat.max_tokens == 150
assert chat.temperature == 0.7
assert chat.top_p == 0.9
print("βœ… Chat defaults correct")
# Generate with minimal fields
gen = GenerateRequest(
model="test",
prompt="test"
)
assert gen.max_tokens == 150
assert gen.temperature == 0.7
assert gen.top_p == 0.9
print("βœ… Generate defaults correct")
if __name__ == "__main__":
print("\n=== Testing Simplified Bielik Service ===\n")
try:
test_app_structure()
test_no_business_logic()
test_request_schemas()
test_default_values()
print("\nβœ… All tests passed!")
print("\n=== Phase 1 Verification Complete ===")
except AssertionError as e:
print(f"\n❌ Test failed: {e}")
exit(1)