import pytest from typer.testing import CliRunner from unittest.mock import Mock, patch import sys from pathlib import Path from loguru import logger # 测试 CLI 命令 runner = CliRunner() def test_cli_init_success(): """测试 init 命令成功执行""" from src.llm_codegen.cli import app # 假设从项目根目录运行测试 # 模拟 CodeGenerator 和其方法,避免实际调用 API with patch('src.llm_codegen.cli.CodeGenerator') as mock_generator: mock_instance = Mock() mock_instance.run = Mock() mock_generator.return_value = mock_instance # 创建一个虚拟的 README 文件用于测试 test_readme = Path("test_readme.md") test_readme.write_text("# Test Project\n\nA test project for CLI.") result = runner.invoke(app, ["init", str(test_readme), "--output", "./test_output"]) # 清理 test_readme.unlink() assert result.exit_code == 0 assert "初始化失败" not in result.stdout mock_generator.assert_called_once() mock_instance.run.assert_called_once_with(test_readme) def test_cli_init_failure_no_readme(): """测试 init 命令当 README 不存在时失败""" from src.llm_codegen.cli import app result = runner.invoke(app, ["init", "nonexistent.md"]) assert result.exit_code != 0 # 应该退出码非零 def test_cli_enhance_success(): """测试 enhance 命令成功执行(简化版,基于工单)""" from src.llm_codegen.cli import app # 模拟依赖文件和环境 with patch('src.llm_codegen.cli.CodeGenerator') as mock_generator, \ patch('src.llm_codegen.cli.Checker') as mock_checker, \ patch('pathlib.Path.exists') as mock_exists: mock_exists.return_value = True # 模拟 design.json 存在 mock_instance = Mock() mock_instance.run_full_check_and_fix = Mock(return_value=True) mock_checker.return_value = mock_instance mock_generator.return_value = Mock() # 创建一个虚拟的工单文件 test_issue = Path("test_feature.issue") test_issue.write_text("name: Add feature\ndescription: Test feature") result = runner.invoke(app, ["enhance", str(test_issue), "--output", "./test_output"]) # 清理 test_issue.unlink() assert result.exit_code == 0 assert "增强失败" not in result.stdout mock_checker.assert_called_once() mock_instance.run_full_check_and_fix.assert_called_once() def test_cli_fix_success(): """测试 fix 命令成功执行(简化版,基于工单)""" from src.llm_codegen.cli import app with patch('src.llm_codegen.cli.CodeGenerator') as mock_generator, \ patch('src.llm_codegen.cli.Checker') as mock_checker, \ patch('pathlib.Path.exists') as mock_exists: mock_exists.return_value = True mock_instance = Mock() mock_instance.run_full_check_and_fix = Mock(return_value=True) mock_checker.return_value = mock_instance mock_generator.return_value = Mock() test_issue = Path("test_bug.issue") test_issue.write_text("name: Fix bug\ndescription: Test bug") result = runner.invoke(app, ["fix", str(test_issue), "--output", "./test_output"]) test_issue.unlink() assert result.exit_code == 0 assert "修复失败" not in result.stdout mock_checker.assert_called_once() mock_instance.run_full_check_and_fix.assert_called_once() def test_cli_help(): """测试 CLI 帮助命令""" from src.llm_codegen.cli import app result = runner.invoke(app, ["--help"]) assert result.exit_code == 0 assert "基于LLM的自动化代码生成与维护工具" in result.stdout # 测试子命令帮助 result = runner.invoke(app, ["init", "--help"]) assert result.exit_code == 0 assert "README.md 文件路径" in result.stdout def test_cli_enhance_no_design(): """测试 enhance 命令当 design.json 不存在时失败""" from src.llm_codegen.cli import app with patch('pathlib.Path.exists') as mock_exists: mock_exists.return_value = False # 模拟 design.json 不存在 test_issue = Path("test_feature.issue") test_issue.write_text("name: Test") result = runner.invoke(app, ["enhance", str(test_issue)]) test_issue.unlink() assert result.exit_code != 0 def test_cli_fix_no_design(): """测试 fix 命令当 design.json 不存在时失败""" from src.llm_codegen.cli import app with patch('pathlib.Path.exists') as mock_exists: mock_exists.return_value = False test_issue = Path("test_bug.issue") test_issue.write_text("name: Test") result = runner.invoke(app, ["fix", str(test_issue)]) test_issue.unlink() assert result.exit_code != 0 if __name__ == "__main__": pytest.main([__file__])