File size: 1,626 Bytes
9c0e837
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import subprocess

from src.community.agent_browser import tools as agent_browser_tools


def test_agent_browser_tool_rejects_unsupported_command():
    result = agent_browser_tools.agent_browser_tool.invoke({"command": "rm -rf /"})
    assert "unsupported agent-browser command" in result


def test_agent_browser_tool_runs_and_returns_stdout(monkeypatch):
    monkeypatch.setattr(agent_browser_tools, "_resolve_agent_browser_command", lambda: ["agent-browser"])

    def _fake_run(cmd, capture_output, text, check, timeout):
        assert cmd[0] == "agent-browser"
        assert "--session" in cmd
        assert "--json" in cmd
        return subprocess.CompletedProcess(cmd, 0, stdout='{"ok":true}\n', stderr="")

    monkeypatch.setattr(agent_browser_tools.subprocess, "run", _fake_run)

    result = agent_browser_tools.agent_browser_tool.invoke(
        {
            "command": "open https://example.com",
            "session": "s1",
            "json_output": True,
            "timeout_seconds": 30,
        }
    )
    assert result == '{"ok":true}'


def test_agent_browser_tool_reports_cli_errors(monkeypatch):
    monkeypatch.setattr(agent_browser_tools, "_resolve_agent_browser_command", lambda: ["agent-browser"])

    def _fake_run(cmd, capture_output, text, check, timeout):
        return subprocess.CompletedProcess(cmd, 1, stdout="", stderr="browser failed")

    monkeypatch.setattr(agent_browser_tools.subprocess, "run", _fake_run)

    result = agent_browser_tools.agent_browser_tool.invoke({"command": "snapshot -i"})
    assert "exited with code 1" in result
    assert "browser failed" in result