can you look at my current error, im trying to run my test file to check our updates:…
- User
can you look at my current error, im trying to run my test file to check our updates:
import pytest from chat.chat import chat_with_model, process_file_updates, detect_file_updates, calculate_diff, remove_full_content_from_response from app_state import get_app_state from efc.efc_cache import get_file_context from unittest.mock import AsyncMock, MagicMock import os @pytest.fixture def mock_app_state(): class MockAppState: def __init__(self): self.active_project = "/path/to/active_project" self.conversation_history = [] self.file_histories = {} self.token_usage = {} def get_active_project_tree_string(self): return "Mocked project tree" return MockAppState() @pytest.fixture def mock_file_context(): class MockFileContextTracker: def __init__(self): self.tracked_files = {} def generate_context_awareness_prompt(self, app_state) -> str: if not self.tracked_files: return "No files in context." context = "" for relative_path in self.tracked_files: content = self.get_file_content(relative_path) context += f"- **{relative_path}**:\n```\n{content}\n```\n" return context def get_file_content(self, relative_path: str) -> str: return self.tracked_files.get(relative_path, "") def add_or_update_file(self, relative_path: str, content: str, operation: str) -> None: self.tracked_files[relative_path] = content return MockFileContextTracker() def test_detect_file_updates(): assistant_message = "Fresh content for file1.txt: ```diff\n+ Added line\n- Removed line\n```" updates = detect_file_updates(assistant_message) assert updates == {"file1.txt": "+ Added line\n- Removed line"} def test_calculate_diff(): old_content = "Line1\nLine2\nLine3" new_content = "Line1\nLine2 modified\nLine3" diff = calculate_diff(old_content, new_content) expected_diff = """--- +++ @@ -1,3 +1,3 @@ Line1 -Line2 +Line2 modified Line3""" assert diff == expected_diff def test_remove_full_content_from_response(): assistant_message = "Some message. Fresh content for file1.txt: ```diff\n+ Added line\n- Removed line\n``` More text." cleaned_message = remove_full_content_from_response(assistant_message, 'file1.txt') assert cleaned_message == "Some message. More text." @pytest.mark.asyncio async def test_initial_file_addition(mock_app_state, mock_file_context): user_input = "Create file1.txt" # Mock AI client response for file addition ai_client_mock = AsyncMock() ai_client_mock.create_message.return_value = MagicMock( usage=None, extract_response=MagicMock(return_value="Added /path/to/active_project/file1.txt to context.") ) monkeypatch.setattr("chat.chat.AIClientFactory.get_ai_client", lambda x: ai_client_mock) assistant_response = await chat_with_model(user_input, MagicMock(), "gpt-4") # Check that a notification message was appended assert len(mock_app_state.conversation_history) == 2 assert mock_app_state.conversation_history[1]['role'] == 'assistant' assert mock_app_state.conversation_history[1]['content'] == 'Added /path/to/active_project/file1.txt to context.' # Check that FileContextTracker was updated assert mock_file_context.tracked_files["file1.txt"] == "This is the content of file1" @pytest.mark.asyncio async def test_file_update_with_diff(mock_app_state, mock_file_context): # Initial addition mock_app_state.conversation_history = [ {'role': 'user', 'content': 'Create file1.txt'}, {'role': 'assistant', 'content': 'Added /path/to/active_project/file1.txt to context.'} ] mock_file_context.tracked_files["file1.txt"] = "This is the content of file1" user_input = "Update file1.txt" # Mock AI client response for file update new_content = "This is the updated content of file1" ai_client_mock = AsyncMock() ai_client_mock.create_message.return_value = MagicMock( usage=None, extract_response=MagicMock(return_value=f"Fresh content for /path/to/active_project/file1.txt: ```diff\n+ This is the updated content of file1\n```") ) monkeypatch.setattr("chat.chat.AIClientFactory.get_ai_client", lambda x: ai_client_mock) assistant_response = await chat_with_model(user_input, MagicMock(), "gpt-4") # Check that a diff message was appended assert len(mock_app_state.conversation_history) == 4 assert mock_app_state.conversation_history[3]['role'] == 'assistant' assert "### Diff for /path/to/active_project/file1.txt" in mock_app_state.conversation_history[3]['content'] # Check that FileContextTracker was updated assert mock_file_context.tracked_files["file1.txt"] == new_content @pytest.mark.asyncio async def test_multiple_file_operations(mock_app_state, mock_file_context): # Initial additions mock_app_state.conversation_history = [ {'role': 'user', 'content': 'Create file1.txt'}, {'role': 'assistant', 'content': 'Added /path/to/active_project/file1.txt to context.'}, {'role': 'user', 'content': 'Create file2.txt'}, {'role': 'assistant', 'content': 'Added /path/to/active_project/file2.txt to context.'} ] mock_file_context.tracked_files["file1.txt"] = "This is the content of file1" mock_file_context.tracked_files["file2.txt"] = "This is the content of file2" user_input1 = "Update file1.txt" user_input2 = "Update file2.txt" # Mock AI client response for file1.txt update new_content1 = "This is the updated content of file1" ai_client_mock1 = AsyncMock() ai_client_mock1.create_message.return_value = MagicMock( usage=None, extract_response=MagicMock(return_value=f"Fresh content for /path/to/active_project/file1.txt: ```diff\n+ This is the updated content of file1\n```") ) # Mock AI client response for file2.txt update new_content2 = "This is the updated content of file2" ai_client_mock2 = AsyncMock() ai_client_mock2.create_message.return_value = MagicMock( usage=None, extract_response=MagicMock(return_value=f"Fresh content for /path/to/active_project/file2.txt: ```diff\n+ This is the updated content of file2\n```") ) ai_client_factory_mock = MagicMock() ai_client_factory_mock.get_ai_client.side_effect = [ai_client_mock1, ai_client_mock2] monkeypatch.setattr("chat.chat.AIClientFactory.get_ai_client", lambda x: ai_client_factory_mock.get_ai_client()) # Update file1.txt assistant_response1 = await chat_with_model(user_input1, MagicMock(), "gpt-4") # Update file2.txt assistant_response2 = await chat_with_model(user_input2, MagicMock(), "gpt-4") # Check that diffs for both files were appended assert len(mock_app_state.conversation_history) == 6 assert "### Diff for /path/to/active_project/file1.txt" in mock_app_state.conversation_history[3]['content'] assert "### Diff for /path/to/active_project/file2.txt" in mock_app_state.conversation_history[5]['content'] # Check that FileContextTracker was updated assert mock_file_context.tracked_files["file1.txt"] == new_content1 assert mock_file_context.tracked_files["file2.txt"] == new_content2 @pytest.mark.asyncio async def test_conversation_history_alternates_roles(mock_app_state, mock_file_context): history = [ {'role': 'user', 'content': 'Hello'}, {'role': 'assistant', 'content': 'Hi there!'}, {'role': 'user', 'content': 'Show me file1.txt'}, {'role': 'assistant', 'content': 'Added /path/to/active_project/file1.txt to context.'}, {'role': 'user', 'content': 'Update file1.txt'}, {'role': 'assistant', 'content': '### Diff for /path/to/active_project/file1.txt\n```diff\n+ Updated content of file1\n```'}, {'role': 'user', 'content': "What's the weather like?"}, {'role': 'assistant', 'content': "I'm sorry, I don't have real-time weather information."}, ] mock_app_state.conversation_history = history mock_file_context.tracked_files["file1.txt"] = "Updated content of file1" user_input = "Update file1.txt again" # Mock AI client response for file1.txt update new_content = "This is the second update of file1" ai_client_mock = AsyncMock() ai_client_mock.create_message.return_value = MagicMock( usage=None, extract_response=MagicMock(return_value=f"Fresh content for /path/to/active_project/file1.txt: ```diff\n+ This is the second update of file1\n```") ) monkeypatch.setattr("chat.chat.AIClientFactory.get_ai_client", lambda x: ai_client_mock) assistant_response = await chat_with_model(user_input, MagicMock(), "gpt-4") # Check role alternation assert len(mock_app_state.conversation_history) == 8 + 2 # Original 8 + 2 new messages assert mock_app_state.conversation_history[-2]['role'] == 'assistant' assert mock_app_state.conversation_history[-2]['content'].startswith("### Diff for /path/to/active_project/file1.txt") assert mock_app_state.conversation_history[-1]['role'] == 'assistant' assert mock_app_state.conversation_history[-1]['content'] == "I'm sorry, I don't have real-time weather information." @pytest.mark.asyncio async def test_non_file_related_messages_preserved(mock_app_state, mock_file_context): history = [ {'role': 'user', 'content': 'Hello'}, {'role': 'assistant', 'content': 'Hi there!'}, {'role': 'user', 'content': "What's the weather like?"}, {'role': 'assistant', 'content': "I'm sorry, I don't have real-time weather information."}, ] mock_app_state.conversation_history = history user_input = "Update file1.txt" # Mock AI client response for file1.txt update (file does not exist) ai_client_mock = AsyncMock() ai_client_mock.create_message.return_value = MagicMock( usage=None, extract_response=MagicMock(return_value=f"Fresh content for /path/to/active_project/file1.txt: ```diff\n+ New content of file1\n```") ) monkeypatch.setattr("chat.chat.AIClientFactory.get_ai_client", lambda x: ai_client_mock) assistant_response = await chat_with_model(user_input, MagicMock(), "gpt-4") # Check that non-file messages are preserved and new diff is added assert len(mock_app_state.conversation_history) == 4 + 2 # Original 4 + 2 new messages assert '### Diff for /path/to/active_project/file1.txt' in mock_app_state.conversation_history[-2]['content'] assert 'Fresh content for /path/to/active_project/file1.txt: ```diff\n+ New content of file1\n```' not in mock_app_state.conversation_history[-2]['content'] assert mock_app_state.conversation_history[-1]['role'] == 'assistant' assert mock_app_state.conversation_history[-1]['content'] == "I'm sorry, I don't have real-time weather information." def test_remove_full_content_from_response(): assistant_message = "Some message. Fresh content for file1.txt: ```diff\n+ Added line\n- Removed line\n``` More text." cleaned_message = remove_full_content_from_response(assistant_message, 'file1.txt') assert cleaned_message == "Some message. More text." @pytest.mark.asyncio async def test_file_addition_and_update_sequence(mock_app_state, mock_file_context): # Sequence: Create file, update file, update file again user_input_create = "Create file1.txt" user_input_update1 = "Update file1.txt" user_input_update2 = "Update file1.txt again" # Mock AI client responses ai_client_mock_create = AsyncMock() ai_client_mock_create.create_message.return_value = MagicMock( usage=None, extract_response=MagicMock(return_value="Added /path/to/active_project/file1.txt to context.") ) ai_client_mock_update1 = AsyncMock() ai_client_mock_update1.create_message.return_value = MagicMock( usage=None, extract_response=MagicMock(return_value=f"Fresh content for /path/to/active_project/file1.txt: ```diff\n+ Updated line in file1.txt\n```") ) ai_client_mock_update2 = AsyncMock() ai_client_mock_update2.create_message.return_value = MagicMock( usage=None, extract_response=MagicMock(return_value=f"Fresh content for /path/to/active_project/file1.txt: ```diff\n+ Second update in file1.txt\n```") ) ai_client_factory_mock = MagicMock() ai_client_factory_mock.get_ai_client.side_effect = [ai_client_mock_create, ai_client_mock_update1, ai_client_mock_update2] monkeypatch.setattr("chat.chat.AIClientFactory.get_ai_client", lambda x: ai_client_factory_mock.get_ai_client()) # Create file1.txt assistant_response_create = await chat_with_model(user_input_create, MagicMock(), "gpt-4") assert len(mock_app_state.conversation_history) == 2 assert mock_app_state.conversation_history[1]['content'] == 'Added /path/to/active_project/file1.txt to context.' assert mock_file_context.tracked_files["file1.txt"] == "Added /path/to/active_project/file1.txt to context." # Update file1.txt first time assistant_response_update1 = await chat_with_model(user_input_update1, MagicMock(), "gpt-4") assert len(mock_app_state.conversation_history) == 4 assert "### Diff for /path/to/active_project/file1.txt" in mock_app_state.conversation_history[3]['content'] assert mock_file_context.tracked_files["file1.txt"] == "Updated line in file1.txt" # Update file1.txt second time assistant_response_update2 = await chat_with_model(user_input_update2, MagicMock(), "gpt-4") assert len(mock_app_state.conversation_history) == 6 assert "### Diff for /path/to/active_project/file1.txt" in mock_app_state.conversation_history[5]['content'] assert mock_file_context.tracked_files["file1.txt"] == "Second update in file1.txt" - Assistant