# -*- coding: utf-8 -*-
"""在线模式下的端到端任务测试，验证所有任务在模拟 API 下能顺利执行。"""
import logging
import pytest

from .task_test_utils import (
    TASK_SPECS,
    FakeAPIClient,
    create_test_config,
    get_db_operations,
)


@pytest.mark.parametrize("spec", TASK_SPECS, ids=lambda spec: spec.code)
def test_task_online_mode(spec, tmp_path):
    """针对每个 TaskSpec 验证：模拟 API 数据下依旧能完整跑完 ETL，并正确统计。"""
    archive_dir = tmp_path / "archive"
    temp_dir = tmp_path / "tmp"
    config = create_test_config("ONLINE", archive_dir, temp_dir)
    fake_api = FakeAPIClient({spec.endpoint: spec.sample_records})
    logger = logging.getLogger(f"test_online_{spec.code.lower()}")

    with get_db_operations() as db_ops:
        task = spec.task_cls(config, db_ops, fake_api, logger)
        result = task.execute()

        assert result["status"] == "SUCCESS"
        assert result["counts"]["fetched"] == len(spec.sample_records)
        assert result["counts"]["inserted"] == len(spec.sample_records)
        if hasattr(db_ops, "commits"):
            assert db_ops.commits == 1
            assert db_ops.rollbacks == 0
