|
5 | 5 | # the root directory of this source tree.
|
6 | 6 |
|
7 | 7 | """
|
8 |
| -Unit tests for LlamaStackAsLibraryClient initialization error handling. |
| 8 | +Unit tests for LlamaStackAsLibraryClient automatic initialization. |
9 | 9 |
|
10 |
| -These tests ensure that users get proper error messages when they forget to call |
11 |
| -initialize() on the library client, preventing AttributeError regressions. |
| 10 | +These tests ensure that the library client is automatically initialized |
| 11 | +and ready to use immediately after construction. |
12 | 12 | """
|
13 | 13 |
|
14 |
| -import pytest |
15 |
| - |
16 | 14 | from llama_stack.core.library_client import (
|
17 | 15 | AsyncLlamaStackAsLibraryClient,
|
18 | 16 | LlamaStackAsLibraryClient,
|
19 | 17 | )
|
20 | 18 |
|
21 | 19 |
|
22 |
| -class TestLlamaStackAsLibraryClientInitialization: |
23 |
| - """Test proper error handling for uninitialized library clients.""" |
24 |
| - |
25 |
| - @pytest.mark.parametrize( |
26 |
| - "api_call", |
27 |
| - [ |
28 |
| - lambda client: client.models.list(), |
29 |
| - lambda client: client.chat.completions.create(model="test", messages=[{"role": "user", "content": "test"}]), |
30 |
| - lambda client: next( |
31 |
| - client.chat.completions.create( |
32 |
| - model="test", messages=[{"role": "user", "content": "test"}], stream=True |
33 |
| - ) |
34 |
| - ), |
35 |
| - ], |
36 |
| - ids=["models.list", "chat.completions.create", "chat.completions.create_stream"], |
37 |
| - ) |
38 |
| - def test_sync_client_proper_error_without_initialization(self, api_call): |
39 |
| - """Test that sync client raises ValueError with helpful message when not initialized.""" |
| 20 | +class TestLlamaStackAsLibraryClientAutoInitialization: |
| 21 | + """Test automatic initialization of library clients.""" |
| 22 | + |
| 23 | + def test_sync_client_auto_initialization(self): |
| 24 | + """Test that sync client is automatically initialized after construction.""" |
40 | 25 | client = LlamaStackAsLibraryClient("nvidia")
|
41 | 26 |
|
42 |
| - with pytest.raises(ValueError) as exc_info: |
43 |
| - api_call(client) |
44 |
| - |
45 |
| - error_msg = str(exc_info.value) |
46 |
| - assert "Client not initialized" in error_msg |
47 |
| - assert "Please call initialize() first" in error_msg |
48 |
| - |
49 |
| - @pytest.mark.parametrize( |
50 |
| - "api_call", |
51 |
| - [ |
52 |
| - lambda client: client.models.list(), |
53 |
| - lambda client: client.chat.completions.create(model="test", messages=[{"role": "user", "content": "test"}]), |
54 |
| - ], |
55 |
| - ids=["models.list", "chat.completions.create"], |
56 |
| - ) |
57 |
| - async def test_async_client_proper_error_without_initialization(self, api_call): |
58 |
| - """Test that async client raises ValueError with helpful message when not initialized.""" |
| 27 | + # Client should be automatically initialized |
| 28 | + assert client.async_client._is_initialized is True |
| 29 | + assert client.async_client.route_impls is not None |
| 30 | + |
| 31 | + async def test_async_client_auto_initialization(self): |
| 32 | + """Test that async client can be initialized and works properly.""" |
59 | 33 | client = AsyncLlamaStackAsLibraryClient("nvidia")
|
60 | 34 |
|
61 |
| - with pytest.raises(ValueError) as exc_info: |
62 |
| - await api_call(client) |
| 35 | + # Initialize the client |
| 36 | + result = await client.initialize() |
| 37 | + assert result is True |
| 38 | + assert client._is_initialized is True |
| 39 | + assert client.route_impls is not None |
| 40 | + |
| 41 | + def test_initialize_method_backward_compatibility(self): |
| 42 | + """Test that initialize() method still works for backward compatibility.""" |
| 43 | + client = LlamaStackAsLibraryClient("nvidia") |
| 44 | + |
| 45 | + # initialize() should return None (historical behavior) and not cause errors |
| 46 | + result = client.initialize() |
| 47 | + assert result is None |
63 | 48 |
|
64 |
| - error_msg = str(exc_info.value) |
65 |
| - assert "Client not initialized" in error_msg |
66 |
| - assert "Please call initialize() first" in error_msg |
| 49 | + # Multiple calls should be safe |
| 50 | + result2 = client.initialize() |
| 51 | + assert result2 is None |
67 | 52 |
|
68 |
| - async def test_async_client_streaming_error_without_initialization(self): |
69 |
| - """Test that async client streaming raises ValueError with helpful message when not initialized.""" |
| 53 | + async def test_async_initialize_method_idempotent(self): |
| 54 | + """Test that async initialize() method can be called multiple times safely.""" |
70 | 55 | client = AsyncLlamaStackAsLibraryClient("nvidia")
|
71 | 56 |
|
72 |
| - with pytest.raises(ValueError) as exc_info: |
73 |
| - stream = await client.chat.completions.create( |
74 |
| - model="test", messages=[{"role": "user", "content": "test"}], stream=True |
75 |
| - ) |
76 |
| - await anext(stream) |
| 57 | + # First initialization |
| 58 | + result1 = await client.initialize() |
| 59 | + assert result1 is True |
| 60 | + assert client._is_initialized is True |
77 | 61 |
|
78 |
| - error_msg = str(exc_info.value) |
79 |
| - assert "Client not initialized" in error_msg |
80 |
| - assert "Please call initialize() first" in error_msg |
| 62 | + # Second initialization should be safe and return True |
| 63 | + result2 = await client.initialize() |
| 64 | + assert result2 is True |
| 65 | + assert client._is_initialized is True |
81 | 66 |
|
82 |
| - def test_route_impls_initialized_to_none(self): |
83 |
| - """Test that route_impls is initialized to None to prevent AttributeError.""" |
84 |
| - # Test sync client |
| 67 | + def test_route_impls_automatically_set(self): |
| 68 | + """Test that route_impls is automatically set during construction.""" |
| 69 | + # Test sync client - should be auto-initialized |
85 | 70 | sync_client = LlamaStackAsLibraryClient("nvidia")
|
86 |
| - assert sync_client.async_client.route_impls is None |
| 71 | + assert sync_client.async_client.route_impls is not None |
87 | 72 |
|
88 |
| - # Test async client directly |
89 |
| - async_client = AsyncLlamaStackAsLibraryClient("nvidia") |
90 |
| - assert async_client.route_impls is None |
| 73 | + # Test that the async client is marked as initialized |
| 74 | + assert sync_client.async_client._is_initialized is True |
0 commit comments