aman/tests/test_diagnostics.py

77 lines
2.6 KiB
Python

import json
import sys
import unittest
from pathlib import Path
from unittest.mock import patch
ROOT = Path(__file__).resolve().parents[1]
SRC = ROOT / "src"
if str(SRC) not in sys.path:
sys.path.insert(0, str(SRC))
from config import Config
from diagnostics import DiagnosticCheck, DiagnosticReport, run_diagnostics
class _FakeDesktop:
def validate_hotkey(self, _hotkey: str) -> None:
return
class DiagnosticsTests(unittest.TestCase):
def test_run_diagnostics_all_checks_pass(self):
cfg = Config()
with patch("diagnostics.load", return_value=cfg), patch(
"diagnostics.resolve_input_device", return_value=1
), patch("diagnostics.get_desktop_adapter", return_value=_FakeDesktop()), patch(
"diagnostics.ensure_model", return_value=Path("/tmp/model.gguf")
):
report = run_diagnostics("/tmp/config.json")
self.assertTrue(report.ok)
ids = [check.id for check in report.checks]
self.assertEqual(
ids,
[
"config.load",
"audio.input",
"hotkey.parse",
"injection.backend",
"provider.runtime",
"model.cache",
],
)
self.assertTrue(all(check.ok for check in report.checks))
def test_run_diagnostics_marks_config_fail_and_skips_dependent_checks(self):
with patch("diagnostics.load", side_effect=ValueError("broken config")), patch(
"diagnostics.ensure_model", return_value=Path("/tmp/model.gguf")
):
report = run_diagnostics("/tmp/config.json")
self.assertFalse(report.ok)
results = {check.id: check for check in report.checks}
self.assertFalse(results["config.load"].ok)
self.assertFalse(results["audio.input"].ok)
self.assertFalse(results["hotkey.parse"].ok)
self.assertFalse(results["injection.backend"].ok)
self.assertFalse(results["provider.runtime"].ok)
self.assertFalse(results["model.cache"].ok)
def test_report_json_schema(self):
report = DiagnosticReport(
checks=[
DiagnosticCheck(id="config.load", ok=True, message="ok", hint=""),
DiagnosticCheck(id="model.cache", ok=False, message="nope", hint="fix"),
]
)
payload = json.loads(report.to_json())
self.assertFalse(payload["ok"])
self.assertEqual(payload["checks"][0]["id"], "config.load")
self.assertEqual(payload["checks"][1]["hint"], "fix")
if __name__ == "__main__":
unittest.main()