fix(test): update LlamaStackAsLibraryClient initialization tests after removing initialize method

The recent refactor (3778a4c3) introduced automatic initialization for
  LlamaStackAsLibraryClient but the unit tests were expecting manual initalization and _is_initialized.
  This caused test failure.

  Changes:
  - Update test assertions to check route_impls is not None instead of _is_initialized
  - Add proper mocking in tests to avoid external provider dependencies
  - Maintain test coverage for automatic initialization behavior
  - Ensure backward compatibility testing for deprecated initialize() method

Signed-off-by: Mustafa Elbehery <melbeher@redhat.com>
This commit is contained in:
Mustafa Elbehery 2025-08-14 11:32:36 +02:00
parent 7d23fe3215
commit bbe2a89adc

View file

@ -15,60 +15,111 @@ from llama_stack.core.library_client import (
AsyncLlamaStackAsLibraryClient, AsyncLlamaStackAsLibraryClient,
LlamaStackAsLibraryClient, LlamaStackAsLibraryClient,
) )
from llama_stack.core.server.routes import RouteImpls
class TestLlamaStackAsLibraryClientAutoInitialization: class TestLlamaStackAsLibraryClientAutoInitialization:
"""Test automatic initialization of library clients.""" """Test automatic initialization of library clients."""
def test_sync_client_auto_initialization(self): def test_sync_client_auto_initialization(self, monkeypatch):
"""Test that sync client is automatically initialized after construction.""" """Test that sync client is automatically initialized after construction."""
client = LlamaStackAsLibraryClient("nvidia") # Mock the stack construction to avoid dependency issues
mock_impls = {}
mock_route_impls = RouteImpls({})
async def mock_construct_stack(config, custom_provider_registry):
return mock_impls
def mock_initialize_route_impls(impls):
return mock_route_impls
monkeypatch.setattr("llama_stack.core.library_client.construct_stack", mock_construct_stack)
monkeypatch.setattr("llama_stack.core.library_client.initialize_route_impls", mock_initialize_route_impls)
client = LlamaStackAsLibraryClient("ci-tests")
# Client should be automatically initialized
assert client.async_client._is_initialized is True
assert client.async_client.route_impls is not None assert client.async_client.route_impls is not None
async def test_async_client_auto_initialization(self): async def test_async_client_auto_initialization(self, monkeypatch):
"""Test that async client can be initialized and works properly.""" """Test that async client can be initialized and works properly."""
client = AsyncLlamaStackAsLibraryClient("nvidia") # Mock the stack construction to avoid dependency issues
mock_impls = {}
mock_route_impls = RouteImpls({})
async def mock_construct_stack(config, custom_provider_registry):
return mock_impls
def mock_initialize_route_impls(impls):
return mock_route_impls
monkeypatch.setattr("llama_stack.core.library_client.construct_stack", mock_construct_stack)
monkeypatch.setattr("llama_stack.core.library_client.initialize_route_impls", mock_initialize_route_impls)
client = AsyncLlamaStackAsLibraryClient("ci-tests")
# Initialize the client # Initialize the client
result = await client.initialize() result = await client.initialize()
assert result is True assert result is True
assert client._is_initialized is True
assert client.route_impls is not None assert client.route_impls is not None
def test_initialize_method_backward_compatibility(self): def test_initialize_method_backward_compatibility(self, monkeypatch):
"""Test that initialize() method still works for backward compatibility.""" """Test that initialize() method still works for backward compatibility."""
client = LlamaStackAsLibraryClient("nvidia") # Mock the stack construction to avoid dependency issues
mock_impls = {}
mock_route_impls = RouteImpls({})
async def mock_construct_stack(config, custom_provider_registry):
return mock_impls
def mock_initialize_route_impls(impls):
return mock_route_impls
monkeypatch.setattr("llama_stack.core.library_client.construct_stack", mock_construct_stack)
monkeypatch.setattr("llama_stack.core.library_client.initialize_route_impls", mock_initialize_route_impls)
client = LlamaStackAsLibraryClient("ci-tests")
# initialize() should return None (historical behavior) and not cause errors
result = client.initialize() result = client.initialize()
assert result is None assert result is None
# Multiple calls should be safe
result2 = client.initialize() result2 = client.initialize()
assert result2 is None assert result2 is None
async def test_async_initialize_method_idempotent(self): async def test_async_initialize_method_idempotent(self, monkeypatch):
"""Test that async initialize() method can be called multiple times safely.""" """Test that async initialize() method can be called multiple times safely."""
client = AsyncLlamaStackAsLibraryClient("nvidia") mock_impls = {}
mock_route_impls = RouteImpls({})
async def mock_construct_stack(config, custom_provider_registry):
return mock_impls
def mock_initialize_route_impls(impls):
return mock_route_impls
monkeypatch.setattr("llama_stack.core.library_client.construct_stack", mock_construct_stack)
monkeypatch.setattr("llama_stack.core.library_client.initialize_route_impls", mock_initialize_route_impls)
client = AsyncLlamaStackAsLibraryClient("ci-tests")
# First initialization
result1 = await client.initialize() result1 = await client.initialize()
assert result1 is True assert result1 is True
assert client._is_initialized is True
# Second initialization should be safe and return True
result2 = await client.initialize() result2 = await client.initialize()
assert result2 is True assert result2 is True
assert client._is_initialized is True
def test_route_impls_automatically_set(self): def test_route_impls_automatically_set(self, monkeypatch):
"""Test that route_impls is automatically set during construction.""" """Test that route_impls is automatically set during construction."""
# Test sync client - should be auto-initialized mock_impls = {}
sync_client = LlamaStackAsLibraryClient("nvidia") mock_route_impls = RouteImpls({})
assert sync_client.async_client.route_impls is not None
# Test that the async client is marked as initialized async def mock_construct_stack(config, custom_provider_registry):
assert sync_client.async_client._is_initialized is True return mock_impls
def mock_initialize_route_impls(impls):
return mock_route_impls
monkeypatch.setattr("llama_stack.core.library_client.construct_stack", mock_construct_stack)
monkeypatch.setattr("llama_stack.core.library_client.initialize_route_impls", mock_initialize_route_impls)
sync_client = LlamaStackAsLibraryClient("ci-tests")
assert sync_client.async_client.route_impls is not None