import unittest
from typing import cast

from htmlnode import HtmlNode
from leafnode import LeafNode
from parentnode import ParentNode


class TestParentNode(unittest.TestCase):
    def test_to_html_when_no_tag_it_should_raise_value_error(self):
        node = ParentNode(cast(str, None), [LeafNode("p", "test")])

        with self.assertRaises(ValueError):
            node.to_html()

    def test_to_html_when_no_children_it_should_raise_value_error(self):
        node = ParentNode("a", cast(list[HtmlNode], None))

        with self.assertRaises(ValueError):
            node.to_html()

    def test_to_html_when_children_is_empty_it_should_raise_value_error(self):
        node = ParentNode("a", [])

        with self.assertRaises(ValueError):
            node.to_html()

    def test_to_html_when_tag_and_children_it_should_return_html(self):
        expected = "<p><b>Bold text</b>Normal text<i>italic text</i>Normal text</p>"

        node = ParentNode(
            "p",
            [
                LeafNode("b", "Bold text"),
                LeafNode(None, "Normal text"),
                LeafNode("i", "italic text"),
                LeafNode(None, "Normal text"),
            ],
        )

        result = node.to_html()

        self.assertEqual(result, expected)

    def test_to_html_when_with_children_it_should_return_html(self):
        child_node = LeafNode("span", "child")
        parent_node = ParentNode("div", [child_node])
        self.assertEqual(parent_node.to_html(), "<div><span>child</span></div>")

    def test_to_html_with_grandchildren_it_should_return_html(self):
        grandchild_node = LeafNode("b", "grandchild")
        child_node = ParentNode("span", [grandchild_node])
        parent_node = ParentNode("div", [child_node])
        self.assertEqual(
            parent_node.to_html(),
            "<div><span><b>grandchild</b></span></div>",
        )
