neutralize upstream service branding

This commit is contained in:
2026-06-12 16:31:08 +08:00
parent 126ae4eafa
commit 42de7f9da0
18 changed files with 340 additions and 256 deletions

View File

@ -9,31 +9,31 @@ import pytest
from core.api import create_app
from core.config import GatewayConfig
from core.everos_client import EverOSClient
from core.backend_client import BackendClient
pytestmark = pytest.mark.integration
def _integration_enabled() -> bool:
return os.environ.get("RUN_EVEROS_INTEGRATION") == "1"
return os.environ.get("RUN_BACKEND_INTEGRATION") == "1"
def _ingest_integration_enabled() -> bool:
return os.environ.get("RUN_EVEROS_INGEST_INTEGRATION") == "1"
return os.environ.get("RUN_BACKEND_INGEST_INTEGRATION") == "1"
def _everos_base_url() -> str:
return os.environ.get("EVEROS_BASE_URL", "http://127.0.0.1:1995")
def _backend_base_url() -> str:
return os.environ.get("MEMORY_GATEWAY_BACKEND_BASE_URL", "http://127.0.0.1:1995")
@pytest.mark.skipif(
not _integration_enabled(),
reason="set RUN_EVEROS_INTEGRATION=1 to run against a real EverOS service",
reason="set RUN_BACKEND_INTEGRATION=1 to run against an upstream memory service",
)
@pytest.mark.asyncio
async def test_real_everos_health_check() -> None:
client = EverOSClient(_everos_base_url(), timeout=10)
async def test_real_backend_health_check() -> None:
client = BackendClient(_backend_base_url(), timeout=10)
health = await client.health_check()
@ -43,17 +43,17 @@ async def test_real_everos_health_check() -> None:
@pytest.mark.skipif(
not _ingest_integration_enabled(),
reason=(
"set RUN_EVEROS_INGEST_INTEGRATION=1 to run real EverOS add/flush ingestion"
"set RUN_BACKEND_INGEST_INTEGRATION=1 to run upstream add/flush ingestion"
),
)
@pytest.mark.asyncio
async def test_gateway_uploads_text_resource_to_real_everos(tmp_path: Path) -> None:
async def test_gateway_uploads_text_resource_to_real_backend(tmp_path: Path) -> None:
config = GatewayConfig(
everos_base_url=_everos_base_url(),
backend_base_url=_backend_base_url(),
database_path=tmp_path / "gateway.sqlite3",
storage_dir=tmp_path / "storage",
everos_ingest_attempts=1,
everos_timeout_seconds=30,
backend_ingest_attempts=1,
backend_timeout_seconds=30,
)
app = create_app(config=config)
transport = httpx.ASGITransport(app=app)
@ -69,8 +69,8 @@ async def test_gateway_uploads_text_resource_to_real_everos(tmp_path: Path) -> N
data={"user_id": user_id, "user_key": user_key},
files={
"file": (
"real-everos.txt",
b"real everos integration",
"integration.txt",
b"upstream memory service integration",
"text/plain",
)
},

35
tests/test_branding.py Normal file
View File

@ -0,0 +1,35 @@
from __future__ import annotations
from pathlib import Path
PROJECT_ROOT = Path(__file__).resolve().parents[1]
FORBIDDEN_TOKEN = "ever" + "os"
SKIPPED_PARTS = {
".git",
".pytest_cache",
".venv",
"__pycache__",
"data",
}
def test_current_project_does_not_expose_upstream_product_name() -> None:
matches: list[str] = []
for path in PROJECT_ROOT.rglob("*"):
relative = path.relative_to(PROJECT_ROOT)
if any(part in SKIPPED_PARTS for part in relative.parts):
continue
if FORBIDDEN_TOKEN in path.name.lower():
matches.append(f"filename: {relative}")
if not path.is_file():
continue
try:
text = path.read_text(encoding="utf-8")
except UnicodeDecodeError:
continue
for line_number, line in enumerate(text.splitlines(), start=1):
if FORBIDDEN_TOKEN in line.lower():
matches.append(f"content: {relative}:{line_number}")
assert matches == []

View File

