"""Tests de localizacion de output para comfyui_fetch_output_audio.

Solo cubren la logica pura de busqueda (_is_audio_item / _find_audio_output): no
tocan red ni disco. La descarga real via HTTP se prueba en el flujo e2e con el
servidor ComfyUI vivo.
"""
import os
import sys

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

from comfyui_fetch_output_audio import _find_audio_output, _is_audio_item


def test_is_audio_item_por_extension():
    assert _is_audio_item({"filename": "comfy_audio_00001_.flac"})
    assert _is_audio_item({"filename": "x.mp3"})
    assert _is_audio_item({"filename": "x.WAV"})
    assert not _is_audio_item({"filename": "x.png"})
    assert not _is_audio_item({"filename": ""})


def test_find_saveaudio_flac_bajo_audio():
    outputs = {
        "9": {"audio": [{"filename": "comfy_audio_00001_.flac",
                          "subfolder": "audio", "type": "output"}]}
    }
    got = _find_audio_output(outputs)
    assert got == {"filename": "comfy_audio_00001_.flac",
                   "subfolder": "audio", "type": "output"}


def test_find_saveaudiomp3_bajo_audio():
    outputs = {"12": {"audio": [{"filename": "track.mp3", "subfolder": "", "type": "output"}]}}
    assert _find_audio_output(outputs)["filename"] == "track.mp3"


def test_find_prioriza_clave_audio():
    # Un nodo deja un png bajo "images" y otro un flac bajo "audio": gana el audio.
    outputs = {
        "9": {"images": [{"filename": "preview.png", "subfolder": "", "type": "output"}]},
        "10": {"audio": [{"filename": "out.flac", "subfolder": "", "type": "output"}]},
    }
    assert _find_audio_output(outputs)["filename"] == "out.flac"


def test_find_sin_audio_devuelve_none():
    outputs = {"9": {"images": [{"filename": "preview.png", "subfolder": "", "type": "output"}]}}
    assert _find_audio_output(outputs) is None
    assert _find_audio_output({}) is None
