Co-authored-by: Carlo <carloantonio.patti@mistral.ai> Co-authored-by: Mathias Gesbert <mathias.gesbert@mistral.ai> Co-authored-by: Clement Sirieix <clem.sirieix@gmail.com> Co-authored-by: Michel Thomazo <michel.thomazo@mistral.ai> Co-authored-by: Clément Drouin <clement.drouin@mistral.ai> Co-authored-by: Vincent Guilloux <vincent.guilloux@mistral.ai> Co-authored-by: Thomas Kenbeek <thomas.kenbeek@mistral.ai> Co-authored-by: Mistral Vibe <vibe@mistral.ai>
91 lines
2.9 KiB
Python
91 lines
2.9 KiB
Python
from __future__ import annotations
|
||
|
||
import pytest
|
||
|
||
from tests.mock.utils import collect_result
|
||
from vibe.core.tools.base import BaseToolState, ToolError, ToolPermission
|
||
from vibe.core.tools.builtins.bash import Bash, BashArgs, BashToolConfig
|
||
|
||
|
||
@pytest.fixture
|
||
def bash(tmp_path, monkeypatch):
|
||
monkeypatch.chdir(tmp_path)
|
||
config = BashToolConfig()
|
||
return Bash(config=config, state=BaseToolState())
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_runs_echo_successfully(bash):
|
||
result = await collect_result(bash.run(BashArgs(command="echo hello")))
|
||
|
||
assert result.returncode == 0
|
||
assert result.stdout == "hello\n"
|
||
assert result.stderr == ""
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_fails_cat_command_with_missing_file(bash):
|
||
with pytest.raises(ToolError) as err:
|
||
await collect_result(bash.run(BashArgs(command="cat missing_file.txt")))
|
||
|
||
message = str(err.value)
|
||
assert "Command failed" in message
|
||
assert "Return code: 1" in message
|
||
assert "No such file or directory" in message
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_uses_effective_workdir(tmp_path, monkeypatch):
|
||
monkeypatch.chdir(tmp_path)
|
||
config = BashToolConfig()
|
||
bash_tool = Bash(config=config, state=BaseToolState())
|
||
|
||
result = await collect_result(bash_tool.run(BashArgs(command="pwd")))
|
||
|
||
assert result.stdout.strip() == str(tmp_path)
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_handles_timeout(bash):
|
||
with pytest.raises(ToolError) as err:
|
||
await collect_result(bash.run(BashArgs(command="sleep 2", timeout=1)))
|
||
|
||
assert "Command timed out after 1s" in str(err.value)
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_truncates_output_to_max_bytes(bash):
|
||
config = BashToolConfig(max_output_bytes=5)
|
||
bash_tool = Bash(config=config, state=BaseToolState())
|
||
|
||
result = await collect_result(
|
||
bash_tool.run(BashArgs(command="printf 'abcdefghij'"))
|
||
)
|
||
|
||
assert result.stdout == "abcde"
|
||
assert result.stderr == ""
|
||
assert result.returncode == 0
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_decodes_non_utf8_bytes(bash):
|
||
result = await collect_result(bash.run(BashArgs(command="printf '\\xff\\xfe'")))
|
||
|
||
# accept both possible encodings, as some shells emit escaped bytes as literal strings
|
||
assert result.stdout in {"<EFBFBD><EFBFBD>", "\xff\xfe", r"\xff\xfe"}
|
||
assert result.stderr == ""
|
||
|
||
|
||
def test_resolve_permission():
|
||
config = BashToolConfig(allowlist=["echo", "pwd"], denylist=["rm"])
|
||
bash_tool = Bash(config=config, state=BaseToolState())
|
||
|
||
allowlisted = bash_tool.resolve_permission(BashArgs(command="echo hi"))
|
||
denylisted = bash_tool.resolve_permission(BashArgs(command="rm -rf /tmp"))
|
||
mixed = bash_tool.resolve_permission(BashArgs(command="pwd && whoami"))
|
||
empty = bash_tool.resolve_permission(BashArgs(command=""))
|
||
|
||
assert allowlisted is ToolPermission.ALWAYS
|
||
assert denylisted is ToolPermission.NEVER
|
||
assert mixed is None
|
||
assert empty is None
|