import unittest
import makesite


class StripTagsAndTruncateTest(unittest.TestCase):

    def test_truncate_plain_text(self):
        """Test truncating plain text without HTML."""
        text = "This is a simple sentence with more than five words in it."
        result = makesite._strip_tags_and_truncate(text, words=5)
        self.assertEqual(result, "This is a simple sentence")

    def test_truncate_default_words(self):
        """Test truncating with default word count (25)."""
        text = " ".join([f"word{i}" for i in range(30)])  # 30 words
        result = makesite._strip_tags_and_truncate(text)
        words_result = result.split()
        self.assertEqual(len(words_result), 25)

    def test_strip_and_truncate_html(self):
        """Test stripping HTML tags and truncating."""
        text = "<p>One <strong>two</strong> three</p> <div>four five six seven</div>"
        result = makesite._strip_tags_and_truncate(text, words=5)
        self.assertEqual(result, "One two three four five")

    def test_truncate_exact_word_count(self):
        """Test text with exactly the word count requested."""
        text = "one two three four five"
        result = makesite._strip_tags_and_truncate(text, words=5)
        self.assertEqual(result, "one two three four five")

    def test_truncate_fewer_words(self):
        """Test text with fewer words than requested."""
        text = "only three words"
        result = makesite._strip_tags_and_truncate(text, words=10)
        self.assertEqual(result, "only three words")

    def test_empty_string(self):
        """Test with empty string."""
        text = ""
        result = makesite._strip_tags_and_truncate(text, words=5)
        self.assertEqual(result, "")

    def test_only_html_tags(self):
        """Test with only HTML tags and no text."""
        text = "<div></div><p></p><span></span>"
        result = makesite._strip_tags_and_truncate(text, words=5)
        self.assertEqual(result, "")

    def test_multiple_spaces_normalized(self):
        """Test that multiple spaces are normalized to single spaces."""
        text = "word1    word2     word3  word4"
        result = makesite._strip_tags_and_truncate(text, words=3)
        self.assertEqual(result, "word1 word2 word3")

    def test_newlines_treated_as_spaces(self):
        """Test that newlines are treated as word separators."""
        text = "line1\nline2\nline3\nline4"
        result = makesite._strip_tags_and_truncate(text, words=2)
        self.assertEqual(result, "line1 line2")

    def test_complex_html_content(self):
        """Test with complex nested HTML."""
        text = """<article>
        <h1>Title</h1>
        <p>This is a <strong>bold</strong> paragraph with <a href="#">link</a>.</p>
        <div>More content here with many words to test truncation properly.</div>
        </article>"""
        result = makesite._strip_tags_and_truncate(text, words=10)
        words = result.split()
        self.assertEqual(len(words), 10)
        self.assertIn("Title", result)
        self.assertIn("bold", result)

    def test_single_word(self):
        """Test with single word."""
        text = "OnlyOneWord"
        result = makesite._strip_tags_and_truncate(text, words=5)
        self.assertEqual(result, "OnlyOneWord")

    def test_truncate_one_word(self):
        """Test truncating to just one word."""
        text = "First second third fourth"
        result = makesite._strip_tags_and_truncate(text, words=1)
        self.assertEqual(result, "First")

    def test_html_entities_preserved(self):
        """Test that HTML entities are preserved in text."""
        text = "<p>&amp; &lt; &gt; &quot; words here</p>"
        result = makesite._strip_tags_and_truncate(text, words=3)
        self.assertEqual(result, "&amp; &lt; &gt;")

    def test_special_characters(self):
        """Test with special characters and accents."""
        text = "Café résumé naïve élève über"
        result = makesite._strip_tags_and_truncate(text, words=3)
        self.assertEqual(result, "Café résumé naïve")

    def test_punctuation_preserved(self):
        """Test that punctuation is preserved."""
        text = "Hello, world! How are you?"
        result = makesite._strip_tags_and_truncate(text, words=3)
        self.assertEqual(result, "Hello, world! How")

    def test_tabs_and_spaces(self):
        """Test with tabs and various whitespace."""
        text = "word1\t\tword2\t  word3   word4"
        result = makesite._strip_tags_and_truncate(text, words=3)
        self.assertEqual(result, "word1 word2 word3")

    def test_html_with_attributes(self):
        """Test HTML with various attributes gets stripped correctly."""
        text = '<p class="intro" id="first">Word1 word2 word3</p><span style="color:red">word4 word5</span>'
        result = makesite._strip_tags_and_truncate(text, words=4)
        self.assertEqual(result, "Word1 word2 word3 word4")

    def test_zero_words(self):
        """Test with zero words requested."""
        text = "Some text here"
        result = makesite._strip_tags_and_truncate(text, words=0)
        self.assertEqual(result, "")

    def test_leading_trailing_whitespace(self):
        """Test that leading/trailing whitespace doesn't affect word count."""
        text = "   word1 word2 word3   "
        result = makesite._strip_tags_and_truncate(text, words=2)
        self.assertEqual(result, "word1 word2")
