Spaces:
Sleeping
Sleeping
| """Unit tests for UI components and logic.""" | |
| import unittest.mock as mock | |
| import tempfile | |
| import os | |
| from src.ui import AdaAssistantUI | |
| class TestFileSelectionHandler: | |
| """Test cases for file selection handler logic.""" | |
| def test_handle_non_ada_file_returns_empty_results(self): | |
| """Test that non-Ada files return empty results and hidden components.""" | |
| # Create mock project handler | |
| mock_project_handler = mock.Mock() | |
| # Create UI instance | |
| ui = AdaAssistantUI(mock_project_handler) | |
| # Test with Python file | |
| result = ui.handle_file_selection("test.py") | |
| # Should return empty strings and False visibility for all components | |
| analysis_result, python_code, unit_tests, analysis_visible, python_visible, tests_visible = result | |
| assert analysis_result == "" | |
| assert python_code == "" | |
| assert unit_tests == "" | |
| assert analysis_visible.visible == False | |
| assert python_visible.visible == False | |
| assert tests_visible.visible == False | |
| # Ensure no project handler methods were called | |
| mock_project_handler.process_ada_file.assert_not_called() | |
| def test_handle_empty_file_selection_returns_empty_results(self): | |
| """Test that empty file selection returns empty results.""" | |
| # Create mock project handler | |
| mock_project_handler = mock.Mock() | |
| # Create UI instance | |
| ui = AdaAssistantUI(mock_project_handler) | |
| # Test with None/empty selection | |
| result = ui.handle_file_selection(None) | |
| # Should return empty strings and False visibility for all components | |
| analysis_result, python_code, unit_tests, analysis_visible, python_visible, tests_visible = result | |
| assert analysis_result == "" | |
| assert python_code == "" | |
| assert unit_tests == "" | |
| assert analysis_visible.visible == False | |
| assert python_visible.visible == False | |
| assert tests_visible.visible == False | |
| # Ensure no project handler methods were called | |
| mock_project_handler.process_ada_file.assert_not_called() | |
| def test_handle_various_non_ada_file_extensions(self): | |
| """Test that various non-Ada file extensions are properly rejected.""" | |
| # Create mock project handler | |
| mock_project_handler = mock.Mock() | |
| # Create UI instance | |
| ui = AdaAssistantUI(mock_project_handler) | |
| # Test various non-Ada file types | |
| non_ada_files = ["test.py", "main.c", "config.txt", "data.json", "readme.md", "script.sh"] | |
| for file_path in non_ada_files: | |
| result = ui.handle_file_selection(file_path) | |
| analysis_result, python_code, unit_tests, analysis_visible, python_visible, tests_visible = result | |
| # All should return empty results | |
| assert analysis_result == "", f"Failed for {file_path}" | |
| assert python_code == "", f"Failed for {file_path}" | |
| assert unit_tests == "", f"Failed for {file_path}" | |
| assert analysis_visible.visible == False, f"Failed for {file_path}" | |
| assert python_visible.visible == False, f"Failed for {file_path}" | |
| assert tests_visible.visible == False, f"Failed for {file_path}" | |
| # Ensure no project handler methods were called for any file | |
| mock_project_handler.process_ada_file.assert_not_called() | |
| class TestZipUploadHandler: | |
| """Test cases for zip upload handler logic.""" | |
| def test_handle_zip_upload_with_none_file(self): | |
| """Test that None file upload returns appropriate message.""" | |
| # Create mock project handler | |
| mock_project_handler = mock.Mock() | |
| # Create UI instance | |
| ui = AdaAssistantUI(mock_project_handler) | |
| result = ui.handle_zip_upload(None) | |
| # Should return None path and error message | |
| extracted_path, message = result | |
| assert extracted_path is None | |
| assert message == "No zip file uploaded" | |
| def test_handle_zip_upload_with_invalid_file(self): | |
| """Test that invalid zip file returns error message.""" | |
| # Create mock project handler | |
| mock_project_handler = mock.Mock() | |
| mock_project_handler.unzip_project.side_effect = Exception("Bad zip file") | |
| # Create UI instance | |
| ui = AdaAssistantUI(mock_project_handler) | |
| # Create a mock file object with a non-zip file | |
| mock_file = mock.Mock() | |
| # Create a temporary text file | |
| with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as temp_file: | |
| temp_file.write("This is not a zip file") | |
| mock_file.name = temp_file.name | |
| try: | |
| result = ui.handle_zip_upload(mock_file) | |
| # extract_project catches the exception and returns error string | |
| # handle_zip_upload treats this as a valid path | |
| extracted_path, message = result | |
| assert "Error processing file: Bad zip file" in extracted_path | |
| assert "Project extracted to:" in message | |
| finally: | |
| # Cleanup | |
| os.unlink(mock_file.name) | |
| def test_handle_zip_upload_with_nonexistent_file(self): | |
| """Test that non-existent file returns error message.""" | |
| # Create mock project handler | |
| mock_project_handler = mock.Mock() | |
| mock_project_handler.unzip_project.side_effect = Exception("File not found") | |
| # Create UI instance | |
| ui = AdaAssistantUI(mock_project_handler) | |
| # Create a mock file object with non-existent path | |
| mock_file = mock.Mock() | |
| mock_file.name = "/path/that/does/not/exist.zip" | |
| result = ui.handle_zip_upload(mock_file) | |
| # extract_project catches the exception and returns error string | |
| # handle_zip_upload treats this as a valid path | |
| extracted_path, message = result | |
| assert "Error processing file: File not found" in extracted_path | |
| assert "Project extracted to:" in message |