"""mem0 云连通性集成测试。默认不跑(需真实 key);执行:pytest -m integration

验证目标:用真实 MEM0_API_KEY 走通 add → search 往返,确认 SDK 接口与密钥可用。
mem0 抽取/索引是最终一致的,故对"立即搜到刚写入"不做强断言,只验证接口契约与连通。
"""

import pytest

from genesis import config
from genesis.memory.record import MemoryRecord

pytestmark = pytest.mark.skipif(not config.MEM0_API_KEY, reason="未配置 MEM0_API_KEY")

_TEST_AGENT = "genesis-ci-probe"


@pytest.mark.integration
def test_mem0_add_and_search_roundtrip():
    from genesis.memory.mem0_store import Mem0Store

    store = Mem0Store(api_key=config.MEM0_API_KEY)
    try:
        rid = store.add(
            MemoryRecord(
                agent_id=_TEST_AGENT,
                content="Linus loves spicy Sichuan hotpot and hates cold weather.",
                created_at=8.0,
                importance=0.7,
            )
        )
        assert isinstance(rid, str)  # 接口契约:返回字符串 id(可能为空,视抽取结果)

        hits = store.search("food preference", agent_id=_TEST_AGENT, limit=5)
        assert isinstance(hits, list)  # 连通性 + 返回结构正确
        for h in hits:
            assert 0.0 <= h.relevance <= 1.0
    finally:
        store.clear(_TEST_AGENT)  # 清理,避免污染
