From 1e31d50b227898fcf47175dcee682300ea077896 Mon Sep 17 00:00:00 2001 From: 苏向夜 Date: Fri, 26 Jan 2024 17:51:30 +0800 Subject: :sparkles: feat(ci): add core tests --- tests/test_core.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 tests/test_core.py (limited to 'tests/test_core.py') diff --git a/tests/test_core.py b/tests/test_core.py new file mode 100644 index 00000000..91d96a2c --- /dev/null +++ b/tests/test_core.py @@ -0,0 +1,75 @@ +from infini.core import Core +from infini.generator import Generator +from infini.handler import Handler +from infini.input import Input +from infini.interceptor import Interceptor +from infini.output import Output +from infini.router import ContainsRouter, StartswithRouter + + +def test_core(): + command_input = Input(".add 1 2") + intercepted_input = Input("这个人叫简律纯.") + valid_input = Input("这个叫苏向夜.") + command_and_valid_input = Input(".echo 苏向夜打爆了简某人的狗头") + + def intercept(_: Input) -> Input | Output: + return Output("text", "block.jianlvchun", block=True) # TODO 拦截器阻塞标识 + + interceptor = Interceptor() + interceptor.interceptors = [ + { + "priority": 1, + "router": ContainsRouter("简律纯"), + "handler": intercept, + } + ] + + def add(input: Input) -> Output: + return Output( + "text", + str(sum(list(map(int, input.get_plain_text().lstrip(".add").split())))), + status=0, + block=False, + ) + + def cmd(_: Input) -> Output: + return Output("text", "cmd", status=0, block=False) + + handler = Handler() + handler.handlers = [ + { + "priority": 2, + "router": StartswithRouter(".add"), + "handler": add, + }, + { + "priority": 1, + "router": StartswithRouter("."), + "handler": cmd, + }, + ] + + core = Core() + core.handler = handler + core.interceptor = interceptor + core.pre_interceptor = interceptor + core.generator = Generator() + + outputs = set() + for output in core.input(command_input): + outputs.add(output) + assert outputs == {"cmd", "3"} + + count = 0 + for _ in core.input(valid_input): + count += 1 + assert count == 0 + + for output in core.input(intercepted_input): + assert output == "block.jianlvchun" + + outputs = set() + for output in core.input(command_and_valid_input): + outputs.add(output) + assert outputs == {"cmd"} -- cgit v1.2.3-70-g09d2