"""Tests para spacy_es_load_model."""

from __future__ import annotations

import os
import sys

import pytest

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..", "..", ".."))

from python.functions.datascience.spacy_es_load_model import (
    _MODEL_CACHE,
    spacy_es_load_model,
)

spacy = pytest.importorskip("spacy", reason="spacy not installed — skip")


def _has_model(model_name: str) -> bool:
    try:
        spacy.load(model_name)
        return True
    except OSError:
        return False


@pytest.mark.skipif(
    not _has_model("es_core_news_md"),
    reason="es_core_news_md not installed",
)
def test_cache_devuelve_la_misma_instancia():
    """cache devuelve la misma instancia"""
    _MODEL_CACHE.clear()
    m1 = spacy_es_load_model("es_core_news_md")
    m2 = spacy_es_load_model("es_core_news_md")
    assert m1 is m2
    _MODEL_CACHE.clear()


def test_oserror_si_el_modelo_no_esta_instalado():
    """OSError si el modelo no esta instalado"""
    _MODEL_CACHE.clear()
    with pytest.raises(OSError):
        spacy_es_load_model("es_nonexistent_model_xyz")
    _MODEL_CACHE.clear()
