import pytest
from unittest.mock import patch, MagicMock
from src.gui_2 import render_text_viewer_window

def test_text_viewer_window_id_stability():
 # Setup a mock app with necessary state
 app = MagicMock()
 app.show_windows = {"Text Viewer": True}
 app.text_viewer_title = "Custom Title"
 app.text_viewer_content = "Some content"
 app.text_viewer_type = "text"
 app.text_viewer_wrap = False
 app._slice_sel_start = -1
 app._slice_sel_end = -1
 app.ui_editing_slices_file = None

 with patch('src.gui_2.imgui') as mock_imgui, \
      patch('src.gui_2.imscope') as mock_imscope:
  
  mock_imgui.begin.return_value = (True, True)
  mock_imgui.checkbox.return_value = (False, True)

  render_text_viewer_window(app)

  # Verify imgui.begin was called with the stable ID suffix
  args, _ = mock_imgui.begin.call_args
  window_title = args[0]
  assert "###Text_Viewer_Unified" in window_title
  assert window_title.startswith("Custom Title")

def test_text_viewer_window_default_title_id_stability():
 app = MagicMock()
 app.show_windows = {"Text Viewer": True}
 app.text_viewer_title = ""
 app.text_viewer_content = "Some content"
 app.text_viewer_type = "text"
 app.text_viewer_wrap = False
 app.ui_editing_slices_file = None

 with patch('src.gui_2.imgui') as mock_imgui, \
      patch('src.gui_2.imscope') as mock_imscope:
  
  mock_imgui.begin.return_value = (True, True)
  mock_imgui.checkbox.return_value = (False, True)

  render_text_viewer_window(app)

  # Verify imgui.begin was called with the stable ID suffix
  args, _ = mock_imgui.begin.call_args
  window_title = args[0]
  assert "###Text_Viewer_Unified" in window_title
  assert window_title.startswith("Text Viewer")
