from __future__ import annotations import httpx import searxng_mcp.extract as extract_module from searxng_mcp.extract import extract_from_html, extract_from_response def make_response( url: str, content: str, content_type: str = "text/html; charset=utf-8" ) -> httpx.Response: return httpx.Response( 200, content=content.encode("utf-8"), headers={"content-type": content_type}, request=httpx.Request("GET", url), ) def test_extract_html_document() -> None: response = make_response( "https://example.org/article", """ Example Article

Heading One

First paragraph.

Second paragraph with a docs link.

""", ) document = extract_from_response(response, max_excerpt_chars=400) assert document.title == "Example Article" assert document.description == "Short description." assert "Heading One" in document.headings assert document.links[0]["url"] == "https://example.org/docs" assert "First paragraph" in document.excerpt def test_extract_pdf_document(monkeypatch) -> None: monkeypatch.setattr( extract_module, "extract_pdf_text", lambda *args, **kwargs: "PDF text body" ) response = httpx.Response( 200, content=b"%PDF-1.4 test", headers={"content-type": "application/pdf"}, request=httpx.Request("GET", "https://example.org/file.pdf"), ) document = extract_from_response(response, max_excerpt_chars=100) assert document.content_type == "application/pdf" assert "PDF text body" in document.text assert document.excerpt == "PDF text body" def test_extract_rendered_html_document_marks_rendered() -> None: document = extract_from_html( """ Rendered Article

Rendered Heading

Rendered body text.

""", url="https://example.org/rendered", max_excerpt_chars=400, rendered=True, ) assert document.rendered is True assert document.to_summary()["rendered"] is True assert document.title == "Rendered Article" assert "Rendered Heading" in document.headings