aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/test_handlers.py84
-rw-r--r--tests/test_input.py16
-rw-r--r--tests/test_interceptor.py33
3 files changed, 133 insertions, 0 deletions
diff --git a/tests/test_handlers.py b/tests/test_handlers.py
new file mode 100644
index 00000000..8d2d2894
--- /dev/null
+++ b/tests/test_handlers.py
@@ -0,0 +1,84 @@
+from infini.handler import Handler
+from infini.input import Input
+from infini.output import Output
+from infini.router import StartswithRouter
+
+
+def test_handler():
+ input = Input(".add 1 2")
+
+ def add(input: Input) -> Output:
+ a, b = map(int, input.get_plain_text().lstrip(".add").split())
+ output = Output()
+ output.block = False
+ output.status = 0
+ output.type = "text"
+ output.name = str(a + b)
+ return output
+
+ def cmd(_: Input) -> Output:
+ output = Output()
+ output.block = False
+ output.status = 0
+ output.type = "text"
+ output.name = "cmd"
+ return output
+
+ handler = Handler()
+ handler.handlers = [
+ {
+ "priority": 2,
+ "router": StartswithRouter(".add"),
+ "handler": add,
+ },
+ {
+ "priority": 1,
+ "router": StartswithRouter("."),
+ "handler": cmd,
+ },
+ ]
+
+ names = []
+ for output in handler.input(input):
+ names.append(output.name)
+ assert names == ["cmd", "3"]
+
+
+def test_handler_block():
+ input = Input(".add 1 2")
+
+ def add(input: Input) -> Output:
+ a, b = map(int, input.get_plain_text().lstrip(".add").split())
+ output = Output()
+ output.block = False
+ output.status = 0
+ output.type = "text"
+ output.name = str(a + b)
+ return output
+
+ def cmd(_: Input) -> Output:
+ output = Output()
+ output.block = True
+ output.status = 0
+ output.type = "text"
+ output.name = "cmd"
+ return output
+
+ handler = Handler()
+ handler.handlers = [
+ {
+ "priority": 2,
+ "router": StartswithRouter(".add"),
+ "handler": add,
+ },
+ {
+ "priority": 1,
+ "router": StartswithRouter("."),
+ "handler": cmd,
+ },
+ ]
+
+ names = []
+ for output in handler.input(input):
+ names.append(output.name)
+ assert names == ["cmd"]
diff --git a/tests/test_input.py b/tests/test_input.py
new file mode 100644
index 00000000..5f44ca1e
--- /dev/null
+++ b/tests/test_input.py
@@ -0,0 +1,16 @@
+from infini.input import Input
+
+
+def test_new_input_without_vars():
+ assert Input("test plain_str").plain_data == "test plain_str"
+ assert Input("test plain_str").get_plain_text() == "test plain_str"
+
+
+def test_new_input_with_session_id():
+ input = Input("test plain_str", variables={"session_id": "test"})
+ assert input.get_session_id() == "test"
+
+
+def test_new_input_without_session_id():
+ input = Input("test plain_str", variables={"user_id": "test"})
+ assert input.get_session_id() == "session_unknown_test"
diff --git a/tests/test_interceptor.py b/tests/test_interceptor.py
new file mode 100644
index 00000000..df5b6750
--- /dev/null
+++ b/tests/test_interceptor.py
@@ -0,0 +1,33 @@
+from infini.input import Input
+from infini.interceptor import Interceptor
+from infini.output import Output
+from infini.router import ContainsRouter
+
+
+def test_interceptor():
+ input = Input("这个人叫简律纯.")
+ valid_input = Input("这个叫苏向夜.")
+
+ def intercept(_: Input) -> Input | Output:
+ output = Output()
+ output.block = True # TODO 拦截器阻塞标识
+ output.name = "block.jianlvchun"
+ output.status = 0
+ output.type = "text"
+ return output
+
+ interceptor = Interceptor()
+ interceptor.interceptors = [
+ {
+ "priority": 1,
+ "router": ContainsRouter("简律纯"),
+ "handler": intercept,
+ }
+ ]
+ output = interceptor.input(input)
+ assert isinstance(output, Output)
+ assert output.name == "block.jianlvchun"
+
+ valid_output = interceptor.input(valid_input)
+ assert isinstance(valid_output, Input)
+ assert valid_output.get_plain_text() == "这个叫苏向夜."