106 lines
3.2 KiB
Python
106 lines
3.2 KiB
Python
from __future__ import annotations
|
|
|
|
import io
|
|
import json
|
|
import urllib.error
|
|
|
|
from memory_gateway_plugin.client import MemoryGatewayClient
|
|
from memory_gateway_plugin.config import PluginConfig
|
|
|
|
|
|
class FakeResponse:
|
|
status = 200
|
|
|
|
def __init__(self, payload):
|
|
self.payload = payload
|
|
|
|
def __enter__(self):
|
|
return self
|
|
|
|
def __exit__(self, *args):
|
|
return False
|
|
|
|
def read(self):
|
|
return json.dumps(self.payload).encode("utf-8")
|
|
|
|
|
|
def test_client_search_success(monkeypatch):
|
|
seen = {}
|
|
|
|
def fake_urlopen(request, timeout):
|
|
seen["url"] = request.full_url
|
|
seen["timeout"] = timeout
|
|
return FakeResponse({"results": [], "total": 0})
|
|
|
|
monkeypatch.setattr("urllib.request.urlopen", fake_urlopen)
|
|
client = MemoryGatewayClient(PluginConfig(gateway_url="http://gateway", timeout=7))
|
|
result = client.search_memory({"user_id": "u", "query": "demo"})
|
|
|
|
assert result["ok"] is True
|
|
assert result["endpoint"] == "/v1/memory/search"
|
|
assert seen["url"] == "http://gateway/v1/memory/search"
|
|
assert seen["timeout"] == 7
|
|
|
|
|
|
def test_client_network_error(monkeypatch):
|
|
def fake_urlopen(request, timeout):
|
|
raise urllib.error.URLError("connection refused")
|
|
|
|
monkeypatch.setattr("urllib.request.urlopen", fake_urlopen)
|
|
client = MemoryGatewayClient(PluginConfig(gateway_url="http://gateway"))
|
|
result = client.search_memory({"user_id": "u", "query": "demo"})
|
|
|
|
assert result["ok"] is False
|
|
assert result["status_code"] is None
|
|
assert "connection refused" in result["error"]
|
|
|
|
|
|
def test_commit_session_calls_correct_endpoint(monkeypatch):
|
|
seen = {}
|
|
|
|
def fake_urlopen(request, timeout):
|
|
seen["url"] = request.full_url
|
|
return FakeResponse({"session_id": "sess_1"})
|
|
|
|
monkeypatch.setattr("urllib.request.urlopen", fake_urlopen)
|
|
client = MemoryGatewayClient(PluginConfig(gateway_url="http://gateway"))
|
|
result = client.commit_session("sess_1", {"user_id": "u", "session_id": "sess_1"})
|
|
|
|
assert result["ok"] is True
|
|
assert seen["url"] == "http://gateway/v1/sessions/sess_1/commit"
|
|
|
|
|
|
def test_feedback_calls_correct_endpoint(monkeypatch):
|
|
seen = {}
|
|
|
|
def fake_urlopen(request, timeout):
|
|
seen["url"] = request.full_url
|
|
return FakeResponse({"status": "ok"})
|
|
|
|
monkeypatch.setattr("urllib.request.urlopen", fake_urlopen)
|
|
client = MemoryGatewayClient(PluginConfig(gateway_url="http://gateway"))
|
|
result = client.send_feedback("mem_1", {"user_id": "u", "feedback": "incorrect"})
|
|
|
|
assert result["ok"] is True
|
|
assert seen["url"] == "http://gateway/v1/memory/mem_1/feedback"
|
|
|
|
|
|
def test_client_http_error(monkeypatch):
|
|
def fake_urlopen(request, timeout):
|
|
raise urllib.error.HTTPError(
|
|
url=request.full_url,
|
|
code=401,
|
|
msg="unauthorized",
|
|
hdrs=None,
|
|
fp=io.BytesIO(b'{"detail":"Invalid or missing API key"}'),
|
|
)
|
|
|
|
monkeypatch.setattr("urllib.request.urlopen", fake_urlopen)
|
|
client = MemoryGatewayClient(PluginConfig(gateway_url="http://gateway"))
|
|
result = client.search_memory({"user_id": "u", "query": "demo"})
|
|
|
|
assert result["ok"] is False
|
|
assert result["status_code"] == 401
|
|
assert result["endpoint"] == "/v1/memory/search"
|
|
|