live_rc: Rate-Limiter-Kalibrierung + Test 03/04 implementiert + ENV-Skip-Fix + README

AUFGABE 1 — Rate-Limiter-Kalibrierung:
- Polling-Intervalle: 2s → 3s → 5s → 8s (statt 100ms→200ms→500ms)
- 429-Backoff: 3s → 6s → 12s → 24s (exponentiell)
- Max 30 Calls/Minute (statt 60) — RC triggert 429 bei 3-4 Calls in <10s
- 3 consecutive 429s → RateLimitSkip (Test wird geskippt, nicht failed)
- Cursor-Caching: channels.history nur beim ersten Poll, dann Cache-Scan

AUFGABE 2 — ENV-Skip-Logik:
- pytest_collection_modifyitems in conftest.py: alle live_rc-Tests werden
  bereits bei Collection geskippt wenn RC_LIVE_URL nicht gesetzt ist
- Kein Fixture-Overhead, keine Imports — sauberer Early-Skip

AUFGABE 3 — TEST 03: Move-Request (vollständig):
- Keyword 'rocketchat' in test-rc-source triggert Archivar
- Bot sendet Move-Request-Buttons (verifiziert)
- Button-Klick via MOVE_REQ:<rid>::confirm_move Payload
- Bot kopiert Nachricht nach test-rc-target mit Präfix
- Verifikation: kopierte Nachricht enthält Original-Text + Präfix
- ENV: RC_LIVE_SOURCE_ROOM, RC_LIVE_TARGET_ROOM

AUFGABE 4 — TEST 04: Multi-Room (DM-Proxy → Archivar → Projektraum):
- Haupt-Testraum als DM-Proxy
- Keyword_100-Match 'rocketchat' → FAISS-Auto-Route in Projektraum
- Verifikation: kopierte Nachricht im Projektraum (wenn FAISS >= 0.85)
- Graceful: kein Fail wenn Auto-Route nicht triggert (Butler-Mode)
- ENV: RC_LIVE_PROJECT_ROOM

AUFGABE 5 — TEST 05+06: Skeletons mit TODO-Markern:
- test_05_stream_retry: skip mit Begründung (Stream-Retry auf .45 pending)
- test_06_concurrent: skip mit Begründung (Themenring auf .45 pending)
- Beide mit vollständigen Implementierungsschritten als TODO-Kommentare

AUFGABE 6 — README.md:
- Alle ENV-Variablen dokumentiert (Pflicht + Optional)
- Ausführ-Anleitung mit Beispielen
- Test-Übersichtstabelle mit Status
- Rate-Limiting-Dokumentation
- Architektur-Übersicht
This commit is contained in:
Bernd (Hermes)
2026-07-28 16:58:50 +02:00
parent 6a09caa1e5
commit 16c1ad927e
9 changed files with 708 additions and 57 deletions
+177 -6
View File
@@ -1,14 +1,185 @@
"""
Test 03 — Move-Flow (skeleton).
Test 03 — Move-Flow: Keyword erkennt Zielraum, Bot sendet Move-Request,
Button-Klick bestätigt, Nachricht wird in Zielraum kopiert.
Benötigt 3 Räume (Ursprung, Ziel, Catch-All) — vorerst skip.
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
"""
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
@pytest.mark.skip(reason="needs multiple rooms setup")
def test_03_move_request():
"""Move-Flow: Bot erkennt Move-Request und routet in Zielraum."""
pass
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 = []
# 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 move-request contains expected elements
assert "kopieren" in move_req_text.lower() or "rüber" in move_req_text.lower(), (
f"Move-request message does not contain copy/rüber prompt: '{move_req_text[:200]}'"
)
# 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>"
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")