1515 RoomMemberPayload ,
1616)
1717from wechaty_puppet .schemas .puppet import PuppetOptions
18-
19- WORKSPACE = dirname (dirname (abspath (__file__ )))
20- SCRIPT_DIR = join (WORKSPACE , "src" )
21- sys .path .append (SCRIPT_DIR )
22-
2318from wechaty .wechaty import Wechaty , WechatyOptions # noqa
19+ from wechaty .fake_puppet import FakePuppet
2420
2521
26- class FakePuppet (Puppet ):
27- """A fake puppet implementation that can be used for tests"""
28-
29- def __init__ (self , options : PuppetOptions , name : str = "fake_puppet" ):
30- super ().__init__ (options , name = name )
31-
32- self .fake_messages : MutableMapping [str , MessagePayload ] = {}
33- self .fake_rooms : MutableMapping [str , RoomPayload ] = {}
34- self .fake_contacts : MutableMapping [str , ContactPayload ] = {}
35- self .fake_room_members : Dict [str , List [RoomMemberPayload ]] = defaultdict (list )
36-
37- self .login_user_id = str (uuid4 ())
38-
39- def add_message (self , payload : MessagePayload ) -> None :
40- """Manually add a message that can be looked up later"""
41- print (payload .id )
42- self .fake_messages [payload .id ] = payload
43-
44- def add_room (self , payload : RoomPayload ) -> None :
45- """Manually add a room that can be looked up later"""
46- self .fake_rooms [payload .id ] = payload
47-
48- def add_contact (self , payload : ContactPayload ) -> None :
49- """Manually add a contact that can be looked up later"""
50- self .fake_contacts [payload .id ] = payload
51-
52- def add_room_member (self , room_id : str , payload : RoomMemberPayload ) -> None :
53- """Manually add a room member that can be looked up later"""
54- self .fake_room_members [room_id ].append (payload )
55-
56- async def message_search (self , query : Optional [MessageQueryFilter ] = None ) -> List [str ]:
57- return [query .id ]
58-
59- async def room_search (self , query : Optional [MessageQueryFilter ] = None ) -> List [str ]:
60- return self .fake_rooms [query .id ] if query else self .fake_rooms .keys ()
61-
62- async def room_members (self , room_id : str ) -> List [str ]:
63- return [member .id for member in self .fake_room_members [room_id ]]
64-
65- async def message_payload (self , message_id : str ) -> MessagePayload :
66- print (f"Finding { message_id } " )
67- return self .fake_messages [message_id ]
68-
69- async def room_member_payload (
70- self , room_id : str , contact_id : str
71- ) -> Optional [RoomMemberPayload ]:
72- for member in self .fake_room_members [room_id ]:
73- if member .id == contact_id :
74- return member
75- return None
76-
77- async def room_payload (self , room_id : str ) -> RoomPayload :
78- return self .fake_rooms [room_id ]
79-
80- async def contact_payload (self , contact_id : str ) -> ContactPayload :
81- return self .fake_contacts [contact_id ]
82-
83- def self_id (self ) -> str :
84- return self .login_user_id
85-
8622@pytest .fixture
8723async def test_bot () -> Wechaty :
8824 """Initialize a Wechaty instance and return it"""
8925 puppet = FakePuppet (options = PuppetOptions ())
90- puppet .add_contact (ContactPayload ("wechaty_user" , name = "Wechaty User" ))
91- puppet .add_contact (ContactPayload ("fake_user" , name = "Fake User" ))
92- puppet .add_contact (ContactPayload ("test_user" , name = "Test User" ))
93- puppet .add_room (
26+ puppet .add_fake_contact (ContactPayload ("wechaty_user" , name = "Wechaty User" ))
27+ puppet .add_fake_contact (ContactPayload ("fake_user" , name = "Fake User" ))
28+ puppet .add_fake_contact (ContactPayload ("test_user" , name = "Test User" ))
29+ puppet .add_fake_room (
9430 RoomPayload (
9531 id = "test_room" ,
9632 topic = "test_room" ,
9733 owner_id = "wechaty_user" ,
9834 member_ids = ["wechaty_user" , "fake_user" , "test_user" ],
9935 )
10036 )
101- puppet .add_room (
37+ puppet .add_fake_room (
10238 RoomPayload (
10339 id = "fake_room" ,
10440 topic = "fake_room" ,
10541 owner_id = "wechaty_user" ,
10642 member_ids = ["wechaty_user" , "fake_user" , "test_user" ],
10743 )
10844 )
109- puppet .add_room_member ("fake_room" , RoomMemberPayload ("wechaty_user" ))
110- puppet .add_room_member ("fake_room" , RoomMemberPayload ("fake_user" , room_alias = "Fake Alias" ))
111- puppet .add_room_member ("fake_room" , RoomMemberPayload ("test_user" ))
112- puppet .add_message (
45+ puppet .add_fake_room_members (
46+ "fake_room" ,
47+ [
48+ RoomMemberPayload ("wechaty_user" ),
49+ RoomMemberPayload ("fake_user" , room_alias = "Fake Alias" ),
50+ RoomMemberPayload ("test_user" )
51+ ]
52+ )
53+ puppet .add_fake_message (
11354 MessagePayload ("no_mention" , text = "foo bar asd" , type = MessageType .MESSAGE_TYPE_TEXT )
11455 )
115- puppet .add_message (
56+ puppet .add_fake_message (
11657 MessagePayload (
11758 "room_no_mention" ,
11859 text = "beep" ,
11960 room_id = "fake_room" ,
12061 type = MessageType .MESSAGE_TYPE_TEXT ,
12162 )
12263 )
123- puppet .add_message (
64+ puppet .add_fake_message (
12465 MessagePayload (
12566 "room_with_mentions" ,
12667 text = "@Wechaty User @Test User test message asd" ,
@@ -129,7 +70,7 @@ async def test_bot() -> Wechaty:
12970 mention_ids = ["wechaty_user" , "test_user" ],
13071 )
13172 )
132- puppet .add_message (
73+ puppet .add_fake_message (
13374 MessagePayload (
13475 "room_with_mentions_and_alias" ,
13576 text = "123123 @Wechaty User @Test User @Fake Alias kkasd" ,
@@ -138,7 +79,7 @@ async def test_bot() -> Wechaty:
13879 mention_ids = ["wechaty_user" , "test_user" , "fake_user" ],
13980 )
14081 )
141- puppet .add_message (
82+ puppet .add_fake_message (
14283 MessagePayload (
14384 "room_with_mentions_and_alias_mismatched" ,
14485 text = "123123@Wechaty User @Test User @Fake User beep" ,
@@ -147,7 +88,7 @@ async def test_bot() -> Wechaty:
14788 mention_ids = ["wechaty_user" , "test_user" , "fake_user" ],
14889 )
14990 )
150- puppet .add_message (
91+ puppet .add_fake_message (
15192 MessagePayload (
15293 "room_with_text_mentions" ,
15394 text = "@Wechaty User @Test User @Fake Alias beep!!" ,
0 commit comments