fix: V8.5 — Onboarding-Bugs A/B/C (Dons Diagnose)
Bug A: DM-Dedup — _start_onboarding prüft ob DM bereits in missing_rooms Bug B: ::onboarding ohne Args nutzt check_room_map_completeness (alle c/p-Räume) Bug C: Zombie-State-Validierung — connect() verwirft State wenn Operator nicht subscribed oder Lock inaktiv
This commit is contained in:
@@ -0,0 +1,460 @@
|
||||
"""
|
||||
V8.5 Onboarding-Bug-Fixes: Tests für Bug A (DM-Dedup), Bug B (::onboarding ohne Args),
|
||||
Bug C (Zombie-State-Validierung).
|
||||
|
||||
Dons Diagnose:
|
||||
Bug A: DM doppelt im Onboarding — check_room_map_completeness() wird zweimal gerufen
|
||||
Bug B: Private Groups (p) fehlen im Onboarding — ::onboarding ohne Args isoliert nur aktuellen Raum
|
||||
Bug C: Blockierter Auto-Trigger durch persistierten Zombie-State
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
import os
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
# Ensure the archivar package is importable
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
|
||||
from plugin.archivar.room_map import RoomMap, RoomEntry
|
||||
from plugin.archivar.onboarding import (
|
||||
check_room_map_completeness,
|
||||
check_room_map_completeness_delta,
|
||||
acquire_onboarding_lock,
|
||||
release_onboarding_lock,
|
||||
is_onboarding_locked,
|
||||
LOCK_DIR,
|
||||
)
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
# Helpers
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
def _make_room_map() -> RoomMap:
|
||||
"""Erzeugt eine RoomMap mit 2 c-Räumen (kein DM)."""
|
||||
rooms: dict[str, RoomEntry] = {
|
||||
"room_plugin": RoomEntry(
|
||||
room_id="room_plugin",
|
||||
name="#plugin-dev",
|
||||
theme="Plugin-Entwicklung",
|
||||
keywords_100=["rocketchat", "ddp"],
|
||||
keywords_context=["gateway", "deployment"],
|
||||
catch_all=False,
|
||||
),
|
||||
"room_bat": RoomEntry(
|
||||
room_id="room_bat",
|
||||
name="#bat-trading",
|
||||
theme="BAT Trading",
|
||||
keywords_100=["bat", "trading"],
|
||||
keywords_context=["docker", "api"],
|
||||
catch_all=False,
|
||||
),
|
||||
}
|
||||
return RoomMap(
|
||||
rooms=rooms,
|
||||
excluded_room_ids=set(),
|
||||
fact_store_path=Path("/tmp/test_docs"),
|
||||
version=2,
|
||||
)
|
||||
|
||||
|
||||
def _make_subscribed_rooms(extra: dict | None = None) -> dict:
|
||||
"""Erzeugt subscribed_rooms mit 2 c-Räumen, 1 p-Raum, 1 DM."""
|
||||
rooms = {
|
||||
"room_plugin": {"name": "#plugin-dev", "type": "c"},
|
||||
"room_bat": {"name": "#bat-trading", "type": "c"},
|
||||
"room_private": {"name": "private-group", "type": "p"},
|
||||
"room_dm": {"name": "andreas", "type": "d"},
|
||||
}
|
||||
if extra:
|
||||
rooms.update(extra)
|
||||
return rooms
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
# Bug A: DM-Dedup
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
class TestBugADmDedup:
|
||||
"""Bug A: DM soll nicht doppelt im Onboarding-Menü erscheinen."""
|
||||
|
||||
def test_check_room_map_completeness_excludes_dm(self):
|
||||
"""check_room_map_completeness() filtert type='d' Räume aus."""
|
||||
rm = _make_room_map()
|
||||
subscribed = _make_subscribed_rooms()
|
||||
# Füge einen neuen c-Raum hinzu, der nicht in der Map ist
|
||||
subscribed["room_new_c"] = {"name": "#new-channel", "type": "c"}
|
||||
|
||||
with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as f:
|
||||
map_path = f.name
|
||||
try:
|
||||
RoomMap.save(rm, Path(map_path))
|
||||
missing = check_room_map_completeness(rm, subscribed, map_path)
|
||||
# Sollte nur room_new_c und room_private enthalten, NICHT room_dm
|
||||
missing_ids = [m["room_id"] for m in missing]
|
||||
assert "room_new_c" in missing_ids
|
||||
assert "room_private" in missing_ids
|
||||
assert "room_dm" not in missing_ids, "DM-Raum darf nicht in missing_rooms sein"
|
||||
finally:
|
||||
os.unlink(map_path)
|
||||
|
||||
def test_dm_not_in_missing_when_already_in_map(self):
|
||||
"""Wenn DM bereits in der Map ist, wird er nicht als fehlend gemeldet."""
|
||||
rm = _make_room_map()
|
||||
# DM ist bereits in der Map
|
||||
rm.add_or_update_room(
|
||||
room_id="room_dm",
|
||||
name="DM",
|
||||
theme="Admin-DM",
|
||||
keywords_100=[],
|
||||
keywords_context=[],
|
||||
catch_all=True,
|
||||
)
|
||||
subscribed = _make_subscribed_rooms()
|
||||
|
||||
with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as f:
|
||||
map_path = f.name
|
||||
try:
|
||||
RoomMap.save(rm, Path(map_path))
|
||||
missing = check_room_map_completeness(rm, subscribed, map_path)
|
||||
missing_ids = [m["room_id"] for m in missing]
|
||||
assert "room_dm" not in missing_ids, "DM in Map soll nicht als fehlend gemeldet werden"
|
||||
finally:
|
||||
os.unlink(map_path)
|
||||
|
||||
def test_dedup_logic_dm_already_in_missing(self):
|
||||
"""Simuliert _start_onboarding Dedup: DM nicht anhängen wenn schon in missing_rooms."""
|
||||
# missing_rooms enthält bereits den DM (z.B. von check_room_map_completeness
|
||||
# wenn DM nicht in Map ist und subscribed_with_types den DM enthält)
|
||||
missing_rooms = [
|
||||
{"room_id": "room_new_c", "name": "#new-channel"},
|
||||
{"room_id": "room_dm", "name": "DM"},
|
||||
]
|
||||
operator_room_id = "room_dm"
|
||||
|
||||
# Dedup-Logik (wie in _start_onboarding)
|
||||
dm_already_in_missing = any(
|
||||
r.get("room_id") == operator_room_id for r in missing_rooms
|
||||
)
|
||||
assert dm_already_in_missing, "DM sollte als bereits vorhanden erkannt werden"
|
||||
|
||||
if not dm_already_in_missing:
|
||||
missing_rooms.append({"room_id": operator_room_id, "name": "DM", "is_dm": True})
|
||||
|
||||
# DM soll nur EINMAL in der Liste sein
|
||||
dm_count = sum(1 for r in missing_rooms if r.get("room_id") == operator_room_id)
|
||||
assert dm_count == 1, f"DM erscheint {dm_count}x statt 1x"
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
# Bug B: ::onboarding ohne Args → alle fehlenden c/p-Räume
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
class TestBugBOnboardingAllRooms:
|
||||
"""Bug B: ::onboarding ohne Args soll alle fehlenden c/p-Räume finden."""
|
||||
|
||||
def test_check_room_map_completeness_includes_private_groups(self):
|
||||
"""check_room_map_completeness() findet type='p' Räume (private groups)."""
|
||||
rm = _make_room_map()
|
||||
subscribed = _make_subscribed_rooms()
|
||||
|
||||
with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as f:
|
||||
map_path = f.name
|
||||
try:
|
||||
RoomMap.save(rm, Path(map_path))
|
||||
missing = check_room_map_completeness(rm, subscribed, map_path)
|
||||
# room_private (type='p') ist nicht in der Map → muss in missing sein
|
||||
missing_ids = [m["room_id"] for m in missing]
|
||||
assert "room_private" in missing_ids, (
|
||||
"Private Group (type='p') muss in missing_rooms sein"
|
||||
)
|
||||
finally:
|
||||
os.unlink(map_path)
|
||||
|
||||
def test_check_room_map_completeness_returns_all_missing(self):
|
||||
"""Wenn mehrere c/p-Räume fehlen, werden ALLE zurückgegeben."""
|
||||
rm = RoomMap(
|
||||
rooms={},
|
||||
excluded_room_ids=set(),
|
||||
fact_store_path=Path("/tmp/test_docs"),
|
||||
version=2,
|
||||
) # Leere Map
|
||||
subscribed = _make_subscribed_rooms()
|
||||
|
||||
with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as f:
|
||||
map_path = f.name
|
||||
try:
|
||||
RoomMap.save(rm, Path(map_path))
|
||||
missing = check_room_map_completeness(rm, subscribed, map_path)
|
||||
missing_ids = {m["room_id"] for m in missing}
|
||||
# Alle c/p-Räume müssen fehlen, DM nicht
|
||||
assert "room_plugin" in missing_ids
|
||||
assert "room_bat" in missing_ids
|
||||
assert "room_private" in missing_ids
|
||||
assert "room_dm" not in missing_ids
|
||||
assert len(missing) == 3, f"Erwarte 3 fehlende Räume, bekam {len(missing)}"
|
||||
finally:
|
||||
os.unlink(map_path)
|
||||
|
||||
def test_check_room_map_completeness_empty_when_complete(self):
|
||||
"""Wenn alle c/p-Räume in der Map sind → leere Liste."""
|
||||
rm = _make_room_map()
|
||||
# room_private auch in die Map aufnehmen
|
||||
rm.add_or_update_room(
|
||||
room_id="room_private",
|
||||
name="private-group",
|
||||
theme="Private Gruppe",
|
||||
keywords_100=["intern"],
|
||||
keywords_context=[],
|
||||
catch_all=False,
|
||||
)
|
||||
subscribed = _make_subscribed_rooms()
|
||||
|
||||
with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as f:
|
||||
map_path = f.name
|
||||
try:
|
||||
RoomMap.save(rm, Path(map_path))
|
||||
missing = check_room_map_completeness(rm, subscribed, map_path)
|
||||
assert missing == [], f"Map vollständig, erwarte [], bekam {missing}"
|
||||
finally:
|
||||
os.unlink(map_path)
|
||||
|
||||
def test_single_room_rule_auto_assigns(self):
|
||||
"""Single-Room-Regel: Nur 1 c/p-Raum → automatisch als Catch-All."""
|
||||
rm = RoomMap(
|
||||
rooms={},
|
||||
excluded_room_ids=set(),
|
||||
fact_store_path=Path("/tmp/test_docs"),
|
||||
version=2,
|
||||
) # Leere Map
|
||||
subscribed = {
|
||||
"room_only": {"name": "#only-channel", "type": "c"},
|
||||
"room_dm": {"name": "andreas", "type": "d"},
|
||||
}
|
||||
|
||||
with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as f:
|
||||
map_path = f.name
|
||||
try:
|
||||
RoomMap.save(rm, Path(map_path))
|
||||
# Vor reload: add_or_update_room hat room_only in-memory hinzugefügt
|
||||
missing = check_room_map_completeness(rm, subscribed, map_path)
|
||||
# Single-Room-Regel: Raum wird automatisch eingetragen → keine missing
|
||||
assert missing == [], f"Single-Room-Regel: erwarte [], bekam {missing}"
|
||||
# Hinweis: reload() lädt vom default path, nicht von map_path.
|
||||
# In Produktion ist map_path der tatsächliche room-map.json Pfad.
|
||||
finally:
|
||||
os.unlink(map_path)
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
# Bug C: Zombie-State-Validierung
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
class TestBugCZombieState:
|
||||
"""Bug C: Persistierter Zombie-State wird beim Start validiert und verworfen."""
|
||||
|
||||
def setup_method(self):
|
||||
"""Clean up any leftover locks before each test."""
|
||||
import glob
|
||||
for lock_file in glob.glob(str(LOCK_DIR / "onboarding-*.lock")):
|
||||
try:
|
||||
os.unlink(lock_file)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
def teardown_method(self):
|
||||
"""Clean up locks after each test."""
|
||||
self.setup_method()
|
||||
|
||||
def test_zombie_state_detected_operator_not_subscribed(self):
|
||||
"""Zombie-State: Operator-Raum nicht mehr subscribed → verwerfen."""
|
||||
# Simuliere: operator_room_id existiert nicht in _room_info
|
||||
persisted_state = {
|
||||
"phase": "fakt_store",
|
||||
"operator_room_id": "room_gone",
|
||||
"missing_rooms": [{"room_id": "room_x", "name": "#x"}],
|
||||
}
|
||||
# _room_info hat diesen Raum nicht
|
||||
room_info = {"room_plugin": {"name": "#plugin-dev", "type": "c"}}
|
||||
|
||||
op_room = persisted_state.get("operator_room_id", "")
|
||||
op_room_subscribed = op_room in room_info
|
||||
assert not op_room_subscribed, "Operator-Raum nicht subscribed → Zombie"
|
||||
|
||||
def test_zombie_state_detected_lock_not_active(self):
|
||||
"""Zombie-State: Kein aktiver Lock → verwerfen."""
|
||||
persisted_state = {
|
||||
"phase": "fakt_store",
|
||||
"operator_room_id": "room_dm",
|
||||
"missing_rooms": [{"room_id": "room_x", "name": "#x"}],
|
||||
}
|
||||
room_info = {"room_dm": {"name": "DM", "type": "d"}}
|
||||
|
||||
op_room = persisted_state.get("operator_room_id", "")
|
||||
op_room_subscribed = op_room in room_info
|
||||
assert op_room_subscribed, "Operator-Raum ist subscribed"
|
||||
|
||||
# Prüfe Lock — keiner aktiv
|
||||
lock_active = False
|
||||
for room in persisted_state.get("missing_rooms", []):
|
||||
if is_onboarding_locked(room.get("room_id", "")):
|
||||
lock_active = True
|
||||
break
|
||||
assert not lock_active, "Kein Lock aktiv → Zombie"
|
||||
|
||||
def test_valid_state_restored_when_lock_active(self):
|
||||
"""Gültiger State: Operator subscribed + Lock aktiv → wiederherstellen."""
|
||||
# Erstelle einen echten Lock
|
||||
room_id = "room_test_valid"
|
||||
acquired = acquire_onboarding_lock(room_id)
|
||||
assert acquired, "Lock sollte erworben werden"
|
||||
|
||||
try:
|
||||
persisted_state = {
|
||||
"phase": "fakt_store",
|
||||
"operator_room_id": "room_dm",
|
||||
"missing_rooms": [{"room_id": room_id, "name": "#test"}],
|
||||
}
|
||||
room_info = {"room_dm": {"name": "DM", "type": "d"}}
|
||||
|
||||
op_room = persisted_state.get("operator_room_id", "")
|
||||
op_room_subscribed = op_room in room_info
|
||||
assert op_room_subscribed
|
||||
|
||||
lock_active = False
|
||||
for room in persisted_state.get("missing_rooms", []):
|
||||
if is_onboarding_locked(room.get("room_id", "")):
|
||||
lock_active = True
|
||||
break
|
||||
assert lock_active, "Lock ist aktiv → State gültig"
|
||||
finally:
|
||||
release_onboarding_lock(room_id)
|
||||
|
||||
def test_zombie_state_with_phase_fakt_store_no_interview(self):
|
||||
"""Zombie: phase=fakt_store aber kein aktives Interview → verwerfen."""
|
||||
# Dies simuliert ::setfakt das einen State mit phase=fakt_store setzt,
|
||||
# aber kein Interview läuft (keine missing_rooms, kein Lock)
|
||||
persisted_state = {
|
||||
"phase": "fakt_store",
|
||||
"operator_room_id": "room_dm",
|
||||
}
|
||||
room_info = {"room_dm": {"name": "DM", "type": "d"}}
|
||||
|
||||
op_room = persisted_state.get("operator_room_id", "")
|
||||
op_room_subscribed = op_room in room_info
|
||||
assert op_room_subscribed
|
||||
|
||||
# Keine missing_rooms → kein Lock kann aktiv sein
|
||||
missing = persisted_state.get("missing_rooms", [])
|
||||
lock_active = False
|
||||
for room in missing:
|
||||
if is_onboarding_locked(room.get("room_id", "")):
|
||||
lock_active = True
|
||||
break
|
||||
assert not lock_active, "Keine missing_rooms → kein Lock → Zombie"
|
||||
|
||||
def test_lock_acquire_release_cycle(self):
|
||||
"""Lock acquire → is_locked → release Zyklus funktioniert."""
|
||||
room_id = "room_test_cycle"
|
||||
# Initial: kein Lock
|
||||
assert not is_onboarding_locked(room_id)
|
||||
|
||||
# Erwerben
|
||||
assert acquire_onboarding_lock(room_id)
|
||||
assert is_onboarding_locked(room_id)
|
||||
|
||||
# Zweiter Erwerb scheitert
|
||||
assert not acquire_onboarding_lock(room_id)
|
||||
|
||||
# Freigeben
|
||||
release_onboarding_lock(room_id)
|
||||
assert not is_onboarding_locked(room_id)
|
||||
|
||||
def test_lock_cleanup_on_state_clear(self):
|
||||
"""Wenn State gecleart wird, werden Locks freigegeben."""
|
||||
room_ids = ["room_test_a", "room_test_b"]
|
||||
for rid in room_ids:
|
||||
acquire_onboarding_lock(rid)
|
||||
|
||||
# Simuliere _cmd_onboarding Cleanup
|
||||
for rid in room_ids:
|
||||
release_onboarding_lock(rid)
|
||||
|
||||
for rid in room_ids:
|
||||
assert not is_onboarding_locked(rid), f"Lock für {rid} sollte freigegeben sein"
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
# Bug B + Bug A Integration: check_room_map_completeness_delta
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
class TestDeltaCheck:
|
||||
"""Delta-Check für einzelne neue Räume (DDP-Event-Pfad)."""
|
||||
|
||||
def test_delta_check_ignores_dm(self):
|
||||
"""Delta-Check ignoriert type='d' Räume."""
|
||||
rm = _make_room_map()
|
||||
with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as f:
|
||||
map_path = f.name
|
||||
try:
|
||||
RoomMap.save(rm, Path(map_path))
|
||||
result = check_room_map_completeness_delta(
|
||||
rm, room_id="room_new_dm", room_name="DM", room_type="d", map_path=map_path
|
||||
)
|
||||
assert result is None, "DM-Raum muss ignoriert werden"
|
||||
finally:
|
||||
os.unlink(map_path)
|
||||
|
||||
def test_delta_check_finds_new_channel(self):
|
||||
"""Delta-Check findet neuen Channel (type='c')."""
|
||||
rm = _make_room_map()
|
||||
with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as f:
|
||||
map_path = f.name
|
||||
try:
|
||||
RoomMap.save(rm, Path(map_path))
|
||||
result = check_room_map_completeness_delta(
|
||||
rm, room_id="room_new_c", room_name="#new-channel",
|
||||
room_type="c", map_path=map_path
|
||||
)
|
||||
assert result is not None
|
||||
assert result["room_id"] == "room_new_c"
|
||||
assert result["name"] == "#new-channel"
|
||||
finally:
|
||||
os.unlink(map_path)
|
||||
|
||||
def test_delta_check_finds_new_private_group(self):
|
||||
"""Delta-Check findet neue Private Group (type='p')."""
|
||||
rm = _make_room_map()
|
||||
with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as f:
|
||||
map_path = f.name
|
||||
try:
|
||||
RoomMap.save(rm, Path(map_path))
|
||||
result = check_room_map_completeness_delta(
|
||||
rm, room_id="room_new_p", room_name="private-group",
|
||||
room_type="p", map_path=map_path
|
||||
)
|
||||
assert result is not None
|
||||
assert result["room_id"] == "room_new_p"
|
||||
finally:
|
||||
os.unlink(map_path)
|
||||
|
||||
def test_delta_check_skips_existing_room(self):
|
||||
"""Delta-Check: Raum bereits in Map → None."""
|
||||
rm = _make_room_map()
|
||||
with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as f:
|
||||
map_path = f.name
|
||||
try:
|
||||
RoomMap.save(rm, Path(map_path))
|
||||
result = check_room_map_completeness_delta(
|
||||
rm, room_id="room_plugin", room_name="#plugin-dev",
|
||||
room_type="c", map_path=map_path
|
||||
)
|
||||
assert result is None, "Bereits in Map → None"
|
||||
finally:
|
||||
os.unlink(map_path)
|
||||
Reference in New Issue
Block a user