67 lines
3.0 KiB
Python
67 lines
3.0 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()
|
|
|
|
def test_permission_error_explains_writable_workspace(self):
|
|
directory = mock.Mock()
|
|
directory.mkdir.side_effect = PermissionError("denied")
|
|
with mock.patch.object(lab, "ROOT", directory):
|
|
with self.assertRaisesRegex(RuntimeError, "writable JEANNIE_AI_LAB_DIR"):
|
|
lab.workspace()
|
|
|
|
def test_colibri_requires_verified_model_before_inference(self):
|
|
missing = mock.Mock()
|
|
missing.is_file.return_value = False
|
|
with mock.patch.object(lab, "QWEN_FILE", missing), mock.patch.object(lab, "command") as call:
|
|
with self.assertRaisesRegex(RuntimeError, "colibri-prepare"):
|
|
lab.benchmark("colibri")
|
|
call.assert_not_called()
|
|
|
|
def test_downloaded_model_hash_is_pinned(self):
|
|
self.assertEqual(len(lab.QWEN_SHA256), 64)
|
|
self.assertIn("Qwen/Qwen3-0.6B-GGUF", lab.QWEN_URL)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|