|
| 1 | +"""Tests for the `index` CLI commands.""" |
| 2 | + |
| 3 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 4 | + |
| 5 | +import pytest |
| 6 | +from click.testing import CliRunner |
| 7 | + |
| 8 | +from redis_sre_agent.cli.index import index |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def cli_runner(): |
| 13 | + """Click CLI test runner.""" |
| 14 | + return CliRunner() |
| 15 | + |
| 16 | + |
| 17 | +class TestIndexListCLI: |
| 18 | + """Test index list CLI command.""" |
| 19 | + |
| 20 | + def test_list_help_shows_options(self, cli_runner): |
| 21 | + """Test that list command shows expected options in help.""" |
| 22 | + result = cli_runner.invoke(index, ["list", "--help"]) |
| 23 | + |
| 24 | + assert result.exit_code == 0 |
| 25 | + assert "--json" in result.output |
| 26 | + |
| 27 | + def test_list_displays_indices(self, cli_runner): |
| 28 | + """Test that list command displays indices.""" |
| 29 | + mock_index = MagicMock() |
| 30 | + mock_index.exists = AsyncMock(return_value=True) |
| 31 | + mock_index._redis_client = MagicMock() |
| 32 | + mock_index._redis_client.execute_command = AsyncMock( |
| 33 | + return_value=[b"num_docs", b"100"] |
| 34 | + ) |
| 35 | + |
| 36 | + with patch( |
| 37 | + "redis_sre_agent.core.redis.get_knowledge_index", |
| 38 | + new_callable=AsyncMock, |
| 39 | + return_value=mock_index, |
| 40 | + ), patch( |
| 41 | + "redis_sre_agent.core.redis.get_schedules_index", |
| 42 | + new_callable=AsyncMock, |
| 43 | + return_value=mock_index, |
| 44 | + ), patch( |
| 45 | + "redis_sre_agent.core.redis.get_threads_index", |
| 46 | + new_callable=AsyncMock, |
| 47 | + return_value=mock_index, |
| 48 | + ), patch( |
| 49 | + "redis_sre_agent.core.redis.get_tasks_index", |
| 50 | + new_callable=AsyncMock, |
| 51 | + return_value=mock_index, |
| 52 | + ), patch( |
| 53 | + "redis_sre_agent.core.redis.get_instances_index", |
| 54 | + new_callable=AsyncMock, |
| 55 | + return_value=mock_index, |
| 56 | + ): |
| 57 | + result = cli_runner.invoke(index, ["list"]) |
| 58 | + |
| 59 | + assert result.exit_code == 0 |
| 60 | + # Should show table with indices |
| 61 | + assert "knowledge" in result.output or "RediSearch" in result.output |
| 62 | + |
| 63 | + def test_list_json_output(self, cli_runner): |
| 64 | + """Test that --json flag outputs JSON.""" |
| 65 | + mock_index = MagicMock() |
| 66 | + mock_index.exists = AsyncMock(return_value=True) |
| 67 | + mock_index._redis_client = MagicMock() |
| 68 | + mock_index._redis_client.execute_command = AsyncMock( |
| 69 | + return_value=[b"num_docs", b"50"] |
| 70 | + ) |
| 71 | + |
| 72 | + with patch( |
| 73 | + "redis_sre_agent.core.redis.get_knowledge_index", |
| 74 | + new_callable=AsyncMock, |
| 75 | + return_value=mock_index, |
| 76 | + ), patch( |
| 77 | + "redis_sre_agent.core.redis.get_schedules_index", |
| 78 | + new_callable=AsyncMock, |
| 79 | + return_value=mock_index, |
| 80 | + ), patch( |
| 81 | + "redis_sre_agent.core.redis.get_threads_index", |
| 82 | + new_callable=AsyncMock, |
| 83 | + return_value=mock_index, |
| 84 | + ), patch( |
| 85 | + "redis_sre_agent.core.redis.get_tasks_index", |
| 86 | + new_callable=AsyncMock, |
| 87 | + return_value=mock_index, |
| 88 | + ), patch( |
| 89 | + "redis_sre_agent.core.redis.get_instances_index", |
| 90 | + new_callable=AsyncMock, |
| 91 | + return_value=mock_index, |
| 92 | + ): |
| 93 | + result = cli_runner.invoke(index, ["list", "--json"]) |
| 94 | + |
| 95 | + assert result.exit_code == 0 |
| 96 | + import json |
| 97 | + |
| 98 | + output_data = json.loads(result.output) |
| 99 | + assert isinstance(output_data, list) |
| 100 | + assert len(output_data) == 5 # 5 indices |
| 101 | + |
| 102 | + |
| 103 | +class TestIndexRecreateCLI: |
| 104 | + """Test index recreate CLI command.""" |
| 105 | + |
| 106 | + def test_recreate_help_shows_options(self, cli_runner): |
| 107 | + """Test that recreate command shows expected options in help.""" |
| 108 | + result = cli_runner.invoke(index, ["recreate", "--help"]) |
| 109 | + |
| 110 | + assert result.exit_code == 0 |
| 111 | + assert "--index-name" in result.output |
| 112 | + assert "--yes" in result.output |
| 113 | + assert "-y" in result.output |
| 114 | + assert "--json" in result.output |
| 115 | + assert "knowledge" in result.output |
| 116 | + assert "schedules" in result.output |
| 117 | + assert "all" in result.output |
| 118 | + |
| 119 | + def test_recreate_requires_confirmation(self, cli_runner): |
| 120 | + """Test that recreate requires confirmation without -y.""" |
| 121 | + result = cli_runner.invoke(index, ["recreate"], input="n\n") |
| 122 | + |
| 123 | + assert result.exit_code == 0 |
| 124 | + assert "Aborted" in result.output |
| 125 | + |
| 126 | + def test_recreate_with_yes_flag(self, cli_runner): |
| 127 | + """Test that -y flag skips confirmation.""" |
| 128 | + mock_result = {"success": True, "indices": {"knowledge": "recreated"}} |
| 129 | + |
| 130 | + with patch( |
| 131 | + "redis_sre_agent.core.redis.recreate_indices", |
| 132 | + new_callable=AsyncMock, |
| 133 | + return_value=mock_result, |
| 134 | + ) as mock_recreate: |
| 135 | + result = cli_runner.invoke(index, ["recreate", "-y"]) |
| 136 | + |
| 137 | + assert result.exit_code == 0 |
| 138 | + mock_recreate.assert_called_once_with(None) # None means all |
| 139 | + assert "Successfully" in result.output or "✅" in result.output |
| 140 | + |
| 141 | + def test_recreate_specific_index(self, cli_runner): |
| 142 | + """Test recreating a specific index.""" |
| 143 | + mock_result = {"success": True, "indices": {"knowledge": "recreated"}} |
| 144 | + |
| 145 | + with patch( |
| 146 | + "redis_sre_agent.core.redis.recreate_indices", |
| 147 | + new_callable=AsyncMock, |
| 148 | + return_value=mock_result, |
| 149 | + ) as mock_recreate: |
| 150 | + result = cli_runner.invoke( |
| 151 | + index, ["recreate", "--index-name", "knowledge", "-y"] |
| 152 | + ) |
| 153 | + |
| 154 | + assert result.exit_code == 0 |
| 155 | + mock_recreate.assert_called_once_with("knowledge") |
| 156 | + |
| 157 | + def test_recreate_json_output(self, cli_runner): |
| 158 | + """Test that --json flag outputs JSON.""" |
| 159 | + mock_result = {"success": True, "indices": {"knowledge": "recreated"}} |
| 160 | + |
| 161 | + with patch( |
| 162 | + "redis_sre_agent.core.redis.recreate_indices", |
| 163 | + new_callable=AsyncMock, |
| 164 | + return_value=mock_result, |
| 165 | + ): |
| 166 | + result = cli_runner.invoke(index, ["recreate", "--json"]) |
| 167 | + |
| 168 | + assert result.exit_code == 0 |
| 169 | + import json |
| 170 | + |
| 171 | + output_data = json.loads(result.output) |
| 172 | + assert output_data["success"] is True |
0 commit comments