""" 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:::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 """ 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:::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 = [] # Step 1: Post keyword message in source room keyword_text = "rocketchat Test Move-Request — bitte in den Zielraum routen" source_cursor = get_latest_timestamp(rc_client, source_room, rate_limiter=rate_limiter) logger.info("Test 03: source cursor = %s", source_cursor[:19]) msg_id = 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", msg_id[:12]) # Step 2: Wait for bot to send move-request buttons in source room try: move_req_msg = wait_for_bot_message( client=rc_client, room_id=source_room, bot_username=bot_username, cursor=source_cursor, timeout=25.0, # Archivar needs time to classify + send buttons rate_limiter=rate_limiter, ) 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 25s. " 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]}'" ) # Step 3: Simulate button click — post MOVE_REQ:::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" try: copied_msg = wait_for_bot_message( client=rc_client, room_id=target_room, bot_username=bot_username, cursor=target_cursor, timeout=25.0, rate_limiter=rate_limiter, ) 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 25s. " 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")