diff options
| author | 2024-01-26 17:51:30 +0800 | |
|---|---|---|
| committer | 2024-01-26 17:51:30 +0800 | |
| commit | 1e31d50b227898fcf47175dcee682300ea077896 (patch) | |
| tree | b9f230f0906a204b71c91cf34903e89845b019e8 /tests | |
| parent | c89694f25be9ea966c4e7eedf9dfb6de8f72dbef (diff) | |
| download | infini-1e31d50b227898fcf47175dcee682300ea077896.tar.gz infini-1e31d50b227898fcf47175dcee682300ea077896.zip | |
:sparkles: feat(ci): add core tests
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_core.py | 75 |
1 files changed, 75 insertions, 0 deletions
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"} |
