Spaces:
Running
Running
Download tests/test_memory.py from 2Btainment/Fuugi-API: direct link, hf CLI and curl.
- Browser
- Download file 6.45 kB
-
https://huggingface.co/spaces/2Btainment/Fuugi-API/resolve/main/tests/test_memory.py
- Command line
-
hf download hf://spaces/2Btainment/Fuugi-API/tests/test_memory.py
-
curl -L -o test_memory.py https://huggingface.co/spaces/2Btainment/Fuugi-API/resolve/main/tests/test_memory.py
6.45 kB
| from __future__ import annotations | |
| import sqlite3 | |
| import uuid | |
| from pathlib import Path | |
| from typing import Any | |
| import httpx | |
| from fastapi.testclient import TestClient | |
| from app.config import Settings | |
| from app.main import create_app | |
| def memory_settings(database: Path, **overrides: Any) -> Settings: | |
| values: dict[str, Any] = { | |
| "openrouter_api_key": "test-key-not-a-secret", | |
| "environment": "test", | |
| "rate_limit_requests": 100, | |
| "memory_enabled": True, | |
| "memory_database_path": str(database), | |
| "memory_session_pepper": "test-memory-pepper-with-more-than-32-characters", | |
| } | |
| values.update(overrides) | |
| return Settings(**values) | |
| def success_transport(captured: list[httpx.Request] | None = None) -> httpx.MockTransport: | |
| async def handler(request: httpx.Request) -> httpx.Response: | |
| if captured is not None: | |
| captured.append(request) | |
| body = httpx.Response(200, content=request.read()).json() | |
| return httpx.Response( | |
| 200, | |
| json={ | |
| "model": body["model"], | |
| "choices": [{"message": {"content": "Memory-aware answer."}}], | |
| }, | |
| ) | |
| return httpx.MockTransport(handler) | |
| def test_explicit_memory_requires_consent_and_is_not_sent_upstream(tmp_path: Path) -> None: | |
| captured: list[httpx.Request] = [] | |
| app = create_app( | |
| memory_settings(tmp_path / "memory.sqlite3"), | |
| transport=success_transport(captured), | |
| ) | |
| session = str(uuid.uuid4()) | |
| headers = {"X-Fuugi-Session": session} | |
| with TestClient(app) as client: | |
| blocked = client.post( | |
| "/api/fuugi", json={"message": "Merke dir: Ich mag kurze Antworten."}, headers=headers | |
| ) | |
| consent = client.post( | |
| "/api/fuugi/memory-consent", json={"enabled": True}, headers=headers | |
| ) | |
| saved = client.post( | |
| "/api/fuugi", json={"message": "Merke dir: Ich mag kurze Antworten."}, headers=headers | |
| ) | |
| listed = client.get("/api/fuugi/memories", headers=headers) | |
| assert blocked.json()["memory_event"]["status"] == "consent_required" | |
| assert consent.json() == {"enabled": True, "memories": []} | |
| assert saved.json()["memory_event"]["status"] == "saved" | |
| assert listed.json()["memories"][0]["content"] == "Ich mag kurze Antworten." | |
| assert captured == [] | |
| def test_memory_is_untrusted_context_and_raw_session_is_not_stored(tmp_path: Path) -> None: | |
| database = tmp_path / "memory.sqlite3" | |
| captured: list[httpx.Request] = [] | |
| app = create_app(memory_settings(database), transport=success_transport(captured)) | |
| session = str(uuid.uuid4()) | |
| headers = {"X-Fuugi-Session": session} | |
| with TestClient(app) as client: | |
| client.post("/api/fuugi/memory-consent", json={"enabled": True}, headers=headers) | |
| client.post( | |
| "/api/fuugi", | |
| json={"message": "Merke dir: Mein Lieblingsformat ist WAV."}, | |
| headers=headers, | |
| ) | |
| answer = client.post( | |
| "/api/fuugi", json={"message": "Welches Format bevorzuge ich?"}, headers=headers | |
| ) | |
| assert answer.status_code == 200 | |
| assert len(captured) == 1 | |
| upstream = httpx.Response(200, content=captured[0].read()).json() | |
| serialized = str(upstream["messages"]) | |
| assert "Untrusted user-approved memory notes" in serialized | |
| assert "Mein Lieblingsformat ist WAV." in serialized | |
| assert session not in serialized | |
| assert session.encode() not in database.read_bytes() | |
| def test_memory_is_isolated_deletable_and_revocable(tmp_path: Path) -> None: | |
| app = create_app( | |
| memory_settings(tmp_path / "memory.sqlite3"), | |
| transport=success_transport(), | |
| ) | |
| first = {"X-Fuugi-Session": str(uuid.uuid4())} | |
| second = {"X-Fuugi-Session": str(uuid.uuid4())} | |
| with TestClient(app) as client: | |
| client.post("/api/fuugi/memory-consent", json={"enabled": True}, headers=first) | |
| client.post("/api/fuugi/memory-consent", json={"enabled": True}, headers=second) | |
| saved = client.post( | |
| "/api/fuugi", json={"message": "Merke dir: Nur Sitzung eins."}, headers=first | |
| ).json() | |
| assert client.get("/api/fuugi/memories", headers=second).json()["memories"] == [] | |
| memory_id = saved["memory_event"]["id"] | |
| assert client.delete(f"/api/fuugi/memories/{memory_id}", headers=second).status_code == 404 | |
| assert client.delete(f"/api/fuugi/memories/{memory_id}", headers=first).status_code == 204 | |
| client.post("/api/fuugi/memory-consent", json={"enabled": False}, headers=first) | |
| revoked = client.get("/api/fuugi/memories", headers=first).json() | |
| assert revoked == {"enabled": False, "memories": []} | |
| def test_sensitive_memory_is_rejected_and_normal_chat_is_not_persisted(tmp_path: Path) -> None: | |
| database = tmp_path / "memory.sqlite3" | |
| app = create_app(memory_settings(database), transport=success_transport()) | |
| headers = {"X-Fuugi-Session": str(uuid.uuid4())} | |
| with TestClient(app) as client: | |
| client.post("/api/fuugi/memory-consent", json={"enabled": True}, headers=headers) | |
| rejected = client.post( | |
| "/api/fuugi", json={"message": "Merke dir: Mein Passwort ist geheim."}, headers=headers | |
| ) | |
| normal = client.post("/api/fuugi", json={"message": "Ein normaler Chat."}, headers=headers) | |
| assert rejected.json()["memory_event"]["status"] == "rejected" | |
| assert normal.status_code == 200 | |
| with sqlite3.connect(database) as connection: | |
| assert connection.execute("SELECT COUNT(*) FROM memory_nodes").fetchone()[0] == 0 | |
| def test_memory_cors_allows_session_header_and_delete(tmp_path: Path) -> None: | |
| origin = "http://192.168.2.149:8080" | |
| app = create_app( | |
| memory_settings( | |
| tmp_path / "memory.sqlite3", | |
| environment="development", | |
| localhost_origins=(origin,), | |
| ), | |
| transport=success_transport(), | |
| ) | |
| headers = { | |
| "Origin": origin, | |
| "Access-Control-Request-Method": "DELETE", | |
| "Access-Control-Request-Headers": "content-type,x-fuugi-session", | |
| } | |
| with TestClient(app) as client: | |
| preflight = client.options("/api/fuugi/memories", headers=headers) | |
| assert preflight.status_code == 200 | |
| assert preflight.headers["access-control-allow-origin"] == origin | |
| assert "X-Fuugi-Session" in preflight.headers["access-control-allow-headers"] | |