my-homelab-configs/tests/jeannie/test_ai_lab.py

48 lines
2.1 KiB
Python

import importlib.util
from pathlib import Path
from types import SimpleNamespace
import unittest
from unittest import mock
ROOT = Path(__file__).resolve().parents[2]
spec = importlib.util.spec_from_file_location("ai_lab", ROOT / "scripts/ai-lab.py")
lab = importlib.util.module_from_spec(spec)
spec.loader.exec_module(lab)
class AiLabTest(unittest.TestCase):
def test_refuses_to_evict_ollama_model(self):
with mock.patch.object(lab, "read_url", return_value={"models": [{"name": "active"}]}):
with self.assertRaisesRegex(RuntimeError, "No model was stopped"):
lab.require_idle_ollama()
def test_allows_empty_ollama(self):
with mock.patch.object(lab, "read_url", return_value={"models": []}):
lab.require_idle_ollama()
def test_invalid_backend_does_not_start_inference(self):
with mock.patch.object(lab, "model_path") as path:
with self.assertRaisesRegex(RuntimeError, "Choose"):
lab.benchmark("unknown")
path.assert_not_called()
def test_ollama_request_has_bounded_context_and_output(self):
output = {"eval_duration": 1000000000, "eval_count": 12, "response": "ok"}
with mock.patch.object(lab, "model_path", return_value=Path("/demo/model")), mock.patch.object(lab, "require_idle_ollama"), mock.patch.object(lab, "read_url", return_value=output) as request, mock.patch.object(lab, "make_report") as report:
lab.benchmark("ollama")
payload = request.call_args.args[1]
self.assertEqual(payload["options"]["num_ctx"], 4096)
self.assertEqual(payload["options"]["num_predict"], 128)
self.assertEqual(payload["keep_alive"], 0)
self.assertEqual(report.call_args.args[1]["generation_tokens_per_second"], 12)
def test_model_must_be_a_unique_absolute_path(self):
result = SimpleNamespace(stdout="FROM llama3.2:3b\n")
with mock.patch.object(lab, "command", return_value=result):
with self.assertRaisesRegex(RuntimeError, "identify"):
lab.model_path()
if __name__ == "__main__":
unittest.main()