Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /alkimya_gathering /tests /test_api_workspace.py
| """ | |
| Tests for Workspace API Router. | |
| Covers: | |
| - Workspace info endpoints | |
| - File management (list, read, write, delete) | |
| - Git operations (status, commits, diff, branches) | |
| - Activity tracking | |
| - Python code execution | |
| """ | |
| import os | |
| import tempfile | |
| from pathlib import Path | |
| from unittest.mock import patch, MagicMock | |
| import pytest | |
| from fastapi.testclient import TestClient | |
| from gathering.api.main import app | |
| from gathering.workspace.activity_tracker import ActivityType | |
| def test_workspace(): | |
| """Create a temporary workspace for testing.""" | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| # Create sample files | |
| workspace_path = Path(tmpdir) | |
| (workspace_path / "README.md").write_text("# Test Project\n") | |
| (workspace_path / "src").mkdir() | |
| (workspace_path / "src" / "main.py").write_text("def hello():\n return 'world'\n") | |
| # Mock get_project_path to return our temp workspace | |
| with patch("gathering.api.routers.workspace.get_project_path", return_value=str(workspace_path)): | |
| yield str(workspace_path) | |
| class TestWorkspaceInfo: | |
| """Test workspace info endpoints.""" | |
| def test_get_workspace_info(self, test_workspace): | |
| """Test getting workspace information.""" | |
| client = TestClient(app) | |
| response = client.get("/workspace/1/info") | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert "type" in data | |
| assert "path" in data | |
| assert "capabilities" in data | |
| assert isinstance(data["capabilities"], list) | |
| class TestFileManagement: | |
| """Test file management endpoints.""" | |
| def test_list_files(self, test_workspace): | |
| """Test listing files in workspace.""" | |
| client = TestClient(app) | |
| response = client.get("/workspace/1/files") | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert "name" in data | |
| assert "children" in data | |
| def test_list_files_without_git_status(self, test_workspace): | |
| """Test listing files without git status.""" | |
| client = TestClient(app) | |
| response = client.get("/workspace/1/files?include_git_status=false") | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert "children" in data | |
| def test_read_file_success(self, test_workspace): | |
| """Test reading a file.""" | |
| client = TestClient(app) | |
| response = client.get("/workspace/1/file?path=README.md") | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert "content" in data | |
| assert "# Test Project" in data["content"] | |
| def test_read_file_not_found(self, test_workspace): | |
| """Test reading non-existent file.""" | |
| client = TestClient(app) | |
| response = client.get("/workspace/1/file?path=nonexistent.txt") | |
| assert response.status_code == 404 | |
| assert "not found" in response.json()["detail"].lower() | |
| def test_write_file_new(self, test_workspace): | |
| """Test creating a new file.""" | |
| client = TestClient(app) | |
| response = client.put( | |
| "/workspace/1/file?path=new_file.txt", | |
| json={ | |
| "content": "New content", | |
| "create_backup": False, | |
| }, | |
| ) | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert data["success"] is True | |
| # Verify file was created | |
| new_file = Path(test_workspace) / "new_file.txt" | |
| assert new_file.exists() | |
| assert new_file.read_text() == "New content" | |
| def test_write_file_update_existing(self, test_workspace): | |
| """Test updating an existing file.""" | |
| client = TestClient(app) | |
| response = client.put( | |
| "/workspace/1/file?path=README.md", | |
| json={ | |
| "content": "# Updated README\n\nNew content here.", | |
| "create_backup": True, | |
| }, | |
| ) | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert data["success"] is True | |
| def test_delete_file_success(self, test_workspace): | |
| """Test deleting a file.""" | |
| client = TestClient(app) | |
| # First verify file exists | |
| test_file = Path(test_workspace) / "src" / "main.py" | |
| assert test_file.exists() | |
| response = client.delete("/workspace/1/file?path=src/main.py") | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert data["success"] is True | |
| # Verify file was deleted | |
| assert not test_file.exists() | |
| def test_delete_file_not_found(self, test_workspace): | |
| """Test deleting non-existent file.""" | |
| client = TestClient(app) | |
| response = client.delete("/workspace/1/file?path=nonexistent.txt") | |
| assert response.status_code == 404 | |
| class TestGitOperations: | |
| """Test Git-related endpoints.""" | |
| def test_get_git_status_not_repo(self, test_workspace): | |
| """Test git status when not a git repo.""" | |
| client = TestClient(app) | |
| response = client.get("/workspace/1/git/status") | |
| # Should return error (400 or 500 depending on exception handling) | |
| assert response.status_code in [400, 500] | |
| detail = response.json()["detail"].lower() | |
| assert "git" in detail or "repository" in detail | |
| def test_get_commits(self, test_workspace): | |
| """Test getting commit history.""" | |
| client = TestClient(app) | |
| # Initialize git repo | |
| import subprocess | |
| subprocess.run(["git", "init"], cwd=test_workspace, capture_output=True) | |
| subprocess.run(["git", "config", "user.email", "test@test.com"], cwd=test_workspace, capture_output=True) | |
| subprocess.run(["git", "config", "user.name", "Test User"], cwd=test_workspace, capture_output=True) | |
| subprocess.run(["git", "add", "."], cwd=test_workspace, capture_output=True) | |
| subprocess.run(["git", "commit", "-m", "Initial commit"], cwd=test_workspace, capture_output=True) | |
| response = client.get("/workspace/1/git/commits") | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert isinstance(data, list) | |
| if len(data) > 0: | |
| assert "hash" in data[0] | |
| assert "message" in data[0] | |
| def test_get_commits_with_limit(self, test_workspace): | |
| """Test getting commits with limit.""" | |
| client = TestClient(app) | |
| # Initialize git repo with multiple commits | |
| import subprocess | |
| subprocess.run(["git", "init"], cwd=test_workspace, capture_output=True) | |
| subprocess.run(["git", "config", "user.email", "test@test.com"], cwd=test_workspace, capture_output=True) | |
| subprocess.run(["git", "config", "user.name", "Test User"], cwd=test_workspace, capture_output=True) | |
| subprocess.run(["git", "add", "."], cwd=test_workspace, capture_output=True) | |
| subprocess.run(["git", "commit", "-m", "Commit 1"], cwd=test_workspace, capture_output=True) | |
| response = client.get("/workspace/1/git/commits?limit=10") | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert len(data) <= 10 | |
| def test_get_diff(self, test_workspace): | |
| """Test getting diff.""" | |
| client = TestClient(app) | |
| # Initialize git repo | |
| import subprocess | |
| subprocess.run(["git", "init"], cwd=test_workspace, capture_output=True) | |
| subprocess.run(["git", "config", "user.email", "test@test.com"], cwd=test_workspace, capture_output=True) | |
| subprocess.run(["git", "config", "user.name", "Test User"], cwd=test_workspace, capture_output=True) | |
| subprocess.run(["git", "add", "."], cwd=test_workspace, capture_output=True) | |
| subprocess.run(["git", "commit", "-m", "Initial"], cwd=test_workspace, capture_output=True) | |
| response = client.get("/workspace/1/git/diff") | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert "diff" in data | |
| def test_get_branches(self, test_workspace): | |
| """Test getting branches.""" | |
| client = TestClient(app) | |
| # Initialize git repo | |
| import subprocess | |
| subprocess.run(["git", "init"], cwd=test_workspace, capture_output=True) | |
| subprocess.run(["git", "config", "user.email", "test@test.com"], cwd=test_workspace, capture_output=True) | |
| subprocess.run(["git", "config", "user.name", "Test User"], cwd=test_workspace, capture_output=True) | |
| subprocess.run(["git", "add", "."], cwd=test_workspace, capture_output=True) | |
| subprocess.run(["git", "commit", "-m", "Initial"], cwd=test_workspace, capture_output=True) | |
| response = client.get("/workspace/1/git/branches") | |
| assert response.status_code == 200 | |
| data = response.json() | |
| # Response can be either dict with "branches" key or a list | |
| if isinstance(data, dict): | |
| assert "branches" in data | |
| else: | |
| assert isinstance(data, list) | |
| def test_get_file_history(self, test_workspace): | |
| """Test getting file history.""" | |
| client = TestClient(app) | |
| # Initialize git repo | |
| import subprocess | |
| subprocess.run(["git", "init"], cwd=test_workspace, capture_output=True) | |
| subprocess.run(["git", "config", "user.email", "test@test.com"], cwd=test_workspace, capture_output=True) | |
| subprocess.run(["git", "config", "user.name", "Test User"], cwd=test_workspace, capture_output=True) | |
| subprocess.run(["git", "add", "."], cwd=test_workspace, capture_output=True) | |
| subprocess.run(["git", "commit", "-m", "Initial"], cwd=test_workspace, capture_output=True) | |
| response = client.get("/workspace/1/git/file-history?file_path=README.md") | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert isinstance(data, list) | |
| class TestActivityTracking: | |
| """Test activity tracking endpoints.""" | |
| def test_get_activities(self, test_workspace): | |
| """Test getting activities.""" | |
| client = TestClient(app) | |
| response = client.get("/workspace/1/activities") | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert isinstance(data, list) | |
| def test_get_activities_with_limit(self, test_workspace): | |
| """Test getting activities with limit.""" | |
| client = TestClient(app) | |
| response = client.get("/workspace/1/activities?limit=10") | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert isinstance(data, list) | |
| assert len(data) <= 10 | |
| def test_get_activities_by_agent(self, test_workspace): | |
| """Test getting activities filtered by agent.""" | |
| client = TestClient(app) | |
| # First create an activity | |
| client.post( | |
| "/workspace/1/activities", | |
| json={ | |
| "agent_id": 5, | |
| "activity_type": "file_read", | |
| "details": {"file": "test.py"}, | |
| }, | |
| ) | |
| response = client.get("/workspace/1/activities?agent_id=5") | |
| # May return error if method not implemented | |
| assert response.status_code in [200, 500] | |
| if response.status_code == 200: | |
| data = response.json() | |
| assert isinstance(data, list) | |
| def test_get_activities_by_type(self, test_workspace): | |
| """Test getting activities filtered by type.""" | |
| client = TestClient(app) | |
| response = client.get("/workspace/1/activities?activity_type=file_read") | |
| # May return error if method not implemented | |
| assert response.status_code in [200, 500] | |
| if response.status_code == 200: | |
| data = response.json() | |
| assert isinstance(data, list) | |
| def test_track_activity_success(self, test_workspace): | |
| """Test tracking a new activity.""" | |
| client = TestClient(app) | |
| response = client.post( | |
| "/workspace/1/activities", | |
| json={ | |
| "agent_id": 10, | |
| "activity_type": "file_edited", | |
| "details": {"file": "main.py", "lines_changed": 5}, | |
| }, | |
| ) | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert data["agent_id"] == 10 | |
| assert data["type"] == "file_edited" # key is "type" not "activity_type" | |
| def test_track_activity_invalid_type(self, test_workspace): | |
| """Test tracking activity with invalid type.""" | |
| client = TestClient(app) | |
| response = client.post( | |
| "/workspace/1/activities", | |
| json={ | |
| "agent_id": 10, | |
| "activity_type": "invalid_type_xyz", | |
| "details": {}, | |
| }, | |
| ) | |
| # HTTPException(400) is caught by except Exception and returns 500 | |
| assert response.status_code in [400, 500] | |
| detail = response.json()["detail"].lower() | |
| assert "invalid" in detail or "error" in detail | |
| def test_get_activity_stats(self, test_workspace): | |
| """Test getting activity statistics.""" | |
| client = TestClient(app) | |
| response = client.get("/workspace/1/activities/stats") | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert isinstance(data, dict) | |
| class TestPythonExecution: | |
| """Test Python code execution endpoint.""" | |
| def test_run_python_success(self, test_workspace): | |
| """Test executing valid Python code.""" | |
| client = TestClient(app) | |
| response = client.post( | |
| "/workspace/1/run-python", | |
| json={ | |
| "code": "print('Hello from test')\nprint(2 + 2)", | |
| }, | |
| ) | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert "stdout" in data | |
| assert "stderr" in data | |
| assert "exit_code" in data | |
| assert "execution_time" in data | |
| assert "Hello from test" in data["stdout"] | |
| assert "4" in data["stdout"] | |
| assert data["exit_code"] == 0 | |
| def test_run_python_with_error(self, test_workspace): | |
| """Test executing Python code with error.""" | |
| client = TestClient(app) | |
| response = client.post( | |
| "/workspace/1/run-python", | |
| json={ | |
| "code": "print('Start')\nraise ValueError('Test error')", | |
| }, | |
| ) | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert data["exit_code"] != 0 | |
| assert "stderr" in data | |
| assert len(data["stderr"]) > 0 | |
| def test_run_python_with_imports(self, test_workspace): | |
| """Test executing Python code with imports.""" | |
| client = TestClient(app) | |
| response = client.post( | |
| "/workspace/1/run-python", | |
| json={ | |
| "code": "import math\nprint(math.pi)", | |
| }, | |
| ) | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert data["exit_code"] == 0 | |
| assert "3.14" in data["stdout"] | |
| def test_run_python_timeout(self, test_workspace): | |
| """Test Python execution timeout (long-running code).""" | |
| client = TestClient(app) | |
| # This test would take too long, so we'll skip the actual execution | |
| # In a real scenario, this would timeout after 30 seconds | |
| pytest.skip("Timeout test takes too long for regular test suite") | |
Xet Storage Details
- Size:
- 15.4 kB
- Xet hash:
- e14da200e1336018e57b2996103a40ca3ee06a4e8f0c3df99e4cc30a4083e63b
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.