64 lines
1.9 KiB
Python
64 lines
1.9 KiB
Python
from __future__ import annotations
|
|
|
|
import importlib.util
|
|
import sys
|
|
from pathlib import Path
|
|
from types import ModuleType
|
|
from typing import Any, cast
|
|
|
|
import pytest
|
|
|
|
|
|
def _load_langchain_example_module() -> ModuleType:
|
|
path = Path("examples/langchain_vm_run.py")
|
|
spec = importlib.util.spec_from_file_location("langchain_vm_run", path)
|
|
if spec is None or spec.loader is None:
|
|
raise AssertionError("failed to load LangChain example module")
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
def test_langchain_example_delegates_to_pyro(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
module = _load_langchain_example_module()
|
|
monkeypatch.setattr(
|
|
module,
|
|
"Pyro",
|
|
lambda: type(
|
|
"StubPyro",
|
|
(),
|
|
{
|
|
"run_in_vm": staticmethod(
|
|
lambda **kwargs: {"exit_code": 0, "stdout": kwargs["command"], "network": False}
|
|
)
|
|
},
|
|
)(),
|
|
)
|
|
result = module.run_vm_run_tool(
|
|
environment="debian:12",
|
|
command="git --version",
|
|
vcpu_count=1,
|
|
mem_mib=1024,
|
|
)
|
|
assert "git --version" in result
|
|
|
|
|
|
def test_langchain_example_builds_vm_run_tool(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
module = _load_langchain_example_module()
|
|
fake_langchain_tools = ModuleType("langchain_core.tools")
|
|
|
|
def fake_tool(name: str) -> Any:
|
|
def decorator(fn: Any) -> Any:
|
|
fn.name = name
|
|
return fn
|
|
|
|
return decorator
|
|
|
|
cast(Any, fake_langchain_tools).tool = fake_tool
|
|
fake_langchain_core = ModuleType("langchain_core")
|
|
monkeypatch.setitem(sys.modules, "langchain_core", fake_langchain_core)
|
|
monkeypatch.setitem(sys.modules, "langchain_core.tools", fake_langchain_tools)
|
|
|
|
tool = module.build_langchain_vm_run_tool()
|
|
|
|
assert tool.name == "vm_run"
|