|
| 1 | +""" |
| 2 | +Integration tests for /update command in CLI. |
| 3 | +
|
| 4 | +Feature: self-update-command |
| 5 | +Tests Requirements: 7.1, 7.2, 7.3, 7.6 |
| 6 | +""" |
| 7 | +import pytest |
| 8 | +from unittest.mock import Mock, patch, MagicMock |
| 9 | +from shello_cli.update.update_manager import UpdateResult |
| 10 | + |
| 11 | + |
| 12 | +class TestUpdateCommandIntegration: |
| 13 | + """Test suite for /update command CLI integration.""" |
| 14 | + |
| 15 | + @patch('shello_cli.update.update_manager.UpdateManager') |
| 16 | + def test_update_command_routing(self, mock_manager_class): |
| 17 | + """ |
| 18 | + Test that /update command is recognized and routed correctly. |
| 19 | + |
| 20 | + Validates: Requirements 7.1, 7.2 |
| 21 | + """ |
| 22 | + # This test verifies the command is detected and not sent to AI |
| 23 | + # The actual CLI integration is tested manually, but we verify |
| 24 | + # the UpdateManager is called correctly |
| 25 | + |
| 26 | + mock_manager = Mock() |
| 27 | + mock_manager_class.return_value = mock_manager |
| 28 | + |
| 29 | + # Simulate successful update |
| 30 | + mock_manager.perform_update.return_value = UpdateResult( |
| 31 | + success=True, |
| 32 | + message="Update completed successfully", |
| 33 | + new_version="0.5.0" |
| 34 | + ) |
| 35 | + |
| 36 | + # Import here to trigger the mock |
| 37 | + from shello_cli.update.update_manager import UpdateManager |
| 38 | + |
| 39 | + # Create manager and call perform_update |
| 40 | + manager = UpdateManager() |
| 41 | + result = manager.perform_update(force=False) |
| 42 | + |
| 43 | + # Verify the manager was called |
| 44 | + assert result.success is True |
| 45 | + assert result.new_version == "0.5.0" |
| 46 | + mock_manager.perform_update.assert_called_once_with(force=False) |
| 47 | + |
| 48 | + @patch('shello_cli.update.update_manager.UpdateManager') |
| 49 | + def test_update_command_with_force_flag(self, mock_manager_class): |
| 50 | + """ |
| 51 | + Test that /update --force parses the flag correctly. |
| 52 | + |
| 53 | + Validates: Requirements 7.6 |
| 54 | + """ |
| 55 | + mock_manager = Mock() |
| 56 | + mock_manager_class.return_value = mock_manager |
| 57 | + |
| 58 | + # Simulate successful forced update |
| 59 | + mock_manager.perform_update.return_value = UpdateResult( |
| 60 | + success=True, |
| 61 | + message="Update completed successfully", |
| 62 | + new_version="0.5.0" |
| 63 | + ) |
| 64 | + |
| 65 | + # Import here to trigger the mock |
| 66 | + from shello_cli.update.update_manager import UpdateManager |
| 67 | + |
| 68 | + # Create manager and call perform_update with force=True |
| 69 | + manager = UpdateManager() |
| 70 | + result = manager.perform_update(force=True) |
| 71 | + |
| 72 | + # Verify the manager was called with force=True |
| 73 | + assert result.success is True |
| 74 | + mock_manager.perform_update.assert_called_once_with(force=True) |
| 75 | + |
| 76 | + @patch('shello_cli.update.update_manager.UpdateManager') |
| 77 | + def test_update_command_displays_error(self, mock_manager_class): |
| 78 | + """ |
| 79 | + Test that /update command displays errors appropriately. |
| 80 | + |
| 81 | + Validates: Requirements 7.3 |
| 82 | + """ |
| 83 | + mock_manager = Mock() |
| 84 | + mock_manager_class.return_value = mock_manager |
| 85 | + |
| 86 | + # Simulate failed update |
| 87 | + mock_manager.perform_update.return_value = UpdateResult( |
| 88 | + success=False, |
| 89 | + message="Update failed", |
| 90 | + error="Network error: Could not connect to GitHub" |
| 91 | + ) |
| 92 | + |
| 93 | + # Import here to trigger the mock |
| 94 | + from shello_cli.update.update_manager import UpdateManager |
| 95 | + |
| 96 | + # Create manager and call perform_update |
| 97 | + manager = UpdateManager() |
| 98 | + result = manager.perform_update(force=False) |
| 99 | + |
| 100 | + # Verify error information is available |
| 101 | + assert result.success is False |
| 102 | + assert result.error is not None |
| 103 | + assert "Network error" in result.error |
| 104 | + |
| 105 | + def test_update_command_in_help(self): |
| 106 | + """ |
| 107 | + Test that /update command is documented in help. |
| 108 | + |
| 109 | + Validates: Requirements 7.1 |
| 110 | + """ |
| 111 | + # Import the display_help function |
| 112 | + from shello_cli.ui.ui_renderer import display_help |
| 113 | + |
| 114 | + # This is a smoke test - just verify the function can be called |
| 115 | + # The actual help text is verified manually |
| 116 | + # We can't easily test Rich output without complex mocking |
| 117 | + try: |
| 118 | + # Just verify the function exists and is callable |
| 119 | + assert callable(display_help) |
| 120 | + except Exception as e: |
| 121 | + pytest.fail(f"display_help function should be callable: {e}") |
| 122 | + |
| 123 | + |
| 124 | +class TestUpdateCommandOutput: |
| 125 | + """Test suite for /update command output formatting.""" |
| 126 | + |
| 127 | + def test_output_when_already_on_latest(self): |
| 128 | + """ |
| 129 | + Test that restart notification is NOT shown when already on latest version. |
| 130 | + |
| 131 | + Validates: Bug fix - no restart needed when already on latest |
| 132 | + """ |
| 133 | + # Simulate the result when already on latest version |
| 134 | + result = UpdateResult( |
| 135 | + success=True, |
| 136 | + message="You are already on the latest version (0.4.3)", |
| 137 | + new_version="0.4.3" |
| 138 | + ) |
| 139 | + |
| 140 | + # Verify the message contains "already on the latest version" |
| 141 | + assert "already on the latest version" in result.message.lower() |
| 142 | + |
| 143 | + # The CLI should check this condition and NOT show restart notification |
| 144 | + should_show_restart = result.new_version and "already on the latest version" not in result.message.lower() |
| 145 | + assert should_show_restart is False |
| 146 | + |
| 147 | + def test_output_when_update_successful(self): |
| 148 | + """ |
| 149 | + Test that restart notification IS shown when update succeeds. |
| 150 | + |
| 151 | + Validates: Requirements 7.4, 7.5 |
| 152 | + """ |
| 153 | + # Simulate the result when update succeeds |
| 154 | + result = UpdateResult( |
| 155 | + success=True, |
| 156 | + message="Update completed successfully", |
| 157 | + new_version="0.5.0" |
| 158 | + ) |
| 159 | + |
| 160 | + # Verify the message does NOT contain "already on the latest version" |
| 161 | + assert "already on the latest version" not in result.message.lower() |
| 162 | + |
| 163 | + # The CLI should show restart notification |
| 164 | + should_show_restart = result.new_version and "already on the latest version" not in result.message.lower() |
| 165 | + assert should_show_restart is True |
| 166 | + |
| 167 | + |
| 168 | +class TestUpdateCommandParsing: |
| 169 | + """Test suite for /update command parsing.""" |
| 170 | + |
| 171 | + def test_parse_update_command_no_flags(self): |
| 172 | + """Test parsing /update with no flags.""" |
| 173 | + user_input = "/update" |
| 174 | + |
| 175 | + # Simulate the parsing logic from cli.py |
| 176 | + force = "--force" in user_input.lower() |
| 177 | + |
| 178 | + assert force is False |
| 179 | + |
| 180 | + def test_parse_update_command_with_force(self): |
| 181 | + """Test parsing /update --force.""" |
| 182 | + user_input = "/update --force" |
| 183 | + |
| 184 | + # Simulate the parsing logic from cli.py |
| 185 | + force = "--force" in user_input.lower() |
| 186 | + |
| 187 | + assert force is True |
| 188 | + |
| 189 | + def test_parse_update_command_force_case_insensitive(self): |
| 190 | + """Test parsing /update --FORCE (case insensitive).""" |
| 191 | + user_input = "/update --FORCE" |
| 192 | + |
| 193 | + # Simulate the parsing logic from cli.py |
| 194 | + force = "--force" in user_input.lower() |
| 195 | + |
| 196 | + assert force is True |
| 197 | + |
| 198 | + def test_parse_update_command_with_extra_text(self): |
| 199 | + """Test parsing /update with extra text after --force.""" |
| 200 | + user_input = "/update --force now" |
| 201 | + |
| 202 | + # Simulate the parsing logic from cli.py |
| 203 | + force = "--force" in user_input.lower() |
| 204 | + |
| 205 | + assert force is True |
0 commit comments