0e39e7076a
R1 (P0): Catch-All-Sticky-Vergiftung behoben - Session-Sticky-Block (adapter.py:1581) prüft jetzt is_catch_all und should_stay_in_room VOR dem Auto-Route - Catch-All-Zielraum → Sticky wird gelöscht, Fall-Through zu Archivar - stay_in_room=always/never_route → Sticky wird gelöscht, Fall-Through R2 (P1): MultiRoomContextRing.move_gc() implementiert - Neue Methode entfernt stale Topics aus Ursprungsraum bei Move - Fügt neuen Ring-Eintrag für Zielraum hinzu - Aufruf bei confirm_move (Button 1) und Auto-Route (100%-Match) R3 (P1): 4 neue Test-Suites (56 Tests) - test_multiroom_context.py: Ring-Buffer, move_gc, System-Prompt - test_sticky_session.py: temporäres Sticky, 30 Min TTL, Guards - test_auto_route.py: 100%-Match, self-match, Catch-All - test_edge_cases.py: leerer Ring, Catch-All+Auto-Route, Move-Ketten R4 (P2): Doc-Kommentar für _CURRENT_ORIGIN_ROOM ContextVar-Reset Verifikation: 401 passed, 1 pre-existing fail (test_no_env_vars)
229 lines
9.4 KiB
Python
229 lines
9.4 KiB
Python
"""
|
|
V8.1.1 R3: Edge-Case-Tests — leerer Ring, Catch-All+Auto-Route, Move+Folgenachricht im Zielraum.
|
|
|
|
Prüft:
|
|
- Leerer MultiRoomContextRing verhält sich korrekt
|
|
- Catch-All-Raum + Auto-Route-Interaktion
|
|
- Move + Folgenachricht im Zielraum
|
|
- Sticky-Expiry + Catch-All gleichzeitig
|
|
- stay_in_room + Session-Sticky Interaktion
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
import time
|
|
import unittest
|
|
|
|
# Add the parent directory to the path
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
|
|
|
|
from multiroom_context import MultiRoomContextRing
|
|
from archivar_state import ArchivarState
|
|
|
|
|
|
class TestEdgeCases(unittest.TestCase):
|
|
"""Edge-Case-Tests für V8.1-Features."""
|
|
|
|
def setUp(self):
|
|
self.temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.json')
|
|
self.temp_file.close()
|
|
self.state = ArchivarState(self.temp_file.name)
|
|
self.ring = MultiRoomContextRing(max_entries=5)
|
|
|
|
def tearDown(self):
|
|
if os.path.exists(self.temp_file.name):
|
|
os.unlink(self.temp_file.name)
|
|
|
|
# ── Leerer Ring ─────────────────────────────────────────────────────
|
|
|
|
def test_empty_ring_get_summary_returns_empty(self):
|
|
"""Leerer Ring: get_summary() gibt leeren String zurück."""
|
|
result = asyncio.run(self.ring.get_summary())
|
|
self.assertEqual(result, "")
|
|
|
|
def test_empty_ring_get_entries_returns_empty_list(self):
|
|
"""Leerer Ring: get_entries() gibt leere Liste zurück."""
|
|
result = asyncio.run(self.ring.get_entries())
|
|
self.assertEqual(result, [])
|
|
|
|
def test_empty_ring_size_is_zero(self):
|
|
"""Leerer Ring: size ist 0."""
|
|
self.assertEqual(self.ring.size, 0)
|
|
|
|
def test_empty_ring_move_gc_adds_entry(self):
|
|
"""Leerer Ring: move_gc() fügt neuen Eintrag für Zielraum hinzu."""
|
|
asyncio.run(self.ring.move_gc("origin-1", "target-1", "Thema", "#DevOps"))
|
|
# move_gc fügt IMMER einen neuen Eintrag für den Zielraum hinzu
|
|
self.assertEqual(self.ring.size, 1)
|
|
entries = asyncio.run(self.ring.get_entries())
|
|
self.assertEqual(entries[0]["room_id"], "target-1")
|
|
self.assertEqual(entries[0]["topic"], "Thema")
|
|
|
|
# ── Catch-All + Auto-Route ──────────────────────────────────────────
|
|
|
|
def test_catch_all_blocks_auto_route_even_with_100_percent_match(self):
|
|
"""Catch-All blockiert Auto-Route auch bei 100%-Match."""
|
|
target_is_catch_all = True
|
|
has_100_percent_match = True
|
|
# Catch-All-Guard hat Vorrang vor 100%-Match
|
|
should_route = has_100_percent_match and not target_is_catch_all
|
|
self.assertFalse(should_route)
|
|
|
|
def test_catch_all_origin_bypass(self):
|
|
"""Nachricht AUS Catch-All-Raum wird nicht geroutet."""
|
|
origin_is_catch_all = True
|
|
should_route = not origin_is_catch_all
|
|
self.assertFalse(should_route)
|
|
|
|
def test_catch_all_target_clears_sticky(self):
|
|
"""Catch-All-Guard räumt Sticky auf wenn Ziel Catch-All ist."""
|
|
room_id = "origin-123"
|
|
prefs = {
|
|
room_id: {
|
|
"target_room": "catch-all-456",
|
|
"target_name": "#CatchAll",
|
|
"expires_at": time.time() + 1800,
|
|
}
|
|
}
|
|
# Catch-All-Guard: Sticky löschen
|
|
target_is_catch_all = True
|
|
if target_is_catch_all:
|
|
del prefs[room_id]
|
|
self.assertNotIn(room_id, prefs)
|
|
|
|
# ── Move + Folgenachricht ───────────────────────────────────────────
|
|
|
|
def test_move_gc_then_followup_in_target(self):
|
|
"""Nach Move: Folgenachricht im Zielraum findet korrekten Ring-Eintrag."""
|
|
asyncio.run(self._async_move_gc_then_followup())
|
|
|
|
async def _async_move_gc_then_followup(self):
|
|
# 1. Ursprungsraum: Thema wird gepostet
|
|
await self.ring.add("origin-1", "#General", "Deployment-Status prüfen")
|
|
|
|
# 2. Move: Ursprungsraum → Zielraum
|
|
await self.ring.move_gc(
|
|
origin_room_id="origin-1",
|
|
target_room_id="target-1",
|
|
topic="Deployment-Status prüfen",
|
|
room_name="#DevOps",
|
|
)
|
|
|
|
# 3. Folgenachricht im Zielraum
|
|
await self.ring.add("target-1", "#DevOps", "Deployment läuft auf Staging")
|
|
|
|
# Ring sollte jetzt 2 Einträge haben:
|
|
# - target-1: "Deployment läuft auf Staging" (neueste)
|
|
# - target-1: "Deployment-Status prüfen" wurde überschrieben
|
|
self.assertEqual(self.ring.size, 1)
|
|
entries = await self.ring.get_entries()
|
|
self.assertEqual(entries[0]["topic"], "Deployment läuft auf Staging")
|
|
|
|
def test_move_then_different_topic_in_origin(self):
|
|
"""Nach Move: anderes Thema im Ursprungsraum bleibt unberührt."""
|
|
asyncio.run(self._async_move_then_different_topic_in_origin())
|
|
|
|
async def _async_move_then_different_topic_in_origin(self):
|
|
# 1. Zwei Themen im Ursprungsraum
|
|
await self.ring.add("origin-1", "#General", "Deployment-Status")
|
|
await self.ring.add("origin-1", "#General", "Login-Fehler") # Überschreibt
|
|
|
|
# 2. Move: Deployment-Status → Zielraum
|
|
# Da "Deployment-Status" durch "Login-Fehler" überschrieben wurde,
|
|
# findet move_gc keinen Match → fügt neuen Eintrag hinzu
|
|
await self.ring.move_gc(
|
|
origin_room_id="origin-1",
|
|
target_room_id="target-1",
|
|
topic="Deployment-Status",
|
|
room_name="#DevOps",
|
|
)
|
|
|
|
# 3. Login-Fehler bleibt (origin-1), Deployment-Status neu in target-1
|
|
self.assertEqual(self.ring.size, 2)
|
|
entries = await self.ring.get_entries()
|
|
room_ids = [e["room_id"] for e in entries]
|
|
self.assertIn("origin-1", room_ids)
|
|
self.assertIn("target-1", room_ids)
|
|
|
|
# ── Sticky + stay_in_room Interaktion ───────────────────────────────
|
|
|
|
def test_sticky_ignored_when_stay_in_room_always(self):
|
|
"""Session-Sticky wird ignoriert wenn stay_in_room='always'."""
|
|
origin_state = "always"
|
|
has_sticky = True
|
|
should_use_sticky = has_sticky and origin_state not in ("always", "never_route")
|
|
self.assertFalse(should_use_sticky)
|
|
|
|
def test_sticky_ignored_when_stay_in_room_never_route(self):
|
|
"""Session-Sticky wird ignoriert wenn stay_in_room='never_route'."""
|
|
origin_state = "never_route"
|
|
has_sticky = True
|
|
should_use_sticky = has_sticky and origin_state not in ("always", "never_route")
|
|
self.assertFalse(should_use_sticky)
|
|
|
|
def test_sticky_used_when_no_stay_decision(self):
|
|
"""Session-Sticky wird verwendet wenn keine stay_in_room-Entscheidung."""
|
|
origin_state = None
|
|
has_sticky = True
|
|
should_use_sticky = has_sticky and origin_state not in ("always", "never_route")
|
|
self.assertTrue(should_use_sticky)
|
|
|
|
# ── Sticky-Expiry + Catch-All gleichzeitig ──────────────────────────
|
|
|
|
def test_expired_sticky_then_catch_all_guard(self):
|
|
"""Abgelaufener Sticky + Catch-All: beide Guards greifen."""
|
|
room_id = "origin-123"
|
|
prefs = {
|
|
room_id: {
|
|
"target_room": "catch-all-456",
|
|
"target_name": "#CatchAll",
|
|
"expires_at": time.time() - 10, # Abgelaufen
|
|
}
|
|
}
|
|
# 1. Expiry-Check
|
|
if time.time() >= prefs[room_id]["expires_at"]:
|
|
del prefs[room_id]
|
|
self.assertNotIn(room_id, prefs)
|
|
# 2. Catch-All-Guard (würde greifen wenn Sticky noch da wäre)
|
|
target_is_catch_all = True
|
|
self.assertTrue(target_is_catch_all)
|
|
|
|
# ── Ring + Sticky Kombination ───────────────────────────────────────
|
|
|
|
def test_ring_entries_survive_sticky_expiry(self):
|
|
"""Ring-Einträge überleben Sticky-Expiry (unabhängige Systeme)."""
|
|
asyncio.run(self._async_ring_entries_survive_sticky_expiry())
|
|
|
|
async def _async_ring_entries_survive_sticky_expiry(self):
|
|
await self.ring.add("room-1", "#Dev", "Thema A")
|
|
# Sticky läuft ab — Ring bleibt unberührt
|
|
self.assertEqual(self.ring.size, 1)
|
|
|
|
# ── Mehrere Moves hintereinander ────────────────────────────────────
|
|
|
|
def test_multiple_moves_chain(self):
|
|
"""Mehrere Moves hintereinander: Ring bleibt konsistent."""
|
|
asyncio.run(self._async_multiple_moves_chain())
|
|
|
|
async def _async_multiple_moves_chain(self):
|
|
# 1. Ursprungsraum → Zielraum A
|
|
await self.ring.add("origin-1", "#General", "Thema X")
|
|
await self.ring.move_gc("origin-1", "target-a", "Thema X", "#RoomA")
|
|
|
|
# 2. Zielraum A → Zielraum B (weiterer Move)
|
|
await self.ring.move_gc("target-a", "target-b", "Thema X", "#RoomB")
|
|
|
|
# Thema X ist jetzt in target-b
|
|
self.assertEqual(self.ring.size, 1)
|
|
entries = await self.ring.get_entries()
|
|
self.assertEqual(entries[0]["room_id"], "target-b")
|
|
self.assertEqual(entries[0]["topic"], "Thema X")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|