"""Self-check for the CLI bridge plumbing (no XMPP server needed).""" import re from xmpp_cli_bridge.__main__ import make_handler from xmpp_cli_bridge import _XML_BAD # plain echoer for quiet mode: response only, no prompt ECHOER = ( "import sys\nfor line in sys.stdin:" "sys.stdout.write('in: ' + line);sys.stdout.flush()\n" ) # REPL-style child like pass-asker/agent.py: startup prompt, then # response followed by the next prompt (no trailing newline) PCHOICE = ( "import sys\nsys.stdout.write('> ');sys.stdout.flush()\n" "for line in sys.stdin:" "sys.stdout.write('in: ' + line + '> ');sys.stdout.flush()\n" ) def test_roundtrip_and_allowlist(): h = make_handler(["python3", "-c", ECHOER], {"me@example.com"}) # authorized sender gets the child's reply assert h("hello", "me@example.com/res") == "in: hello\n" # unauthorized sender gets silence and the child saw nothing assert h("evil", "attacker@evil.net/x") is None assert h("hello", "me@example.com") == "in: hello\n" def test_prompt_mode(): h = make_handler(["python3", "-c", PCHOICE], {"a@b"}, prompt=re.compile(r"> $")) # startup prompt is drained, trailing prompt is stripped assert h("hello", "a@b") == "in: hello\n" def test_drain_discards_stale_prompt(): h = make_handler(["python3", "-c", PCHOICE], {"a@b"}, prompt=re.compile(r"> $")) assert h("first", "a@b") == "in: first\n" assert h("second", "a@b") == "in: second\n" def test_multiline_anchored_prompt(): # like pass-asker: multiline output, prompt after a newline, no trailing NL h = make_handler(["python3", "-c", PCHOICE], {"a@b"}, prompt=re.compile(r"^> $")) assert h("hello", "a@b") == "in: hello\n" def test_timeout_still_strips_prompt(): # child that never emits the prompt again: 30s wait is too slow for CI, so # use max_wait to shrink it and assert the trailing prompt is still gone never = "import sys\nsys.stdout.write('> ');sys.stdout.flush()\nfor line in sys.stdin:sys.stdout.write('x');sys.stdout.flush()\n" h = make_handler( ["python3", "-c", never], {"a@b"}, prompt=re.compile(r"^> $"), max_wait=1.5, ) assert h("go", "a@b") == "x" def test_slow_producer_with_prompt(): # child that sleeps before answering: quiet-mode would cut at 0.5s of # silence; prompt mode must wait for the prompt instead slow = "import sys,time\nsys.stdout.write('> ');sys.stdout.flush()\nfor line in sys.stdin:\n time.sleep(1.2)\n sys.stdout.write('out: ' + line + '> ')\n sys.stdout.flush()\n" h = make_handler(["python3", "-c", slow], {"a@b"}, prompt=re.compile(r"> $")) assert h("slow", "a@b") == "out: slow\n" def test_dead_process_reported(): h = make_handler( ["python3", "-c", "import sys;sys.stdin.readline();sys.exit(3)"], {"a@b"} ) assert h("go", "a@b") == "process exited (3)" def test_prompt_like_line_mid_reply_does_not_stall(): # a line ending with "> " inside the reply (blockquote, PS2 example) # must not shadow the real trailing prompt; search()'s leftmost match # used to, stalling until the max-wait timeout tricky = ( "import sys\nsys.stdout.write('> ');sys.stdout.flush()\n" "for line in sys.stdin:\n" " sys.stdout.write('note:\\n> \\nreply: ' + line + '> ')\n" " sys.stdout.flush()\n" ) h = make_handler( ["python3", "-c", tricky], {"a@b"}, prompt=re.compile(r"> $"), max_wait=3 ) assert h("hello", "a@b") == "note:\n> \nreply: hello\n" def test_xml_bad_stripped(): assert _XML_BAD.sub("", "a\x1b[0mb\x07c\x00d") == "a[0mbcd" # surrogates and other XML-illegal chars are also removed assert _XML_BAD.sub("", "ok\ud800") == "ok" def test_startup_prompt_raced_by_first_message(): # child prints its first prompt LATE: the bridge must consume it at # startup, otherwise the first message is answered with an empty reply # and its real answer is discarded by the next drain sleepy = ( "import sys,time\ntime.sleep(0.3)\n" "sys.stdout.write('> ');sys.stdout.flush()\n" "for line in sys.stdin:" "sys.stdout.write('in: ' + line + '> ');sys.stdout.flush()\n" ) h = make_handler(["python3", "-c", sleepy], {"a@b"}, prompt=re.compile(r"> $")) assert h("first", "a@b") == "in: first\n" def test_blank_reply_suppressed(): # child that completes a turn with no output at all blank = ( "import sys\nsys.stdout.write('> ');sys.stdout.flush()\n" "for line in sys.stdin:sys.stdout.write('> ');sys.stdout.flush()\n" ) h = make_handler(["python3", "-c", blank], {"a@b"}, prompt=re.compile(r"> $")) # an empty turn is silence, not an empty XMPP message assert h("x", "a@b") is None