"""Integration tests for API endpoints.""" import os import pytest from httpx import AsyncClient, ASGITransport from memory_manager.api.app import app from memory_manager.db.repository import MemoryRepository TEST_DB_PATH = ".memory/test_codebase_memory.db" @pytest.fixture(autouse=True) async def clean_db(): if os.path.exists(TEST_DB_PATH): os.remove(TEST_DB_PATH) yield if os.path.exists(TEST_DB_PATH): os.remove(TEST_DB_PATH) @pytest.fixture async def client(): os.environ["MEMORY_DB_PATH"] = TEST_DB_PATH transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as ac: yield ac @pytest.mark.asyncio async def test_health_endpoint(client): response = await client.get("/health") assert response.status_code == 200 data = response.json() assert data["status"] == "ok" assert "version" in data @pytest.mark.asyncio async def test_create_memory_entry(client): entry_data = { "title": "Test Decision", "content": "We decided to use SQLite for the database.", "category": "decision", "tags": ["database", "sqlite"], } response = await client.post("/api/memory", json=entry_data) assert response.status_code == 201 data = response.json() assert data["title"] == "Test Decision" assert data["category"] == "decision" assert "id" in data @pytest.mark.asyncio async def test_list_memory_entries(client): entry_data = { "title": "Test Feature", "content": "Implementing new feature", "category": "feature", "tags": [], } await client.post("/api/memory", json=entry_data) response = await client.get("/api/memory") assert response.status_code == 200 data = response.json() assert isinstance(data, list) assert len(data) >= 1 @pytest.mark.asyncio async def test_get_memory_entry(client): entry_data = { "title": "Test Get", "content": "Testing get endpoint", "category": "note", "tags": ["test"], } create_response = await client.post("/api/memory", json=entry_data) entry_id = create_response.json()["id"] response = await client.get(f"/api/memory/{entry_id}") assert response.status_code == 200 data = response.json() assert data["title"] == "Test Get" @pytest.mark.asyncio async def test_get_memory_entry_not_found(client): response = await client.get("/api/memory/99999") assert response.status_code == 404 @pytest.mark.asyncio async def test_update_memory_entry(client): entry_data = { "title": "Original Title", "content": "Original content", "category": "note", "tags": [], } create_response = await client.post("/api/memory", json=entry_data) entry_id = create_response.json()["id"] update_data = {"title": "Updated Title"} response = await client.put(f"/api/memory/{entry_id}", json=update_data) assert response.status_code == 200 data = response.json() assert data["title"] == "Updated Title" @pytest.mark.asyncio async def test_delete_memory_entry(client): entry_data = { "title": "To Delete", "content": "Will be deleted", "category": "note", "tags": [], } create_response = await client.post("/api/memory", json=entry_data) entry_id = create_response.json()["id"] response = await client.delete(f"/api/memory/{entry_id}") assert response.status_code == 204 get_response = await client.get(f"/api/memory/{entry_id}") assert get_response.status_code == 404 @pytest.mark.asyncio async def test_create_commit(client): entry_data = { "title": "Before Commit", "content": "Testing commit", "category": "decision", "tags": [], } await client.post("/api/memory", json=entry_data) commit_data = {"message": "Initial commit with first entry"} response = await client.post("/api/memory/commit", json=commit_data) assert response.status_code == 201 data = response.json() assert data["message"] == "Initial commit with first entry" assert "hash" in data assert len(data["snapshot"]) >= 1 @pytest.mark.asyncio async def test_get_log(client): entry_data = { "title": "Log Test", "content": "Testing log", "category": "feature", "tags": [], } await client.post("/api/memory", json=entry_data) commit_data = {"message": "Test commit"} await client.post("/api/memory/commit", json=commit_data) response = await client.get("/api/memory/log") assert response.status_code == 200 data = response.json() assert isinstance(data, list) assert len(data) >= 1 @pytest.mark.asyncio async def test_diff_commits(client): entry1 = { "title": "Entry 1", "content": "First entry", "category": "decision", "tags": [], } await client.post("/api/memory", json=entry1) commit1_response = await client.post("/api/memory/commit", json={"message": "Commit 1"}) hash1 = commit1_response.json()["hash"] entry2 = { "title": "Entry 2", "content": "Second entry", "category": "feature", "tags": [], } await client.post("/api/memory", json=entry2) commit2_response = await client.post("/api/memory/commit", json={"message": "Commit 2"}) hash2 = commit2_response.json()["hash"] response = await client.get(f"/api/memory/diff/{hash1}/{hash2}") assert response.status_code == 200 data = response.json() assert "added" in data assert "removed" in data assert "modified" in data @pytest.mark.asyncio async def test_stats_endpoint(client): entry_data = { "title": "Stats Test", "content": "Testing stats", "category": "architecture", "tags": ["test"], } await client.post("/api/memory", json=entry_data) response = await client.get("/api/memory/stats") assert response.status_code == 200 data = response.json() assert "total_entries" in data assert "entries_by_category" in data assert "total_commits" in data