from datetime import UTC, datetime, timedelta from types import SimpleNamespace from unittest.mock import AsyncMock async def test_private_admin_can_toggle_identity_notifications(app_modules, monkeypatch): panel = app_modules.load("wbb.modules.admin_panel") apply_settings = AsyncMock(return_value={}) show_section = AsyncMock() monkeypatch.setattr(panel, "ensure_permission", AsyncMock()) monkeypatch.setattr( panel, "get_automation_settings", AsyncMock( return_value={ "identity_monitor": {"enabled": True, "notify_in_chat": False} } ), ) monkeypatch.setattr(panel, "apply_automation_settings", apply_settings) monkeypatch.setattr(panel, "record_audit", AsyncMock()) monkeypatch.setattr(panel, "_show_section", show_section) query = SimpleNamespace( from_user=SimpleNamespace(id=88, first_name="管理员"), answer=AsyncMock(), ) panel._confirmations["identity-toggle"] = { "actor_id": 88, "direct": { "action": "identity_monitor_toggle", "chat_id": -100, "key": "notify_in_chat", }, "expires_at": datetime.now(UTC) + timedelta(minutes=1), } await panel._execute_direct_confirmation(query, "identity-toggle") apply_settings.assert_awaited_once_with( -100, {"identity_monitor": {"enabled": True, "notify_in_chat": True}}, actor_id=88, ) show_section.assert_awaited_once_with(query, -100, "identity") query.answer.assert_awaited_once_with("操作成功。", show_alert=True)