|
| 1 | +"""Tests for CLI app module.""" |
| 2 | + |
| 3 | +import json |
| 4 | +from pathlib import Path |
| 5 | +from unittest.mock import MagicMock, patch |
| 6 | + |
| 7 | +import pytest |
| 8 | +from typer.testing import CliRunner |
| 9 | + |
| 10 | +from code_assistant_manager.cli.app import app, config_app, list_config, validate_config |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def runner(): |
| 15 | + """Create a CLI test runner.""" |
| 16 | + return CliRunner() |
| 17 | + |
| 18 | + |
| 19 | +class TestValidateConfig: |
| 20 | + """Tests for config validation command.""" |
| 21 | + |
| 22 | + def test_validate_config_success(self, runner, tmp_path): |
| 23 | + """validate_config passes with valid configuration.""" |
| 24 | + config_file = tmp_path / "providers.json" |
| 25 | + config_file.write_text(json.dumps({"providers": []})) |
| 26 | + |
| 27 | + mock_cm = MagicMock() |
| 28 | + mock_cm.validate_config.return_value = (True, []) |
| 29 | + |
| 30 | + with patch("code_assistant_manager.config.ConfigManager", return_value=mock_cm): |
| 31 | + result = runner.invoke( |
| 32 | + config_app, ["validate", "--config", str(config_file)] |
| 33 | + ) |
| 34 | + |
| 35 | + assert result.exit_code == 0 |
| 36 | + assert "passed" in result.output.lower() |
| 37 | + |
| 38 | + def test_validate_config_failure(self, runner, tmp_path): |
| 39 | + """validate_config reports errors with invalid configuration.""" |
| 40 | + config_file = tmp_path / "providers.json" |
| 41 | + config_file.write_text(json.dumps({"providers": []})) |
| 42 | + |
| 43 | + mock_cm = MagicMock() |
| 44 | + mock_cm.validate_config.return_value = (False, ["Missing required field"]) |
| 45 | + |
| 46 | + with patch("code_assistant_manager.config.ConfigManager", return_value=mock_cm): |
| 47 | + result = runner.invoke( |
| 48 | + config_app, ["validate", "--config", str(config_file)] |
| 49 | + ) |
| 50 | + |
| 51 | + assert "failed" in result.output.lower() |
| 52 | + assert "Missing required field" in result.output |
| 53 | + |
| 54 | + def test_validate_config_file_not_found(self, runner, tmp_path): |
| 55 | + """validate_config handles missing file gracefully.""" |
| 56 | + config_file = tmp_path / "nonexistent.json" |
| 57 | + |
| 58 | + with patch( |
| 59 | + "code_assistant_manager.config.ConfigManager", |
| 60 | + side_effect=FileNotFoundError("Config not found"), |
| 61 | + ): |
| 62 | + result = runner.invoke( |
| 63 | + config_app, ["validate", "--config", str(config_file)] |
| 64 | + ) |
| 65 | + |
| 66 | + assert "not found" in result.output.lower() |
| 67 | + |
| 68 | + |
| 69 | +class TestListConfig: |
| 70 | + """Tests for config list command.""" |
| 71 | + |
| 72 | + def test_list_config_shows_locations(self, runner): |
| 73 | + """list_config shows configuration file locations.""" |
| 74 | + result = runner.invoke(config_app, ["list"]) |
| 75 | + |
| 76 | + assert result.exit_code == 0 |
| 77 | + assert "Configuration Files" in result.output |
| 78 | + # Should mention some standard locations |
| 79 | + assert "providers.json" in result.output |
| 80 | + |
| 81 | + def test_list_config_shows_editor_configs(self, runner): |
| 82 | + """list_config shows editor configuration locations.""" |
| 83 | + result = runner.invoke(config_app, ["list"]) |
| 84 | + |
| 85 | + assert result.exit_code == 0 |
| 86 | + # Should show editor configurations section |
| 87 | + assert "Editor" in result.output or "claude" in result.output.lower() |
| 88 | + |
| 89 | + |
| 90 | +class TestMainApp: |
| 91 | + """Tests for main app commands.""" |
| 92 | + |
| 93 | + def test_app_help(self, runner): |
| 94 | + """app shows help message.""" |
| 95 | + result = runner.invoke(app, ["--help"]) |
| 96 | + |
| 97 | + assert result.exit_code == 0 |
| 98 | + assert "Code Assistant Manager" in result.output |
| 99 | + |
| 100 | + def test_app_subcommands_available(self, runner): |
| 101 | + """app has expected subcommands.""" |
| 102 | + result = runner.invoke(app, ["--help"]) |
| 103 | + |
| 104 | + assert result.exit_code == 0 |
| 105 | + # Check for main subcommands |
| 106 | + assert "mcp" in result.output |
| 107 | + assert "prompt" in result.output |
| 108 | + assert "skill" in result.output |
| 109 | + assert "plugin" in result.output |
| 110 | + |
| 111 | + |
| 112 | +class TestGlobalOptions: |
| 113 | + """Tests for global CLI options.""" |
| 114 | + |
| 115 | + def test_debug_option(self, runner): |
| 116 | + """--debug option enables debug logging.""" |
| 117 | + with patch("code_assistant_manager.cli.app.logging") as mock_logging: |
| 118 | + result = runner.invoke(app, ["--debug", "--help"]) |
| 119 | + |
| 120 | + # The debug option should be recognized |
| 121 | + assert result.exit_code == 0 |
0 commit comments