"""Tests de estructura para comfyui_build_controlnet_workflow (funcion pura)."""

import os
import sys

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

from ml.comfyui_build_controlnet_workflow import comfyui_build_controlnet_workflow
from _comfyui_wf_assert import assert_api_format, class_types, node_by_ct


def test_estructura_controlnet():
    wf = comfyui_build_controlnet_workflow("ck.safetensors", "ctrl.png", "cn.pth", "POS", "NEG")
    assert_api_format(wf)
    cts = class_types(wf)
    assert "ControlNetLoader" in cts
    assert "ControlNetApply" in cts


def test_control_image_modelo_y_strength():
    wf = comfyui_build_controlnet_workflow(
        "ck.safetensors", "pose.png", "control_openpose.pth", "POS", "NEG", strength=0.65, seed=5
    )
    assert node_by_ct(wf, "ControlNetLoader")["inputs"]["control_net_name"] == "control_openpose.pth"
    # La imagen de control se carga via LoadImage.
    assert node_by_ct(wf, "LoadImage")["inputs"]["image"] == "pose.png"
    apply_in = node_by_ct(wf, "ControlNetApply")["inputs"]
    assert apply_in["strength"] == 0.65
    assert node_by_ct(wf, "KSampler")["inputs"]["seed"] == 5


def test_determinista():
    # Builder puro: misma entrada -> mismo dict (sin red, seed fijo, sin estado).
    args = ("ck.safetensors", "pose.png", "control_openpose.pth", "POS", "NEG")
    a = comfyui_build_controlnet_workflow(*args, strength=0.65, seed=5)
    b = comfyui_build_controlnet_workflow(*args, strength=0.65, seed=5)
    assert a == b
