import unittest
import makesite


class ParseHeadersTest(unittest.TestCase):

    def test_single_header(self):
        """Test parsing a single header."""
        text = "<!-- title: My Post Title -->\nContent here"
        headers = list(makesite._parse_headers(text))
        self.assertEqual(len(headers), 1)
        self.assertEqual(headers[0][0], "title")
        self.assertEqual(headers[0][1], "My Post Title")

    def test_multiple_headers(self):
        """Test parsing multiple headers."""
        text = """<!-- title: My Post -->
<!-- category: Tech Python -->
<!-- tag: blog tutorial -->
Content starts here"""
        headers = list(makesite._parse_headers(text))
        self.assertEqual(len(headers), 3)
        self.assertEqual(headers[0], ("title", "My Post", headers[0][2]))
        self.assertEqual(headers[1], ("category", "Tech Python", headers[1][2]))
        self.assertEqual(headers[2], ("tag", "blog tutorial", headers[2][2]))

    def test_headers_with_extra_whitespace(self):
        """Test parsing headers with extra whitespace."""
        text = "<!--   title  :  My Post  -->\n<!-- category:Tech-->\nContent"
        headers = list(makesite._parse_headers(text))
        self.assertEqual(len(headers), 2)
        self.assertEqual(headers[0][0], "title")
        self.assertEqual(headers[0][1], "My Post")
        self.assertEqual(headers[1][0], "category")
        self.assertEqual(headers[1][1], "Tech")

    def test_no_headers(self):
        """Test parsing content with no headers."""
        text = "Just regular content\nNo headers here"
        headers = list(makesite._parse_headers(text))
        self.assertEqual(len(headers), 0)

    def test_headers_stop_at_content(self):
        """Test that parsing stops when content (non-header) is found."""
        text = """<!-- title: Post Title -->
<!-- category: Tech -->
This is content
<!-- tag: not-a-header -->"""
        headers = list(makesite._parse_headers(text))
        self.assertEqual(len(headers), 2)
        # Should only get title and category, not the tag after content

    def test_header_with_special_characters(self):
        """Test parsing headers with special characters in values."""
        text = "<!-- title: C'est l'été! Les accents: àéêô -->\nContent"
        headers = list(makesite._parse_headers(text))
        self.assertEqual(len(headers), 1)
        self.assertEqual(headers[0][1], "C'est l'été! Les accents: àéêô")

    def test_header_with_colon_in_value(self):
        """Test parsing headers where the value contains colons."""
        text = "<!-- title: Docker: Configuration et Sécurité -->\nContent"
        headers = list(makesite._parse_headers(text))
        self.assertEqual(len(headers), 1)
        self.assertEqual(headers[0][1], "Docker: Configuration et Sécurité")

    def test_empty_header_value(self):
        """Test parsing headers with empty values."""
        text = "<!-- title: -->\n<!-- category: Tech -->\nContent"
        headers = list(makesite._parse_headers(text))
        self.assertEqual(len(headers), 2)
        # Empty value results in a single space due to regex whitespace handling
        self.assertEqual(headers[0][1].strip(), "")
        self.assertEqual(headers[1][1], "Tech")

    def test_end_index_tracking(self):
        """Test that end index correctly tracks position after headers."""
        text = "<!-- title: Test -->\n<!-- tag: python -->\nContent starts"
        headers = list(makesite._parse_headers(text))
        # The last header's end index should point to where content starts
        last_end = headers[-1][2]
        self.assertTrue(last_end > 0)
        self.assertTrue(text[last_end:].startswith("Content") or text[last_end:].startswith("\nContent"))
