"""Tests for services/translation_service.py."""

import pytest
from services.translation_service import TranslationService, TranslationProvider


class DummyProvider(TranslationProvider):
    """Provider that records every unique text it is asked to translate."""

    def __init__(self):
        self.calls: list[str] = []

    def translate(self, text: str, target_language: str, source_language: str = "auto") -> str:
        self.calls.append(text)
        return f"[{text}]"


class TestTranslateBatchDeduplication:
    def test_duplicate_texts_translated_once(self):
        provider = DummyProvider()
        service = TranslationService(provider=provider)

        texts = ["hello", "world", "hello", "planet", "world", ""]
        results = service.translate_batch(texts, "fr", "en")

        assert results == ["[hello]", "[world]", "[hello]", "[planet]", "[world]", ""]
        # Only 3 unique non-empty texts should be sent to the provider
        assert sorted(provider.calls) == sorted(["hello", "world", "planet"])

    def test_empty_and_whitespace_preserved(self):
        provider = DummyProvider()
        service = TranslationService(provider=provider)

        texts = ["", "   ", "ok"]
        results = service.translate_batch(texts, "fr", "en")

        assert results[0] == ""
        assert results[1] == "   "
        assert results[2] == "[ok]"
        assert provider.calls == ["ok"]
