|
| 1 | +"""Tests for the shared NeurOS response cache.""" |
| 2 | + |
| 3 | +import importlib.util |
| 4 | +import json |
| 5 | +import os |
| 6 | +import pathlib |
| 7 | +import tempfile |
| 8 | +import unittest |
| 9 | +from importlib.machinery import SourceFileLoader |
| 10 | +from unittest.mock import patch |
| 11 | + |
| 12 | + |
| 13 | +LIBRARY_PATH = pathlib.Path(__file__).resolve().parents[1] / "config/includes.chroot/usr/local/bin/neuroslib.py" |
| 14 | + |
| 15 | + |
| 16 | +def load_library(): |
| 17 | + loader = SourceFileLoader("neuroslib_cache_test", str(LIBRARY_PATH)) |
| 18 | + spec = importlib.util.spec_from_loader(loader.name, loader) |
| 19 | + module = importlib.util.module_from_spec(spec) |
| 20 | + loader.exec_module(module) |
| 21 | + return module |
| 22 | + |
| 23 | + |
| 24 | +class FakeResponse: |
| 25 | + def __init__(self, payload): |
| 26 | + self.payload = json.dumps(payload).encode() |
| 27 | + |
| 28 | + def __enter__(self): |
| 29 | + return self |
| 30 | + |
| 31 | + def __exit__(self, *args): |
| 32 | + return False |
| 33 | + |
| 34 | + def read(self): |
| 35 | + return self.payload |
| 36 | + |
| 37 | + |
| 38 | +class ResponseCacheTests(unittest.TestCase): |
| 39 | + def test_same_prompt_and_model_uses_cached_response(self): |
| 40 | + module = load_library() |
| 41 | + with tempfile.TemporaryDirectory() as tmp: |
| 42 | + module.RESPONSE_CACHE_PATH = os.path.join(tmp, "responses.json") |
| 43 | + with patch.object(module, "load_config", return_value={"model": "mistral", "host": "localhost", "port": "11434"}), \ |
| 44 | + patch("urllib.request.urlopen", return_value=FakeResponse({"response": "cached"})) as request: |
| 45 | + self.assertEqual(module.query_llm("same"), "cached") |
| 46 | + self.assertEqual(module.query_llm("same"), "cached") |
| 47 | + self.assertEqual(request.call_count, 1) |
| 48 | + |
| 49 | + def test_model_is_part_of_cache_key(self): |
| 50 | + module = load_library() |
| 51 | + with tempfile.TemporaryDirectory() as tmp: |
| 52 | + module.RESPONSE_CACHE_PATH = os.path.join(tmp, "responses.json") |
| 53 | + configs = iter([ |
| 54 | + {"model": "mistral", "host": "localhost", "port": "11434"}, |
| 55 | + {"model": "llama3", "host": "localhost", "port": "11434"}, |
| 56 | + ]) |
| 57 | + with patch.object(module, "load_config", side_effect=lambda: next(configs)), \ |
| 58 | + patch("urllib.request.urlopen", side_effect=[FakeResponse({"response": "one"}), FakeResponse({"response": "two"})]) as request: |
| 59 | + self.assertEqual(module.query_llm("same"), "one") |
| 60 | + self.assertEqual(module.query_llm("same"), "two") |
| 61 | + self.assertEqual(request.call_count, 2) |
| 62 | + |
| 63 | + def test_cache_write_is_json_and_failure_is_not_cached(self): |
| 64 | + module = load_library() |
| 65 | + with tempfile.TemporaryDirectory() as tmp: |
| 66 | + module.RESPONSE_CACHE_PATH = os.path.join(tmp, "responses.json") |
| 67 | + with patch.object(module, "load_config", return_value={"model": "mistral", "host": "localhost", "port": "11434"}), \ |
| 68 | + patch("urllib.request.urlopen", return_value=FakeResponse({"response": ""})) as request: |
| 69 | + self.assertEqual(module.query_llm("empty"), "") |
| 70 | + self.assertEqual(module.query_llm("empty"), "") |
| 71 | + self.assertEqual(request.call_count, 2) |
| 72 | + self.assertFalse(os.path.exists(module.RESPONSE_CACHE_PATH)) |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == "__main__": |
| 76 | + unittest.main() |
0 commit comments