-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtest_opencode.py
More file actions
70 lines (52 loc) · 2.05 KB
/
Copy pathtest_opencode.py
File metadata and controls
70 lines (52 loc) · 2.05 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""Tests for the opencode agent wrapper — argv/env construction and timeout,
exercised without invoking the real `opencode` binary.
"""
from __future__ import annotations
import asyncio
from typing import Any
import agentix.agents.opencode as opencode
class _FakeProc:
def __init__(self, *, returncode: int = 0, out: bytes = b"done", err: bytes = b"", hang: bool = False) -> None:
self.returncode = returncode
self._out = out
self._err = err
self._hang = hang
self.killed = False
async def communicate(self) -> tuple[bytes, bytes]:
if self._hang and not self.killed:
await asyncio.sleep(10)
return self._out, self._err
def kill(self) -> None:
self.killed = True
async def test_run_builds_argv_and_env(monkeypatch: Any) -> None:
captured: dict[str, Any] = {}
async def _fake(*cmd: str, **kwargs: Any) -> _FakeProc:
captured["cmd"] = list(cmd)
captured["kwargs"] = kwargs
return _FakeProc(out=b"edited")
monkeypatch.setattr(asyncio, "create_subprocess_exec", _fake)
result = await opencode.run(
"fix the bug",
workdir="/repo",
model="openai/gpt-4o",
extra_args=["--agent", "build"],
env={"OPENAI_BASE_URL": "http://bridge", "OPENAI_API_KEY": "k"},
)
cmd = captured["cmd"]
assert cmd[1] == "run"
assert cmd[cmd.index("--model") + 1] == "openai/gpt-4o"
assert cmd[-1] == "fix the bug"
assert "--agent" in cmd
assert captured["kwargs"]["cwd"] == "/repo"
assert captured["kwargs"]["env"]["OPENAI_BASE_URL"] == "http://bridge"
assert result.exit_code == 0
assert result.stdout == "edited"
async def test_run_times_out_and_kills(monkeypatch: Any) -> None:
proc = _FakeProc(hang=True)
async def _fake(*cmd: str, **kwargs: Any) -> _FakeProc:
return proc
monkeypatch.setattr(asyncio, "create_subprocess_exec", _fake)
result = await opencode.run("x", timeout=0.01)
assert result.exit_code == -1
assert "timed out" in result.stderr
assert proc.killed