# tests/test_layout.py
import unittest
from core.ddp_parser import DynamicDataParser

class TestLayout(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.parser = DynamicDataParser()
        cls.sample_text = """
• 中心主题
    • 分支1
        1. 子节点1
        2. 子节点2
    • 分支2
        """

    def test_hierarchy_depth(self):
        tree = self.parser.parse_to_tree(self.sample_text)
        self.assertEqual(self._max_depth(tree), 3)

    def test_node_count(self):
        tree = self.parser.parse_to_tree(self.sample_text)
        self.assertEqual(self._count_nodes(tree), 5)

    def test_json_export(self):
        tree = self.parser.parse_to_tree(self.sample_text)
        json_data = self.parser.export_json(tree)
        self.assertIn('"name": "中心主题"', json_data)

    def _max_depth(self, node, current_depth=0):
        if not node['children']:
            return current_depth
        return max(self._max_depth(child, current_depth+1) for child in node['children'])

    def _count_nodes(self, node):
        return 1 + sum(self._count_nodes(child) for child in node['children'])

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