"""Unit tests for ``AutoModelConfigLoader`` internals.

Exercises remote-code model-type conversion and the ModelScope config-only
allowlist using monkeypatched ``transformers``/``modelscope`` — fully offline,
no Hub access. The live-Hub integration counterpart lives in
``test_auto_model_config.py``'s network-marked ``AutoModelAndConfigRemoteTestCase``.
"""

from __future__ import annotations

from types import SimpleNamespace

from tensor_cast.transformers import utils
from tensor_cast.transformers.utils import AutoModelConfigLoader


def test_load_config_remote_code_converts_real_model_type(monkeypatch) -> None:
    calls: list[dict] = []

    class FakeConfig:
        model_type = "kimi_k2"

        def to_dict(self):
            return {"model_type": "deepseek_v3"}

    class FakeNativeConfig:
        model_type = "deepseek_v3"

    class FakeAutoConfig:
        @staticmethod
        def get_config_dict(model_id):
            assert model_id == "moonshotai/Kimi-K2-Base"
            return (
                {
                    "model_type": "kimi_k2",
                    "auto_map": {"AutoConfig": "configuration_kimi.KimiConfig"},
                },
                {},
            )

        @staticmethod
        def from_pretrained(model_id, **kwargs):
            assert model_id == "moonshotai/Kimi-K2-Base"
            if not kwargs.get("trust_remote_code"):
                raise ValueError("requires trust_remote_code")
            calls.append(kwargs)
            return FakeConfig()

        @staticmethod
        def for_model(model_type):
            assert model_type == "deepseek_v3"
            return SimpleNamespace(from_dict=lambda _: FakeNativeConfig())

    monkeypatch.setattr("transformers.AutoConfig", FakeAutoConfig)

    loader = AutoModelConfigLoader()
    config = loader.load_config("moonshotai/Kimi-K2-Base")

    assert calls == [{"trust_remote_code": True}]
    assert isinstance(config, FakeNativeConfig)
    assert loader.is_transformers_natively_supported is True


def test_load_config_probe_passes_trust_remote_code_false(monkeypatch) -> None:
    """The native-support probe must pass trust_remote_code=False explicitly.

    With ``False`` (rather than ``None``), transformers never enters the
    interactive y/N prompt branch when the model id resolves to a config
    that does not require remote code.
    """

    seen: list[dict] = []

    class FakeNativeConfig:
        model_type = "llama"

        def to_dict(self):
            return {"model_type": "llama"}

    class FakeAutoConfig:
        @staticmethod
        def from_pretrained(model_id, **kwargs):
            seen.append(kwargs)
            return FakeNativeConfig()

    monkeypatch.setattr("transformers.AutoConfig", FakeAutoConfig)

    loader = AutoModelConfigLoader()
    config = loader.load_config("meta-llama/Llama-3-8B")

    assert seen == [{"trust_remote_code": False}]
    assert isinstance(config, FakeNativeConfig)
    assert loader.is_transformers_natively_supported is True


def test_modelscope_snapshot_config_only_uses_allowlist(monkeypatch) -> None:
    call: dict = {}

    def fake_snapshot_download(model_id, ignore_patterns=None):
        call["model_id"] = model_id
        call["kwargs"] = {"ignore_patterns": ignore_patterns}
        return "/tmp/snapshot"

    monkeypatch.setattr("modelscope.snapshot_download", fake_snapshot_download)

    result = utils._modelscope_snapshot_config_only("ZhipuAI/GLM-4.7")

    assert result == "/tmp/snapshot"
    assert call["model_id"] == "ZhipuAI/GLM-4.7"
    assert call["kwargs"]["ignore_patterns"] == utils._MODELSCOPE_WEIGHT_IGNORE_PATTERNS