|
| 1 | +import pytest |
| 2 | + |
| 3 | +from lf_toolkit.chat import ChatCapabilities |
| 4 | +from lf_toolkit.chat import ChatHealthResponse |
| 5 | +from lf_toolkit.chat import ChatRequest |
| 6 | +from lf_toolkit.chat import ChatResponse |
| 7 | +from lf_toolkit.chat import Message |
| 8 | +from lf_toolkit.io.file_server import FileHandler |
| 9 | +from lf_toolkit.shared.mued_api_v0_1_0 import DataPolicySupport |
| 10 | +from lf_toolkit.shared.mued_api_v0_1_0 import HealthStatus |
| 11 | +from lf_toolkit.shared.mued_api_v0_1_0 import Role |
| 12 | + |
| 13 | +pytest_plugins = ('pytest_asyncio',) |
| 14 | + |
| 15 | + |
| 16 | +class TestHandleChat: |
| 17 | + |
| 18 | + @pytest.fixture |
| 19 | + def handler(self): |
| 20 | + return FileHandler() |
| 21 | + |
| 22 | + @pytest.mark.asyncio |
| 23 | + async def test_calls_registered_handler_with_chat_request(self, handler): |
| 24 | + received = {} |
| 25 | + |
| 26 | + def chat_fn(request: ChatRequest) -> ChatResponse: |
| 27 | + received["request"] = request |
| 28 | + return ChatResponse(output=Message(role=Role.ASSISTANT, content="hi there")) |
| 29 | + |
| 30 | + handler.register("chat", chat_fn) |
| 31 | + |
| 32 | + result = await handler.handle_chat({ |
| 33 | + "params": {"messages": [{"role": "USER", "content": "hello"}]} |
| 34 | + }) |
| 35 | + |
| 36 | + assert isinstance(received["request"], ChatRequest) |
| 37 | + assert received["request"].messages[0].content == "hello" |
| 38 | + assert result == {"output": {"role": "ASSISTANT", "content": "hi there"}} |
| 39 | + |
| 40 | + @pytest.mark.asyncio |
| 41 | + async def test_passes_through_non_chat_response_result(self, handler): |
| 42 | + handler.register("chat", lambda request: {"output": {"role": "ASSISTANT", "content": "raw"}}) |
| 43 | + |
| 44 | + result = await handler.handle_chat({ |
| 45 | + "params": {"messages": [{"role": "USER", "content": "hello"}]} |
| 46 | + }) |
| 47 | + |
| 48 | + assert result == {"output": {"role": "ASSISTANT", "content": "raw"}} |
| 49 | + |
| 50 | + @pytest.mark.asyncio |
| 51 | + async def test_raises_when_no_handler_registered(self, handler): |
| 52 | + with pytest.raises(ValueError, match="No user handler for 'chat'"): |
| 53 | + await handler.handle_chat({ |
| 54 | + "params": {"messages": [{"role": "USER", "content": "hello"}]} |
| 55 | + }) |
| 56 | + |
| 57 | + |
| 58 | +class TestHandleChatHealth: |
| 59 | + |
| 60 | + @pytest.fixture |
| 61 | + def handler(self): |
| 62 | + return FileHandler() |
| 63 | + |
| 64 | + @pytest.mark.asyncio |
| 65 | + async def test_calls_registered_handler_with_no_arguments(self, handler): |
| 66 | + def chat_health_fn() -> ChatHealthResponse: |
| 67 | + return ChatHealthResponse( |
| 68 | + status=HealthStatus.OK, |
| 69 | + capabilities=ChatCapabilities( |
| 70 | + supportsChat=True, |
| 71 | + supportsDataPolicy=DataPolicySupport.NOT_SUPPORTED, |
| 72 | + ), |
| 73 | + ) |
| 74 | + |
| 75 | + handler.register("chat/health", chat_health_fn) |
| 76 | + |
| 77 | + result = await handler.handle_chat_health({"params": {}}) |
| 78 | + |
| 79 | + assert result == { |
| 80 | + "status": "OK", |
| 81 | + "capabilities": {"supportsChat": True, "supportsDataPolicy": "NOT_SUPPORTED"}, |
| 82 | + } |
| 83 | + |
| 84 | + @pytest.mark.asyncio |
| 85 | + async def test_passes_through_non_chat_health_response_result(self, handler): |
| 86 | + handler.register("chat/health", lambda: {"status": "OK"}) |
| 87 | + |
| 88 | + result = await handler.handle_chat_health({"params": {}}) |
| 89 | + |
| 90 | + assert result == {"status": "OK"} |
| 91 | + |
| 92 | + |
| 93 | +class TestHandleDispatch: |
| 94 | + """Covers the name -> method lookup in Handler.handle, including the |
| 95 | + 'chat/health' slash normalisation.""" |
| 96 | + |
| 97 | + @pytest.fixture |
| 98 | + def handler(self): |
| 99 | + return FileHandler() |
| 100 | + |
| 101 | + @pytest.mark.asyncio |
| 102 | + async def test_dispatches_chat_to_handle_chat(self, handler): |
| 103 | + handler.register("chat", lambda request: ChatResponse( |
| 104 | + output=Message(role=Role.ASSISTANT, content="ok") |
| 105 | + )) |
| 106 | + |
| 107 | + result = await handler.handle("chat", { |
| 108 | + "params": {"messages": [{"role": "USER", "content": "hi"}]} |
| 109 | + }) |
| 110 | + |
| 111 | + assert result == {"output": {"role": "ASSISTANT", "content": "ok"}} |
| 112 | + |
| 113 | + @pytest.mark.asyncio |
| 114 | + async def test_dispatches_chat_slash_health_to_handle_chat_health(self, handler): |
| 115 | + handler.register("chat/health", lambda: ChatHealthResponse( |
| 116 | + status=HealthStatus.OK, |
| 117 | + capabilities=ChatCapabilities( |
| 118 | + supportsChat=True, |
| 119 | + supportsDataPolicy=DataPolicySupport.NOT_SUPPORTED, |
| 120 | + ), |
| 121 | + )) |
| 122 | + |
| 123 | + result = await handler.handle("chat/health", {"params": {}}) |
| 124 | + |
| 125 | + assert result["status"] == "OK" |
| 126 | + |
| 127 | + @pytest.mark.asyncio |
| 128 | + async def test_unknown_command_raises(self, handler): |
| 129 | + with pytest.raises(ValueError, match="No handler for 'unknown'"): |
| 130 | + await handler.handle("unknown", {"params": {}}) |
0 commit comments