/ tests / tools / test_memory_tool_import_fallback.py
test_memory_tool_import_fallback.py
 1  """Regression tests for memory-tool import fallbacks."""
 2  
 3  import builtins
 4  import importlib
 5  import sys
 6  
 7  from tools.registry import registry
 8  
 9  
10  def test_memory_tool_imports_without_fcntl(monkeypatch, tmp_path):
11      original_import = builtins.__import__
12  
13      def fake_import(name, globals=None, locals=None, fromlist=(), level=0):
14          if name == "fcntl":
15              raise ImportError("simulated missing fcntl")
16          return original_import(name, globals, locals, fromlist, level)
17  
18      registry.deregister("memory")
19      monkeypatch.delitem(sys.modules, "tools.memory_tool", raising=False)
20      monkeypatch.setattr(builtins, "__import__", fake_import)
21  
22      memory_tool = importlib.import_module("tools.memory_tool")
23      monkeypatch.setattr(memory_tool, "get_memory_dir", lambda: tmp_path)
24  
25      store = memory_tool.MemoryStore(memory_char_limit=200, user_char_limit=200)
26      store.load_from_disk()
27      result = store.add("memory", "fact learned during import fallback test")
28  
29      assert memory_tool.fcntl is None
30      assert registry.get_entry("memory") is not None
31      assert result["success"] is True