Skip to content

Commit 1f08769

Browse files
authored
Add unit test for LLM dump_debug method (#23)
* Add unit test for LLM dump_debug method - Introduced a new test, `test_dump_debug`, to validate the functionality of the LLM's `_dump_debug` method. - The test verifies that the method correctly creates a log file, writes the expected content, and handles exceptions appropriately. * fix * fix
1 parent 40e69c9 commit 1f08769

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

tests/test_llm.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,42 @@ def test_tool(message: str) -> str:
371371
with patch("litai.llm.SDKLLM.chat", return_value=response):
372372
result = llm.call_tool(response, tools=[test_tool])
373373
assert result == "Tool received: How do I get a refund?"
374+
375+
376+
@patch("builtins.open", new_callable=MagicMock)
377+
@patch("os.makedirs")
378+
def test_dump_debug(mock_makedirs, mock_open):
379+
"""Test the LLM dump_debug method."""
380+
mock_file = MagicMock()
381+
mock_open.return_value.__enter__.return_value = mock_file
382+
383+
logs = []
384+
mock_file.write = lambda x: logs.append(x)
385+
386+
llm = LLM(model="openai/gpt-4")
387+
llm._dump_debug(
388+
payload={"prompt": "Hello, world!"},
389+
headers={"Authorization": "Bearer test-token"},
390+
exception=Exception("Test exception"),
391+
response=MagicMock(status_code=200, text="Test response"),
392+
)
393+
394+
mock_makedirs.assert_called_once_with("llm_debug_logs", exist_ok=True)
395+
396+
mock_open.assert_called_once()
397+
call_args = mock_open.call_args
398+
assert call_args[0][1] == "w", "write mode"
399+
assert call_args[1]["encoding"] == "utf-8"
400+
401+
assert len(logs) > 0, "content must be written"
402+
written_content = "".join(logs)
403+
assert "❌ LLM CALL DEBUG INFO" in written_content
404+
assert "Model: openai/gpt-4" in written_content
405+
assert "📬 Headers:" in written_content
406+
assert "Authorization: Bearer tes..." in written_content, "Authorization header should be redacted"
407+
assert "📤 Payload:" in written_content
408+
assert "Hello, world!" in written_content
409+
assert "📥 Response status: 200" in written_content
410+
assert "Test response" in written_content
411+
assert "📛 Exception:" in written_content
412+
assert "Test exception" in written_content

0 commit comments

Comments
 (0)