import pytest
from src.patch_modal import (
  PatchModalManager, PendingPatch, 
  get_patch_modal_manager, reset_patch_modal_manager
)

def test_patch_modal_manager_init():
  manager = PatchModalManager()
  assert manager.get_pending_patch() is None
  assert manager.is_modal_shown() is False

def test_request_patch_approval():
  manager = PatchModalManager()
  patch_text = "--- a/test.py\n+++ b/test.py\n@@ -1 +1 @@\n-old\n+new"
  file_paths = ["test.py"]
  
  result = manager.request_patch_approval(patch_text, file_paths)
  assert result is True
  assert manager.is_modal_shown() is True
  
  pending = manager.get_pending_patch()
  assert pending is not None
  assert pending.patch_text == patch_text
  assert pending.file_paths == file_paths

def test_reject_patch():
  manager = PatchModalManager()
  manager.request_patch_approval("diff", ["file.py"])
  
  manager.reject_patch()
  assert manager.get_pending_patch() is None
  assert manager.is_modal_shown() is False

def test_close_modal():
  manager = PatchModalManager()
  manager.request_patch_approval("diff", ["file.py"])
  
  manager.close_modal()
  assert manager.is_modal_shown() is False

def test_apply_callback():
  manager = PatchModalManager()
  called = []
  
  def apply_fn(patch: str) -> bool:
    called.append(patch)
    return True
  
  manager.set_apply_callback(apply_fn)
  
  patch_text = "--- a/test.py\n+++ b/test.py\n"
  result = manager.apply_patch(patch_text)
  
  assert result is True
  assert len(called) == 1
  assert called[0] == patch_text

def test_reject_callback():
  manager = PatchModalManager()
  called = []
  
  def reject_fn() -> None:
    called.append(True)
  
  manager.set_reject_callback(reject_fn)
  manager.reject_patch()
  
  assert len(called) == 1

def test_reset():
  manager = PatchModalManager()
  manager.request_patch_approval("diff", ["file.py"])
  manager.set_apply_callback(lambda x: True)
  manager.set_reject_callback(lambda: None)
  
  manager.reset()
  
  assert manager.get_pending_patch() is None
  assert manager.is_modal_shown() is False

def test_get_patch_modal_manager_singleton():
  reset_patch_modal_manager()
  
  mgr1 = get_patch_modal_manager()
  mgr2 = get_patch_modal_manager()
  
  assert mgr1 is mgr2
  
  reset_patch_modal_manager()