"""Tests for Chat memory integration — 记忆注入 + MemoryTool + 日志生成 (U4).""" from pathlib import Path from unittest.mock import AsyncMock, MagicMock, patch import pytest from agentkit.memory.profile import MemoryStore, MemorySnapshot from agentkit.tools.memory_tool import MemoryTool class TestChatMemoryInjection: """Chat 启动时记忆注入 system prompt 测试.""" def test_memory_store_initializes_with_base_dir(self, tmp_path: Path): store = MemoryStore(base_dir=tmp_path) store.ensure_defaults() snapshot = store.load_all() prompt = store.build_system_prompt(snapshot, "Be helpful.") assert "" in prompt assert "AgentKit" in prompt assert "Be helpful." in prompt def test_no_memory_files_returns_base_prompt(self, tmp_path: Path): store = MemoryStore(base_dir=tmp_path) snapshot = store.load_all() prompt = store.build_system_prompt(snapshot, "Be helpful.") assert prompt == "Be helpful." def test_default_soul_injected(self, tmp_path: Path): store = MemoryStore(base_dir=tmp_path) store.ensure_defaults() snapshot = store.load_all() prompt = store.build_system_prompt(snapshot) assert "" in prompt assert "专业" in prompt or "AgentKit" in prompt class TestChatMemoryToolAvailable: """MemoryTool 在对话中可用测试.""" async def test_memory_tool_in_tools_list(self, tmp_path: Path): store = MemoryStore(base_dir=tmp_path) tool = MemoryTool(memory_store=store) assert tool.name == "memory" assert tool.input_schema is not None async def test_memory_tool_add_and_read(self, tmp_path: Path): store = MemoryStore(base_dir=tmp_path) tool = MemoryTool(memory_store=store) # Add result = await tool.execute(action="add", file="user", section="称呼", content="叫我老板") assert result["success"] is True # Read result = await tool.execute(action="read", file="user") assert "老板" in result["content"] class TestChatMemoryPersistence: """记忆跨 /clear 会话持久化测试.""" def test_memory_survives_session_clear(self, tmp_path: Path): """/clear 只清除会话历史,不清除记忆文件.""" store = MemoryStore(base_dir=tmp_path) store.get_file("user").write("## 称呼\n叫我老板") # 模拟 /clear — 重新创建 MemoryStore store2 = MemoryStore(base_dir=tmp_path) content = store2.get_file("user").read() assert "老板" in content def test_memory_persists_across_store_instances(self, tmp_path: Path): store1 = MemoryStore(base_dir=tmp_path) store1.get_file("memory").write("## 项目\nAgentKit框架") store2 = MemoryStore(base_dir=tmp_path) content = store2.get_file("memory").read() assert "AgentKit" in content class TestChatDailyLogGeneration: """会话结束时日志生成测试.""" def test_daily_file_path_is_today(self, tmp_path: Path): from datetime import datetime, timezone store = MemoryStore(base_dir=tmp_path) daily = store.get_file("daily") today = datetime.now(timezone.utc).strftime("%Y-%m-%d") assert today in str(daily.path) def test_write_daily_log(self, tmp_path: Path): store = MemoryStore(base_dir=tmp_path) daily = store.get_file("daily") daily.write("讨论了AgentKit记忆系统架构") content = daily.read() assert "记忆系统" in content def test_daily_log_loads_in_snapshot(self, tmp_path: Path): store = MemoryStore(base_dir=tmp_path) store.get_file("daily").write("今天完成了记忆系统开发") snapshot = store.load_all() assert "记忆系统" in snapshot.daily