"""Tests for log string sanitization."""

from local_deep_research.security.log_sanitizer import (
    sanitize_for_log,
    strip_control_chars,
)


class TestLogSanitizer:
    """Unit tests for sanitize_for_log."""

    def test_normal_string_passes_through(self):
        assert sanitize_for_log("hello") == "hello"

    def test_non_printable_chars_stripped(self):
        assert sanitize_for_log("hello\x00world\x07") == "helloworld"

    def test_truncation_respects_max_length(self):
        result = sanitize_for_log("a" * 100, max_length=10)
        assert result == "a" * 7 + "..."
        assert len(result) == 10

    def test_no_truncation_at_exact_max_length(self):
        result = sanitize_for_log("a" * 50, max_length=50)
        assert result == "a" * 50

    def test_empty_string(self):
        assert sanitize_for_log("") == ""

    def test_newlines_stripped(self):
        assert sanitize_for_log("line1\nline2") == "line1line2"

    def test_tabs_stripped(self):
        assert sanitize_for_log("col1\tcol2") == "col1col2"

    def test_unicode_preserved(self):
        assert sanitize_for_log("café") == "café"

    def test_cjk_preserved(self):
        assert sanitize_for_log("你好") == "你好"

    def test_control_chars_stripped_unicode_preserved(self):
        assert sanitize_for_log("café\x00\x07") == "café"


class TestStripControlChars:
    """Unit tests for strip_control_chars."""

    def test_strips_c0_control_chars(self):
        assert strip_control_chars("a\x00b\x1fc") == "abc"

    def test_strips_c1_control_chars(self):
        assert strip_control_chars("a\x7fb\x9fc") == "abc"

    def test_preserves_normal_text(self):
        assert strip_control_chars("hello world") == "hello world"

    def test_preserves_unicode(self):
        assert strip_control_chars("café 你好 émoji") == "café 你好 émoji"

    def test_strips_rlo_override(self):
        assert strip_control_chars("hello\u202eworld") == "helloworld"

    def test_strips_arabic_letter_mark(self):
        assert strip_control_chars("hello\u061cworld") == "helloworld"

    def test_strips_zero_width_space(self):
        assert strip_control_chars("hello\u200bworld") == "helloworld"

    def test_strips_bom(self):
        assert strip_control_chars("\ufeffhello") == "hello"

    def test_strips_word_joiner(self):
        assert strip_control_chars("hello\u2060world") == "helloworld"

    def test_strips_digit_shape_controls(self):
        assert strip_control_chars("hello\u206aworld") == "helloworld"

    def test_strips_mixed_format_chars(self):
        assert (
            strip_control_chars("café\u202e\u200b\ufeff 你好\u2060")
            == "café 你好"
        )

    def test_empty_string(self):
        assert strip_control_chars("") == ""
