初始化提交
This commit is contained in:
commit
887c1f449e
963 files changed
+166869
No files matched your search
@@ -0,0 +1,76 @@
|
||||
"""大周天记忆/世界抽取生成测试(_phase_memory 阶段)。
|
||||
|
||||
高置信度(≥0.7)低敏感度记忆候选自动落地到 memories 表;
|
||||
世界实体候选(confidence=0.6)低于阈值仍留 pending。
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
from app.application.big_cycle import drain_pending_big_cycles, maybe_trigger_big_cycle
|
||||
from app.db.session import AsyncSessionLocal
|
||||
from app.models.memory import Memory
|
||||
from app.repositories import world as world_repo
|
||||
from app.services.characters import get_active_character_model
|
||||
|
||||
_EXTRACTION_RESPONSE = (
|
||||
'{"memories":[{"type":"preference","title":"喜欢简洁","content":"用户偏好简洁",'
|
||||
'"importance":0.8,"sensitivity":"low","confidence":0.7}],'
|
||||
'"entities":[{"type":"project","name":"项目X","attributes":{"status":"active"},'
|
||||
'"state_type":"inference","confidence":0.6}]}'
|
||||
)
|
||||
|
||||
|
||||
async def _seed_memories(character_id: str, count: int) -> None:
|
||||
async with AsyncSessionLocal() as session:
|
||||
for i in range(count):
|
||||
session.add(
|
||||
Memory(
|
||||
character_id=character_id,
|
||||
type="user",
|
||||
title=f"记忆{i}",
|
||||
content=f"内容{i}",
|
||||
)
|
||||
)
|
||||
await session.commit()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_extraction_chat(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
async def fake_chat(*_args, **_kwargs):
|
||||
return _EXTRACTION_RESPONSE
|
||||
|
||||
monkeypatch.setattr("app.application.big_cycle._call_model", fake_chat)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_big_cycle_extracts_memory_and_world(
|
||||
mock_extraction_chat, # noqa: ARG001 - pytest fixture 靠 monkeypatch 副作用生效
|
||||
) -> None:
|
||||
async with AsyncSessionLocal() as session:
|
||||
character = await get_active_character_model(session)
|
||||
await _seed_memories(character.id, 20)
|
||||
|
||||
cycle_id = await maybe_trigger_big_cycle(character.id, trigger="test")
|
||||
assert cycle_id is not None
|
||||
await drain_pending_big_cycles()
|
||||
|
||||
async with AsyncSessionLocal() as session:
|
||||
# 高置信度(≥0.7)低敏感度记忆自动落地到 memories 表
|
||||
from sqlalchemy import select
|
||||
|
||||
result = await session.execute(
|
||||
select(Memory).where(
|
||||
Memory.character_id == character.id,
|
||||
Memory.source == "big_cycle",
|
||||
)
|
||||
)
|
||||
auto_memories = result.scalars().all()
|
||||
# 世界实体候选(confidence=0.6 < 0.7)仍留 pending
|
||||
entities, _ = await world_repo.list_entities(session, character.id, status="pending")
|
||||
|
||||
assert len(auto_memories) == 1
|
||||
assert auto_memories[0].title == "喜欢简洁"
|
||||
assert len(entities) == 1
|
||||
assert entities[0].name == "项目X"
|
||||
assert entities[0].status == "pending"
|
||||
assert entities[0].state_type == "inference"
|
||||
Reference in new issue
Block a user