-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmockbot.py
81 lines (62 loc) · 2.48 KB
/
mockbot.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
import asyncio
import logging
import os
from forest.core import Message, QuestionBot
# Sample bot number alice
BOT_NUMBER = "+11111111111"
USER_NUMBER = "+22222222222"
USER_UUID = "11111111-1111-1111-1111-111111111111"
os.environ["ENV"] = "test"
class MockMessage(Message):
"""Makes a Mock Message that has a predefined source and uuid"""
def __init__(self, text: str) -> None:
self.text = text
self.full_text = text
self.source = USER_NUMBER
self.uuid = USER_UUID
self.group = ""
self.mentions: list[dict[str, str]] = []
super().__init__({})
class MockBot(QuestionBot):
"""Makes a bot that bypasses the normal start_process allowing
us to have an inbox and outbox that doesn't depend on Signal"""
async def start_process(self) -> None:
pass
async def send_input(self, text: str) -> None:
"""Puts a MockMessage in the inbox queue"""
await self.inbox.put(MockMessage(text))
async def get_output(self) -> str:
"""Reads messages in the outbox that would otherwise be sent over signal"""
try:
outgoing_msg = await asyncio.wait_for(self.outbox.get(), timeout=1)
return outgoing_msg["params"]["message"]
except asyncio.TimeoutError:
logging.error("timed out waiting for output")
return ""
async def get_cmd_output(self, text: str) -> str:
"""Runs commands as normal but intercepts the output instead of passing it onto signal"""
## TODO This currently does not guarantee that the output from get output will
# indeed be the output expected of what's just been put in input for now it mostly works,
# but will need to be addressed
await self.send_input(text)
return await self.get_output()
class Tree:
"""tree implementation for test dialog trees"""
def __init__(self, dialog: list[str], children: list["Tree"] = None) -> None:
if children is None:
children = []
self.dialog = dialog
self.children = children
def __str__(self) -> str:
return str(self.dialog)
__repr__ = __str__
def __getitem__(self, item: int) -> str:
return self.dialog[item]
def get_all_paths(self, path: list = None) -> list:
"""returns all paths"""
if path is None:
path = []
path.append(self)
if self.children:
return [child.get_all_paths(path[:]) for child in self.children]
return path