"""Test that real tool functions return non-dummy output."""

import pytest
import requests
from agenlang.tools import web_search, summarize


def _network_available():
    try:
        requests.get("https://lite.duckduckgo.com/lite/", timeout=3)
        return True
    except requests.RequestException:
        return False


needs_network = pytest.mark.skipif(
    not _network_available(), reason="No network access"
)


@needs_network
def test_web_search_returns_real_results():
    result = web_search({"query": "Python programming language"})
    assert "Dummy" not in result
    assert len(result) > 20


@needs_network
def test_web_search_empty_query():
    result = web_search({"query": ""})
    assert result == "No query provided."


def test_web_search_no_network(monkeypatch):
    def mock_get(*a, **kw):
        raise requests.ConnectionError("no network")
    monkeypatch.setattr(requests, "get", mock_get)
    result = web_search({"query": "test"})
    assert "Search failed" in result


def test_summarize_truncates():
    long_text = "a" * 200
    result = summarize({"text": long_text})
    assert len(result) == 103  # 100 chars + "..."
    assert result.endswith("...")


def test_summarize_short_text():
    result = summarize({"text": "short"})
    assert result == "short"


def test_summarize_empty():
    result = summarize({"text": ""})
    assert result == "No text provided."
