"""Butler-Lernkonzept V2 — Commit 2: 4-Button-Rückfrage + Keyword-Transparenz. Tests für ``ask_routing_learning_choice`` (plugin/adapter.py). V2-Anforderungen: 1. 4 Buttons statt 3 (Button 4 = "✏️ Anderes Stichwort"). 2. Keyword ist im Fragetext SICHTBAR (``**{keyword}**``) + Herkunft-Tag. 3. Bei ``keyword=None``: ehrliche UX, Option 4 als Hauptempfehlung. Diese Tests sind RED vor dem Fix und GREEN nach dem Fix. """ from __future__ import annotations import asyncio import sys import tempfile import unittest from pathlib import Path from unittest.mock import AsyncMock # Ensure the plugin package is importable (mirrors test_v8_3_4.py). sys.path.insert(0, str(Path(__file__).resolve().parents[3])) # Reuse the mock-adapter builder from the existing V8.3.4 integration tests. from test_v8_3_4 import _build_mock_adapter, _make_minimal_room_map_json # noqa: E402 class TestAskRoutingV2(unittest.TestCase): """Commit 2: 4-Button-Render + Keyword-Sichtbarkeit + None-Case.""" @classmethod def setUpClass(cls): cls._tmpdir = tempfile.TemporaryDirectory() cls._room_map_path = _make_minimal_room_map_json(cls._tmpdir.name) @classmethod def tearDownClass(cls): cls._tmpdir.cleanup() def setUp(self): self.adapter, self.mock_rest = _build_mock_adapter(self._room_map_path) # Mock _post_message_with_retry so we can inspect text + attachments. self.adapter._post_message_with_retry = AsyncMock(return_value=True) # Mock presence engine to avoid None-attribute errors. self.adapter._presence_engine = None # _build_mock_adapter uses __new__ and does not init these V2 states. self.adapter._learn_state = {} self.adapter._keyword_override_state = {} def _run_ask(self, keyword, source=""): return asyncio.run(self.adapter.ask_routing_learning_choice( room_id="room_bat_team", keyword=keyword, decision="route", origin_room_id="room_bat_team", target_room_name="#Rocketchat-Plugin", source=source, )) def _last_text(self): """Text des letzten _post_message_with_retry-Aufrufs.""" return self.adapter._post_message_with_retry.call_args[0][1] def _last_attachments(self): """Attachments des letzten _post_message_with_retry-Aufrufs.""" return self.adapter._post_message_with_retry.call_args[1].get("attachments") # ── 4-Button-Render ─────────────────────────────────────────────── def test_four_buttons_rendered(self): """4 Buttons werden gerendert (nicht mehr 3).""" self._run_ask("switch-konfiguration") attachments = self._last_attachments() # Alle actions über alle Attachments einsammeln. all_actions = [] for att in attachments: all_actions.extend(att.get("actions", [])) self.assertEqual(len(all_actions), 4, f"Expected 4 buttons, got {len(all_actions)}") def test_button_4_is_anderes_stichwort(self): """Button 4 hat Label '✏️ Anderes Stichwort' + Payload LEARN::4.""" self._run_ask("switch-konfiguration") attachments = self._last_attachments() all_actions = [] for att in attachments: all_actions.extend(att.get("actions", [])) # Button 4 ist der letzte (Index 3). btn4 = all_actions[3] self.assertIn("Anderes Stichwort", btn4["text"]) self.assertTrue(btn4["msg"].endswith(":4"), f"Button 4 payload should end with :4, got {btn4['msg']}") def test_button_1_label_contains_keyword(self): """Button 1 Label enthält das Keyword (nicht mehr generisch 'Ja').""" self._run_ask("switch-konfiguration") attachments = self._last_attachments() all_actions = [] for att in attachments: all_actions.extend(att.get("actions", [])) btn1 = all_actions[0] self.assertIn("switch-konfiguration", btn1["text"]) # ── Keyword-Sichtbarkeit ────────────────────────────────────────── def test_keyword_visible_in_question_text(self): """Keyword ist im Fragetext sichtbar (fett markiert).""" self._run_ask("switch-konfiguration") text = self._last_text() self.assertIn("**switch-konfiguration**", text, f"Keyword should be visible in text, got: {text}") def test_source_tag_visible(self): """Herkunft-Tag ist sichtbar wenn source gesetzt.""" self._run_ask("switch-konfiguration", source="keywords_100") text = self._last_text() self.assertIn("Herkunft:", text) def test_no_source_tag_when_empty(self): """Kein Herkunft-Tag wenn source leer.""" self._run_ask("switch-konfiguration", source="") text = self._last_text() self.assertNotIn("Herkunft:", text) # ── None-Case ───────────────────────────────────────────────────── def test_none_keyword_asks_for_stichwort(self): """keyword=None → ehrliche UX, fragt nach Stichwort.""" self._run_ask(None) text = self._last_text() self.assertIn("keinen konkreten Vorschlag", text, f"None-case should ask for a keyword, got: {text}") def test_none_keyword_still_four_buttons(self): """keyword=None → trotzdem 4 Buttons (Option 4 als Hauptempfehlung).""" self._run_ask(None) attachments = self._last_attachments() all_actions = [] for att in attachments: all_actions.extend(att.get("actions", [])) self.assertEqual(len(all_actions), 4) def test_none_keyword_no_bold_keyword(self): """keyword=None → kein '**...**' im Text.""" self._run_ask(None) text = self._last_text() self.assertNotIn("**", text) if __name__ == "__main__": unittest.main()