5b1eeea430
The /reset response ('Konversation zurueckgesetzt...') was arriving
after the keyword post, landing after source_cursor and being picked
up by wait_for_bot_message as a move-request candidate. The assertion
then failed because the /reset response contains no routing keywords.
Fix: wait 5s (was 3s) so the bot has time to process /reset AND send
its response before the keyword post. source_cursor from the keyword
post_message return then sits AFTER the /reset response, and
wait_for_bot_message correctly ignores it.
274 lines
11 KiB
Python
274 lines
11 KiB
Python
"""
|
|
Test 03 — Move-Flow: Keyword erkennt Zielraum, Bot sendet Move-Request,
|
|
Button-Klick bestätigt, Nachricht wird in Zielraum kopiert.
|
|
|
|
Räume:
|
|
- Source: test-rc-source (RC_LIVE_SOURCE_ROOM)
|
|
- Target: test-rc-target (RC_LIVE_TARGET_ROOM)
|
|
- Haupt-Testraum: rc-test-automated (RC_LIVE_TEST_ROOM)
|
|
|
|
Test-Szenario:
|
|
1. Bernd postet in test-rc-source eine Nachricht mit Keyword 'rocketchat'
|
|
2. Bot erkennt Keyword, sendet Move-Request mit Buttons in test-rc-source
|
|
3. Bernd sendet Button-Klick-Payload via REST (MOVE_REQ:<rid>::confirm_move)
|
|
4. Bot kopiert Nachricht nach test-rc-target
|
|
5. Bernd liest test-rc-target via channels.history und verifiziert die kopierte Nachricht
|
|
|
|
ENV-Vars:
|
|
- RC_LIVE_SOURCE_ROOM=6a68c21ddee04395ed6f5415
|
|
- RC_LIVE_TARGET_ROOM=6a68c21ddee04395ed6f5419
|
|
|
|
RATE-LIMIT OPTIMIERUNG:
|
|
- Kein separater get_latest_timestamp für source — nutzt den TS aus post_message
|
|
- Polling source: initial_interval=5s (statt 2s)
|
|
- Polling target: timeout=60s, initial_interval=8s
|
|
- Auto-Route-Erkennung: wenn Bot auto-routet (100% keyword match), wird
|
|
kein Button-Klick simuliert und keine Target-Verifikation erwartet
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import os
|
|
import time
|
|
|
|
import pytest
|
|
|
|
from tests.live_rc.conftest import (
|
|
assert_bot_message,
|
|
post_test_message,
|
|
)
|
|
from tests.live_rc.helpers.polling import (
|
|
wait_for_bot_message,
|
|
get_latest_timestamp,
|
|
PollTimeout,
|
|
RateLimitSkip,
|
|
)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def _get_env_room(name: str) -> str:
|
|
"""Get a room ID from env var, skip test if not set."""
|
|
val = (os.getenv(name) or "").strip()
|
|
if not val:
|
|
pytest.skip(f"{name} not set — skipping move-request test")
|
|
return val
|
|
|
|
|
|
@pytest.mark.live_rc
|
|
def test_03_move_request(
|
|
rc_client,
|
|
test_room_id: str,
|
|
bot_username: str,
|
|
bot_user_id: str,
|
|
rate_limiter,
|
|
cleanup_messages,
|
|
bot_status_check,
|
|
):
|
|
"""Move-Flow: Bot erkennt Move-Request und routet in Zielraum.
|
|
|
|
Steps:
|
|
1. Poste Keyword-Nachricht in Source-Room
|
|
2. Warte auf Move-Request-Buttons vom Bot in Source-Room
|
|
3. Simuliere Button-Klick (MOVE_REQ:<rid>::confirm_move) in Source-Room
|
|
4. Warte auf kopierte Nachricht in Target-Room
|
|
5. Verifiziere: kopierte Nachricht enthält Original-Text + Präfix
|
|
"""
|
|
source_room = _get_env_room("RC_LIVE_SOURCE_ROOM")
|
|
target_room = _get_env_room("RC_LIVE_TARGET_ROOM")
|
|
|
|
posted_ids_source: list = []
|
|
posted_ids_target: list = []
|
|
|
|
# Pre-step: Send /reset to clear sticky topics from previous tests.
|
|
# Previous tests (test_02, etc.) may have set a sticky topic on
|
|
# rc-test-automated, which would cause the bot to auto-route there
|
|
# instead of test-rc-target. /reset clears all sticky state.
|
|
_reset_msg_id, _ = post_test_message(
|
|
rc_client, source_room, "/reset",
|
|
posted_ids_source, rate_limiter,
|
|
)
|
|
logger.info(
|
|
"Test 03: sent /reset to source room (msg %s) — clearing sticky topics",
|
|
_reset_msg_id[:12],
|
|
)
|
|
# Give the bot 5 seconds to process /reset AND send its response
|
|
# before posting the keyword. The /reset response ("Konversation
|
|
# zurückgesetzt...") must arrive BEFORE the keyword post so that
|
|
# source_cursor (from the keyword post_message return) sits AFTER
|
|
# the /reset response. Otherwise wait_for_bot_message picks up the
|
|
# /reset response as a "move-request" and the assertion fails.
|
|
time.sleep(5.0)
|
|
|
|
# Step 1: Post keyword message in source room.
|
|
# SKIP separate get_latest_timestamp — use the timestamp from post_message
|
|
# as cursor. Saves one channels.history API call.
|
|
keyword_text = "rocketchat Test Move-Request — bitte in den Zielraum routen"
|
|
|
|
msg_id, source_cursor = post_test_message(
|
|
rc_client, source_room, keyword_text,
|
|
posted_ids_source, rate_limiter,
|
|
)
|
|
logger.info(
|
|
"Test 03: posted keyword message %s in source room (cursor=%s)",
|
|
msg_id[:12], source_cursor[:19],
|
|
)
|
|
|
|
# Step 2: Wait for bot to send move-request buttons in source room.
|
|
# Use initial_interval=5s to reduce API call density (was 2s).
|
|
try:
|
|
move_req_msg = wait_for_bot_message(
|
|
client=rc_client,
|
|
room_id=source_room,
|
|
bot_username=bot_username,
|
|
cursor=source_cursor,
|
|
timeout=30.0, # Archivar needs time to classify + send buttons
|
|
rate_limiter=rate_limiter,
|
|
initial_interval=5.0,
|
|
)
|
|
except RateLimitSkip as e:
|
|
pytest.skip(f"Rate limit triggered: {e}")
|
|
except PollTimeout as e:
|
|
pytest.fail(
|
|
f"Bot did not send move-request buttons in source room within 30s. "
|
|
f"Check that the Archivar is running and the keyword 'rocketchat' "
|
|
f"matches the target room. Error: {e}"
|
|
)
|
|
|
|
move_req_text = move_req_msg.get("msg", "")
|
|
logger.info("Test 03: bot move-request text: '%s'", move_req_text[:120])
|
|
posted_ids_source.append(move_req_msg.get("_id", ""))
|
|
|
|
# Verify the bot response is a valid routing action.
|
|
# Two valid forms:
|
|
# A) Move-Request with buttons (Butler mode): contains "kopieren" or "rüber"
|
|
# B) Auto-Route announcement (100% keyword / FAISS): contains "wechseln",
|
|
# "thema", "sehe dich", "move", "raum", or the target room name.
|
|
# Both are valid routing behaviors — the bot either asks for confirmation
|
|
# (Butler) or auto-routes with an announcement.
|
|
_routing_keywords = [
|
|
"kopieren", "rüber", "wechseln", "thema", "sehe dich",
|
|
"move", "raum", "rüberkopieren", "wechsel",
|
|
]
|
|
_has_routing_keyword = any(kw in move_req_text.lower() for kw in _routing_keywords)
|
|
_has_target_room = (
|
|
"rc-test-automated" in move_req_text.lower()
|
|
or "test-rc-target" in move_req_text.lower()
|
|
)
|
|
assert _has_routing_keyword or _has_target_room, (
|
|
f"Bot response is neither a move-request nor an auto-route announcement. "
|
|
f"Expected routing keywords ({_routing_keywords}) or target room name. "
|
|
f"Got: '{move_req_text[:200]}'"
|
|
)
|
|
|
|
# AUFGABE 4: Detect auto-route vs. button-move-request.
|
|
# Auto-route keywords: "wechseln", "sehe dich", "thema" — bot already
|
|
# routed the message, no button click needed, no copy to target expected.
|
|
# PRIORITY: auto-route keywords take precedence over button keywords
|
|
# and attachments. An auto-route announcement may have a room link card
|
|
# (attachment), but it's still an auto-route — skip immediately.
|
|
_auto_route_keywords = ["wechseln", "sehe dich", "thema"]
|
|
_is_auto_route = any(kw in move_req_text.lower() for kw in _auto_route_keywords)
|
|
|
|
if _is_auto_route:
|
|
# Bot auto-routed (100% keyword match) — no button click needed,
|
|
# no copy to target expected. This is valid bot behavior.
|
|
# Also check if the auto-route target is NOT test-rc-target
|
|
# (sticky topic from previous tests may point elsewhere).
|
|
_target_is_wrong = (
|
|
"rc-test-automated" in move_req_text.lower()
|
|
and "test-rc-target" not in move_req_text.lower()
|
|
)
|
|
if _target_is_wrong:
|
|
logger.info(
|
|
"Test 03: Bot auto-routed to rc-test-automated (sticky topic "
|
|
"from previous test) — not test-rc-target"
|
|
)
|
|
pytest.skip(
|
|
"Bot has sticky topic on rc-test-automated from previous test. "
|
|
"Run /reset in source room before test_03 to clear sticky topics."
|
|
)
|
|
logger.info(
|
|
"Test 03: Bot auto-routed (100%% keyword match) — "
|
|
"no button click needed, skipping target verification"
|
|
)
|
|
pytest.skip(
|
|
"Bot auto-routed (100% keyword match), no copy to target expected"
|
|
)
|
|
|
|
# Step 3: Simulate button click — post MOVE_REQ:<source_room>::confirm_move
|
|
# This is the payload the bot's native button sends when clicked.
|
|
# The adapter parses it at line 2395-2396 and routes to confirm_move handler.
|
|
target_cursor = get_latest_timestamp(rc_client, target_room, rate_limiter=rate_limiter)
|
|
logger.info("Test 03: target cursor = %s", target_cursor[:19])
|
|
|
|
button_payload = f"MOVE_REQ:{source_room}::confirm_move"
|
|
button_msg_id, _ = post_test_message(
|
|
rc_client, source_room, button_payload,
|
|
posted_ids_source, rate_limiter,
|
|
)
|
|
logger.info(
|
|
"Test 03: posted button-click payload '%s' → msg %s",
|
|
button_payload[:60], button_msg_id[:12],
|
|
)
|
|
|
|
# Step 4: Wait for copied message in target room.
|
|
# The bot posts: "*(Auftrag von @bernd, hierher kopiert)*\n<original text>"
|
|
# Use timeout=60s and initial_interval=8s to reduce API call density.
|
|
# The 429 retry in rc_rest.py now has 5 retries (71s total), so a single
|
|
# 429 on the target room won't kill the test.
|
|
try:
|
|
copied_msg = wait_for_bot_message(
|
|
client=rc_client,
|
|
room_id=target_room,
|
|
bot_username=bot_username,
|
|
cursor=target_cursor,
|
|
timeout=60.0,
|
|
rate_limiter=rate_limiter,
|
|
initial_interval=8.0,
|
|
)
|
|
except RateLimitSkip as e:
|
|
pytest.skip(f"Rate limit triggered: {e}")
|
|
except PollTimeout as e:
|
|
pytest.fail(
|
|
f"Bot did not copy message to target room within 60s. "
|
|
f"Check that the bot is subscribed to the target room and "
|
|
f"the move-request handler is working. Error: {e}"
|
|
)
|
|
|
|
copied_text = copied_msg.get("msg", "")
|
|
logger.info("Test 03: copied message in target: '%s'", copied_text[:200])
|
|
posted_ids_target.append(copied_msg.get("_id", ""))
|
|
|
|
# Step 5: Verify the copied message
|
|
# Should contain the prefix and the original keyword text
|
|
assert "Auftrag von" in copied_text or "hierher kopiert" in copied_text, (
|
|
f"Copied message missing move prefix. Got: '{copied_text[:200]}'"
|
|
)
|
|
assert "rocketchat" in copied_text.lower(), (
|
|
f"Copied message does not contain original keyword 'rocketchat'. "
|
|
f"Got: '{copied_text[:200]}'"
|
|
)
|
|
|
|
# Verify message ownership
|
|
assert_bot_message(
|
|
copied_msg,
|
|
expected_username=bot_username,
|
|
context="test_03_move_request_target",
|
|
)
|
|
|
|
# Cleanup: delete messages from both rooms
|
|
for mid in posted_ids_source:
|
|
try:
|
|
rc_client.delete_message(source_room, mid)
|
|
except Exception as e:
|
|
logger.warning("Cleanup source: failed to delete %s: %s", mid[:12], e)
|
|
for mid in posted_ids_target:
|
|
try:
|
|
rc_client.delete_message(target_room, mid)
|
|
except Exception as e:
|
|
logger.warning("Cleanup target: failed to delete %s: %s", mid[:12], e)
|
|
|
|
logger.info("Test 03 PASSED: move-request flow OK")
|