"""Tests de estructura para comfyui_build_txt2img_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_txt2img_workflow import comfyui_build_txt2img_workflow
from _comfyui_wf_assert import assert_api_format, class_types, node_by_ct


def test_estructura_y_class_types():
    wf = comfyui_build_txt2img_workflow("ck.safetensors", "POS", "NEG")
    assert_api_format(wf)
    assert class_types(wf) == {
        "CheckpointLoaderSimple",
        "CLIPTextEncode",
        "EmptyLatentImage",
        "KSampler",
        "VAEDecode",
        "SaveImage",
    }


def test_params_se_reflejan_en_los_nodos():
    wf = comfyui_build_txt2img_workflow(
        "ck.safetensors", "POS", "NEG", steps=8, cfg=6.0, width=640, height=480, seed=123
    )
    ks = node_by_ct(wf, "KSampler")["inputs"]
    assert ks["seed"] == 123
    assert ks["steps"] == 8
    assert ks["cfg"] == 6.0
    lat = node_by_ct(wf, "EmptyLatentImage")["inputs"]
    assert lat["width"] == 640 and lat["height"] == 480
    assert node_by_ct(wf, "CheckpointLoaderSimple")["inputs"]["ckpt_name"] == "ck.safetensors"
    textos = sorted(n["inputs"]["text"] for n in wf.values() if n["class_type"] == "CLIPTextEncode")
    assert textos == ["NEG", "POS"]


def test_filename_prefix_en_saveimage():
    wf = comfyui_build_txt2img_workflow("ck.safetensors", "POS", filename_prefix="demo_run")
    assert node_by_ct(wf, "SaveImage")["inputs"]["filename_prefix"] == "demo_run"


def test_determinista():
    # Builder puro: misma entrada -> mismo dict (sin red, seed fijo, sin estado).
    args = ("ck.safetensors", "POS", "NEG")
    a = comfyui_build_txt2img_workflow(*args, seed=123)
    b = comfyui_build_txt2img_workflow(*args, seed=123)
    assert a == b
