"""预览生成 voice_library_id 透传修复测试.

Bug: 预览生成接口硬编码 voice_library_id=""，导致用户选择的上传音频
在预览渲染时从未下载和混入，预览视频无声。

Fix: CreatePreviewGenerationTaskRequest 增加 voice_library_id 字段，
预览端点透传 request.voice_library_id。
"""

from __future__ import annotations

import os
import sys
from pathlib import Path

os.environ.setdefault("JWT_SECRET_KEY", "unit-test-secret-key-for-testing")
os.environ.setdefault("DATABASE_URL", "sqlite:///test.db")

sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "apps" / "api"))


class TestPreviewVoiceLibraryIdSchema:
    """CreatePreviewGenerationTaskRequest voice_library_id 字段测试."""

    def test_default_empty_string(self):
        """不传 voice_library_id 时默认为空字符串."""
        from app.schemas.generation_task import CreatePreviewGenerationTaskRequest

        req = CreatePreviewGenerationTaskRequest(
            template_id="tmpl_1",
            asset_ids=["a1"],
        )
        assert req.voice_library_id == ""

    def test_accepts_voice_library_id(self):
        """传入 voice_library_id 正常接收."""
        from app.schemas.generation_task import CreatePreviewGenerationTaskRequest

        req = CreatePreviewGenerationTaskRequest(
            template_id="tmpl_1",
            asset_ids=["a1"],
            voice_library_id="asset_abc123",
        )
        assert req.voice_library_id == "asset_abc123"

    def test_accepts_empty_voice_library_id(self):
        """显式传空字符串也正常."""
        from app.schemas.generation_task import CreatePreviewGenerationTaskRequest

        req = CreatePreviewGenerationTaskRequest(
            template_id="tmpl_1",
            asset_ids=["a1"],
            voice_library_id="",
        )
        assert req.voice_library_id == ""

    def test_all_fields_including_voice_library_id(self):
        """包含 voice_library_id 的完整请求."""
        from app.schemas.generation_task import CreatePreviewGenerationTaskRequest

        req = CreatePreviewGenerationTaskRequest(
            template_id="tmpl_123",
            asset_ids=["a1", "a2"],
            title_ids=["t1"],
            voice_ids=["v1"],
            voice_library_id="voice_asset_456",
            video_title="测试预览",
            duration=30.0,
            video_ratio="9:16",
            bgm_config={"enabled": True, "volume": 0.5},
        )
        assert req.voice_library_id == "voice_asset_456"
        assert req.asset_ids == ["a1", "a2"]
        assert req.bgm_config["enabled"] is True
