"""Regression: ``unwrap_setting`` must not reintroduce the api<->security
import cycle.

``unwrap_setting`` briefly lived in ``api.settings_utils``, whose package
``__init__`` eagerly imports the research/search/security chain. Low-level
modules (``security.egress.policy``, ``web_search_engines.search_engine_factory``,
``notifications.manager``, ...) importing it created a circular import that
crashed at import time::

    ImportError: cannot import name 'PolicyDeniedError' from partially
    initialized module 'local_deep_research.security.egress.policy'

It now lives in the dependency-free ``utilities.type_utils`` leaf. These
checks import the cycle-critical modules in FRESH interpreters — pytest's
module cache would mask a real cycle within a single process — and assert
they load cleanly.
"""

import subprocess
import sys

import pytest

# Statements that sat on (or triggered) the api<->security import cycle.
# Each must import on its own in a clean interpreter.
CYCLE_CRITICAL = [
    "from local_deep_research.web_search_engines.search_engine_factory "
    "import PolicyDeniedError",
    "from local_deep_research.security.egress.policy import PolicyDeniedError",
    "import local_deep_research.security",
    "import local_deep_research.web.app_factory",
]


@pytest.mark.parametrize("stmt", CYCLE_CRITICAL)
def test_cycle_critical_module_imports_clean(stmt):
    result = subprocess.run(
        [sys.executable, "-c", stmt],
        capture_output=True,
        text=True,
        timeout=120,
    )
    assert result.returncode == 0, (
        "Import failed (possible reintroduced circular import):\n"
        f"  statement: {stmt}\n{result.stderr}"
    )


def test_unwrap_setting_lives_in_leaf_and_behaves():
    from local_deep_research.utilities.type_utils import unwrap_setting

    assert unwrap_setting({"value": 5}) == 5
    assert unwrap_setting({"value": None}) is None
    # A dict without a "value" key is returned unchanged.
    assert unwrap_setting({"other": 1}) == {"other": 1}
    assert unwrap_setting("raw") == "raw"
    assert unwrap_setting(None) is None


def test_settings_utils_still_reexports_unwrap_setting():
    # Backwards-compat: the function is still importable from its old home.
    from local_deep_research.api.settings_utils import unwrap_setting

    assert unwrap_setting({"value": "x"}) == "x"
