|
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 |
| - # Client should be automatically initialized |
28 |
| - assert client.async_client._is_initialized is True |
29 | 41 | assert client.async_client.route_impls is not None
|
30 | 42 |
|
31 |
| - async def test_async_client_auto_initialization(self): |
| 43 | + async def test_async_client_auto_initialization(self, monkeypatch): |
32 | 44 | """Test that async client can be initialized and works properly."""
|
33 |
| - client = AsyncLlamaStackAsLibraryClient("nvidia") |
| 45 | + # Mock the stack construction to avoid dependency issues |
| 46 | + mock_impls = {} |
| 47 | + mock_route_impls = RouteImpls({}) |
| 48 | + |
| 49 | + async def mock_construct_stack(config, custom_provider_registry): |
| 50 | + return mock_impls |
| 51 | + |
| 52 | + def mock_initialize_route_impls(impls): |
| 53 | + return mock_route_impls |
| 54 | + |
| 55 | + monkeypatch.setattr("llama_stack.core.library_client.construct_stack", mock_construct_stack) |
| 56 | + monkeypatch.setattr("llama_stack.core.library_client.initialize_route_impls", mock_initialize_route_impls) |
| 57 | + |
| 58 | + client = AsyncLlamaStackAsLibraryClient("ci-tests") |
34 | 59 |
|
35 | 60 | # Initialize the client
|
36 | 61 | result = await client.initialize()
|
37 | 62 | assert result is True
|
38 |
| - assert client._is_initialized is True |
39 | 63 | assert client.route_impls is not None
|
40 | 64 |
|
41 |
| - def test_initialize_method_backward_compatibility(self): |
| 65 | + def test_initialize_method_backward_compatibility(self, monkeypatch): |
42 | 66 | """Test that initialize() method still works for backward compatibility."""
|
43 |
| - client = LlamaStackAsLibraryClient("nvidia") |
| 67 | + # Mock the stack construction to avoid dependency issues |
| 68 | + mock_impls = {} |
| 69 | + mock_route_impls = RouteImpls({}) |
| 70 | + |
| 71 | + async def mock_construct_stack(config, custom_provider_registry): |
| 72 | + return mock_impls |
| 73 | + |
| 74 | + def mock_initialize_route_impls(impls): |
| 75 | + return mock_route_impls |
| 76 | + |
| 77 | + monkeypatch.setattr("llama_stack.core.library_client.construct_stack", mock_construct_stack) |
| 78 | + monkeypatch.setattr("llama_stack.core.library_client.initialize_route_impls", mock_initialize_route_impls) |
| 79 | + |
| 80 | + client = LlamaStackAsLibraryClient("ci-tests") |
44 | 81 |
|
45 |
| - # initialize() should return None (historical behavior) and not cause errors |
46 | 82 | result = client.initialize()
|
47 | 83 | assert result is None
|
48 | 84 |
|
49 |
| - # Multiple calls should be safe |
50 | 85 | result2 = client.initialize()
|
51 | 86 | assert result2 is None
|
52 | 87 |
|
53 |
| - async def test_async_initialize_method_idempotent(self): |
| 88 | + async def test_async_initialize_method_idempotent(self, monkeypatch): |
54 | 89 | """Test that async initialize() method can be called multiple times safely."""
|
55 |
| - client = AsyncLlamaStackAsLibraryClient("nvidia") |
| 90 | + mock_impls = {} |
| 91 | + mock_route_impls = RouteImpls({}) |
| 92 | + |
| 93 | + async def mock_construct_stack(config, custom_provider_registry): |
| 94 | + return mock_impls |
| 95 | + |
| 96 | + def mock_initialize_route_impls(impls): |
| 97 | + return mock_route_impls |
| 98 | + |
| 99 | + monkeypatch.setattr("llama_stack.core.library_client.construct_stack", mock_construct_stack) |
| 100 | + monkeypatch.setattr("llama_stack.core.library_client.initialize_route_impls", mock_initialize_route_impls) |
| 101 | + |
| 102 | + client = AsyncLlamaStackAsLibraryClient("ci-tests") |
56 | 103 |
|
57 |
| - # First initialization |
58 | 104 | result1 = await client.initialize()
|
59 | 105 | assert result1 is True
|
60 |
| - assert client._is_initialized is True |
61 | 106 |
|
62 |
| - # Second initialization should be safe and return True |
63 | 107 | result2 = await client.initialize()
|
64 | 108 | assert result2 is True
|
65 |
| - assert client._is_initialized is True |
66 | 109 |
|
67 |
| - def test_route_impls_automatically_set(self): |
| 110 | + def test_route_impls_automatically_set(self, monkeypatch): |
68 | 111 | """Test that route_impls is automatically set during construction."""
|
69 |
| - # Test sync client - should be auto-initialized |
70 |
| - sync_client = LlamaStackAsLibraryClient("nvidia") |
71 |
| - assert sync_client.async_client.route_impls is not None |
| 112 | + mock_impls = {} |
| 113 | + mock_route_impls = RouteImpls({}) |
| 114 | + |
| 115 | + async def mock_construct_stack(config, custom_provider_registry): |
| 116 | + return mock_impls |
| 117 | + |
| 118 | + def mock_initialize_route_impls(impls): |
| 119 | + return mock_route_impls |
72 | 120 |
|
73 |
| - # Test that the async client is marked as initialized |
74 |
| - assert sync_client.async_client._is_initialized is True |
| 121 | + monkeypatch.setattr("llama_stack.core.library_client.construct_stack", mock_construct_stack) |
| 122 | + monkeypatch.setattr("llama_stack.core.library_client.initialize_route_impls", mock_initialize_route_impls) |
| 123 | + |
| 124 | + sync_client = LlamaStackAsLibraryClient("ci-tests") |
| 125 | + assert sync_client.async_client.route_impls is not None |
0 commit comments