aboutsummaryrefslogtreecommitdiff
path: root/xmpp-cli-bridge/tests
diff options
context:
space:
mode:
Diffstat (limited to 'xmpp-cli-bridge/tests')
-rw-r--r--xmpp-cli-bridge/tests/test_agent_wrapper.py28
-rw-r--r--xmpp-cli-bridge/tests/test_cli.py119
2 files changed, 147 insertions, 0 deletions
diff --git a/xmpp-cli-bridge/tests/test_agent_wrapper.py b/xmpp-cli-bridge/tests/test_agent_wrapper.py
new file mode 100644
index 0000000..c5cddc1
--- /dev/null
+++ b/xmpp-cli-bridge/tests/test_agent_wrapper.py
@@ -0,0 +1,28 @@
+"""Self-check for the coding-agent wrapper (no XMPP server needed)."""
+from xmpp_cli_bridge.agent import PROMPT
+from xmpp_cli_bridge.__main__ import make_handler
+
+# replicates agent.py's I/O shape: startup prompt, then "<turn>(N) > "
+AGENT_LIKE = (
+ "import sys\nsys.stdout.write('> ');sys.stdout.flush()\n"
+ "for line in sys.stdin:\n"
+ " sys.stdout.write('turn: ' + line + '(42) > ')\n"
+ " sys.stdout.flush()\n"
+)
+
+
+def test_prompt_matches_both_forms():
+ assert PROMPT.search("reply text\n> ")
+ assert PROMPT.search("reply text\n(78412) > ")
+ # a reply merely containing '>' is not matched as the prompt
+ m = PROMPT.search("a > b\n> ")
+ assert m and m.end() == len("a > b\n> ")
+
+
+def test_agent_like_roundtrip():
+ h = make_handler(
+ ["python3", "-c", AGENT_LIKE], {"a@b"}, prompt=PROMPT, max_wait=5
+ )
+ # startup prompt drained, token-usage-style prompt stripped
+ assert h("list files", "a@b") == "turn: list files\n"
+ assert h("second", "a@b") == "turn: second\n"
diff --git a/xmpp-cli-bridge/tests/test_cli.py b/xmpp-cli-bridge/tests/test_cli.py
new file mode 100644
index 0000000..fbe5188
--- /dev/null
+++ b/xmpp-cli-bridge/tests/test_cli.py
@@ -0,0 +1,119 @@
+"""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 \ No newline at end of file