"""Tests for interactive editor.""" import pytest from pathlib import Path from unittest.mock import Mock, patch from src.confgen.editor import ( is_secret_variable, is_boolean_variable, is_numeric_variable, get_prompt_for_var, ) class TestEditorHelpers: """Tests for editor helper functions.""" def test_is_secret_variable_with_secret(self): """Test detecting secret variables.""" assert is_secret_variable("SECRET_PASSWORD") is True assert is_secret_variable("API_KEY") is True assert is_secret_variable("DB_PASSWORD") is True assert is_secret_variable("PRIVATE_KEY") is True assert is_secret_variable("AUTH_TOKEN") is True def test_is_secret_variable_with_normal(self): """Test that normal variables are not detected as secrets.""" assert is_secret_variable("APP_NAME") is False assert is_secret_variable("PORT") is False assert is_secret_variable("HOST") is False assert is_secret_variable("DEBUG") is False def test_is_boolean_variable_with_bool_names(self): """Test detecting boolean variables by name.""" assert is_boolean_variable("DEBUG", "") is True assert is_boolean_variable("ENABLED", "") is True assert is_boolean_variable("DISABLED", "") is True assert is_boolean_variable("VERBOSE", "") is True def test_is_boolean_variable_with_bool_values(self): """Test detecting boolean variables by value.""" assert is_boolean_variable("SETTING", "true") is True assert is_boolean_variable("SETTING", "false") is True assert is_boolean_variable("SETTING", "yes") is True assert is_boolean_variable("SETTING", "no") is True def test_is_boolean_variable_with_normal(self): """Test that normal variables are not detected as booleans.""" assert is_boolean_variable("TIMEOUT", "30") is False assert is_boolean_variable("NAME", "app") is False def test_is_numeric_variable_with_port(self): """Test detecting port variables.""" assert is_numeric_variable("PORT", "") is True assert is_numeric_variable("DB_PORT", "") is True def test_is_numeric_variable_with_number_value(self): """Test detecting numeric variables by value.""" assert is_numeric_variable("SETTING", "8080") is True assert is_numeric_variable("SETTING", "30.5") is True def test_is_numeric_variable_with_normal(self): """Test that normal variables are not detected as numeric.""" assert is_numeric_variable("NAME", "app") is False assert is_numeric_variable("HOST", "localhost") is False def test_get_prompt_for_var_secret(self): """Test getting prompt for secret variable.""" prompt = get_prompt_for_var("SECRET_PASSWORD", "") assert "[secret]" in prompt def test_get_prompt_for_var_boolean(self): """Test getting prompt for boolean variable.""" prompt = get_prompt_for_var("DEBUG", "true") assert "[boolean]" in prompt def test_get_prompt_for_var_numeric(self): """Test getting prompt for numeric variable.""" prompt = get_prompt_for_var("PORT", "8080") assert "[number]" in prompt def test_get_prompt_for_var_normal(self): """Test getting prompt for normal variable.""" prompt = get_prompt_for_var("APP_NAME", "myapp") assert prompt == "APP_NAME" class TestInteractiveEditor: """Tests for InteractiveEditor class.""" def test_init_with_valid_params(self): """Test initializing InteractiveEditor.""" mock_core = Mock() mock_core.get_template.return_value = Mock( path=Path("/fake/template.j2"), ) mock_core.get_environment.return_value = Mock(variables={"VAR1": "val1"}) from src.confgen.editor import InteractiveEditor with patch.object(Path, "read_text", return_value="{{VAR1}}"): editor = InteractiveEditor(mock_core, "test_template", "dev") assert editor.template_name == "test_template" assert editor.environment == "dev" def test_init_with_nonexistent_template(self): """Test initializing InteractiveEditor with nonexistent template.""" mock_core = Mock() mock_core.get_template.return_value = None from src.confgen.editor import InteractiveEditor with pytest.raises(ValueError, match="Template 'nonexistent' not found"): InteractiveEditor(mock_core, "nonexistent", "dev") class TestConfigEditor: """Tests for ConfigEditor class.""" def test_init(self): """Test initializing ConfigEditor.""" from src.confgen.editor import ConfigEditor editor = ConfigEditor() assert editor.values == {}