from __future__ import annotations

import pytest

from pymulator.core import can as can_module


def test_missing_python_can_modules_requires_gs_usb_on_macos(monkeypatch: pytest.MonkeyPatch) -> None:
    monkeypatch.setattr(can_module, "python_can", object())
    monkeypatch.setattr(
        can_module.importlib.util,
        "find_spec",
        lambda name: None if name == "gs_usb" else object(),
    )

    missing = can_module.missing_python_can_modules("can0", platform_name="darwin")

    assert "gs_usb" in missing


def test_missing_python_can_modules_does_not_require_gs_usb_for_socketcan_linux(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    monkeypatch.setattr(can_module, "python_can", object())
    monkeypatch.setattr(can_module.importlib.util, "find_spec", lambda _name: None)

    missing = can_module.missing_python_can_modules("can0", platform_name="linux")

    assert "gs_usb" not in missing


def test_missing_python_can_modules_requires_gs_usb_when_explicit_backend_selected(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    monkeypatch.setattr(can_module, "python_can", object())
    monkeypatch.setattr(can_module.importlib.util, "find_spec", lambda _name: None)

    missing = can_module.missing_python_can_modules("gs_usb:0", platform_name="linux")

    assert "gs_usb" in missing


def test_missing_python_can_modules_reports_python_can(monkeypatch: pytest.MonkeyPatch) -> None:
    monkeypatch.setattr(can_module, "python_can", None)
    monkeypatch.setattr(can_module.importlib.util, "find_spec", lambda _name: object())

    missing = can_module.missing_python_can_modules("socketcan:can0", platform_name="linux")

    assert "python-can" in missing


def test_missing_python_can_modules_requires_cancorder_utils_for_toucan(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    monkeypatch.setattr(can_module, "python_can", object())
    monkeypatch.setattr(
        can_module.importlib.util,
        "find_spec",
        lambda name: None if name == "cancorder_utils" else object(),
    )

    missing = can_module.missing_python_can_modules("toucan:0", platform_name="darwin")

    assert "cancorder-utils" in missing


def test_missing_python_can_modules_allows_toucan_without_python_can(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    monkeypatch.setattr(can_module, "python_can", None)
    monkeypatch.setattr(can_module.importlib.util, "find_spec", lambda _name: object())

    missing = can_module.missing_python_can_modules("toucan:0", platform_name="darwin")

    assert "python-can" not in missing
