"""Tests for changelog_generator.py.""" from unittest.mock import MagicMock, patch import pytest from git_commit_generator.changelog_generator import ChangelogGenerator class TestChangelogGenerator: """Test cases for ChangelogGenerator.""" @pytest.fixture def changelog_generator(self): """Create a ChangelogGenerator instance with mocked dependencies.""" with patch("git_commit_generator.changelog_generator.get_config") as mock_config: config = MagicMock() config.ollama_host = "http://localhost:11434" config.ollama_model = "llama3" config.read_prompt.return_value = "Generate a changelog." mock_config.return_value = config ollama_client = MagicMock() ollama_client.generate_changelog.return_value = "# Changelog\n\n## Features\n- New feature" git_utils = MagicMock() git_utils.get_commit_history.return_value = [] generator = ChangelogGenerator( config=config, ollama_client=ollama_client, git_utils=git_utils, ) yield generator def test_group_commits_by_type(self, changelog_generator): """Test grouping commits by type.""" commits = [ {"type": "feat", "scope": "api", "description": "add endpoint"}, {"type": "fix", "scope": "db", "description": "fix bug"}, {"type": "feat", "scope": "ui", "description": "add button"}, ] result = changelog_generator._group_commits_by_type(commits) assert "feat" in result assert "fix" in result assert len(result["feat"]) == 2 assert len(result["fix"]) == 1 def test_format_simple_changelog(self, changelog_generator): """Test simple changelog formatting.""" grouped = { "feat": [ {"type": "feat", "scope": "api", "description": "add endpoint"}, {"type": "feat", "scope": "ui", "description": "add button"}, ], "fix": [ {"type": "fix", "scope": "db", "description": "fix bug"}, ], } result = changelog_generator._format_simple_changelog(grouped) assert "# Changelog" in result assert "## Features" in result assert "## Bug Fixes" in result assert "**feat(api):** add endpoint" in result assert "**feat(ui):** add button" in result assert "**fix(db):** fix bug" in result def test_generate_simple_no_commits_raises_error(self, changelog_generator): """Test that generate_simple raises error when no commits.""" changelog_generator.git_utils.get_conventional_commits.return_value = [] with pytest.raises(ValueError, match="No conventional commits"): changelog_generator.generate_simple() def test_format_commits_for_prompt(self, changelog_generator): """Test commit formatting for LLM prompt.""" commits = [ {"hash": "abc123", "message": "feat: add feature", "author": "John", "date": "2024-01-01"}, {"hash": "def456", "message": "fix: fix bug", "author": "Jane", "date": "2024-01-02"}, ] result = changelog_generator._format_commits_for_prompt(commits) assert "[abc123] feat: add feature" in result assert "[def456] fix: fix bug" in result assert "John" in result assert "2024-01-01" in result def test_clean_changelog(self, changelog_generator): """Test changelog cleaning.""" raw = """ # Changelog ## Features - New feature ```python print("hello") ``` """ result = changelog_generator._clean_changelog(raw) assert "```python" not in result assert "# Changelog" in result assert "## Features" in result def test_generate_no_commits_raises_error(self, changelog_generator): """Test that generate raises error when no commits.""" changelog_generator.git_utils.get_commit_history.return_value = [] with pytest.raises(ValueError, match="No commits found"): changelog_generator.generate()