"""Shared pytest fixtures for sluice tests."""

from __future__ import annotations

from typing import TYPE_CHECKING

import pytest
from celery import Celery

if TYPE_CHECKING:
    from collections.abc import Iterator

import sluice._init as _init_module


@pytest.fixture(autouse=True)
def _reset_sdk_state() -> Iterator[None]:
    """Reset SDK state before and after each test to ensure isolation."""
    _init_module._reset()
    yield
    _init_module._reset()
    _disconnect_task_signals()


@pytest.fixture()
def celery_app() -> Iterator[Celery]:
    """Create a minimal Celery app for testing configuration."""
    app = Celery("test")
    app.config_from_object(
        {
            "broker_url": "memory://",
            "result_backend": "cache+memory://",
        }
    )
    yield app


def _disconnect_task_signals() -> None:
    """Disconnect all task signal handlers to prevent leakage between tests."""
    from celery.signals import (
        task_failure,
        task_postrun,
        task_prerun,
        task_received,
        task_rejected,
        task_retry,
        task_revoked,
        task_sent,
    )

    for signal in (
        task_sent,
        task_received,
        task_prerun,
        task_postrun,
        task_failure,
        task_retry,
        task_revoked,
        task_rejected,
    ):
        signal.receivers.clear()
