File indexing completed on 2026-08-12 08:24:57
0001 """
0002 Unit tests for modular CLI structure.
0003
0004 Tests command registration, imports, and modular organization.
0005 """
0006
0007 import pytest
0008 from click.testing import CliRunner
0009
0010
0011 from aid2e.cli.aid2e_cli import cli as cli_direct
0012 from aid2e.cli import cli as cli_package
0013
0014
0015 class TestCLIStructure:
0016 """Test CLI organization and command registration."""
0017
0018 def setup_method(self):
0019 """Set up test fixtures."""
0020 self.runner = CliRunner()
0021
0022 def test_cli_group_exists(self):
0023 """Main CLI group should exist."""
0024 assert cli_direct is not None
0025 assert callable(cli_direct)
0026
0027 def test_both_import_patterns_work(self):
0028 """Both import patterns should return same CLI."""
0029 assert cli_direct is cli_package
0030
0031 def test_cli_help(self):
0032 """CLI should display help message."""
0033 result = self.runner.invoke(cli_direct, ["--help"])
0034 assert result.exit_code == 0
0035 assert "AID2E" in result.output
0036 assert "AI assisted Detector Design for EIC" in result.output
0037
0038 def test_version_option(self):
0039 """CLI should support --version flag."""
0040 result = self.runner.invoke(cli_direct, ["--version"])
0041 assert result.exit_code == 0
0042 assert "aid2e" in result.output.lower()
0043
0044
0045 class TestCommandRegistration:
0046 """Test that all commands are properly registered."""
0047
0048 def setup_method(self):
0049 """Set up test fixtures."""
0050 self.runner = CliRunner()
0051
0052 def get_registered_commands(self):
0053 """Get list of registered command names."""
0054 return list(cli_direct.commands.keys())
0055
0056 def test_config_commands_registered(self):
0057 """Config commands should be registered."""
0058 commands = self.get_registered_commands()
0059 assert "describe" in commands
0060 assert "inspect" in commands
0061 assert "validate" in commands
0062
0063 def test_workflow_commands_registered(self):
0064 """Workflow commands should be registered."""
0065 commands = self.get_registered_commands()
0066 assert "optimize" in commands
0067
0068 def test_utility_commands_registered(self):
0069 """Utility commands should be registered."""
0070 commands = self.get_registered_commands()
0071 assert "list" in commands
0072 assert "version" in commands
0073
0074 def test_all_expected_commands_present(self):
0075 """All expected commands should be present."""
0076 expected = {
0077 "describe", "inspect", "validate",
0078 "optimize",
0079 "list", "version"
0080 }
0081 commands = set(self.get_registered_commands())
0082 assert expected.issubset(commands)
0083
0084
0085 class TestConfigCommands:
0086 """Test config inspection commands."""
0087
0088 def setup_method(self):
0089 """Set up test fixtures."""
0090 self.runner = CliRunner()
0091
0092 def test_describe_command_exists(self):
0093 """Describe command should be accessible."""
0094 result = self.runner.invoke(cli_direct, ["describe", "--help"])
0095 assert result.exit_code == 0
0096 assert "Describe the contents" in result.output
0097
0098 def test_inspect_command_exists(self):
0099 """Inspect command should be accessible."""
0100 result = self.runner.invoke(cli_direct, ["inspect", "--help"])
0101 assert result.exit_code == 0
0102 assert "detailed information" in result.output
0103
0104 def test_validate_command_exists(self):
0105 """Validate command should be accessible."""
0106 result = self.runner.invoke(cli_direct, ["validate", "--help"])
0107 assert result.exit_code == 0
0108 assert "Validate" in result.output
0109
0110 def test_describe_missing_file(self):
0111 """Describe should error on missing file."""
0112 result = self.runner.invoke(cli_direct, ["describe", "nonexistent.yml"])
0113 assert result.exit_code != 0
0114
0115 def test_validate_missing_file(self):
0116 """Validate should error on missing file."""
0117 result = self.runner.invoke(cli_direct, ["validate", "nonexistent.yml"])
0118 assert result.exit_code != 0
0119
0120
0121 class TestWorkflowCommands:
0122 """Test workflow execution commands."""
0123
0124 def setup_method(self):
0125 """Set up test fixtures."""
0126 self.runner = CliRunner()
0127
0128 def test_optimize_command_exists(self):
0129 """Optimize command should be accessible."""
0130 result = self.runner.invoke(cli_direct, ["optimize", "--help"])
0131 assert result.exit_code == 0
0132 assert "optimization" in result.output.lower()
0133
0134 def test_optimize_has_validate_only_option(self):
0135 """Optimize should have --validate-only option."""
0136 result = self.runner.invoke(cli_direct, ["optimize", "--help"])
0137 assert "--validate-only" in result.output
0138
0139 def test_optimize_has_verbosity_option(self):
0140 """Optimize should have -v/--verbosity option."""
0141 result = self.runner.invoke(cli_direct, ["optimize", "--help"])
0142 assert "--verbosity" in result.output or "-v" in result.output
0143
0144
0145 class TestUtilityCommands:
0146 """Test utility commands."""
0147
0148 def setup_method(self):
0149 """Set up test fixtures."""
0150 self.runner = CliRunner()
0151
0152 def test_list_command_exists(self):
0153 """List command should be accessible."""
0154 result = self.runner.invoke(cli_direct, ["list", "--help"])
0155 assert result.exit_code == 0
0156
0157 def test_list_no_args(self):
0158 """List without args should show all categories."""
0159 result = self.runner.invoke(cli_direct, ["list"])
0160 assert result.exit_code == 0
0161 assert "Optimizers" in result.output
0162 assert "Templates" in result.output
0163 assert "Problem Types" in result.output
0164
0165 def test_list_optimizers(self):
0166 """List optimizers should show optimizer info."""
0167 result = self.runner.invoke(cli_direct, ["list", "optimizers"])
0168 assert result.exit_code == 0
0169 assert "ax" in result.output
0170 assert "Bayesian" in result.output
0171
0172 def test_list_templates(self):
0173 """List templates should show template info."""
0174 result = self.runner.invoke(cli_direct, ["list", "templates"])
0175 assert result.exit_code == 0
0176 assert "dtlz2" in result.output or "basic" in result.output
0177
0178 def test_list_problems(self):
0179 """List problems should show problem types."""
0180 result = self.runner.invoke(cli_direct, ["list", "problems"])
0181 assert result.exit_code == 0
0182 assert "toy" in result.output or "custom" in result.output
0183
0184 def test_version_command(self):
0185 """Version command should display version."""
0186 result = self.runner.invoke(cli_direct, ["version"])
0187 assert result.exit_code == 0
0188 assert "AID2E" in result.output
0189
0190
0191
0192 class TestHelpOrganization:
0193 """Test that help output is well organized."""
0194
0195 def setup_method(self):
0196 """Set up test fixtures."""
0197 self.runner = CliRunner()
0198
0199 def test_help_shows_command_categories(self):
0200 """Main help should show command categories."""
0201 result = self.runner.invoke(cli_direct, ["--help"])
0202 assert result.exit_code == 0
0203
0204 output_lower = result.output.lower()
0205 assert "configuration" in output_lower or "config" in output_lower
0206 assert "workflow" in output_lower or "execution" in output_lower
0207 assert "utilit" in output_lower
0208
0209 def test_help_shows_all_commands(self):
0210 """Main help should list all commands."""
0211 result = self.runner.invoke(cli_direct, ["--help"])
0212 assert result.exit_code == 0
0213
0214 expected_commands = ["describe", "inspect", "validate", "optimize", "list", "version"]
0215 for cmd in expected_commands:
0216 assert cmd in result.output
0217
0218
0219 class TestBackwardCompatibility:
0220 """Test backward compatibility with existing code."""
0221
0222 def test_entry_point_import(self):
0223 """Entry point import pattern should work."""
0224 from aid2e.cli.aid2e_cli import cli
0225 assert cli is not None
0226 assert callable(cli)
0227
0228 def test_package_import(self):
0229 """Package import pattern should work."""
0230 from aid2e.cli import cli
0231 assert cli is not None
0232 assert callable(cli)
0233
0234 def test_commands_accessible_from_cli(self):
0235 """Commands should be accessible from main cli object."""
0236 assert hasattr(cli_direct, "commands")
0237 assert "describe" in cli_direct.commands
0238 assert "optimize" in cli_direct.commands