""" Test TDD workflow integration for Issue #11: Setup TDD workspace infrastructure This test validates the complete TDD workflow from workspace creation through test generation to completion and cleanup. Issue Reference: #11 - Setup TDD workspace infrastructure """ import pytest import os import subprocess import tempfile import shutil from pathlib import Path from unittest.mock import patch, MagicMock class TestTDDWorkflowIntegration: """Test complete TDD workflow integration.""" def setup_method(self): """Set up test environment.""" self.original_cwd = os.getcwd() self.test_dir = tempfile.mkdtemp() os.chdir(self.test_dir) def teardown_method(self): """Clean up test environment.""" os.chdir(self.original_cwd) if os.path.exists(self.test_dir): shutil.rmtree(self.test_dir) @patch('tddai.IssueFetcher.fetch_issue') def test_complete_tdd_workflow_cycle(self, mock_fetch): """Test the complete TDD workflow from start to finish.""" # Arrange mock_fetch.return_value = { 'number': 11, 'title': 'Setup TDD workspace infrastructure', 'body': 'Complete workflow test', 'state': 'open' } # Simulate the make commands workflow from tddai import WorkspaceManager workspace_manager = WorkspaceManager('.markitect_workspace') # Act & Assert - Workspace Creation issue_data = mock_fetch.return_value workspace_path = workspace_manager.create_workspace(issue_data) assert workspace_path.exists() # Act & Assert - Status Check status = workspace_manager.get_workspace_status() assert status.issue_number == 11 assert status.title == 'Setup TDD workspace infrastructure' # Act & Assert - Test Generation (simulate multiple tests) test_files = [ 'test_issue_11_basic.py', 'test_issue_11_advanced.py' ] for test_file in test_files: test_path = workspace_path / 'tests' / test_file test_path.write_text(f'# Test file: {test_file}\ndef test_example(): pass') # Verify tests are tracked status = workspace_manager.get_workspace_status() assert len(status.generated_tests) == 2 # Act & Assert - Workspace Completion main_tests_dir = Path('tests') main_tests_dir.mkdir(exist_ok=True) workspace_manager.finish_workspace(main_tests_dir) # Verify tests moved to main for test_file in test_files: main_test_path = main_tests_dir / test_file assert main_test_path.exists() # Verify workspace cleaned up assert not workspace_path.exists() def test_workspace_git_exclusion(self): """Test that workspace files are properly excluded from git.""" # Arrange gitignore_path = Path('.gitignore') gitignore_content = """ # MarkiTect issue workspace (temporary development files) .markitect_workspace/ """ gitignore_path.write_text(gitignore_content) # Create workspace directory workspace_dir = Path('.markitect_workspace') workspace_dir.mkdir() test_file = workspace_dir / 'test_file.py' test_file.write_text('# Test content') # Act - Check git status would ignore workspace files # This simulates what git status would show gitignore_patterns = ['.markitect_workspace/'] # Assert assert any(str(test_file).startswith(pattern.rstrip('/')) for pattern in gitignore_patterns) @patch('subprocess.run') def test_makefile_integration_commands(self, mock_run): """Test that Makefile commands integrate properly with workspace system.""" # Arrange mock_run.return_value = MagicMock(returncode=0, stdout='Success', stderr='') # Act & Assert - Test make tdd-start command integration from tddai_cli import main as cli_main # Simulate CLI call for tdd-start with patch('sys.argv', ['tddai_cli.py', 'start-issue', '11']): with patch('tddai.IssueFetcher.fetch_issue') as mock_fetch: mock_fetch.return_value = { 'number': 11, 'title': 'Setup TDD workspace infrastructure', 'body': 'Makefile integration test', 'state': 'open' } # This would normally create workspace # Just verify the CLI interface works assert callable(cli_main) def test_error_handling_invalid_workflow_states(self): """Test error handling for invalid workflow states.""" from tddai import WorkspaceManager, WorkspaceError workspace_manager = WorkspaceManager('.markitect_workspace') # Test adding test without workspace with pytest.raises(WorkspaceError, match="No active workspace"): workspace_manager.add_test_to_workspace("test_file.py", "test content") # Test finishing workspace without workspace with pytest.raises(WorkspaceError, match="No active workspace"): workspace_manager.finish_workspace(Path('tests')) # Test getting status without workspace status = workspace_manager.get_workspace_status() assert status.issue_number is None def test_workspace_status_monitoring_accuracy(self): """Test that workspace status monitoring provides accurate information.""" # Arrange from tddai import WorkspaceManager workspace_manager = WorkspaceManager('.markitect_workspace') issue_data = { 'number': 11, 'title': 'Setup TDD workspace infrastructure', 'body': 'Status monitoring test', 'state': 'open' } # Act workspace_path = workspace_manager.create_workspace(issue_data) # Add some test files test_files = ['test_a.py', 'test_b.py', 'test_c.py'] for test_file in test_files: test_path = workspace_path / 'tests' / test_file test_path.write_text(f'# {test_file}') status = workspace_manager.get_workspace_status() # Assert assert status.issue_number == 11 assert status.title == 'Setup TDD workspace infrastructure' assert len(status.generated_tests) == 3 assert all(test_file in status.generated_tests for test_file in test_files) if __name__ == '__main__': pytest.main([__file__, '-v'])