forked from modelscope/agentscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsghub_test.py
102 lines (78 loc) · 2.52 KB
/
msghub_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# -*- coding: utf-8 -*-
""" Unit test for msghub."""
import unittest
from typing import Optional
from agentscope.agents import AgentBase
from agentscope import msghub
from agentscope.message import Msg
class TestAgent(AgentBase):
"""Test agent class for msghub."""
def reply(self, x: Optional[dict] = None) -> dict:
"""Reply function for agent."""
if x is not None:
self.memory.add(x)
return x
else:
return {}
class MsgHubTest(unittest.TestCase):
"""
Test for MsgHub
"""
def setUp(self) -> None:
"""Init for ExampleTest."""
self.wisper = TestAgent("wisper")
self.agent1 = TestAgent("agent1")
self.agent2 = TestAgent("agent2")
self.agent3 = TestAgent("agent3")
def test_msghub_operation(self) -> None:
"""Test add, delete and broadcast operations"""
msg1 = Msg(name="a1", content="msg1")
msg2 = Msg(name="a2", content="msg2")
msg3 = Msg(name="a3", content="msg3")
msg4 = Msg(name="a4", content="msg4")
with msghub(participants=[self.agent1, self.agent2]) as hub:
self.agent1(msg1)
self.agent2(msg2)
hub.delete(self.agent1)
hub.add(self.agent3)
self.agent3(msg3)
hub.broadcast(msg4)
self.assertListEqual(
self.agent2.memory.get_memory(),
[
msg1,
msg2,
msg3,
msg4,
],
)
self.assertListEqual(self.agent1.memory.get_memory(), [msg1, msg2])
self.assertListEqual(self.agent3.memory.get_memory(), [msg3, msg4])
def test_msghub(self) -> None:
"""msghub test."""
ground_truth = [
Msg(
name="w1",
content="This secret that my password is 123456 can't be"
" leaked!",
role="wisper",
),
]
with msghub(participants=[self.wisper, self.agent1, self.agent2]):
self.wisper(ground_truth)
# agent1 and agent2 heard wisper's secret!
self.assertListEqual(
self.agent1.memory.get_memory(),
ground_truth,
)
self.assertListEqual(
self.agent2.memory.get_memory(),
ground_truth,
)
# agent3 didn't hear wisper's secret!
self.assertListEqual(
self.agent3.memory.get_memory(),
[],
)
if __name__ == "__main__":
unittest.main()