#!/usr/bin/env python3 # Copyright (c) 2025 Patrick Motsch # All rights reserved. """ Test script: verify getService resolves core and importable services correctly. Run: pytest gateway/tests/unit/serviceCenter/test_service_center_resolution.py -v """ import pytest from modules.datamodels.datamodelUam import User from modules.serviceCenter import getService, clear_cache from modules.serviceCenter.context import ServiceCenterContext def _make_test_context(): """Create a minimal ServiceCenterContext for tests.""" user = User(id="test-user", username="testuser", email="test@example.com") return ServiceCenterContext( user=user, mandate_id="test-mandate", feature_instance_id="test-fi", workflow_id=None, workflow=None, ) @pytest.fixture(autouse=True) def clear_resolution_cache(): """Clear resolution cache between tests to avoid cross-test pollution.""" clear_cache() yield clear_cache() # ========== Core services ========== def test_resolve_utils(): """getService('utils') returns UtilsService instance.""" context = _make_test_context() svc = getService("utils", context) assert svc is not None assert hasattr(svc, "configGet") assert hasattr(svc, "jsonStripCodeFences") assert hasattr(svc, "sanitizePromptContent") def test_resolve_security(): """getService('security') returns SecurityService instance.""" context = _make_test_context() svc = getService("security", context) assert svc is not None assert hasattr(svc, "_context") def test_resolve_streaming(): """getService('streaming') returns StreamingService instance.""" context = _make_test_context() svc = getService("streaming", context) assert svc is not None assert hasattr(svc, "_context") # ========== Importable services (migrated to serviceCenter) ========== def test_resolve_ticket(): """getService('ticket') returns TicketService instance.""" context = _make_test_context() svc = getService("ticket", context) assert svc is not None assert hasattr(svc, "connectTicket") def test_resolve_sharepoint(): """getService('sharepoint') returns SharepointService instance (depends on security).""" context = _make_test_context() svc = getService("sharepoint", context) assert svc is not None assert hasattr(svc, "extractSiteFromStandardPath") assert hasattr(svc, "setAccessTokenFromConnection") def test_resolve_chat(): """getService('chat') returns ChatService instance (depends on utils).""" context = _make_test_context() svc = getService("chat", context) assert svc is not None assert hasattr(svc, "getUserConnectionFromConnectionReference") assert hasattr(svc, "calculateObjectSize") def test_resolve_extraction(): """getService('extraction') returns ExtractionService instance (depends on chat, utils).""" context = _make_test_context() svc = getService("extraction", context) assert svc is not None assert hasattr(svc, "extractContent") assert hasattr(svc, "mergePartResults") def test_resolve_web(): """getService('web') returns WebService instance (has ai, chat, utils deps).""" context = _make_test_context() # Web depends on ai, chat, utils - may need legacy_hub if ai/chat not migrated try: svc = getService("web", context) assert svc is not None assert hasattr(svc, "performWebResearch") except (KeyError, ImportError, ModuleNotFoundError): pytest.skip("WebService depends on ai/chat which may not be in serviceCenter yet") # ========== Caching ========== def test_resolution_is_cached(): """Same context + key returns same instance.""" context = _make_test_context() svc1 = getService("utils", context) svc2 = getService("utils", context) assert svc1 is svc2 def test_different_contexts_different_instances(): """Different contexts produce different instances (different cache keys).""" ctx1 = _make_test_context() user2 = User(id="other-user", username="other", email="other@example.com") ctx2 = ServiceCenterContext(user=user2, mandate_id="m2", feature_instance_id=None) svc1 = getService("utils", ctx1) svc2 = getService("utils", ctx2) assert svc1 is not svc2 # ========== Unknown service ========== def test_unknown_service_raises(): """getService with unknown key raises KeyError.""" context = _make_test_context() with pytest.raises(KeyError, match="Unknown service"): getService("nonexistent", context)