Skip to content

Commit 3334941

Browse files
committed
fix(test): update LlamaStackAsLibraryClient initialization tests after removing initialize method
The recent refactor (3778a4c) 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 <[email protected]>
1 parent 3778a4c commit 3334941

File tree

1 file changed

+76
-14
lines changed

1 file changed

+76
-14
lines changed

tests/unit/distribution/test_library_client_initialization.py

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,70 @@
1515
AsyncLlamaStackAsLibraryClient,
1616
LlamaStackAsLibraryClient,
1717
)
18+
from llama_stack.core.server.routes import RouteImpls
1819

1920

2021
class TestLlamaStackAsLibraryClientAutoInitialization:
2122
"""Test automatic initialization of library clients."""
2223

23-
def test_sync_client_auto_initialization(self):
24+
def test_sync_client_auto_initialization(self, monkeypatch):
2425
"""Test that sync client is automatically initialized after construction."""
25-
client = LlamaStackAsLibraryClient("nvidia")
26+
# Mock the stack construction to avoid dependency issues
27+
mock_impls = {}
28+
mock_route_impls = RouteImpls({})
29+
30+
async def mock_construct_stack(config, custom_provider_registry):
31+
return mock_impls
32+
33+
def mock_initialize_route_impls(impls):
34+
return mock_route_impls
35+
36+
monkeypatch.setattr("llama_stack.core.library_client.construct_stack", mock_construct_stack)
37+
monkeypatch.setattr("llama_stack.core.library_client.initialize_route_impls", mock_initialize_route_impls)
38+
39+
client = LlamaStackAsLibraryClient("ci-tests")
2640

2741
# Client should be automatically initialized
28-
assert client.async_client._is_initialized is True
2942
assert client.async_client.route_impls is not None
3043

31-
async def test_async_client_auto_initialization(self):
44+
async def test_async_client_auto_initialization(self, monkeypatch):
3245
"""Test that async client can be initialized and works properly."""
33-
client = AsyncLlamaStackAsLibraryClient("nvidia")
46+
# Mock the stack construction to avoid dependency issues
47+
mock_impls = {}
48+
mock_route_impls = RouteImpls({})
49+
50+
async def mock_construct_stack(config, custom_provider_registry):
51+
return mock_impls
52+
53+
def mock_initialize_route_impls(impls):
54+
return mock_route_impls
55+
56+
monkeypatch.setattr("llama_stack.core.library_client.construct_stack", mock_construct_stack)
57+
monkeypatch.setattr("llama_stack.core.library_client.initialize_route_impls", mock_initialize_route_impls)
58+
59+
client = AsyncLlamaStackAsLibraryClient("ci-tests")
3460

3561
# Initialize the client
3662
result = await client.initialize()
3763
assert result is True
38-
assert client._is_initialized is True
3964
assert client.route_impls is not None
4065

41-
def test_initialize_method_backward_compatibility(self):
66+
def test_initialize_method_backward_compatibility(self, monkeypatch):
4267
"""Test that initialize() method still works for backward compatibility."""
43-
client = LlamaStackAsLibraryClient("nvidia")
68+
# Mock the stack construction to avoid dependency issues
69+
mock_impls = {}
70+
mock_route_impls = RouteImpls({})
71+
72+
async def mock_construct_stack(config, custom_provider_registry):
73+
return mock_impls
74+
75+
def mock_initialize_route_impls(impls):
76+
return mock_route_impls
77+
78+
monkeypatch.setattr("llama_stack.core.library_client.construct_stack", mock_construct_stack)
79+
monkeypatch.setattr("llama_stack.core.library_client.initialize_route_impls", mock_initialize_route_impls)
80+
81+
client = LlamaStackAsLibraryClient("ci-tests")
4482

4583
# initialize() should return None (historical behavior) and not cause errors
4684
result = client.initialize()
@@ -50,24 +88,48 @@ def test_initialize_method_backward_compatibility(self):
5088
result2 = client.initialize()
5189
assert result2 is None
5290

53-
async def test_async_initialize_method_idempotent(self):
91+
async def test_async_initialize_method_idempotent(self, monkeypatch):
5492
"""Test that async initialize() method can be called multiple times safely."""
55-
client = AsyncLlamaStackAsLibraryClient("nvidia")
93+
# Mock the stack construction to avoid dependency issues
94+
mock_impls = {}
95+
mock_route_impls = RouteImpls({})
96+
97+
async def mock_construct_stack(config, custom_provider_registry):
98+
return mock_impls
99+
100+
def mock_initialize_route_impls(impls):
101+
return mock_route_impls
102+
103+
monkeypatch.setattr("llama_stack.core.library_client.construct_stack", mock_construct_stack)
104+
monkeypatch.setattr("llama_stack.core.library_client.initialize_route_impls", mock_initialize_route_impls)
105+
106+
client = AsyncLlamaStackAsLibraryClient("ci-tests")
56107

57108
# First initialization
58109
result1 = await client.initialize()
59110
assert result1 is True
60-
assert client._is_initialized is True
61111

62112
# Second initialization should be safe and return True
63113
result2 = await client.initialize()
64114
assert result2 is True
65-
assert client._is_initialized is True
66115

67-
def test_route_impls_automatically_set(self):
116+
def test_route_impls_automatically_set(self, monkeypatch):
68117
"""Test that route_impls is automatically set during construction."""
118+
# Mock the stack construction to avoid dependency issues
119+
mock_impls = {}
120+
mock_route_impls = RouteImpls({})
121+
122+
async def mock_construct_stack(config, custom_provider_registry):
123+
return mock_impls
124+
125+
def mock_initialize_route_impls(impls):
126+
return mock_route_impls
127+
128+
monkeypatch.setattr("llama_stack.core.library_client.construct_stack", mock_construct_stack)
129+
monkeypatch.setattr("llama_stack.core.library_client.initialize_route_impls", mock_initialize_route_impls)
130+
69131
# Test sync client - should be auto-initialized
70-
sync_client = LlamaStackAsLibraryClient("nvidia")
132+
sync_client = LlamaStackAsLibraryClient("ci-tests")
71133
assert sync_client.async_client.route_impls is not None
72134

73135
# Test that the async client is marked as initialized

0 commit comments

Comments
 (0)