@ -1,6 +1,6 @@
# Memory Gateway multimodal API test
This file records a real end-to-end test through **Memory Gateway**, not direct EverOS calls.
This file records a real end-to-end test through **Memory Gateway**, not direct upstream memory service calls.
Gateway URL used by curl:
@ -8,7 +8,7 @@ Gateway URL used by curl:
http://127.0.0.1:8010
```
Gateway upstream EverOS:
Gateway upstream memory service:
```text
http://10.6.80.123:1995
@ -35,7 +35,7 @@ Command:
```bash
cd /home/tom/memory-gateway
EVEROS_BASE_URL=http://10.6.80.123:1995 \
MEMORY_GATEWAY_BACKEND_BASE_URL=http://10.6.80.123:1995 \
MEMORY_GATEWAY_DB_PATH=/tmp/memory_gateway_curl.sqlite3 \
MEMORY_GATEWAY_STORAGE_DIR=/tmp/memory_gateway_curl_storage \
MEMORY_GATEWAY_HOST=127.0.0.1 \
@ -136,7 +136,7 @@ Response:
```json
{
"session_id": "chat:gateway-multimodal-20260611180257",
"everos": {
"backend": {
"request_id": "c9e24b8d27ee4ad08a8df70273336637",
"data": {
"message_count": 1,
@ -174,7 +174,7 @@ Response:
```json
{
"session_id": "chat:gateway-multimodal-20260611180257",
"everos": {
"backend": {
"request_id": "8eb7d5db2d3b43f4999f445aabb813b1",
"data": {
"status": "extracted"
@ -192,7 +192,7 @@ TOTAL_TIME:2.135721
## 4. Search through Gateway
EverOS indexing can lag briefly after `flush`, so this test waited about 2 seconds before searching.
upstream memory service indexing can lag briefly after `flush`, so this test waited about 2 seconds before searching.
Request:
@ -279,7 +279,7 @@ Response:
{
"status": "ok",
"api": {"status": "ok"},
"everos": {
"backend": {
"status": "ok",
"base_url": "http://10.6.80.123:1995",
"data": {"status": "ok"}

View File

@ -17,7 +17,7 @@ import core.api as api_module
import core.service as service_module
class FakeEverOSClient:
class FakeBackendClient:
def __init__(
self,
search_results: list[dict[str, Any]] | None = None,
@ -67,7 +67,7 @@ class FakeEverOSClient:
@pytest.fixture
def config(tmp_path: Path) -> GatewayConfig:
return GatewayConfig(
everos_base_url="http://everos.test",
backend_base_url="http://backend.test",
database_path=tmp_path / "gateway.sqlite3",
storage_dir=tmp_path / "storage",
)
@ -81,9 +81,9 @@ def repo(config: GatewayConfig) -> MemoryRepository:
def app_client(
config: GatewayConfig,
everos_client: FakeEverOSClient,
backend_client: FakeBackendClient,
) -> httpx.AsyncClient:
app = create_app(config=config, everos_client=everos_client)
app = create_app(config=config, backend_client=backend_client)
transport = httpx.ASGITransport(app=app)
return httpx.AsyncClient(transport=transport, base_url="http://test")
@ -124,20 +124,20 @@ async def create_user(client: httpx.AsyncClient, user_id: str = "u_123") -> str:
return body["user_key"]
def test_create_app_uses_configured_everos_timeout(config: GatewayConfig) -> None:
def test_create_app_uses_configured_backend_timeout(config: GatewayConfig) -> None:
config = GatewayConfig(
everos_base_url=config.everos_base_url,
backend_base_url=config.backend_base_url,
database_path=config.database_path,
storage_dir=config.storage_dir,
everos_timeout_seconds=7.5,
backend_timeout_seconds=7.5,
)
app = create_app(config=config)
assert app.state.everos_client.timeout == 7.5
assert app.state.backend_client.timeout == 7.5
def test_create_app_uses_project_name(config: GatewayConfig) -> None:
app = create_app(config=config, everos_client=FakeEverOSClient())
app = create_app(config=config, backend_client=FakeBackendClient())
assert app.title == "memory-gateway"
@ -160,8 +160,8 @@ async def test_api_logs_request_time_address_input_and_output(
caplog: pytest.LogCaptureFixture,
) -> None:
caplog.set_level(logging.INFO, logger="memory_gateway.api")
everos = FakeEverOSClient()
async with app_client(config, everos) as client:
backend = FakeBackendClient()
async with app_client(config, backend) as client:
user_key = await create_user(client, "u_log")
response = await client.get(
"/resources",
@ -192,8 +192,8 @@ async def test_api_logs_do_not_expose_secrets_from_large_json_bodies(
caplog: pytest.LogCaptureFixture,
) -> None:
caplog.set_level(logging.INFO, logger="memory_gateway.api")
everos = FakeEverOSClient()
async with app_client(config, everos) as client:
backend = FakeBackendClient()
async with app_client(config, backend) as client:
user_key = await create_user(client, "u_large_log")
caplog.clear()
response = await client.post(
@ -224,40 +224,40 @@ async def test_api_logs_do_not_expose_secrets_from_large_json_bodies(
@pytest.mark.asyncio
async def test_health_reports_api_and_everos_ok(
async def test_health_reports_api_and_backend_ok(
config: GatewayConfig,
) -> None:
everos = FakeEverOSClient()
async with app_client(config, everos) as client:
backend = FakeBackendClient()
async with app_client(config, backend) as client:
response = await client.get("/health")
assert response.status_code == 200, response.text
assert response.json() == {
"status": "ok",
"api": {"status": "ok"},
"everos": {
"backend": {
"status": "ok",
"base_url": "http://everos.test",
"base_url": "http://backend.test",
"data": {"status": "ok"},
},
}
@pytest.mark.asyncio
async def test_health_reports_degraded_when_everos_fails(
async def test_health_reports_degraded_when_backend_fails(
config: GatewayConfig,
) -> None:
everos = FakeEverOSClient(health_error=RuntimeError("everos down"))
async with app_client(config, everos) as client:
backend = FakeBackendClient(health_error=RuntimeError("backend down"))
async with app_client(config, backend) as client:
response = await client.get("/health")
assert response.status_code == 200, response.text
body = response.json()
assert body["status"] == "degraded"
assert body["api"] == {"status": "ok"}
assert body["everos"]["status"] == "unavailable"
assert body["everos"]["base_url"] == "http://everos.test"
assert body["everos"]["error"] == "everos down"
assert body["backend"]["status"] == "unavailable"
assert body["backend"]["base_url"] == "http://backend.test"
assert body["backend"]["error"] == "backend down"
@pytest.mark.asyncio
@ -265,8 +265,8 @@ async def test_create_user_generates_and_persists_user_key(
config: GatewayConfig,
repo: MemoryRepository,
) -> None:
everos = FakeEverOSClient()
async with app_client(config, everos) as client:
backend = FakeBackendClient()
async with app_client(config, backend) as client:
user_key = await create_user(client, "u_123")
user = repo.get_user("u_123")
@ -275,11 +275,11 @@ async def test_create_user_generates_and_persists_user_key(
@pytest.mark.asyncio
async def test_upload_resource_creates_record_and_calls_everos(
async def test_upload_resource_creates_record_and_calls_backend(
config: GatewayConfig,
) -> None:
everos = FakeEverOSClient()
async with app_client(config, everos) as client:
backend = FakeBackendClient()
async with app_client(config, backend) as client:
user_key = await create_user(client)
response = await client.post(
"/resources",
@ -304,15 +304,15 @@ async def test_upload_resource_creates_record_and_calls_everos(
assert resource["size_bytes"] == len(b"pay in 30 days")
assert not resource["uri"].startswith("resource://")
assert len(everos.add_calls) == 1
add_payload = everos.add_calls[0]
assert len(backend.add_calls) == 1
add_payload = backend.add_calls[0]
assert add_payload["session_id"] == f"resource:u_123:{resource_id}"
content = add_payload["messages"][0]["content"][0]
assert content["type"] == "text"
assert content["text"] == "pay in 30 days"
assert "uri" not in content
assert content["extras"] == {"resource_id": resource_id, "source": "user_upload"}
assert everos.flush_calls == [
assert backend.flush_calls == [
{
"session_id": f"resource:u_123:{resource_id}",
"app_id": "default",
@ -322,11 +322,11 @@ async def test_upload_resource_creates_record_and_calls_everos(
@pytest.mark.asyncio
async def test_upload_binary_resource_sends_base64_content_to_everos(
async def test_upload_binary_resource_sends_base64_content_to_backend(
config: GatewayConfig,
) -> None:
everos = FakeEverOSClient()
async with app_client(config, everos) as client:
backend = FakeBackendClient()
async with app_client(config, backend) as client:
user_key = await create_user(client)
response = await client.post(
"/resources",
@ -335,7 +335,7 @@ async def test_upload_binary_resource_sends_base64_content_to_everos(
)
assert response.status_code == 200, response.text
content = everos.add_calls[0]["messages"][0]["content"][0]
content = backend.add_calls[0]["messages"][0]["content"][0]
assert content["type"] == "pdf"
assert content["base64"] == base64.b64encode(b"%PDF bytes").decode("ascii")
assert content["ext"] == "pdf"
@ -354,8 +354,8 @@ async def test_upload_resource_uses_current_timestamp(
lambda: 1234567890123,
raising=False,
)
everos = FakeEverOSClient()
async with app_client(config, everos) as client:
backend = FakeBackendClient()
async with app_client(config, backend) as client:
user_key = await create_user(client)
response = await client.post(
"/resources",
@ -364,22 +364,22 @@ async def test_upload_resource_uses_current_timestamp(
)
assert response.status_code == 200, response.text
assert everos.add_calls[0]["messages"][0]["timestamp"] == 1234567890123
assert backend.add_calls[0]["messages"][0]["timestamp"] == 1234567890123
@pytest.mark.asyncio
async def test_upload_retries_transient_everos_failure(
async def test_upload_retries_transient_backend_failure(
config: GatewayConfig,
) -> None:
config = GatewayConfig(
everos_base_url=config.everos_base_url,
backend_base_url=config.backend_base_url,
database_path=config.database_path,
storage_dir=config.storage_dir,
everos_ingest_attempts=2,
everos_retry_delay_seconds=0,
backend_ingest_attempts=2,
backend_retry_delay_seconds=0,
)
everos = FakeEverOSClient(add_failures=1, flush_failures=1)
async with app_client(config, everos) as client:
backend = FakeBackendClient(add_failures=1, flush_failures=1)
async with app_client(config, backend) as client:
user_key = await create_user(client)
response = await client.post(
"/resources",
@ -389,16 +389,16 @@ async def test_upload_retries_transient_everos_failure(
assert response.status_code == 200, response.text
assert response.json()["status"] == "extracted"
assert len(everos.add_calls) == 2
assert len(everos.flush_calls) == 2
assert len(backend.add_calls) == 2
assert len(backend.flush_calls) == 2
@pytest.mark.asyncio
async def test_upload_duplicate_resource_is_idempotent_for_same_user(
config: GatewayConfig,
) -> None:
everos = FakeEverOSClient()
async with app_client(config, everos) as client:
backend = FakeBackendClient()
async with app_client(config, backend) as client:
user_key = await create_user(client)
first = await client.post(
"/resources",
@ -414,8 +414,8 @@ async def test_upload_duplicate_resource_is_idempotent_for_same_user(
assert first.status_code == 200, first.text
assert second.status_code == 200, second.text
assert second.json()["resource_id"] == first.json()["resource_id"]
assert len(everos.add_calls) == 1
assert len(everos.flush_calls) == 1
assert len(backend.add_calls) == 1
assert len(backend.flush_calls) == 1
@pytest.mark.asyncio
@ -424,13 +424,13 @@ async def test_upload_rejects_file_larger_than_configured_limit(
repo: MemoryRepository,
) -> None:
config = GatewayConfig(
everos_base_url=config.everos_base_url,
backend_base_url=config.backend_base_url,
database_path=config.database_path,
storage_dir=config.storage_dir,
max_upload_bytes=4,
)
everos = FakeEverOSClient()
async with app_client(config, everos) as client:
backend = FakeBackendClient()
async with app_client(config, backend) as client:
user_key = await create_user(client)
response = await client.post(
"/resources",
@ -441,7 +441,7 @@ async def test_upload_rejects_file_larger_than_configured_limit(
assert response.status_code == 413, response.text
assert repo.list_resources("u_123") == []
assert not any(config.storage_dir.rglob("*"))
assert everos.add_calls == []
assert backend.add_calls == []
@pytest.mark.asyncio
@ -449,8 +449,8 @@ async def test_upload_rejects_unsupported_mime_type(
config: GatewayConfig,
repo: MemoryRepository,
) -> None:
everos = FakeEverOSClient()
async with app_client(config, everos) as client:
backend = FakeBackendClient()
async with app_client(config, backend) as client:
user_key = await create_user(client)
response = await client.post(
"/resources",
@ -460,15 +460,15 @@ async def test_upload_rejects_unsupported_mime_type(
assert response.status_code == 415, response.text
assert repo.list_resources("u_123") == []
assert everos.add_calls == []
assert backend.add_calls == []
@pytest.mark.asyncio
async def test_resource_detail_does_not_leak_internal_uri(
config: GatewayConfig,
) -> None:
everos = FakeEverOSClient()
async with app_client(config, everos) as client:
backend = FakeBackendClient()
async with app_client(config, backend) as client:
user_key = await create_user(client)
created = await client.post(
"/resources",
@ -492,8 +492,8 @@ async def test_resource_detail_does_not_leak_internal_uri(
async def test_resource_detail_returns_empty_when_user_has_no_resource(
config: GatewayConfig,
) -> None:
everos = FakeEverOSClient()
async with app_client(config, everos) as client:
backend = FakeBackendClient()
async with app_client(config, backend) as client:
user_key = await create_user(client, "u_empty")
response = await client.get(
"/resources/r_missing",
@ -508,8 +508,8 @@ async def test_resource_detail_returns_empty_when_user_has_no_resource(
async def test_resources_are_isolated_by_user_key(
config: GatewayConfig,
) -> None:
everos = FakeEverOSClient()
async with app_client(config, everos) as client:
backend = FakeBackendClient()
async with app_client(config, backend) as client:
alice_key = await create_user(client, "alice")
bob_key = await create_user(client, "bob")
created = await client.post(
@ -537,8 +537,8 @@ async def test_resources_are_isolated_by_user_key(
async def test_resource_api_rejects_invalid_user_key(
config: GatewayConfig,
) -> None:
everos = FakeEverOSClient()
async with app_client(config, everos) as client:
backend = FakeBackendClient()
async with app_client(config, backend) as client:
await create_user(client, "u_123")
response = await client.get(
"/resources",
@ -549,10 +549,10 @@ async def test_resource_api_rejects_invalid_user_key(
@pytest.mark.asyncio
async def test_add_memory_forwards_multimodal_payload_to_everos(
async def test_add_memory_forwards_multimodal_payload_to_backend(
config: GatewayConfig,
) -> None:
everos = FakeEverOSClient()
backend = FakeBackendClient()
audio = base64.b64encode(b"wav bytes").decode("ascii")
content = [
{"type": "text", "text": "remember the picture and audio"},
@ -564,7 +564,7 @@ async def test_add_memory_forwards_multimodal_payload_to_everos(
"name": "simple-multimodal-image.png",
},
]
async with app_client(config, everos) as client:
async with app_client(config, backend) as client:
user_key = await create_user(client)
response = await client.post(
"/memories/add",
@ -588,9 +588,9 @@ async def test_add_memory_forwards_multimodal_payload_to_everos(
assert response.status_code == 200, response.text
assert response.json() == {
"session_id": "chat:c_multimodal",
"everos": {"request_id": "add", "data": {"status": "accumulated"}},
"backend": {"request_id": "add", "data": {"status": "accumulated"}},
}
assert everos.add_calls == [
assert backend.add_calls == [
{
"session_id": "chat:c_multimodal",
"app_id": "default",
@ -608,11 +608,11 @@ async def test_add_memory_forwards_multimodal_payload_to_everos(
@pytest.mark.asyncio
async def test_flush_memory_forwards_request_to_everos(
async def test_flush_memory_forwards_request_to_backend(
config: GatewayConfig,
) -> None:
everos = FakeEverOSClient()
async with app_client(config, everos) as client:
backend = FakeBackendClient()
async with app_client(config, backend) as client:
user_key = await create_user(client)
response = await client.post(
"/memories/flush",
@ -628,9 +628,9 @@ async def test_flush_memory_forwards_request_to_everos(
assert response.status_code == 200, response.text
assert response.json() == {
"session_id": "chat:c_multimodal",
"everos": {"request_id": "flush", "data": {"status": "extracted"}},
"backend": {"request_id": "flush", "data": {"status": "extracted"}},
}
assert everos.flush_calls == [
assert backend.flush_calls == [
{
"session_id": "chat:c_multimodal",
"app_id": "default",
@ -643,10 +643,10 @@ async def test_flush_memory_forwards_request_to_everos(
async def test_deleted_resource_is_excluded_from_resource_scope_search(
config: GatewayConfig,
) -> None:
everos = FakeEverOSClient(
backend = FakeBackendClient(
[{"id": "mem_1", "session_id": "resource:u_123:r_live", "episode": "live"}]
)
async with app_client(config, everos) as client:
async with app_client(config, backend) as client:
user_key = await create_user(client)
created = await client.post(
"/resources",
@ -671,7 +671,7 @@ async def test_deleted_resource_is_excluded_from_resource_scope_search(
assert delete_response.status_code == 200
assert search_response.status_code == 200
assert everos.search_calls == []
assert backend.search_calls == []
assert search_response.json()["results"] == []
repo = MemoryRepository(config.database_path)
deleted = repo.get_resource(resource_id)
@ -691,14 +691,14 @@ async def test_tombstone_filters_search_results(
session_id=None,
reason="user deleted",
)
everos = FakeEverOSClient(
backend = FakeBackendClient(
[
{"id": "mem_deleted", "session_id": "resource:u_123:r_1", "episode": "x"},
{"id": "mem_live", "session_id": "resource:u_123:r_1", "episode": "y"},
]
)
async with app_client(config, everos) as client:
async with app_client(config, backend) as client:
user_key = await create_user(client)
response = await client.post(
"/memories/search",
@ -721,10 +721,10 @@ async def test_override_replaces_search_result_text(
repo: MemoryRepository,
) -> None:
create_test_resource(repo, resource_id="r_1", user_id="u_123")
everos = FakeEverOSClient(
backend = FakeBackendClient(
[{"id": "mem_1", "session_id": "resource:u_123:r_1", "episode": "old text"}]
)
async with app_client(config, everos) as client:
async with app_client(config, backend) as client:
user_key = await create_user(client)
patch_response = await client.patch(
"/memories/mem_1",
@ -758,8 +758,8 @@ async def test_memory_override_rejects_session_owned_by_another_user(
repo: MemoryRepository,
) -> None:
create_test_resource(repo, resource_id="r_bob", user_id="bob")
everos = FakeEverOSClient()
async with app_client(config, everos) as client:
backend = FakeBackendClient()
async with app_client(config, backend) as client:
user_key = await create_user(client, "alice")
response = await client.patch(
"/memories/mem_1",
@ -780,8 +780,8 @@ async def test_memory_delete_requires_owned_session(
config: GatewayConfig,
repo: MemoryRepository,
) -> None:
everos = FakeEverOSClient()
async with app_client(config, everos) as client:
backend = FakeBackendClient()
async with app_client(config, backend) as client:
user_key = await create_user(client, "u_123")
response = await client.request(
"DELETE",
@ -802,8 +802,8 @@ async def test_memory_delete_requires_owned_session(
async def test_list_resources_returns_only_not_deleted(
config: GatewayConfig,
) -> None:
everos = FakeEverOSClient()
async with app_client(config, everos) as client:
backend = FakeBackendClient()
async with app_client(config, backend) as client:
user_key = await create_user(client)
first = await client.post(
"/resources",
@ -837,8 +837,8 @@ async def test_delete_memory_writes_tombstone(
repo: MemoryRepository,
) -> None:
create_test_resource(repo, resource_id="r_1", user_id="u_123")
everos = FakeEverOSClient()
async with app_client(config, everos) as client:
backend = FakeBackendClient()
async with app_client(config, backend) as client:
user_key = await create_user(client)
response = await client.request(
"DELETE",