"""Unit tests for market slug helpers."""
from __future__ import annotations

import pytest

from market.bp.browse.services.slugs import (
    product_slug_from_href, canonical_html_slug,
)


class TestProductSlugFromHref:
    def test_html_extension(self):
        result = product_slug_from_href("https://site.com/foo/bar-thing.html")
        assert result == "bar-thing-html"

    def test_htm_extension(self):
        result = product_slug_from_href("https://site.com/products/widget.htm")
        assert result == "widget-html"

    def test_no_extension(self):
        result = product_slug_from_href("https://site.com/item/cool-product")
        assert result == "cool-product-html"

    def test_empty_path(self):
        result = product_slug_from_href("https://site.com/")
        assert result == ""

    def test_already_has_html_suffix_in_slug(self):
        result = product_slug_from_href("https://site.com/prod/item-html.html")
        assert result == "item-html"


class TestCanonicalHtmlSlug:
    def test_adds_html_suffix(self):
        assert canonical_html_slug("product-name") == "product-name-html"

    def test_idempotent(self):
        assert canonical_html_slug("product-name-html") == "product-name-html"

    def test_double_html_kept(self):
        # canonical_html_slug only appends -html if not already present
        assert canonical_html_slug("product-name-html-html") == "product-name-html-html"

    def test_strips_htm(self):
        assert canonical_html_slug("product-name-htm") == "product-name-html"
