"""Tests for accessibility diagnostics."""

import sys
from unittest.mock import MagicMock, patch

from llmhosts.accessibility.diagnostics import DiagnosticResult, DiagnosticRunner, Severity


class TestDiagnosticRunner:
    def test_run_all_returns_list(self):
        runner = DiagnosticRunner()
        results = runner.run_all()
        assert isinstance(results, list)
        assert len(results) >= 5

    def test_python_version_current(self):
        runner = DiagnosticRunner()
        result = runner._check_python_version()
        assert result.severity == Severity.OK  # we're running on 3.10+
        assert "Python" in result.message

    def test_python_version_old(self):
        runner = DiagnosticRunner()
        with patch.object(sys, "version_info", (3, 9, 0)):
            result = runner._check_python_version()
        assert result.severity == Severity.ERROR
        assert "too old" in result.message.lower()

    def test_disk_space_low(self):
        runner = DiagnosticRunner()
        with patch("shutil.disk_usage", return_value=(100 * 1024**3, 98 * 1024**3, 1 * 1024**3)):
            result = runner._check_disk_space()
        assert result.severity == Severity.ERROR
        assert "1.0 GB" in result.message

    def test_disk_space_ample(self):
        runner = DiagnosticRunner()
        with patch("shutil.disk_usage", return_value=(500 * 1024**3, 300 * 1024**3, 200 * 1024**3)):
            result = runner._check_disk_space()
        assert result.severity == Severity.OK

    def test_gpu_check_nvidia_present(self):
        runner = DiagnosticRunner()
        mock = MagicMock()
        mock.returncode = 0
        mock.stdout = "NVIDIA RTX 4090\n"
        with patch("subprocess.run", return_value=mock):
            result = runner._check_gpu_available()
        assert result.severity == Severity.OK
        assert "RTX 4090" in result.message

    def test_gpu_check_no_gpu(self):
        runner = DiagnosticRunner()
        with patch("subprocess.run", side_effect=FileNotFoundError):
            result = runner._check_gpu_available()
        assert result.severity == Severity.INFO

    def test_ollama_no_models(self):
        runner = DiagnosticRunner()
        mock = MagicMock()
        mock.returncode = 0
        mock.stdout = "NAME    ID    SIZE\n"
        with patch("subprocess.run", return_value=mock):
            result = runner._check_ollama_running()
        assert result.severity == Severity.WARNING
        assert "no models" in result.message.lower()

    def test_ollama_not_installed(self):
        runner = DiagnosticRunner()
        with patch("subprocess.run", side_effect=FileNotFoundError):
            result = runner._check_ollama_running()
        assert result.severity == Severity.ERROR

    def test_diagnostic_result_is_ok(self):
        r = DiagnosticResult(name="test", severity=Severity.OK, message="ok")
        assert r.is_ok()
        r2 = DiagnosticResult(name="test", severity=Severity.ERROR, message="bad")
        assert not r2.is_ok()
