"""Tests para gpu_info."""

import sys
import unittest
from unittest.mock import MagicMock, patch

sys.path.insert(0, "python/functions")

from ml.gpu_info import gpu_info


class TestGpuInfo(unittest.TestCase):

    def test_sin_nvidia_smi_devuelve_lista_vacia(self):
        """sin nvidia-smi devuelve lista vacia"""
        with patch("subprocess.run", side_effect=FileNotFoundError()):
            result = gpu_info()
        self.assertEqual(result, [])

    def test_formato_CSV_correcto_devuelve_lista_con_un_dict_por_GPU(self):
        """formato CSV correcto devuelve lista con un dict por GPU"""
        csv_output = " 0, NVIDIA RTX 4090, 24564, 22000, 535.183.01, 8.9\n"
        mock_result = MagicMock()
        mock_result.returncode = 0
        mock_result.stdout = csv_output
        with patch("subprocess.run", return_value=mock_result):
            result = gpu_info()
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0]["index"], 0)
        self.assertEqual(result[0]["name"], "NVIDIA RTX 4090")
        self.assertEqual(result[0]["vram_total_mb"], 24564)
        self.assertEqual(result[0]["vram_free_mb"], 22000)
        self.assertEqual(result[0]["driver_version"], "535.183.01")
        self.assertEqual(result[0]["cuda_version"], "8.9")

    def test_fila_malformada_en_CSV_se_ignora_sin_excepcion(self):
        """fila malformada en CSV se ignora sin excepcion"""
        csv_output = " 0, RTX 4090, NONNUMERIC, 22000, 535.183.01, 8.9\n"
        mock_result = MagicMock()
        mock_result.returncode = 0
        mock_result.stdout = csv_output
        with patch("subprocess.run", return_value=mock_result):
            result = gpu_info()
        self.assertEqual(result, [])


if __name__ == "__main__":
    unittest.main()
