Bot & Automation

AI Assistants

/root/hermes-projects/AI Assistants

tests/test_web_search.py text
from apps.whatsapp_control_api.app.services.web_search import (
    clean_bing_url,
    clean_duckduckgo_url,
    parse_bing_html,
    parse_duckduckgo_html,
    should_use_web_search,
)


def test_current_info_query_uses_web_search() -> None:
    assert should_use_web_search("Carikan data AI terbaru 2026") is True


def test_static_question_does_not_need_web_search() -> None:
    assert should_use_web_search("Apa itu artificial intelligence?") is False


def test_clean_duckduckgo_redirect_url() -> None:
    url = "/l/?uddg=https%3A%2F%2Fexample.com%2Farticle"
    assert clean_duckduckgo_url(url) == "https://example.com/article"


def test_clean_bing_redirect_url() -> None:
    url = "https://www.bing.com/ck/a?u=a1aHR0cHM6Ly9leGFtcGxlLmNvbS9haQ"
    assert clean_bing_url(url) == "https://example.com/ai"


def test_parse_duckduckgo_html_result() -> None:
    raw_html = """
    <div class="result">
      <a rel="nofollow" class="result__a" href="/l/?uddg=https%3A%2F%2Fexample.com%2Fai">AI News</a>
      <a class="result__snippet">Latest AI update and market data.</a>
    </div>
    """
    results = parse_duckduckgo_html(raw_html)
    assert len(results) == 1
    assert results[0].title == "AI News"
    assert results[0].url == "https://example.com/ai"
    assert results[0].snippet == "Latest AI update and market data."


def test_parse_bing_html_result() -> None:
    raw_html = """
    <li class="b_algo">
      <h2><a href="https://example.com/realtime">Realtime AI Data</a></h2>
      <div><p>Fresh source snippet from the web.</p></div>
    </li>
    """
    results = parse_bing_html(raw_html)
    assert len(results) == 1
    assert results[0].title == "Realtime AI Data"
    assert results[0].url == "https://example.com/realtime"
    assert results[0].snippet == "Fresh source snippet from the web."