From 495cf11b4307ba690f6d8066cbbfc7009a41268c Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 4 Jun 2026 07:11:24 +0000 Subject: [PATCH] [TEST] Untested run_llm_rca function Implemented unit tests for the run_llm_rca background task in hookwise/tasks.py. Enhanced the test environment in tests/conftest.py by mocking Redis globally and setting the mandatory GUI_PASSWORD environment variable, ensuring suite-wide test stability and compatibility with environments lacking a live Redis server. Co-authored-by: arumes31 <114224498+arumes31@users.noreply.github.com> --- tests/conftest.py | 15 ++++++++++++-- tests/test_tasks.py | 50 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 tests/test_tasks.py diff --git a/tests/conftest.py b/tests/conftest.py index 5399d8a..e46ecda 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,5 @@ import os +from unittest.mock import MagicMock, patch os.environ["SOCKETIO_ASYNC_MODE"] = "threading" os.environ["SECRET_KEY"] = "test-secret" @@ -6,11 +7,21 @@ os.environ["DATABASE_URL"] = "sqlite:///:memory:" os.environ["TESTING"] = "true" os.environ["LIMITER_STORAGE_URI"] = "memory://" +os.environ["GUI_PASSWORD"] = "test-password" import pytest @pytest.fixture(autouse=True, scope="session") def setup_test_env(): - # Already set at top level, but kept for clarity - pass + mock_redis = MagicMock() + # Mock some common redis methods used in the app + mock_redis.get.return_value = None + mock_redis.set.return_value = True + + with ( + patch("hookwise.extensions.redis_client", mock_redis), + patch("hookwise.tasks.redis_client", mock_redis), + patch("hookwise.api.redis_client", mock_redis), + ): + yield diff --git a/tests/test_tasks.py b/tests/test_tasks.py new file mode 100644 index 0000000..2c1ac0a --- /dev/null +++ b/tests/test_tasks.py @@ -0,0 +1,50 @@ +import json +from unittest.mock import patch + +from hookwise.tasks import run_llm_rca + + +def test_run_llm_rca_success(): + payload = {"error": "connection failed"} + config_id = "test-config" + ai_prompt_template = "Custom system prompt" + expected_rca = "The connection failed because of X, Y, Z." + + with patch("hookwise.utils.call_llm") as mock_call_llm: + mock_call_llm.return_value = expected_rca + + result = run_llm_rca(config_id, payload, ai_prompt_template) + + assert result["status"] == "ok" + assert result["rca"] == expected_rca + + # Verify call_llm was called with expected arguments + args, kwargs = mock_call_llm.call_args + assert "Payload: " + json.dumps(payload) in args[0] + assert kwargs["system_prompt"] == ai_prompt_template + + +def test_run_llm_rca_no_response(): + payload = {"error": "timeout"} + config_id = "test-config" + + with patch("hookwise.utils.call_llm") as mock_call_llm: + mock_call_llm.return_value = None + + result = run_llm_rca(config_id, payload, None) + + assert result["status"] == "error" + assert "LLM returned no response" in result["rca"] + + +def test_run_llm_rca_exception(): + payload = {"error": "unknown"} + config_id = "test-config" + + with patch("hookwise.utils.call_llm") as mock_call_llm: + mock_call_llm.side_effect = Exception("LLM is down") + + result = run_llm_rca(config_id, payload, None) + + assert result["status"] == "error" + assert "LLM error: Exception" in result["rca"]