P-086 (P2): Catch-All-Guard in add_excluded_room + Tests für _persist_excluded_rooms
Auflage 1 (P2-1): add_excluded_room() erhält defensiven Catch-All-Guard analog remove_room() — ein excluded Raum darf nicht gleichzeitig Catch-All sein. Behebt API-Asymmetrie (remove_room hatte den Guard, add_excluded_room nicht). Ohne Guard würde ein übersprungener Catch-All-Raum beim nächsten validate() einen FATAL auslösen. Auflage 2 (P2-2): _persist_excluded_rooms (adapter.py) war testlos. Neue Tests: add+save+reload (Disk-Roundtrip), Idempotenz (kein Doppelt-Eintrag), Multi-Room-Persistenz, early-return ohne archivar, Catch-All-Schutz. Zusätzlich add_excluded_room_rejects_catch_all auf RoomMap-Ebene.
This commit is contained in:
@@ -1040,6 +1040,20 @@ class RoomMap:
|
|||||||
Args:
|
Args:
|
||||||
room_id: Die Raum-ID.
|
room_id: Die Raum-ID.
|
||||||
"""
|
"""
|
||||||
|
# P-086 (P2-1): Catch-All-Schutz — ein excluded Raum darf NICHT
|
||||||
|
# gleichzeitig Catch-All sein. API-Asymmetrie zu remove_room()
|
||||||
|
# (Z.1024-1027) behoben: remove_room hat diesen Guard, add_excluded_room
|
||||||
|
# hatte ihn nicht. Ohne Guard würde ein übersprungener Catch-All-Raum
|
||||||
|
# beim nächsten validate() einen FATAL auslösen (Z.576-580).
|
||||||
|
catch_all = self.catch_all_room
|
||||||
|
if catch_all is not None and room_id == catch_all.room_id:
|
||||||
|
logger.warning(
|
||||||
|
"Catch-All-Raum '%s' kann nicht excluded werden — "
|
||||||
|
"add_excluded_room ignoriert room_id '%s'",
|
||||||
|
catch_all.name,
|
||||||
|
room_id,
|
||||||
|
)
|
||||||
|
return
|
||||||
if room_id in self._excluded_room_ids:
|
if room_id in self._excluded_room_ids:
|
||||||
return
|
return
|
||||||
self._excluded_room_ids.add(room_id)
|
self._excluded_room_ids.add(room_id)
|
||||||
|
|||||||
@@ -149,3 +149,18 @@ class TestAddExcludedRoom:
|
|||||||
room_map.add_excluded_room("r_never_onboarded")
|
room_map.add_excluded_room("r_never_onboarded")
|
||||||
assert room_map.is_excluded("r_never_onboarded") is True
|
assert room_map.is_excluded("r_never_onboarded") is True
|
||||||
assert "r_never_onboarded" not in room_map.rooms
|
assert "r_never_onboarded" not in room_map.rooms
|
||||||
|
|
||||||
|
def test_add_excluded_room_rejects_catch_all(self):
|
||||||
|
"""P-086 (P2-1): Catch-All-Raum kann NICHT excluded werden.
|
||||||
|
|
||||||
|
API-Asymmetrie zu remove_room() behoben — der Guard verhindert, dass
|
||||||
|
ein übersprungener Catch-All-Raum beim nächsten validate() einen
|
||||||
|
FATAL auslöst (Z.576-580).
|
||||||
|
"""
|
||||||
|
room_map = _make_map([_room("r_catchall", "#CatchAll", catch_all=True)])
|
||||||
|
room_map.add_excluded_room("r_catchall")
|
||||||
|
assert room_map.is_excluded("r_catchall") is False
|
||||||
|
assert len(room_map.excluded_room_ids) == 0
|
||||||
|
# Catch-All bleibt intakt
|
||||||
|
assert room_map.catch_all_room is not None
|
||||||
|
assert room_map.catch_all_room.room_id == "r_catchall"
|
||||||
|
|||||||
@@ -0,0 +1,160 @@
|
|||||||
|
"""Tests für P-086 (P2-2) — Adapter-Helper _persist_excluded_rooms.
|
||||||
|
|
||||||
|
Der Helper ``RocketChatAdapter._persist_excluded_rooms()`` war bislang testlos.
|
||||||
|
Diese Tests decken ab:
|
||||||
|
1. room_id wird zu excluded_rooms hinzugefügt + save + reload (Disk-Roundtrip).
|
||||||
|
2. Idempotenz — bereits excluded Räume erzeugen keinen Doppelt-Eintrag.
|
||||||
|
3. E2E-artig: Onboarding-'Überspringen' persistiert room_id in room-map.json.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
|
||||||
|
|
||||||
|
from plugin.archivar.room_map import RoomMap
|
||||||
|
|
||||||
|
|
||||||
|
# ── Helpers ────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
def _room(room_id: str, name: str, **kwargs) -> dict:
|
||||||
|
d = {
|
||||||
|
"room_id": room_id,
|
||||||
|
"name": name,
|
||||||
|
"theme": name,
|
||||||
|
"keywords_100": [],
|
||||||
|
"keywords_context": [],
|
||||||
|
"catch_all": False,
|
||||||
|
}
|
||||||
|
d.update(kwargs)
|
||||||
|
return d
|
||||||
|
|
||||||
|
|
||||||
|
def _make_map(rooms: list[dict], excluded: list[str] | None = None) -> RoomMap:
|
||||||
|
data: dict = {
|
||||||
|
"version": 2,
|
||||||
|
"rooms": {},
|
||||||
|
"excluded_rooms": excluded or [],
|
||||||
|
"fact_store_path": "/tmp/test_docs",
|
||||||
|
}
|
||||||
|
for room in rooms:
|
||||||
|
rid = room.pop("room_id")
|
||||||
|
data["rooms"][rid] = room
|
||||||
|
return RoomMap._from_dict(data)
|
||||||
|
|
||||||
|
|
||||||
|
def _make_adapter(room_map: RoomMap):
|
||||||
|
"""Baut einen RocketChatAdapter via __new__ (ohne __init__).
|
||||||
|
|
||||||
|
Mirrors das Muster aus test_cmd_compress.py: __new__ überspringt
|
||||||
|
super().__init__, danach wird nur ``archivar`` als SimpleNamespace mit
|
||||||
|
``_room_map`` gesetzt — genau das, was _persist_excluded_rooms braucht.
|
||||||
|
"""
|
||||||
|
from plugin.adapter import RocketChatAdapter
|
||||||
|
|
||||||
|
adapter = RocketChatAdapter.__new__(RocketChatAdapter)
|
||||||
|
adapter.archivar = SimpleNamespace(_room_map=room_map)
|
||||||
|
return adapter
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def tmp_map_path(tmp_path, monkeypatch):
|
||||||
|
"""Biegt ROOM_MAP_PATH + ARCHIVAR_ROOM_MAP_PATH auf ein Temp-Verzeichnis.
|
||||||
|
|
||||||
|
_persist_excluded_rooms importiert ``ROOM_MAP_PATH`` aus archivar.config
|
||||||
|
(Import-Zeit-Wert) und ruft ``room_map.reload()`` ohne Pfad — reload()
|
||||||
|
liest die Env-Var ``ARCHIVAR_ROOM_MAP_PATH``. Beide müssen auf denselben
|
||||||
|
Temp-Pfad zeigen, damit save + reload denselben Ort treffen.
|
||||||
|
"""
|
||||||
|
map_path = tmp_path / "room-map.json"
|
||||||
|
monkeypatch.setattr("archivar.config.ROOM_MAP_PATH", str(map_path))
|
||||||
|
monkeypatch.setenv("ARCHIVAR_ROOM_MAP_PATH", str(map_path))
|
||||||
|
return map_path
|
||||||
|
|
||||||
|
|
||||||
|
# ── Tests ─────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
class TestPersistExcludedRooms:
|
||||||
|
"""_persist_excluded_rooms() — fügt hinzu, persistiert, idempotent."""
|
||||||
|
|
||||||
|
def test_adds_room_id_and_persists_through_save_reload(self, tmp_map_path):
|
||||||
|
"""room_id wird zu excluded_rooms hinzugefügt + save + reload.
|
||||||
|
|
||||||
|
Nach dem Aufruf muss die room_id (a) in der In-Memory-Map excluded
|
||||||
|
sein und (b) nach einem frischen RoomMap.load() vom Disk weiterhin
|
||||||
|
excluded sein (Gateway-Restart-Simulation).
|
||||||
|
"""
|
||||||
|
room_map = _make_map([_room("r_catchall", "#CatchAll", catch_all=True)])
|
||||||
|
adapter = _make_adapter(room_map)
|
||||||
|
|
||||||
|
adapter._persist_excluded_rooms(["r_skip"])
|
||||||
|
|
||||||
|
# In-Memory: excluded
|
||||||
|
assert room_map.is_excluded("r_skip") is True
|
||||||
|
|
||||||
|
# Disk-Roundtrip: frisch laden → weiterhin excluded
|
||||||
|
reloaded = RoomMap.load(tmp_map_path)
|
||||||
|
assert reloaded.is_excluded("r_skip") is True
|
||||||
|
|
||||||
|
def test_idempotent_no_duplicate_entry(self, tmp_map_path):
|
||||||
|
"""Bereits excluded → kein Doppelt-Eintrag (weder In-Memory noch Disk)."""
|
||||||
|
room_map = _make_map([_room("r_catchall", "#CatchAll", catch_all=True)])
|
||||||
|
adapter = _make_adapter(room_map)
|
||||||
|
|
||||||
|
adapter._persist_excluded_rooms(["r_skip"])
|
||||||
|
adapter._persist_excluded_rooms(["r_skip"])
|
||||||
|
|
||||||
|
assert len(room_map.excluded_room_ids) == 1
|
||||||
|
|
||||||
|
# Disk: genau ein Eintrag in excluded_rooms
|
||||||
|
with open(tmp_map_path, "r", encoding="utf-8") as f:
|
||||||
|
data = json.load(f)
|
||||||
|
assert data["excluded_rooms"] == ["r_skip"]
|
||||||
|
|
||||||
|
def test_multiple_rooms_all_persisted(self, tmp_map_path):
|
||||||
|
"""Mehrere room_ids werden alle persistiert (skip_all-Flow)."""
|
||||||
|
room_map = _make_map([_room("r_catchall", "#CatchAll", catch_all=True)])
|
||||||
|
adapter = _make_adapter(room_map)
|
||||||
|
|
||||||
|
adapter._persist_excluded_rooms(["r_a", "r_b", "r_c"])
|
||||||
|
|
||||||
|
assert room_map.is_excluded("r_a") is True
|
||||||
|
assert room_map.is_excluded("r_b") is True
|
||||||
|
assert room_map.is_excluded("r_c") is True
|
||||||
|
|
||||||
|
reloaded = RoomMap.load(tmp_map_path)
|
||||||
|
assert reloaded.is_excluded("r_a") is True
|
||||||
|
assert reloaded.is_excluded("r_b") is True
|
||||||
|
assert reloaded.is_excluded("r_c") is True
|
||||||
|
|
||||||
|
def test_no_archivar_returns_early(self, tmp_map_path):
|
||||||
|
"""Ohne archivar (None) → early return, kein Crash."""
|
||||||
|
from plugin.adapter import RocketChatAdapter
|
||||||
|
|
||||||
|
adapter = RocketChatAdapter.__new__(RocketChatAdapter)
|
||||||
|
adapter.archivar = None
|
||||||
|
|
||||||
|
# Darf nicht werfen
|
||||||
|
adapter._persist_excluded_rooms(["r_skip"])
|
||||||
|
|
||||||
|
def test_catch_all_room_not_excluded(self, tmp_map_path):
|
||||||
|
"""Catch-All-Raum wird NICHT excluded (P2-1-Guard greift durch)."""
|
||||||
|
room_map = _make_map([_room("r_catchall", "#CatchAll", catch_all=True)])
|
||||||
|
adapter = _make_adapter(room_map)
|
||||||
|
|
||||||
|
adapter._persist_excluded_rooms(["r_catchall"])
|
||||||
|
|
||||||
|
assert room_map.is_excluded("r_catchall") is False
|
||||||
|
assert len(room_map.excluded_room_ids) == 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
pytest.main([__file__, "-v"])
|
||||||
Reference in New Issue
Block a user