import unittest
from unittest.mock import patch, MagicMock
from src.app_controller import parse_symbols, get_symbol_definition

class TestSymbolLookup(unittest.TestCase):
 def test_parse_symbols_basic(self):
  text = "Check @MyClass and @my_func."
  symbols = parse_symbols(text)
  self.assertEqual(symbols, ["MyClass", "my_func"])

 def test_parse_symbols_methods(self):
  text = "Calling @MyClass.my_method and @AnotherClass.method_name."
  symbols = parse_symbols(text)
  self.assertEqual(symbols, ["MyClass.my_method", "AnotherClass.method_name"])

 def test_parse_symbols_no_symbols(self):
  text = "This string has no symbols."
  symbols = parse_symbols(text)
  self.assertEqual(symbols, [])

 def test_parse_symbols_mixed(self):
  text = "Mixed text: @Class1, @func_2, and some text @MyClass.method."
  symbols = parse_symbols(text)
  self.assertEqual(symbols, ["Class1", "func_2", "MyClass.method"])

 def test_parse_symbols_edge_cases(self):
  text = "@LeadingSymbol and @SymbolAtEnd"
  symbols = parse_symbols(text)
  self.assertEqual(symbols, ["LeadingSymbol", "SymbolAtEnd"])

 def test_get_symbol_definition_found(self):
  files = ["file1.py", "file2.py"]
  symbol = "my_func"
  def_content = "def my_func():\n pass"
  line_number = 10
  
  with patch("src.mcp_client.py_get_symbol_info") as mock_get_info:
   # First file not found, second file found
   mock_get_info.side_effect = [
    "ERROR: definition 'my_func' not found in file1.py",
    (def_content, line_number)
   ]
   
   result = get_symbol_definition(symbol, files)
   self.assertEqual(result, ("file2.py", def_content, line_number))
   self.assertEqual(mock_get_info.call_count, 2)

 def test_get_symbol_definition_not_found(self):
  files = ["file1.py"]
  symbol = "my_func"
  
  with patch("src.mcp_client.py_get_symbol_info") as mock_get_info:
   mock_get_info.return_value = "ERROR: definition 'my_func' not found in file1.py"
   
   result = get_symbol_definition(symbol, files)
   self.assertIsNone(result)

if __name__ == "__main__":
 unittest.main()
