summaryrefslogtreecommitdiff
path: root/evals/tests/test_assertions.py
blob: a6d5002c3391ffe5c9bb92b5b68cd1d2d97547be (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
from drill.assertions import AssertionResult, run_verify_assertions


class TestAssertionResult:
    def test_passing_to_criterion_result(self):
        ar = AssertionResult(
            command="tool-called Read",
            passed=True,
            exit_code=0,
            stdout="PASS: Read called 3 time(s)",
            stderr="",
        )
        cr = ar.to_criterion_result()
        assert cr.verdict == "pass"
        assert cr.source == "assertion"
        assert "[assertion]" in cr.criterion
        assert "tool-called Read" in cr.criterion

    def test_failing_to_criterion_result(self):
        ar = AssertionResult(
            command="tool-not-called Write",
            passed=False,
            exit_code=1,
            stdout="",
            stderr="FAIL: Write called 2 time(s)",
        )
        cr = ar.to_criterion_result()
        assert cr.verdict == "fail"
        assert cr.source == "assertion"
        assert "stderr: FAIL" in cr.evidence


class TestRunVerifyAssertions:
    def test_passing_assertion(self, tmp_path):
        tc = '{"tool": "Read", "args": {}, "source": "native"}\n'
        (tmp_path / "tool_calls.jsonl").write_text(tc)
        results = run_verify_assertions(
            assertions=["grep -q Read tool_calls.jsonl"],
            results_dir=tmp_path,
            workdir=tmp_path,
        )
        assert len(results) == 1
        assert results[0].passed is True
        assert results[0].exit_code == 0

    def test_failing_assertion(self, tmp_path):
        tc = '{"tool": "Read", "args": {}, "source": "native"}\n'
        (tmp_path / "tool_calls.jsonl").write_text(tc)
        results = run_verify_assertions(
            assertions=["grep -q NonexistentTool tool_calls.jsonl"],
            results_dir=tmp_path,
            workdir=tmp_path,
        )
        assert len(results) == 1
        assert results[0].passed is False

    def test_runs_all_assertions(self, tmp_path):
        (tmp_path / "tool_calls.jsonl").write_text('{"tool": "Read"}\n')
        results = run_verify_assertions(
            assertions=[
                "grep -q Read tool_calls.jsonl",
                "grep -q Write tool_calls.jsonl",
                "grep -q Read tool_calls.jsonl",
            ],
            results_dir=tmp_path,
            workdir=tmp_path,
        )
        assert len(results) == 3
        assert results[0].passed is True
        assert results[1].passed is False
        assert results[2].passed is True

    def test_timeout_handling(self, tmp_path):
        (tmp_path / "tool_calls.jsonl").write_text("{}\n")
        results = run_verify_assertions(
            assertions=["sleep 30"],
            results_dir=tmp_path,
            workdir=tmp_path,
            timeout_seconds=1,
        )
        assert len(results) == 1
        assert results[0].passed is False
        assert results[0].exit_code == 124
        assert "Timed out" in results[0].stderr

    def test_drill_workdir_env_var(self, tmp_path):
        (tmp_path / "tool_calls.jsonl").write_text("{}\n")
        workdir = tmp_path / "scenario-workdir"
        workdir.mkdir()
        results = run_verify_assertions(
            assertions=['test "$DRILL_WORKDIR" = "' + str(workdir) + '"'],
            results_dir=tmp_path,
            workdir=workdir,
        )
        assert len(results) == 1
        assert results[0].passed is True

    def test_bin_dir_on_path(self, tmp_path):
        (tmp_path / "tool_calls.jsonl").write_text("{}\n")
        results = run_verify_assertions(
            assertions=["echo $PATH | grep -q bin"],
            results_dir=tmp_path,
            workdir=tmp_path,
        )
        assert len(results) == 1
        assert results[0].passed is True