from unittest.mock import patch, MagicMock
from juris_base.tiers.tier3_aspnet import BaseAspNetClient

SAMPLE_HTML = """
<input id="__VIEWSTATE" value="VS123" />
<input id="__EVENTVALIDATION" value="EV456" />
<p>Resultado: 1 de 42</p>
"""


class ConcreteAspNetClient(BaseAspNetClient):
    SEARCH_URL = "https://example.gov.ar/Buscar.aspx"

    def search(self, query: str, **filters) -> dict:
        html, cookies = self._fetch(self.SEARCH_URL)
        tokens = self._extract_tokens(html)
        result_html, _ = self._fetch(self.SEARCH_URL, data={**tokens, "q": query}, cookies=cookies)
        total = self._get_total(result_html)
        return {"total": total, "results": []}

    def get(self, id: str):
        from juris_base.models import JurisDocument
        return JurisDocument(id=id, texto_completo="")


def test_extract_tokens():
    client = ConcreteAspNetClient()
    tokens = client._extract_tokens(SAMPLE_HTML)
    assert tokens["__VIEWSTATE"] == "VS123"
    assert tokens["__EVENTVALIDATION"] == "EV456"


def test_get_total():
    client = ConcreteAspNetClient()
    assert client._get_total(SAMPLE_HTML) == 42
    assert client._get_total("<p>nada aqui</p>") == 0
