import pytest from httpx import AsyncClient @pytest.fixture async def auth_headers(client: AsyncClient): resp = await client.post("/api/v1/auth/register", json={ "email": "memuser@example.com", "username": "memuser", "password": "testpass123", }) assert resp.status_code == 201 return {"Authorization": f"Bearer {resp.json()['access_token']}"} async def test_create_memory(client: AsyncClient, auth_headers: dict): resp = await client.post("/api/v1/memory/", json={ "category": "condition", "title": "Diabetes Type 2", "content": "Diagnosed in 2024, managed with metformin", "importance": "critical", }, headers=auth_headers) assert resp.status_code == 201 data = resp.json() assert data["category"] == "condition" assert data["title"] == "Diabetes Type 2" assert data["importance"] == "critical" assert data["is_active"] is True async def test_list_memories(client: AsyncClient, auth_headers: dict): await client.post("/api/v1/memory/", json={ "category": "allergy", "title": "Penicillin", "content": "Severe allergic reaction", "importance": "critical", }, headers=auth_headers) resp = await client.get("/api/v1/memory/", headers=auth_headers) assert resp.status_code == 200 assert len(resp.json()["entries"]) >= 1 async def test_filter_by_category(client: AsyncClient, auth_headers: dict): await client.post("/api/v1/memory/", json={ "category": "medication", "title": "Metformin", "content": "500mg twice daily", "importance": "high", }, headers=auth_headers) resp = await client.get("/api/v1/memory/", params={"category": "medication"}, headers=auth_headers) assert resp.status_code == 200 entries = resp.json()["entries"] assert all(e["category"] == "medication" for e in entries) async def test_update_memory(client: AsyncClient, auth_headers: dict): resp = await client.post("/api/v1/memory/", json={ "category": "vital", "title": "Blood Pressure", "content": "130/85", "importance": "medium", }, headers=auth_headers) entry_id = resp.json()["id"] resp = await client.patch(f"/api/v1/memory/{entry_id}", json={ "content": "125/80 (improved)", "importance": "low", }, headers=auth_headers) assert resp.status_code == 200 assert resp.json()["content"] == "125/80 (improved)" assert resp.json()["importance"] == "low" async def test_delete_memory(client: AsyncClient, auth_headers: dict): resp = await client.post("/api/v1/memory/", json={ "category": "other", "title": "To Delete", "content": "Test", "importance": "low", }, headers=auth_headers) entry_id = resp.json()["id"] resp = await client.delete(f"/api/v1/memory/{entry_id}", headers=auth_headers) assert resp.status_code == 204 resp = await client.get(f"/api/v1/memory/{entry_id}", headers=auth_headers) assert resp.status_code == 404 async def test_memory_ownership_isolation(client: AsyncClient, auth_headers: dict): resp = await client.post("/api/v1/memory/", json={ "category": "condition", "title": "Private Info", "content": "Sensitive", "importance": "critical", }, headers=auth_headers) entry_id = resp.json()["id"] resp = await client.post("/api/v1/auth/register", json={ "email": "memother@example.com", "username": "memother", "password": "testpass123", }) other_headers = {"Authorization": f"Bearer {resp.json()['access_token']}"} resp = await client.get(f"/api/v1/memory/{entry_id}", headers=other_headers) assert resp.status_code == 404