15
15
AsyncLlamaStackAsLibraryClient ,
16
16
LlamaStackAsLibraryClient ,
17
17
)
18
+ from llama_stack .core .server .routes import RouteImpls
18
19
19
20
20
21
class TestLlamaStackAsLibraryClientAutoInitialization :
21
22
"""Test automatic initialization of library clients."""
22
23
23
- def test_sync_client_auto_initialization (self ):
24
+ def test_sync_client_auto_initialization (self , monkeypatch ):
24
25
"""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" )
26
40
27
41
# Client should be automatically initialized
28
- assert client .async_client ._is_initialized is True
29
42
assert client .async_client .route_impls is not None
30
43
31
- async def test_async_client_auto_initialization (self ):
44
+ async def test_async_client_auto_initialization (self , monkeypatch ):
32
45
"""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" )
34
60
35
61
# Initialize the client
36
62
result = await client .initialize ()
37
63
assert result is True
38
- assert client ._is_initialized is True
39
64
assert client .route_impls is not None
40
65
41
- def test_initialize_method_backward_compatibility (self ):
66
+ def test_initialize_method_backward_compatibility (self , monkeypatch ):
42
67
"""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" )
44
82
45
83
# initialize() should return None (historical behavior) and not cause errors
46
84
result = client .initialize ()
@@ -50,24 +88,48 @@ def test_initialize_method_backward_compatibility(self):
50
88
result2 = client .initialize ()
51
89
assert result2 is None
52
90
53
- async def test_async_initialize_method_idempotent (self ):
91
+ async def test_async_initialize_method_idempotent (self , monkeypatch ):
54
92
"""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" )
56
107
57
108
# First initialization
58
109
result1 = await client .initialize ()
59
110
assert result1 is True
60
- assert client ._is_initialized is True
61
111
62
112
# Second initialization should be safe and return True
63
113
result2 = await client .initialize ()
64
114
assert result2 is True
65
- assert client ._is_initialized is True
66
115
67
- def test_route_impls_automatically_set (self ):
116
+ def test_route_impls_automatically_set (self , monkeypatch ):
68
117
"""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
+
69
131
# Test sync client - should be auto-initialized
70
- sync_client = LlamaStackAsLibraryClient ("nvidia " )
132
+ sync_client = LlamaStackAsLibraryClient ("ci-tests " )
71
133
assert sync_client .async_client .route_impls is not None
72
134
73
135
# Test that the async client is marked as initialized
0 commit comments