"""Tests for the CLI module.""" import tempfile import yaml from click.testing import CliRunner from openapi_mock.cli.cli import main, validate, info, generate class TestCLI: """Tests for CLI commands.""" def test_cli_help(self): """Test CLI help output.""" runner = CliRunner() result = runner.invoke(main, ["--help"]) assert result.exit_code == 0 assert "OpenAPI Mock Server" in result.output def test_validate_valid_spec(self, temp_spec_file): """Test validate command with valid spec.""" runner = CliRunner() result = runner.invoke(validate, [temp_spec_file]) assert result.exit_code == 0 assert "Valid OpenAPI spec" in result.output def test_validate_nonexistent_file(self): """Test validate command with non-existent file.""" runner = CliRunner() result = runner.invoke(validate, ["/nonexistent/spec.yaml"]) assert result.exit_code != 0 assert "not found" in result.output.lower() def test_info_command(self, temp_spec_file, sample_openapi_spec): """Test info command displays spec information.""" runner = CliRunner() result = runner.invoke(info, [temp_spec_file]) assert result.exit_code == 0 assert "Test API" in result.output def test_generate_command(self, temp_spec_file): """Test generate command creates output.""" runner = CliRunner() with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f: result = runner.invoke(generate, [temp_spec_file, "--output", f.name]) assert result.exit_code == 0 assert "Generated mock server" in result.output def test_generate_to_stdout(self, temp_spec_file): """Test generate command outputs to stdout.""" runner = CliRunner() result = runner.invoke(generate, [temp_spec_file]) assert result.exit_code == 0 assert "Auto-generated mock server" in result.output def test_start_command_help(self): """Test start command help output.""" runner = CliRunner() result = runner.invoke(main, ["start", "--help"]) assert result.exit_code == 0 assert "--port" in result.output assert "--host" in result.output assert "--delay" in result.output def test_start_command_with_delay_option(self, temp_spec_file): """Test start command accepts delay option.""" runner = CliRunner() result = runner.invoke(main, ["start", temp_spec_file, "--delay", "0.5", "--port", "9999"]) assert result.exit_code == 0 assert "Response delay" in result.output def test_start_command_with_delay_range(self, temp_spec_file): """Test start command accepts delay range.""" runner = CliRunner() result = runner.invoke(main, ["start", temp_spec_file, "--delay", "0.1,1.0", "--port", "9998"]) assert result.exit_code == 0 assert "Response delay" in result.output def test_start_command_with_invalid_delay(self, temp_spec_file): """Test start command rejects invalid delay.""" runner = CliRunner() result = runner.invoke(main, ["start", temp_spec_file, "--delay", "invalid"]) assert result.exit_code != 0 assert "Invalid delay format" in result.output def test_start_command_with_auth(self, temp_spec_file): """Test start command accepts auth option.""" runner = CliRunner() result = runner.invoke(main, ["start", temp_spec_file, "--auth", "bearer", "--port", "9997"]) assert result.exit_code == 0 assert "Authentication enabled" in result.output def test_start_command_with_watch(self, temp_spec_file): """Test start command with watch flag.""" runner = CliRunner() result = runner.invoke(main, ["start", temp_spec_file, "--watch", "--port", "9996"]) assert result.exit_code == 0 assert "Watching" in result.output def test_port_validation(self, temp_spec_file): """Test port validation rejects invalid ports.""" runner = CliRunner() result = runner.invoke(main, ["start", temp_spec_file, "--port", "70000"]) assert result.exit_code != 0 assert "between 1 and 65535" in result.output def test_auth_type_validation(self, temp_spec_file): """Test auth type validation.""" runner = CliRunner() result = runner.invoke(main, ["start", temp_spec_file, "--auth", "invalid"]) assert result.exit_code != 0