|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from deepset_mcp.api.exceptions import UnexpectedAPIError |
| 6 | +from deepset_mcp.tools.haystack_service import list_component_families |
| 7 | +from test.unit.conftest import BaseFakeClient |
| 8 | + |
| 9 | + |
| 10 | +class FakeHaystackServiceResource: |
| 11 | + def __init__( |
| 12 | + self, get_component_schemas_response: dict[str, Any] | None = None, exception: Exception | None = None |
| 13 | + ): |
| 14 | + self._get_component_schemas_response = get_component_schemas_response |
| 15 | + self._exception = exception |
| 16 | + |
| 17 | + async def get_component_schemas(self) -> dict[str, Any]: |
| 18 | + if self._exception: |
| 19 | + raise self._exception |
| 20 | + if self._get_component_schemas_response is not None: |
| 21 | + return self._get_component_schemas_response |
| 22 | + raise NotImplementedError |
| 23 | + |
| 24 | + |
| 25 | +class FakeClient(BaseFakeClient): |
| 26 | + def __init__(self, resource: FakeHaystackServiceResource): |
| 27 | + self._resource = resource |
| 28 | + super().__init__() |
| 29 | + |
| 30 | + def haystack_service(self) -> FakeHaystackServiceResource: |
| 31 | + return self._resource |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.asyncio |
| 35 | +async def test_list_component_families_no_families() -> None: |
| 36 | + response: dict[str, Any] = {"component_schema": {"definitions": {"Components": {}}}} |
| 37 | + resource = FakeHaystackServiceResource(get_component_schemas_response=response) |
| 38 | + client = FakeClient(resource) |
| 39 | + result = await list_component_families(client) |
| 40 | + assert "No component families found" in result |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.asyncio |
| 44 | +async def test_list_component_families_success() -> None: |
| 45 | + response = { |
| 46 | + "component_schema": { |
| 47 | + "definitions": { |
| 48 | + "Components": { |
| 49 | + "Component1": { |
| 50 | + "properties": {"type": {"family": "converters", "family_description": "Convert data format"}} |
| 51 | + }, |
| 52 | + "Component2": {"properties": {"type": {"family": "readers", "family_description": "Read data"}}}, |
| 53 | + # Should be ignored - same family as Component1 |
| 54 | + "Component3": { |
| 55 | + "properties": {"type": {"family": "converters", "family_description": "Convert data format"}} |
| 56 | + }, |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + resource = FakeHaystackServiceResource(get_component_schemas_response=response) |
| 62 | + client = FakeClient(resource) |
| 63 | + result = await list_component_families(client) |
| 64 | + |
| 65 | + assert "Available Haystack component families" in result |
| 66 | + assert "**converters**" in result |
| 67 | + assert "Convert data format" in result |
| 68 | + assert "**readers**" in result |
| 69 | + assert "Read data" in result |
| 70 | + # Only two unique families should be present |
| 71 | + assert result.count("**") == 4 # Two sets of ** for each family |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.asyncio |
| 75 | +async def test_list_component_families_api_error() -> None: |
| 76 | + resource = FakeHaystackServiceResource(exception=UnexpectedAPIError(status_code=500, message="API Error")) |
| 77 | + client = FakeClient(resource) |
| 78 | + result = await list_component_families(client) |
| 79 | + assert "Failed to retrieve component families" in result |
| 80 | + assert "API Error" in result |
0 commit comments