"""Unit tests for ngsite.build_url — slug + share-URL generation.

These drive the canonical-URL behavior the SEO setup leans on: a build's
/b/<code>/<slug> slug must be deterministic, keyword-bearing, and never
leak a profane user title into the URL.
"""
from ngsite import build_url


def test_slugify_basic():
    assert build_url._slugify("Slim Build") == "slim-build"


def test_slugify_collapses_punctuation_and_trims():
    assert build_url._slugify("  My!!  AR-15  Build??  ") == "my-ar-15-build"


def test_slugify_empty_and_none():
    assert build_url._slugify("") == ""
    assert build_url._slugify(None) == ""


def test_slugify_caps_at_80_chars():
    assert len(build_url._slugify("a " * 100)) <= 80


def test_parts_based_slug_empty():
    assert build_url.parts_based_slug([]) == "ar-build"


def test_parts_based_slug_uses_type_labels():
    items = [{"type": "strippedlower"}, {"type": "trigger"}]
    slug = build_url.parts_based_slug(items)
    assert slug.startswith("ar-build-")
    # labels are slugified into the URL ("Stripped Lowers" -> stripped-lowers)
    assert "stripped-lowers" in slug
    assert "triggers" in slug


def test_parts_based_slug_dedupes_and_caps_at_four_types():
    items = [{"type": "trigger"}] * 3 + [
        {"type": f"t{i}"} for i in range(10)]
    slug = build_url.parts_based_slug(items)
    # "triggers" appears once despite three trigger items, and the total
    # distinct types baked in is capped at 4.
    assert slug.count("triggers") == 1


def test_build_slug_prefers_clean_title():
    assert build_url.build_slug("My Cool Build", [{"type": "trigger"}]) \
        == "my-cool-build"


def test_build_slug_falls_back_to_parts_when_title_empty():
    slug = build_url.build_slug("", [{"type": "trigger"}])
    assert slug.startswith("ar-build-")


def test_build_slug_falls_back_when_title_profane():
    # A profane title must not reach the URL — fall back to the parts slug.
    # Guard: only meaningful if the optional better-profanity lib is present
    # and actually flags the word in this environment.
    import pytest
    if not build_url.is_profane("fuck this build"):
        pytest.skip("better-profanity not detecting in this environment")
    slug = build_url.build_slug("fuck this build", [{"type": "trigger"}])
    assert slug.startswith("ar-build")
    assert "fuck" not in slug
