import importlib.util from pathlib import Path import unittest from unittest import mock ROOT = Path(__file__).resolve().parents[2] spec = importlib.util.spec_from_file_location("ai_lab_suite", ROOT / "scripts/ai-lab-suite.py") suite = importlib.util.module_from_spec(spec) spec.loader.exec_module(suite) class AiLabSuiteTest(unittest.TestCase): def test_grades_only_exact_structured_results(self): self.assertEqual(suite.grade(suite.CASES[0], "102"), "pass") self.assertEqual(suite.grade(suite.CASES[0], "102 or 103"), "fail") self.assertEqual(suite.grade(suite.CASES[1], '{"status":"ok"}'), "pass") self.assertEqual(suite.grade(suite.CASES[1], '{"status":"ok","other":1}'), "fail") self.assertEqual(suite.grade(suite.CASES[4], "an answer"), "manual_review") self.assertEqual(suite.grade(suite.CASES[0], None), "unavailable") def test_extracts_captured_llama_answer(self): log = "> What is 17 multiplied by 6? Reply with just the number.\r\n102\r\n[ Prompt: 250.0 t/s | Generation: 50.0 t/s ]\r\n" self.assertEqual(suite.llama_answer(log, suite.CASES[0]["prompt"]), "102") self.assertIsNone(suite.llama_answer("no answer", "prompt")) def test_extracts_colibri_answer_after_banner(self): self.assertEqual(suite.colibri_answer("colibri\n ───────────────────\n102\n"), "102") self.assertIsNone(suite.colibri_answer("unknown format")) def test_summary_does_not_count_failed_or_manual_as_passed(self): rows = [{"backend": "ollama", "case": "arithmetic", "elapsed_seconds": 1.0, "check": "pass"}, {"backend": "ollama", "case": "kubernetes", "elapsed_seconds": 3.0, "check": "manual_review"}, {"backend": "ollama", "case": "arithmetic", "error": "failed"}] result = suite.summarize(rows, ["ollama"])["ollama"] self.assertEqual(result["median_end_to_end_seconds"], 2.0) self.assertEqual(result["simple_checks_evaluated"], 1) self.assertEqual(result["failed_runs"], 1) self.assertEqual(result["by_case"]["arithmetic"]["median_end_to_end_seconds"], 1.0) def test_invalid_repeat_count_does_not_touch_host(self): with mock.patch.object(suite.lab, "workspace") as workspace: with self.assertRaisesRegex(RuntimeError, "between"): suite.suite(0) workspace.assert_not_called() def test_memory_guard_stops_inference(self): with mock.patch.object(suite, "memory_available_kib", return_value=1000), mock.patch.object(suite.lab, "terminal_command") as command: with self.assertRaisesRegex(RuntimeError, "RAM"): suite.run_case("llama-cpu", suite.CASES[0]) command.assert_not_called() if __name__ == "__main__": unittest.main()