"""Tests for the statute scraper."""

import pytest
from unittest.mock import AsyncMock, patch

from ars25.ingestion.scraper import _clean_text, scrape_all


def test_clean_text_removes_scripts():
    html = "<html><body><script>alert('x')</script><p>Hello world</p></body></html>"
    text = _clean_text(html)
    assert "alert" not in text
    assert "Hello world" in text


def test_clean_text_collapses_whitespace():
    html = "<html><body><p>Hello     world</p><p>\n\n\n\nMore text</p></body></html>"
    text = _clean_text(html)
    assert "     " not in text
    assert "\n\n\n" not in text


def test_clean_text_removes_nav():
    html = '<html><body><nav>Navigation</nav><div>Content here</div></body></html>'
    text = _clean_text(html)
    assert "Navigation" not in text
    assert "Content here" in text


@pytest.mark.asyncio
async def test_scrape_all_returns_results():
    """Test scrape_all by mocking scrape_statute at the function level."""

    async def mock_scrape(session, url):
        return "Statute text about marriage"

    with patch("ars25.ingestion.scraper.scrape_statute", side_effect=mock_scrape):
        results = await scrape_all(
            {"25-101": "https://example.com/101"},
            concurrency=1,
            delay_ms=0,
        )

    assert "25-101" in results
    assert "marriage" in results["25-101"].lower()


@pytest.mark.asyncio
async def test_scrape_all_handles_failures():
    """Test that scrape_all continues past individual failures."""

    async def mock_scrape(session, url):
        if "101" in url:
            raise RuntimeError("HTTP 404")
        return "Some text"

    with patch("ars25.ingestion.scraper.scrape_statute", side_effect=mock_scrape):
        results = await scrape_all(
            {
                "25-101": "https://example.com/101",
                "25-102": "https://example.com/102",
            },
            concurrency=1,
            delay_ms=0,
        )

    assert "25-101" not in results
    assert "25-102" in results
