Automation foundation model for tiny devices: 2-bit, 8-29 MB, tool calls, ASR, structured extraction and embeddings on phones, wearables, smart homes, robots, cars and microcontrollers.
fix(tests): look for the engine where the runtime caches it (#109)
kevin53df049c4a1a
diff --git a/tests/conftest.py b/tests/conftest.pyindex ad2bb2e..26b1d8e 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -4,15 +4,34 @@ import pickleimport pytest-def _engine_available():+def _engine_available(generation=2):+ """Whether ``needle._library_path`` would find an engine without downloading.++ Every location this looks in has to be one that function looks in, or the+ gate skips tests an installed engine could have run.+ """try:import needlefrom needle.agent import fetch++ override = os.environ.get(f"NEEDLE{generation}_LIB_PATH")+ if generation == 2 and not override:+ override = os.environ.get("NEEDLE_LIB_PATH")+ if override:+ return os.path.exists(override)+here = os.path.dirname(needle.__file__)name = fetch._lib_name()- cache = os.path.join(os.path.expanduser("~"), ".cache",- "cactus-needle", fetch.ENGINE_VERSION, name)- return os.path.exists(os.path.join(here, name)) or os.path.exists(cache)+ stem, suffix = os.path.splitext(name)+ local_names = [f"{stem}{generation}{suffix}"]+ if generation == 2:+ local_names.append(name)+ if any(os.path.exists(os.path.join(here, local)) for local in local_names):+ return True++ cache = os.path.join(os.path.expanduser("~"), ".cache", "cactus-needle",+ f"v{generation}", fetch.engine_version(generation), name)+ return os.path.exists(cache)except Exception:return Falsediff --git a/tests/test_fetch.py b/tests/test_fetch.pyindex 9c579f7..520e071 100644--- a/tests/test_fetch.py+++ b/tests/test_fetch.py@@ -62,3 +62,45 @@ def test_fetch_library_creates_destination(tmp_path, monkeypatch):assert (tmp_path / "new" / "libneedle.so").read_bytes() == b"engine"assert out == str(tmp_path / "new" / "libneedle.so")+++def test_engine_gate_finds_the_cache_the_runtime_loads_from(tmp_path, monkeypatch):+ import needle+ from needle.agent import fetch+ from conftest import _engine_available++ package = tmp_path / "pkg"+ package.mkdir()+ monkeypatch.setattr(needle, "__file__", str(package / "__init__.py"))+ monkeypatch.setenv("HOME", str(tmp_path))+ monkeypatch.setenv("USERPROFILE", str(tmp_path))+ monkeypatch.delenv("NEEDLE_LIB_PATH", raising=False)+ monkeypatch.delenv("NEEDLE2_LIB_PATH", raising=False)++ assert not _engine_available()++ cache = tmp_path / ".cache" / "cactus-needle" / "v2" / fetch.engine_version(2)+ cache.mkdir(parents=True)+ (cache / fetch._lib_name()).write_bytes(b"")++ assert _engine_available()+++def test_engine_gate_honours_the_library_override(tmp_path, monkeypatch):+ import needle+ from needle.agent import fetch+ from conftest import _engine_available++ package = tmp_path / "pkg"+ package.mkdir()+ monkeypatch.setattr(needle, "__file__", str(package / "__init__.py"))+ monkeypatch.setenv("HOME", str(tmp_path))+ monkeypatch.setenv("USERPROFILE", str(tmp_path))++ engine = tmp_path / fetch._lib_name()+ engine.write_bytes(b"")+ monkeypatch.setenv("NEEDLE_LIB_PATH", str(engine))+ assert _engine_available()++ monkeypatch.setenv("NEEDLE_LIB_PATH", str(tmp_path / "gone"))+ assert not _engine_available()