Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

6.2 KB
Raw Blame
"""High-value tests for small utility modules: url_utils, network_utils, type_utils, text_cleaner."""

import unittest

from local_deep_research.security.network_utils import is_private_ip
from local_deep_research.utilities.type_utils import to_bool
from local_deep_research.utilities.url_utils import normalize_url
from local_deep_research.text_processing.text_cleaner import remove_surrogates


# ── is_private_ip ──────────────────────────────────────────────────────


class TestIsPrivateIpLocalhost(unittest.TestCase):
    def test_localhost_string(self):
        assert is_private_ip("localhost") is True

    def test_127_0_0_1(self):
        assert is_private_ip("127.0.0.1") is True

    def test_ipv6_loopback_bracketed(self):
        assert is_private_ip("[::1]") is True

    def test_0_0_0_0(self):
        assert is_private_ip("0.0.0.0") is True


class TestIsPrivateIpRanges(unittest.TestCase):
    def test_10_network(self):
        assert is_private_ip("10.0.0.1") is True

    def test_172_16_network(self):
        assert is_private_ip("172.16.0.50") is True

    def test_192_168_network(self):
        assert is_private_ip("192.168.1.100") is True

    def test_public_ip_8888(self):
        assert is_private_ip("8.8.8.8") is False

    def test_public_ip_1111(self):
        assert is_private_ip("1.1.1.1") is False


class TestIsPrivateIpDomains(unittest.TestCase):
    def test_local_domain(self):
        assert is_private_ip("myhost.local") is True

    def test_external_domain(self):
        assert is_private_ip("api.openai.com") is False

    def test_random_hostname(self):
        assert is_private_ip("somehost") is False


class TestIsPrivateIpIpv6(unittest.TestCase):
    def test_ipv6_link_local(self):
        assert is_private_ip("fe80::1") is True

    def test_ipv6_private(self):
        assert is_private_ip("fc00::1") is True

    def test_bracketed_ipv6_private(self):
        assert is_private_ip("[fc00::1]") is True


# ── normalize_url ──────────────────────────────────────────────────────


class TestNormalizeUrl(unittest.TestCase):
    def test_already_http(self):
        assert normalize_url("http://example.com") == "http://example.com"

    def test_already_https(self):
        assert normalize_url("https://example.com") == "https://example.com"

    def test_malformed_http_colon(self):
        assert normalize_url("http:example.com") == "http://example.com"

    def test_malformed_https_colon(self):
        assert normalize_url("https:example.com") == "https://example.com"

    def test_localhost_gets_http(self):
        result = normalize_url("localhost:11434")
        assert result == "http://localhost:11434"

    def test_external_host_gets_https(self):
        result = normalize_url("api.openai.com:443")
        assert result == "https://api.openai.com:443"

    def test_empty_raises_value_error(self):
        with self.assertRaises(ValueError):
            normalize_url("")

    def test_whitespace_stripped(self):
        result = normalize_url("  http://example.com  ")
        assert result == "http://example.com"

    def test_double_slash_prefix(self):
        result = normalize_url("//localhost:8080")
        assert result == "http://localhost:8080"

    def test_private_ip_gets_http(self):
        result = normalize_url("192.168.1.1:8080")
        assert result == "http://192.168.1.1:8080"


# ── to_bool ────────────────────────────────────────────────────────────


class TestToBoolPassthrough(unittest.TestCase):
    def test_true(self):
        assert to_bool(True) is True

    def test_false(self):
        assert to_bool(False) is False


class TestToBoolStrings(unittest.TestCase):
    def test_true_string(self):
        assert to_bool("true") is True

    def test_yes(self):
        assert to_bool("yes") is True

    def test_one_string(self):
        assert to_bool("1") is True

    def test_on(self):
        assert to_bool("on") is True

    def test_enabled(self):
        assert to_bool("enabled") is True

    def test_false_string(self):
        assert to_bool("false") is False

    def test_zero_string(self):
        assert to_bool("0") is False

    def test_no_string(self):
        assert to_bool("no") is False

    def test_empty_string(self):
        assert to_bool("") is False

    def test_case_insensitive(self):
        assert to_bool("TRUE") is True
        assert to_bool("True") is True

    def test_whitespace_handling(self):
        assert to_bool("  true  ") is True


class TestToBoolNoneAndOther(unittest.TestCase):
    def test_none_default_false(self):
        assert to_bool(None) is False

    def test_none_default_true(self):
        assert to_bool(None, default=True) is True

    def test_integer_zero(self):
        assert to_bool(0) is False

    def test_integer_one(self):
        assert to_bool(1) is True

    def test_integer_negative(self):
        assert to_bool(-1) is True

    def test_empty_list(self):
        assert to_bool([]) is False

    def test_nonempty_list(self):
        assert to_bool([1]) is True


# ── remove_surrogates ──────────────────────────────────────────────────


class TestRemoveSurrogates(unittest.TestCase):
    def test_normal_text(self):
        assert remove_surrogates("hello world") == "hello world"

    def test_empty_string_returns_empty(self):
        assert remove_surrogates("") == ""

    def test_unicode_preserved(self):
        assert remove_surrogates("café résumé") == "café résumé"

    def test_surrogate_characters_handled(self):
        # Create text with a surrogate character
        text = "hello \ud800 world"
        result = remove_surrogates(text)
        # Should not raise and should produce valid UTF-8
        result.encode("utf-8")
        assert "hello" in result
        assert "world" in result


if __name__ == "__main__":
    unittest.main()