summaryrefslogtreecommitdiff
path: root/evals/tests/test_helpers.py
blob: 6094a4f681fc5b07625406fb1fe2766b17d672bc (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import subprocess
from pathlib import Path

BIN_DIR = Path(__file__).parent.parent / "bin"
FIXTURES_DIR = Path(__file__).parent / "fixtures"


def run_helper(name: str, args: list[str], cwd: Path) -> subprocess.CompletedProcess[str]:
    return subprocess.run(
        [str(BIN_DIR / name), *args],
        cwd=cwd,
        capture_output=True,
        text=True,
    )


class TestToolCalled:
    def test_tool_present(self, tmp_path):
        (tmp_path / "tool_calls.jsonl").write_text((FIXTURES_DIR / "tools_multi.jsonl").read_text())
        result = run_helper("tool-called", ["Read"], tmp_path)
        assert result.returncode == 0

    def test_tool_absent(self, tmp_path):
        (tmp_path / "tool_calls.jsonl").write_text((FIXTURES_DIR / "tools_multi.jsonl").read_text())
        result = run_helper("tool-called", ["Write"], tmp_path)
        assert result.returncode == 1
        assert "FAIL" in result.stdout

    def test_empty_jsonl(self, tmp_path):
        (tmp_path / "tool_calls.jsonl").write_text("")
        result = run_helper("tool-called", ["Read"], tmp_path)
        assert result.returncode == 1


class TestToolNotCalled:
    def test_tool_absent(self, tmp_path):
        (tmp_path / "tool_calls.jsonl").write_text((FIXTURES_DIR / "tools_multi.jsonl").read_text())
        result = run_helper("tool-not-called", ["Write"], tmp_path)
        assert result.returncode == 0

    def test_tool_present(self, tmp_path):
        (tmp_path / "tool_calls.jsonl").write_text((FIXTURES_DIR / "tools_multi.jsonl").read_text())
        result = run_helper("tool-not-called", ["Read"], tmp_path)
        assert result.returncode == 1
        assert "FAIL" in result.stdout

    def test_empty_jsonl(self, tmp_path):
        (tmp_path / "tool_calls.jsonl").write_text("")
        result = run_helper("tool-not-called", ["Read"], tmp_path)
        assert result.returncode == 0


class TestToolCount:
    def test_gte_passes(self, tmp_path):
        (tmp_path / "tool_calls.jsonl").write_text((FIXTURES_DIR / "tools_multi.jsonl").read_text())
        result = run_helper("tool-count", ["Read", "gte", "2"], tmp_path)
        assert result.returncode == 0

    def test_gte_fails(self, tmp_path):
        (tmp_path / "tool_calls.jsonl").write_text((FIXTURES_DIR / "tools_multi.jsonl").read_text())
        result = run_helper("tool-count", ["Read", "gte", "5"], tmp_path)
        assert result.returncode == 1
        assert "FAIL" in result.stdout

    def test_eq(self, tmp_path):
        (tmp_path / "tool_calls.jsonl").write_text((FIXTURES_DIR / "tools_multi.jsonl").read_text())
        result = run_helper("tool-count", ["Read", "eq", "2"], tmp_path)
        assert result.returncode == 0

    def test_lt(self, tmp_path):
        (tmp_path / "tool_calls.jsonl").write_text((FIXTURES_DIR / "tools_multi.jsonl").read_text())
        result = run_helper("tool-count", ["Read", "lt", "3"], tmp_path)
        assert result.returncode == 0


class TestToolBefore:
    def test_correct_order(self, tmp_path):
        (tmp_path / "tool_calls.jsonl").write_text(
            (FIXTURES_DIR / "tools_ordered.jsonl").read_text()
        )
        result = run_helper("tool-before", ["Read", "Edit"], tmp_path)
        assert result.returncode == 0

    def test_wrong_order(self, tmp_path):
        (tmp_path / "tool_calls.jsonl").write_text(
            (FIXTURES_DIR / "tools_ordered.jsonl").read_text()
        )
        result = run_helper("tool-before", ["Edit", "EnterWorktree"], tmp_path)
        assert result.returncode == 1
        assert "FAIL" in result.stdout

    def test_first_tool_missing(self, tmp_path):
        (tmp_path / "tool_calls.jsonl").write_text(
            (FIXTURES_DIR / "tools_ordered.jsonl").read_text()
        )
        result = run_helper("tool-before", ["Write", "Read"], tmp_path)
        assert result.returncode == 1
        assert "never called" in result.stdout

    def test_second_tool_missing(self, tmp_path):
        (tmp_path / "tool_calls.jsonl").write_text(
            (FIXTURES_DIR / "tools_ordered.jsonl").read_text()
        )
        result = run_helper("tool-before", ["Read", "Write"], tmp_path)
        assert result.returncode == 1
        assert "never called" in result.stdout


class TestToolArgMatch:
    def test_matching_arg(self, tmp_path):
        (tmp_path / "tool_calls.jsonl").write_text((FIXTURES_DIR / "tools_multi.jsonl").read_text())
        result = run_helper(
            "tool-arg-match", ["Skill", '.skill == "superpowers:worktree"'], tmp_path
        )
        assert result.returncode == 0

    def test_no_matching_arg(self, tmp_path):
        (tmp_path / "tool_calls.jsonl").write_text((FIXTURES_DIR / "tools_multi.jsonl").read_text())
        result = run_helper("tool-arg-match", ["Skill", '.skill == "nonexistent"'], tmp_path)
        assert result.returncode == 1
        assert "FAIL" in result.stdout

    def test_tool_not_present(self, tmp_path):
        (tmp_path / "tool_calls.jsonl").write_text((FIXTURES_DIR / "tools_multi.jsonl").read_text())
        result = run_helper("tool-arg-match", ["Write", '.file_path == "/tmp/foo"'], tmp_path)
        assert result.returncode == 1