"""
Tests for format_links() in advanced_search_system/findings/repository.py

Tests cover:
- Single link formatting
- Multiple links with correct numbering
- Numbering starts at 1
- Title and URL in output
- Empty list
- Special characters in title and URL
"""

from local_deep_research.advanced_search_system.findings.repository import (
    format_links,
)


class TestFormatLinks:
    """Tests for format_links()."""

    def test_single_link(self):
        """Single link is formatted with number 1."""
        links = [{"title": "Example", "url": "https://example.com"}]
        result = format_links(links)
        assert "1." in result
        assert "Example" in result
        assert "https://example.com" in result

    def test_multiple_links(self):
        """Multiple links are formatted with incrementing numbers."""
        links = [
            {"title": "First", "url": "https://first.com"},
            {"title": "Second", "url": "https://second.com"},
            {"title": "Third", "url": "https://third.com"},
        ]
        result = format_links(links)
        assert "1." in result
        assert "2." in result
        assert "3." in result

    def test_numbering_starts_at_1(self):
        """First link is numbered 1, not 0."""
        links = [{"title": "Test", "url": "https://test.com"}]
        result = format_links(links)
        assert result.startswith("1.")
        assert "0." not in result

    def test_title_and_url_on_separate_lines(self):
        """Title and URL appear with URL on indented line."""
        links = [{"title": "My Title", "url": "https://example.com/page"}]
        result = format_links(links)
        lines = result.split("\n")
        assert len(lines) == 2
        assert "My Title" in lines[0]
        assert "URL:" in lines[1]
        assert "https://example.com/page" in lines[1]

    def test_empty_list(self):
        """Empty list returns empty string."""
        result = format_links([])
        assert result == ""

    def test_special_characters_in_title(self):
        """Special characters in title are preserved."""
        links = [{"title": "Q&A <Guide> 'Test'", "url": "https://example.com"}]
        result = format_links(links)
        assert "Q&A <Guide> 'Test'" in result

    def test_special_characters_in_url(self):
        """Special characters in URL are preserved."""
        links = [
            {"title": "Test", "url": "https://example.com/path?q=hello&lang=en"}
        ]
        result = format_links(links)
        assert "https://example.com/path?q=hello&lang=en" in result

    def test_multiline_output_for_multiple_links(self):
        """Each link produces two lines (title + URL)."""
        links = [
            {"title": "A", "url": "https://a.com"},
            {"title": "B", "url": "https://b.com"},
        ]
        result = format_links(links)
        # 2 links × 2 lines each, joined with newline between link blocks
        lines = result.split("\n")
        assert len(lines) == 4
