"""Tests de estructura para comfyui_build_image_to_3d_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_image_to_3d_workflow import comfyui_build_image_to_3d_workflow
from _comfyui_wf_assert import assert_api_format, class_types, node_by_ct


def test_cadena_hunyuan3d_nativa():
    wf = comfyui_build_image_to_3d_workflow("obj.png")
    assert_api_format(wf)
    # Los 9 nodos nativos de Hunyuan3D-2 (sin custom node).
    esperado = {
        "LoadImage",
        "ImageOnlyCheckpointLoader",
        "CLIPVisionEncode",
        "Hunyuan3Dv2Conditioning",
        "EmptyLatentHunyuan3Dv2",
        "KSampler",
        "VAEDecodeHunyuan3D",
        "VoxelToMeshBasic",
        "SaveGLB",
    }
    assert class_types(wf) == esperado


def test_imagen_checkpoint_y_salida_glb():
    wf = comfyui_build_image_to_3d_workflow(
        "robot.png", ckpt_name="3D_hunyuan3d-dit-v2-mini.safetensors", seed=42
    )
    assert node_by_ct(wf, "LoadImage")["inputs"]["image"] == "robot.png"
    assert (
        node_by_ct(wf, "ImageOnlyCheckpointLoader")["inputs"]["ckpt_name"]
        == "3D_hunyuan3d-dit-v2-mini.safetensors"
    )
    assert node_by_ct(wf, "KSampler")["inputs"]["seed"] == 42
    # SaveGLB es el nodo de salida: produce la malla .glb.
    assert "SaveGLB" in class_types(wf)


def test_default_conserva_voxeltomeshbasic():
    # Retro-compatibilidad: sin watertight el nodo "8" es VoxelToMeshBasic.
    wf = comfyui_build_image_to_3d_workflow("obj.png")
    assert wf["8"]["class_type"] == "VoxelToMeshBasic"
    assert "algorithm" not in wf["8"]["inputs"]


def test_watertight_usa_voxeltomesh_surface_net():
    wf = comfyui_build_image_to_3d_workflow("obj.png", watertight=True)
    assert wf["8"]["class_type"] == "VoxelToMesh"
    assert wf["8"]["inputs"]["algorithm"] == "surface net"
    assert wf["8"]["inputs"]["voxel"] == ["7", 0]
    # El resto de la cadena no cambia: SaveGLB sigue colgando del nodo "8".
    assert wf["9"]["inputs"]["mesh"] == ["8", 0]


def test_determinista():
    # Builder puro: misma entrada -> mismo dict (sin red, seed fijo, sin estado).
    a = comfyui_build_image_to_3d_workflow("obj.png", seed=42)
    b = comfyui_build_image_to_3d_workflow("obj.png", seed=42)
    assert a == b
