"""Unit tests for CommitService.""" import pytest from unittest.mock import AsyncMock, MagicMock from memory_manager.core.services import CommitService @pytest.fixture def mock_repository(): return AsyncMock() @pytest.fixture def commit_service(mock_repository): return CommitService(mock_repository) class TestCommitService: @pytest.mark.asyncio async def test_create_commit(self, commit_service, mock_repository): mock_commit = MagicMock() mock_commit.to_dict.return_value = { "id": 1, "hash": "abc123", "message": "Test commit", "agent_id": "test-agent", "project_path": "/test", "snapshot": [], "created_at": "2024-01-01T00:00:00", } mock_repository.get_all_entries_snapshot = AsyncMock(return_value=[]) mock_repository.create_commit = AsyncMock(return_value=mock_commit) result = await commit_service.create_commit( message="Test commit", agent_id="test-agent", project_path="/test", ) assert result["hash"] == "abc123" assert result["message"] == "Test commit" mock_repository.create_commit.assert_called_once() @pytest.mark.asyncio async def test_get_commit(self, commit_service, mock_repository): mock_commit = MagicMock() mock_commit.to_dict.return_value = {"id": 1, "hash": "abc123"} mock_repository.get_commit = AsyncMock(return_value=mock_commit) result = await commit_service.get_commit("abc123") assert result["hash"] == "abc123" mock_repository.get_commit.assert_called_once_with("abc123") @pytest.mark.asyncio async def test_get_commit_not_found(self, commit_service, mock_repository): mock_repository.get_commit = AsyncMock(return_value=None) result = await commit_service.get_commit("nonexistent") assert result is None @pytest.mark.asyncio async def test_list_commits(self, commit_service, mock_repository): mock_commits = [ MagicMock(to_dict=lambda: {"id": 1, "hash": "abc123"}), MagicMock(to_dict=lambda: {"id": 2, "hash": "def456"}), ] mock_repository.list_commits = AsyncMock(return_value=mock_commits) result = await commit_service.list_commits() assert len(result) == 2 assert result[0]["hash"] == "abc123" @pytest.mark.asyncio async def test_diff(self, commit_service, mock_repository): mock_commit1 = MagicMock() mock_commit1.to_dict.return_value = {"id": 1, "hash": "abc123"} mock_commit1.snapshot = [{"id": 1, "title": "Entry 1"}] mock_commit2 = MagicMock() mock_commit2.to_dict.return_value = {"id": 2, "hash": "def456"} mock_commit2.snapshot = [{"id": 1, "title": "Entry 1 Updated"}, {"id": 2, "title": "Entry 2"}] mock_repository.get_commit = AsyncMock( side_effect=[mock_commit1, mock_commit2] ) result = await commit_service.diff("abc123", "def456") assert result is not None assert len(result["modified"]) == 1 assert len(result["added"]) == 1 @pytest.mark.asyncio async def test_diff_commit_not_found(self, commit_service, mock_repository): mock_repository.get_commit = AsyncMock(return_value=None) result = await commit_service.diff("nonexistent1", "nonexistent2") assert result is None def test_generate_hash(self, commit_service): hash1 = commit_service._generate_hash("test data") hash2 = commit_service._generate_hash("test data") hash3 = commit_service._generate_hash("different data") assert hash1 == hash2 assert hash1 != hash3 assert len(hash1) == 40