"""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: with patch("git_commit_generator.changelog_generator.get_ollama_client") as mock_ollama: 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" mock_ollama.return_value = ollama_client with patch("git_commit_generator.changelog_generator.get_git_utils") as mock_git: git_utils = MagicMock() git_utils.get_commit_history.return_value = [] mock_git.return_value = git_utils generator = ChangelogGenerator( config=config, ollama_client=ollama_client, ) generator.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 formatting simple changelog.""" grouped = { "feat": [ {"type": "feat", "scope": "api", "description": "add endpoint"}, ], "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 "**fix(db):** fix bug" in result def test_generate_simple_no_commits_raises_error(self, changelog_generator): """Test that simple generation raises error with no commits.""" changelog_generator.git_utils.get_conventional_commits.return_value = [] with pytest.raises(ValueError, match="No conventional commits found"): changelog_generator.generate_simple() def test_format_commits_for_prompt(self, changelog_generator): """Test formatting commits for LLM prompt.""" commits = [ { "hash": "abc1234", "message": "feat: add feature", "author": "Test User", "date": "2024-01-15T10:30:00", }, ] result = changelog_generator._format_commits_for_prompt(commits) assert "[abc1234] feat: add feature" in result assert "Test User" in result def test_clean_changelog(self, changelog_generator): """Test cleaning changelog output.""" raw_changelog = """``` # Changelog ## Features - Feature 1 ```""" result = changelog_generator._clean_changelog(raw_changelog) assert "```" not in result assert "# Changelog" in result assert "## Features" in result def test_generate_no_commits_raises_error(self, changelog_generator): """Test that generation raises error with no commits.""" changelog_generator.git_utils.get_commit_history.return_value = [] with pytest.raises(ValueError, match="No commits found"): changelog_generator.generate()