"""需求池同步内部子阶段的异常隔离测试。"""
from __future__ import annotations

from unittest.mock import MagicMock, patch

from supply_infra.scheduler.jobs.demand_pool.sync import _classify_words


@patch(
    "supply_infra.scheduler.jobs.demand_pool.sync.classify_demand_words"
)
@patch(
    "supply_infra.scheduler.jobs.demand_pool.sync.DemandBelongCategoryRepository"
)
@patch(
    "supply_infra.scheduler.jobs.demand_pool.sync.MultiDemandPoolDiRepository"
)
@patch("supply_infra.scheduler.jobs.demand_pool.sync.get_session")
def test_classification_batch_failure_continues_remaining_batches(
    mock_get_session,
    mock_pool_repo_cls,
    mock_belong_repo_cls,
    mock_classify,
) -> None:
    mock_get_session.return_value.__enter__.return_value = MagicMock()
    mock_pool_repo_cls.return_value.list_demand_names_by_biz_dt.return_value = [
        f"需求{i:03d}" for i in range(120)
    ]
    mock_belong_repo_cls.return_value.get_existing_names.return_value = set()
    mock_classify.side_effect = [RuntimeError("first batch failed"), None, None]

    result = _classify_words("20260721")

    assert mock_classify.call_count == 3
    assert result["success"] is False
    assert result["batches"] == 3
    assert result["failed_batches"] == [
        {"batch": 1, "size": 50, "error": "first batch failed"}
    ]
