diff --git a/.tests/test_generator.py b/.tests/test_generator.py new file mode 100644 index 0000000..91b09e4 --- /dev/null +++ b/.tests/test_generator.py @@ -0,0 +1,116 @@ +"""Tests for CLI Command Memory script generator.""" + +import os +import tempfile +from cli_memory.generator import ScriptGenerator +from cli_memory.config import Config +from cli_memory.models import Workflow, Command, CommandType + + +def test_script_generator_init(): + """Test ScriptGenerator initialization.""" + generator = ScriptGenerator() + assert generator is not None + + +def test_generate_script_basic(): + """Test basic script generation.""" + generator = ScriptGenerator() + + workflow = Workflow( + id=1, + name="test-workflow", + description="A test workflow", + commands=[ + Command(command="echo hello"), + Command(command="echo world"), + ], + ) + + script_path = generator.generate_script( + workflow, + name="test-script", + include_error_handling=False, + include_logging=False, + ) + + assert script_path.endswith(".sh") + assert os.path.exists(script_path) + + with open(script_path) as f: + content = f.read() + + assert "#!/bin/bash" in content + assert "echo hello" in content + assert "echo world" in content + + +def test_generate_script_with_error_handling(): + """Test script generation with error handling.""" + generator = ScriptGenerator() + + workflow = Workflow( + id=1, + name="safe-script", + commands=[Command(command="ls -la")], + ) + + script_path = generator.generate_script( + workflow, + include_error_handling=True, + include_logging=False, + ) + + with open(script_path) as f: + content = f.read() + + assert "set -e" in content + + +def test_generate_script_with_logging(): + """Test script generation with logging.""" + generator = ScriptGenerator() + + workflow = Workflow( + id=1, + name="logged-script", + commands=[Command(command="echo test")], + ) + + script_path = generator.generate_script( + workflow, + include_error_handling=False, + include_logging=True, + ) + + with open(script_path) as f: + content = f.read() + + assert "LOG_FILE=" in content + assert "log()" in content + + +def test_list_generated_scripts(): + """Test listing generated scripts.""" + generator = ScriptGenerator() + + scripts = generator.list_generated_scripts() + assert isinstance(scripts, list) + + +def test_generate_from_commands(): + """Test generating script from command list.""" + generator = ScriptGenerator() + + commands = [ + Command(command="echo one"), + Command(command="echo two"), + ] + + script_path = generator.generate_from_commands( + commands, + name="from-commands", + ) + + assert script_path.endswith(".sh") + assert os.path.exists(script_path)