import unittest
import sys
import os

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from binary_tree import BinarySearchTree


class TestBinarySearchTree(unittest.TestCase):
    def setUp(self):
        self.bst = BinarySearchTree()
        self.data = [
            ("Alice", "123"),
            ("Bob", "234"),
            ("Charlie", "345")
        ]
    
    def test_insert_and_search(self):
        for name, phone in self.data:
            self.bst.insert(name, phone)
        
        for name, phone in self.data:
            self.assertEqual(self.bst.search(name), phone)
    
    def test_search_not_found(self):
        self.bst.insert("Alice", "123")
        self.assertIsNone(self.bst.search("Bob"))
    
    def test_delete(self):
        self.bst.insert("Alice", "123")
        self.assertTrue(self.bst.delete("Alice"))
        self.assertIsNone(self.bst.search("Alice"))
        self.assertEqual(self.bst.get_size(), 0)
    
    def test_size(self):
        self.assertEqual(self.bst.get_size(), 0)
        self.bst.insert("Alice", "123")
        self.assertEqual(self.bst.get_size(), 1)
        self.bst.insert("Bob", "234")
        self.assertEqual(self.bst.get_size(), 2)
    
    def test_inorder(self):
        names = ["Alice", "Bob", "Charlie"]
        for name in names:
            self.bst.insert(name, "123")
        
        result = self.bst.inorder()
        self.assertEqual(len(result), 3)
        for i, name in enumerate(names):
            self.assertEqual(result[i]['name'], name)
    
    def test_clear(self):
        self.bst.insert("Test", "123")
        self.assertFalse(self.bst.is_empty())
        self.bst.clear()
        self.assertTrue(self.bst.is_empty())


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