"""
VoiceLibraryItem 配音库领域模型单元测试
"""

from packages.domain.voice_library import VoiceLibraryItem


class TestVoiceLibraryItem:
    """VoiceLibraryItem 测试"""

    def test_create_minimal(self):
        item = VoiceLibraryItem(id="v1", user_id="u1", name="我的配音")
        assert item.id == "v1"
        assert item.user_id == "u1"
        assert item.name == "我的配音"

    def test_default_values(self):
        item = VoiceLibraryItem(id="v1", user_id="u1", name="n")
        assert item.text == ""
        assert item.voice_provider == ""
        assert item.voice_id == ""
        assert item.voice_name == ""
        assert item.audio_url == ""
        assert item.duration == 0
        assert item.file_size == 0
        assert item.status == "completed"
        assert item.project_id is None
        assert item.tags == []
        assert item.metadata_ == {}

    def test_with_voice_info(self):
        item = VoiceLibraryItem(
            id="v1",
            user_id="u1",
            name="温柔女声",
            text="大家好",
            voice_provider="cosyvoice",
            voice_id="longxiaochun_v3",
            voice_name="龙小淳",
        )
        assert item.voice_provider == "cosyvoice"
        assert item.voice_id == "longxiaochun_v3"
        assert item.voice_name == "龙小淳"

    def test_with_audio_info(self):
        item = VoiceLibraryItem(
            id="v1",
            user_id="u1",
            name="n",
            audio_url="https://example.com/audio.wav",
            duration=15.5,
            file_size=102400,
        )
        assert item.audio_url == "https://example.com/audio.wav"
        assert item.duration == 15.5
        assert item.file_size == 102400

    def test_with_project_id(self):
        item = VoiceLibraryItem(
            id="v1",
            user_id="u1",
            name="n",
            project_id="proj-123",
        )
        assert item.project_id == "proj-123"

    def test_status_values(self):
        for status in ["pending", "processing", "completed", "failed"]:
            item = VoiceLibraryItem(id="v1", user_id="u1", name="n", status=status)
            assert item.status == status

    def test_with_tags(self):
        item = VoiceLibraryItem(
            id="v1",
            user_id="u1",
            name="n",
            tags=["温柔", "女声", "解说"],
        )
        assert len(item.tags) == 3
        assert "温柔" in item.tags

    def test_with_metadata(self):
        item = VoiceLibraryItem(
            id="v1",
            user_id="u1",
            name="n",
            metadata_={"speed": 1.0, "pitch": 0.5},
        )
        assert item.metadata_["speed"] == 1.0
        assert item.metadata_["pitch"] == 0.5

    def test_has_timestamps(self):
        item = VoiceLibraryItem(id="v1", user_id="u1", name="n")
        assert item.created_at is not None
        assert item.updated_at is not None


# ── VoiceLibraryItem 更多边界测试 ──────────────────────────────────────


class TestVoiceLibraryItemMore:
    """VoiceLibraryItem 补充测试."""

    def test_status_pending(self):
        """pending状态."""
        item = VoiceLibraryItem(id="v1", user_id="u1", name="n", status="pending")
        assert item.status == "pending"

    def test_status_processing(self):
        """processing状态."""
        item = VoiceLibraryItem(id="v1", user_id="u1", name="n", status="processing")
        assert item.status == "processing"

    def test_status_failed(self):
        """failed状态."""
        item = VoiceLibraryItem(id="v1", user_id="u1", name="n", status="failed")
        assert item.status == "failed"

    def test_zero_duration_and_size(self):
        """零时长零大小."""
        item = VoiceLibraryItem(id="v1", user_id="u1", name="n", duration=0, file_size=0)
        assert item.duration == 0
        assert item.file_size == 0

    def test_large_duration_and_size(self):
        """大时长大文件."""
        item = VoiceLibraryItem(id="v1", user_id="u1", name="n", duration=3600.0, file_size=1024 * 1024 * 100)
        assert item.duration == 3600.0
        assert item.file_size == 104857600

    def test_empty_tags_list(self):
        """空标签列表."""
        item = VoiceLibraryItem(id="v1", user_id="u1", name="n", tags=[])
        assert item.tags == []

    def test_many_tags(self):
        """多个标签."""
        tags = ["温柔", "女声", "情感", "治愈", "朗读", "故事"]
        item = VoiceLibraryItem(id="v1", user_id="u1", name="n", tags=tags)
        assert len(item.tags) == 6
        assert "治愈" in item.tags

    def test_empty_metadata(self):
        """空元数据."""
        item = VoiceLibraryItem(id="v1", user_id="u1", name="n")
        assert item.metadata_ == {}

    def test_metadata_with_nested_values(self):
        """嵌套元数据."""
        meta = {
            "settings": {"speed": 1.0, "pitch": 0.5},
            "source": "upload",
            "version": 2,
        }
        item = VoiceLibraryItem(id="v1", user_id="u1", name="n", metadata_=meta)
        assert item.metadata_["settings"]["speed"] == 1.0
        assert item.metadata_["source"] == "upload"

    def test_project_id_none_by_default(self):
        """默认project_id为None."""
        item = VoiceLibraryItem(id="v1", user_id="u1", name="n")
        assert item.project_id is None

    def test_project_id_set(self):
        """设置project_id."""
        item = VoiceLibraryItem(id="v1", user_id="u1", name="n", project_id="proj-abc-123")
        assert item.project_id == "proj-abc-123"
