Files
odysseus/tests/test_unknown_tool_calls.py
T
RosenTomov c46d37d876 test(tool_execution): stop two tests leaking src.tool_execution into the suite (#2686)
* Make in-venv pip-fallback test independent of the runner's environment

test_pip_install_fallback_chain_propagates_failure_in_venv simulated the in-venv case by probing the real interpreter (sys.prefix != sys.base_prefix). That assumes the test runner is itself inside a venv. CI runs pytest with no venv, so venv_check reported not-in-venv, the negated guard flipped, the --user branch fired, and the assertion failed. Make venv_check exit 0 directly to simulate the in-venv condition deterministically, mirroring the outside-venv companion test.

* Stop agent-tool import shims from leaking into the admin-gate test

test_function_call_non_object_args and test_unknown_tool_calls stub heavy DB/auth deps at import time to load the real agent-tool stack, but they popped src.tool_execution and left core.auth stubbed without restoring. Popping and re-importing src.tool_execution rebinds the src package's tool_execution attribute, so test_edit_file's later 'import src.tool_execution as te' resolved to a different module object than the one execute_tool_block lives in. The monkeypatch on _owner_is_admin then missed, the non-admin edit_file gate never fired, and the edit went through (exit_code 0). Stop touching src.tool_execution and restore the heavy stubs after import. Verified the full suite is green on Linux (Python 3.11, matching CI).

---------

Co-authored-by: Alexandre Teixeira <111787685+alteixeira20@users.noreply.github.com>
2026-06-09 16:35:10 +01:00

78 lines
3.2 KiB
Python

import sys
from unittest.mock import MagicMock
# This module needs the real agent-tool stack; importing it pulls in heavy
# DB/auth deps, so we stub those just long enough to import, then restore them.
# We deliberately do NOT pop src.tool_execution: popping and re-importing it
# rebinds the `src` package's `tool_execution` attribute, so a later
# `import src.tool_execution as te` resolves to a different module object than
# the one its functions live in - which silently breaks tests that monkeypatch
# it (e.g. test_edit_file's admin gate).
_ABSENT = object()
_AGENT_MODULES = ["src.agent_tools", "src.tool_parsing", "src.tool_schemas"]
_STUBBED = [
"sqlalchemy", "sqlalchemy.orm", "sqlalchemy.ext", "sqlalchemy.ext.declarative",
"sqlalchemy.ext.hybrid", "sqlalchemy.sql", "sqlalchemy.sql.expression",
"src.database", "core.models", "core.database", "core.auth",
]
_saved_stubs = {name: sys.modules.get(name, _ABSENT) for name in _STUBBED}
for _mod in _AGENT_MODULES:
sys.modules.pop(_mod, None)
for _mod in _STUBBED:
if _mod not in sys.modules:
sys.modules[_mod] = MagicMock()
import pytest # noqa: E402
import src.agent_tools # noqa: E402,F401
from src.tool_parsing import parse_tool_blocks # noqa: E402
from src.tool_schemas import function_call_to_tool_block # noqa: E402
# Drop the stubs we installed so they do not leak into later tests.
for _name, _original in _saved_stubs.items():
if _original is _ABSENT:
sys.modules.pop(_name, None)
else:
sys.modules[_name] = _original
def test_parse_xml_unknown_tool_returns_none():
"""XML-style <invoke> tags with truly unknown tools should be filtered out (return None)."""
text = '<invoke name="super_secret_tool"><parameter name="arg1">value1</parameter></invoke>'
blocks = parse_tool_blocks(text)
assert len(blocks) == 0
def test_parse_tool_call_unknown_tool_returns_none():
"""[TOOL_CALL] blocks with truly unknown tools should be filtered out (return None)."""
text = '[TOOL_CALL] {tool => "mega_blast", command => "run energy"} [/TOOL_CALL]'
blocks = parse_tool_blocks(text)
assert len(blocks) == 0
def test_function_call_to_tool_block_unknown_tool_returns_none():
"""Native function calls of truly unknown tools should return None."""
block = function_call_to_tool_block("ultra_zap", '{"power": 9000}')
assert block is None
def test_function_call_to_tool_block_invalid_json_returns_none():
"""Unparseable JSON arguments should result in returning None."""
block = function_call_to_tool_block("web_search", '{"query": "valid json') # invalid JSON
assert block is None
def test_google_search_mapping():
"""google_search should map to web_search and extract the first query from queries list or string."""
# List of queries case
block = function_call_to_tool_block("google_search", '{"queries": ["testing google search"]}')
assert block is not None
assert block.tool_type == "web_search"
assert block.content == "testing google search"
# Single string query case
block = function_call_to_tool_block("google_search_retrieval", '{"queries": "testing google search string"}')
assert block is not None
assert block.tool_type == "web_search"
assert block.content == "testing google search string"