import unittest
from slugify import slugify

class TestSlugify(unittest.TestCase):
    def test_basic(self):
        self.assertEqual(slugify("Hello, World!"), "hello-world")

    def test_collapse(self):
        self.assertEqual(slugify("a  --  b"), "a-b")

    def test_no_max_length_backward_compat(self):
        self.assertEqual(slugify("hello-world-foo"), "hello-world-foo")

    def test_max_length_none_explicit(self):
        self.assertEqual(slugify("hello-world-foo", None), "hello-world-foo")

    def test_max_length_longer_than_slug(self):
        self.assertEqual(slugify("hello-world", max_length=100), "hello-world")

    def test_max_length_exact_fit(self):
        self.assertEqual(slugify("Hello, World!", max_length=11), "hello-world")

    def test_truncate_at_clean_boundary_keeps_full_words(self):
        self.assertEqual(slugify("abcdef-ghijkl", max_length=6), "abcdef")

    def test_truncate_mid_word_retreats_to_hyphen(self):
        self.assertEqual(slugify("hello-world-foo", max_length=10), "hello")

    def test_truncate_strips_trailing_hyphen(self):
        self.assertEqual(slugify("hello-world-foo", max_length=12), "hello-world")

    def test_truncate_no_hyphen_available_keeps_partial(self):
        self.assertEqual(slugify("hello-world-foo", max_length=4), "hell")

    def test_truncate_no_hyphen_in_slug_at_all(self):
        self.assertEqual(slugify("abcdef-ghijkl", max_length=8), "abcdef")

    def test_max_length_zero(self):
        self.assertEqual(slugify("hello-world-foo", max_length=0), "")

    def test_max_length_negative(self):
        self.assertEqual(slugify("hello-world-foo", max_length=-3), "")

    def test_max_length_one(self):
        self.assertEqual(slugify("hello-world", max_length=1), "h")

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