from unittest.mock import patch, MagicMock
from src.gui_2 import render_comms_history_panel

def test_render_comms_history_panel_no_extraneous_pop_in_prior_mode(app_instance):
 """Regression test: render_comms_history_panel must not pop_style_color
  more times than it pushes when is_viewing_prior_session is True.
  Previously, line 3761 had a stray pop with no matching push (the push
  was removed during the comms_scroll child-size fix).
 """
 app_instance.is_viewing_prior_session = True
 app_instance.perf_profiling_enabled = False
 app_instance._scroll_comms_to_bottom = False
 app_instance._comms_log_cache = []
 app_instance.cb_load_prior_log = MagicMock()
 app_instance.controller = MagicMock()
 app_instance.controller.event_queue.put = MagicMock()
 push_calls = []
 pop_calls = []
 with patch("src.gui_2.imgui") as mock_imgui, patch("src.gui_2.imscope") as mock_imscope, patch("src.gui_2.theme") as mock_theme:
  mock_imgui.push_style_color.side_effect = lambda *a, **k: push_calls.append(a)
  mock_imgui.pop_style_color.side_effect = lambda *a, **k: pop_calls.append(a)
  mock_imgui.text = MagicMock()
  mock_imgui.text_colored = MagicMock()
  mock_imgui.same_line = MagicMock()
  mock_imgui.separator = MagicMock()
  mock_imgui.button = MagicMock(return_value=False)
  mock_imgui.set_scroll_here_y = MagicMock()
  mock_imgui.begin_child = MagicMock(return_value=True)
  mock_imgui.end_child = MagicMock()
  mock_imgui.get_content_region_avail = MagicMock(return_value=MagicMock(x=800.0, y=600.0))
  mock_imgui.begin = MagicMock(return_value=True)
  mock_imgui.end = MagicMock()
  mock_imgui.ImVec2 = lambda *a: MagicMock(x=float(a[0]) if a else 0, y=float(a[1]) if len(a) > 1 else 0)
  mock_imgui.ImVec4 = lambda *a: ("ImVec4", a)
  for attr in ["Col_", "WindowFlags_", "TableFlags_", "TableColumnFlags_", "SelectableFlags_", "TreeNodeFlags_", "Cond_", "StyleVar_", "StyleColor_", "HoveredFlags_", "Key", "TabItemFlags_"]:
   setattr(mock_imgui, attr, MagicMock())
  def _scope_enter(): return True
  def _scope_exit(*a): return False
  for sc in [mock_imscope.style_color, mock_imscope.style_var, mock_imscope.child, mock_imscope.tab_bar, mock_imscope.tab_item, mock_imscope.tree_node_ex, mock_imscope.group, mock_imscope.indent, mock_imscope.id, mock_imscope.text_wrap, mock_imscope.tooltip, mock_imscope.menu, mock_imscope.menu_bar, mock_imscope.popup, mock_imscope.popup_modal, mock_imscope.window, mock_imscope.table]:
   sc.return_value.__enter__ = MagicMock(side_effect=_scope_enter)
   sc.return_value.__exit__ = MagicMock(side_effect=_scope_exit)
  mock_theme.ai_text_style.return_value.__enter__ = MagicMock()
  mock_theme.ai_text_style.return_value.__exit__ = MagicMock(side_effect=_scope_exit)
  try:
   render_comms_history_panel(app_instance)
  except Exception as e:
   import pytest
   pytest.fail(f"render_comms_history_panel raised: {e}")
 assert len(push_calls) == len(pop_calls), f"push_style_color/pop_style_color imbalance in prior mode: {len(push_calls)} pushes vs {len(pop_calls)} pops"
