"""素材入库任务 UseCase 单元测试."""

from __future__ import annotations

from unittest.mock import MagicMock

import pytest

from packages.application.ingest_jobs import (
    SubmitIngestJobCommand,
    SubmitIngestJobUseCase,
)
from packages.domain import IngestJob


@pytest.fixture
def mock_repo():
    return MagicMock()


class TestSubmitIngestJobUseCase:
    """SubmitIngestJobUseCase 测试"""

    def test_submit_job_success(self, mock_repo):
        """正常提交入库任务"""
        mock_repo.create.side_effect = lambda j: j
        use_case = SubmitIngestJobUseCase(mock_repo)

        command = SubmitIngestJobCommand(
            project_id="proj_001",
            library_id="lib_001",
            storage_key="videos/test.mp4",
            file_hash="abc123def",
        )
        result = use_case.execute(command)

        assert isinstance(result, IngestJob)
        assert result.project_id == "proj_001"
        assert result.library_id == "lib_001"
        assert result.storage_key == "videos/test.mp4"
        assert result.file_hash == "abc123def"
        mock_repo.create.assert_called_once()

    def test_submit_job_without_hash(self, mock_repo):
        """不传 file_hash 时默认为空"""
        mock_repo.create.side_effect = lambda j: j
        use_case = SubmitIngestJobUseCase(mock_repo)

        command = SubmitIngestJobCommand(
            project_id="proj_001",
            library_id="lib_001",
            storage_key="images/test.png",
        )
        result = use_case.execute(command)

        assert result.file_hash == ""
        mock_repo.create.assert_called_once()

    def test_submit_job_returns_repo_result(self, mock_repo):
        """返回 repository.create 的结果"""
        expected_job = MagicMock(spec=IngestJob)
        mock_repo.create.return_value = expected_job

        use_case = SubmitIngestJobUseCase(mock_repo)
        command = SubmitIngestJobCommand(
            project_id="proj_001",
            library_id="lib_001",
            storage_key="test.mp4",
        )
        result = use_case.execute(command)

        assert result is expected_job
