"""Tests für P-086 — Onboarding überspringt excluded_rooms nicht. Prüft: 1. check_room_map_completeness() meldet excluded Räume NICHT als missing. 2. RoomMap.add_excluded_room() ist idempotent und persistiert via save/reload. 3. Bestehende excluded_rooms werden respektiert (kein Onboarding-Trigger). """ from __future__ import annotations import json import sys import tempfile from pathlib import Path import pytest sys.path.insert(0, str(Path(__file__).resolve().parents[3])) from plugin.archivar.room_map import RoomMap from plugin.archivar.onboarding import check_room_map_completeness # ── 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) # ── Tests ───────────────────────────────────────────────────────────────── class TestExcludedRoomsNotMissing: """check_room_map_completeness() ignoriert excluded Räume.""" def test_excluded_room_not_reported_missing(self): """Ein excluded Raum wird NICHT als missing gemeldet.""" room_map = _make_map( [ _room("r_catchall", "#CatchAll", catch_all=True), ], excluded=["r_excluded"], ) subscribed = { "r_excluded": {"name": "#Excluded", "type": "c"}, "r_new": {"name": "#NeuerRaum", "type": "c"}, } missing = check_room_map_completeness( room_map=room_map, subscribed_rooms=subscribed, map_path="/tmp/test_map.json", ) missing_ids = [m["room_id"] for m in missing] assert "r_excluded" not in missing_ids assert "r_new" in missing_ids def test_excluded_room_not_in_map_still_ignored(self): """Excluded Raum, der gar nicht in rooms{} steht, wird ignoriert.""" room_map = _make_map( [_room("r_catchall", "#CatchAll", catch_all=True)], excluded=["r_ghost"], ) subscribed = { "r_ghost": {"name": "#Ghost", "type": "c"}, } missing = check_room_map_completeness( room_map=room_map, subscribed_rooms=subscribed, map_path="/tmp/test_map.json", ) assert missing == [] def test_existing_excluded_rooms_respected(self): """Mehrere bestehende excluded_rooms werden alle respektiert.""" room_map = _make_map( [_room("r_catchall", "#CatchAll", catch_all=True)], excluded=["r_ex1", "r_ex2", "r_ex3"], ) subscribed = { "r_ex1": {"name": "#Ex1", "type": "c"}, "r_ex2": {"name": "#Ex2", "type": "c"}, "r_ex3": {"name": "#Ex3", "type": "c"}, "r_new": {"name": "#Neu", "type": "c"}, } missing = check_room_map_completeness( room_map=room_map, subscribed_rooms=subscribed, map_path="/tmp/test_map.json", ) missing_ids = [m["room_id"] for m in missing] assert missing_ids == ["r_new"] class TestAddExcludedRoom: """RoomMap.add_excluded_room() — idempotent + persistierbar.""" def test_add_excluded_room_idempotent(self): """Doppeltes Hinzufügen erzeugt keinen Duplikat-Eintrag.""" room_map = _make_map([_room("r_catchall", "#CatchAll", catch_all=True)]) room_map.add_excluded_room("r_skip") room_map.add_excluded_room("r_skip") assert room_map.is_excluded("r_skip") is True assert len(room_map.excluded_room_ids) == 1 def test_add_excluded_room_persists_through_save_reload(self): """add_excluded_room + save + reload überlebt (Gateway-Restart-Simulation).""" tmp = tempfile.NamedTemporaryFile( mode="w", suffix=".json", delete=False, encoding="utf-8" ) tmp_path = Path(tmp.name) tmp.close() room_map = _make_map([_room("r_catchall", "#CatchAll", catch_all=True)]) room_map.add_excluded_room("r_skip") RoomMap.save(room_map, tmp_path) # Reload aus Disk — simuliert Gateway-Restart reloaded = RoomMap.load(tmp_path) assert reloaded.is_excluded("r_skip") is True tmp_path.unlink(missing_ok=True) def test_add_excluded_room_does_not_require_room_entry(self): """add_excluded_room funktioniert ohne Eintrag in rooms{}.""" room_map = _make_map([_room("r_catchall", "#CatchAll", catch_all=True)]) room_map.add_excluded_room("r_never_onboarded") assert room_map.is_excluded("r_never_onboarded") is True 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"