|
| 1 | +"""Tests for conversation persona inheritance behavior.""" |
| 2 | + |
| 3 | +from types import SimpleNamespace |
| 4 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from astrbot.builtin_stars.builtin_commands.commands.conversation import ( |
| 9 | + ConversationCommands, |
| 10 | +) |
| 11 | +from astrbot.core.conversation_mgr import ConversationManager |
| 12 | +from astrbot.core.persona_utils import PERSONA_NONE_MARKER |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.asyncio |
| 16 | +async def test_new_conversation_inherits_current_persona_when_not_provided(): |
| 17 | + db = MagicMock() |
| 18 | + db.get_conversation_by_id = AsyncMock( |
| 19 | + return_value=SimpleNamespace(persona_id="psychologist") |
| 20 | + ) |
| 21 | + db.create_conversation = AsyncMock( |
| 22 | + return_value=SimpleNamespace(conversation_id="new-cid") |
| 23 | + ) |
| 24 | + |
| 25 | + manager = ConversationManager(db) |
| 26 | + manager.session_conversations["test:private:u1"] = "old-cid" |
| 27 | + |
| 28 | + with patch( |
| 29 | + "astrbot.core.conversation_mgr.sp.session_put", |
| 30 | + new=AsyncMock(return_value=None), |
| 31 | + ): |
| 32 | + await manager.new_conversation("test:private:u1", platform_id="test") |
| 33 | + |
| 34 | + assert db.create_conversation.await_args.kwargs["persona_id"] == "psychologist" |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.asyncio |
| 38 | +async def test_new_conversation_does_not_inherit_persona_none_marker(): |
| 39 | + db = MagicMock() |
| 40 | + db.get_conversation_by_id = AsyncMock( |
| 41 | + return_value=SimpleNamespace(persona_id=PERSONA_NONE_MARKER) |
| 42 | + ) |
| 43 | + db.create_conversation = AsyncMock( |
| 44 | + return_value=SimpleNamespace(conversation_id="new-cid") |
| 45 | + ) |
| 46 | + |
| 47 | + manager = ConversationManager(db) |
| 48 | + manager.session_conversations["test:private:u1"] = "old-cid" |
| 49 | + |
| 50 | + with patch( |
| 51 | + "astrbot.core.conversation_mgr.sp.session_put", |
| 52 | + new=AsyncMock(return_value=None), |
| 53 | + ): |
| 54 | + await manager.new_conversation("test:private:u1", platform_id="test") |
| 55 | + |
| 56 | + assert db.create_conversation.await_args.kwargs["persona_id"] is None |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.asyncio |
| 60 | +async def test_new_conversation_keeps_explicit_persona_id(): |
| 61 | + db = MagicMock() |
| 62 | + db.get_conversation_by_id = AsyncMock( |
| 63 | + return_value=SimpleNamespace(persona_id="psychologist") |
| 64 | + ) |
| 65 | + db.create_conversation = AsyncMock( |
| 66 | + return_value=SimpleNamespace(conversation_id="new-cid") |
| 67 | + ) |
| 68 | + |
| 69 | + manager = ConversationManager(db) |
| 70 | + manager.session_conversations["test:private:u1"] = "old-cid" |
| 71 | + |
| 72 | + with patch( |
| 73 | + "astrbot.core.conversation_mgr.sp.session_put", |
| 74 | + new=AsyncMock(return_value=None), |
| 75 | + ): |
| 76 | + await manager.new_conversation( |
| 77 | + "test:private:u1", |
| 78 | + platform_id="test", |
| 79 | + persona_id="teacher", |
| 80 | + ) |
| 81 | + |
| 82 | + assert db.create_conversation.await_args.kwargs["persona_id"] == "teacher" |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.asyncio |
| 86 | +async def test_get_current_persona_id_returns_none_for_none_marker(): |
| 87 | + context = MagicMock() |
| 88 | + context.conversation_manager.get_curr_conversation_id = AsyncMock( |
| 89 | + return_value="old-cid" |
| 90 | + ) |
| 91 | + context.conversation_manager.get_conversation = AsyncMock( |
| 92 | + return_value=MagicMock(persona_id=PERSONA_NONE_MARKER) |
| 93 | + ) |
| 94 | + |
| 95 | + command = ConversationCommands(context) |
| 96 | + |
| 97 | + result = await command._get_current_persona_id("test:private:u1") |
| 98 | + |
| 99 | + assert result is None |
0 commit comments