"""Tests offline para comfyui_score_clip_alignment (impura: similitud CLIP via subproceso torch).

Sin GPU, sin torch, sin servidor: ejercita SOLO los guards previos al subproceso (imagen
ausente, prompt vacio, python del venv ComfyUI ausente).
"""

import os
import sys

import pytest

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

from ml.comfyui_score_clip_alignment import comfyui_score_clip_alignment

PIL = pytest.importorskip("PIL")
from PIL import Image  # noqa: E402


def _png(path):
    Image.new("RGB", (8, 8), (0, 0, 0)).save(path)
    return str(path)


def test_error_imagen_inexistente(tmp_path):
    res = comfyui_score_clip_alignment(str(tmp_path / "no.png"), "a cat")
    assert res["ok"] is False and res["score_0_1"] == 0.0
    assert "imagen no encontrada" in res["error"]


def test_error_prompt_vacio(tmp_path):
    res = comfyui_score_clip_alignment(_png(tmp_path / "i.png"), "   ")
    assert res["ok"] is False and "prompt vacio" in res["error"]


def test_error_venv_python_inexistente(tmp_path):
    res = comfyui_score_clip_alignment(_png(tmp_path / "i.png"), "a cat",
                                       venv_python=str(tmp_path / "no_python"))
    assert res["ok"] is False and "python del venv ComfyUI no encontrado" in res["error"]


def test_nunca_lanza_y_es_determinista(tmp_path):
    img = _png(tmp_path / "i.png")
    a = comfyui_score_clip_alignment(img, "a cat", venv_python=str(tmp_path / "x"))
    b = comfyui_score_clip_alignment(img, "a cat", venv_python=str(tmp_path / "x"))
    assert a == b and a["ok"] is False
