|
| 1 | +"""Tests for the `query` CLI command.""" |
| 2 | + |
| 3 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 4 | + |
| 5 | +from click.testing import CliRunner |
| 6 | + |
| 7 | +from redis_sre_agent.cli.query import query |
| 8 | + |
| 9 | + |
| 10 | +def test_query_cli_help_shows_instance_option(): |
| 11 | + runner = CliRunner() |
| 12 | + result = runner.invoke(query, ["--help"]) |
| 13 | + |
| 14 | + assert result.exit_code == 0 |
| 15 | + assert "--redis-instance-id" in result.output |
| 16 | + assert "-r" in result.output |
| 17 | + |
| 18 | + |
| 19 | +def test_query_without_instance_uses_knowledge_agent(): |
| 20 | + runner = CliRunner() |
| 21 | + |
| 22 | + mock_agent = MagicMock() |
| 23 | + mock_agent.process_query = AsyncMock(return_value="ok") |
| 24 | + |
| 25 | + with ( |
| 26 | + patch( |
| 27 | + "redis_sre_agent.cli.query.get_knowledge_agent", return_value=mock_agent |
| 28 | + ) as mock_get_knowledge, |
| 29 | + patch("redis_sre_agent.cli.query.get_sre_agent") as mock_get_sre, |
| 30 | + patch( |
| 31 | + "redis_sre_agent.cli.query.get_instance_by_id", |
| 32 | + new=AsyncMock(), |
| 33 | + ) as mock_get_instance, |
| 34 | + ): |
| 35 | + result = runner.invoke(query, ["What is Redis SRE?"]) |
| 36 | + |
| 37 | + assert result.exit_code == 0, result.output |
| 38 | + mock_get_knowledge.assert_called_once() |
| 39 | + mock_get_sre.assert_not_called() |
| 40 | + mock_get_instance.assert_not_awaited() |
| 41 | + mock_agent.process_query.assert_awaited_once() |
| 42 | + |
| 43 | + |
| 44 | +def test_query_with_instance_uses_sre_agent_and_passes_instance_context(): |
| 45 | + runner = CliRunner() |
| 46 | + |
| 47 | + class DummyInstance: |
| 48 | + def __init__(self, id: str, name: str): # noqa: A003 - keep click-style arg name |
| 49 | + self.id = id |
| 50 | + self.name = name |
| 51 | + |
| 52 | + instance = DummyInstance("redis-prod-123", "Haink Production") |
| 53 | + |
| 54 | + mock_sre_agent = MagicMock() |
| 55 | + mock_sre_agent.process_query = AsyncMock(return_value="ok") |
| 56 | + |
| 57 | + with ( |
| 58 | + patch( |
| 59 | + "redis_sre_agent.cli.query.get_instance_by_id", |
| 60 | + new=AsyncMock(return_value=instance), |
| 61 | + ) as mock_get_instance, |
| 62 | + patch( |
| 63 | + "redis_sre_agent.cli.query.get_sre_agent", return_value=mock_sre_agent |
| 64 | + ) as mock_get_sre, |
| 65 | + patch("redis_sre_agent.cli.query.get_knowledge_agent") as mock_get_knowledge, |
| 66 | + ): |
| 67 | + # Use -r / --redis-instance-id option to select instance |
| 68 | + result = runner.invoke( |
| 69 | + query, |
| 70 | + [ |
| 71 | + "Should I scale this instance yet?", |
| 72 | + "-r", |
| 73 | + instance.id, |
| 74 | + ], |
| 75 | + ) |
| 76 | + |
| 77 | + assert result.exit_code == 0, result.output |
| 78 | + |
| 79 | + # Instance lookup should happen once with the provided ID |
| 80 | + mock_get_instance.assert_awaited_once_with(instance.id) |
| 81 | + |
| 82 | + # SRE agent should be used (not the knowledge agent) |
| 83 | + mock_get_sre.assert_called_once() |
| 84 | + mock_get_knowledge.assert_not_called() |
| 85 | + |
| 86 | + # The agent should be called exactly once |
| 87 | + mock_sre_agent.process_query.assert_awaited_once() |
| 88 | + _, kwargs = mock_sre_agent.process_query.call_args |
| 89 | + |
| 90 | + # Critical behavior: CLI must pass instance context through to the agent |
| 91 | + assert kwargs.get("context") == {"instance_id": instance.id} |
| 92 | + |
| 93 | + |
| 94 | +def test_query_with_unknown_instance_exits_with_error_and_skips_agents(): |
| 95 | + """If -r is provided but the instance does not exist, CLI should error and exit. |
| 96 | +
|
| 97 | + This directly tests the new existence-check logic in redis_sre_agent.cli.query. |
| 98 | + """ |
| 99 | + |
| 100 | + runner = CliRunner() |
| 101 | + |
| 102 | + mock_agent = MagicMock() |
| 103 | + mock_agent.process_query = AsyncMock(return_value="ok") |
| 104 | + |
| 105 | + missing_id = "nonexistent-instance-id" |
| 106 | + |
| 107 | + with ( |
| 108 | + patch( |
| 109 | + "redis_sre_agent.cli.query.get_instance_by_id", |
| 110 | + new=AsyncMock(return_value=None), |
| 111 | + ) as mock_get_instance, |
| 112 | + patch( |
| 113 | + "redis_sre_agent.cli.query.get_knowledge_agent", return_value=mock_agent |
| 114 | + ) as mock_get_knowledge, |
| 115 | + patch("redis_sre_agent.cli.query.get_sre_agent") as mock_get_sre, |
| 116 | + ): |
| 117 | + result = runner.invoke( |
| 118 | + query, |
| 119 | + [ |
| 120 | + "-r", |
| 121 | + missing_id, |
| 122 | + "Check the health of this instance", |
| 123 | + ], |
| 124 | + ) |
| 125 | + |
| 126 | + # CLI should exit with non-zero status (explicitly exit(1) in implementation) |
| 127 | + assert result.exit_code == 1 |
| 128 | + assert f"Instance not found: {missing_id}" in result.output |
| 129 | + |
| 130 | + # We attempted to resolve the instance ID once |
| 131 | + mock_get_instance.assert_awaited_once_with(missing_id) |
| 132 | + |
| 133 | + # Since the instance doesn't exist, no agent should be initialized or invoked |
| 134 | + mock_get_knowledge.assert_not_called() |
| 135 | + mock_get_sre.assert_not_called() |
| 136 | + mock_agent.process_query.assert_not_awaited() |
0 commit comments