summaryrefslogtreecommitdiff
path: root/evals/tests/test_session.py
blob: 762ce923ccd7631fb870055af6c3cc0ee9f77e49 (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
import subprocess
import time
from unittest.mock import call, patch

from drill.session import TmuxSession


class TestTmuxSession:
    def test_create_and_kill(self):
        session = TmuxSession(name="drill-test-create", cols=80, rows=24)
        session.create()
        result = subprocess.run(
            ["tmux", "has-session", "-t", "drill-test-create"],
            capture_output=True,
        )
        assert result.returncode == 0
        session.kill()
        result = subprocess.run(
            ["tmux", "has-session", "-t", "drill-test-create"],
            capture_output=True,
        )
        assert result.returncode != 0

    def test_send_keys_and_capture(self):
        session = TmuxSession(name="drill-test-keys", cols=80, rows=24)
        session.create()
        try:
            session.send_keys("echo hello-drill-test")
            time.sleep(0.5)
            output = session.capture()
            assert "hello-drill-test" in output
        finally:
            session.kill()

    def test_send_keys_pastes_text_then_submits(self):
        session = TmuxSession(name="drill-test-command-shape")

        with (
            patch("drill.session.subprocess.run") as run,
            patch("drill.session.time.sleep") as sleep,
        ):
            session.send_keys("hello `weird` text")

        assert run.call_args_list == [
            call(
                [
                    "tmux",
                    "set-buffer",
                    "-b",
                    "drill-test-command-shape-input",
                    "hello `weird` text",
                ],
                check=True,
            ),
            call(
                [
                    "tmux",
                    "paste-buffer",
                    "-d",
                    "-b",
                    "drill-test-command-shape-input",
                    "-t",
                    "drill-test-command-shape",
                ],
                check=True,
            ),
            call(["tmux", "send-keys", "-t", "drill-test-command-shape", "Enter"], check=True),
        ]
        sleep.assert_called_once_with(0.1)

    def test_launch_command(self, tmp_path):
        session = TmuxSession(name="drill-test-launch", cols=80, rows=24)
        session.create()
        try:
            session.launch(["python3", "-c", "import time; time.sleep(30)"], cwd=str(tmp_path))
            time.sleep(0.5)
            assert session.is_process_alive()
        finally:
            session.kill()

    def test_send_special_key(self, tmp_path):
        session = TmuxSession(name="drill-test-special", cols=80, rows=24)
        proof_file = tmp_path / "after-ctrl-c"
        session.create()
        try:
            session.send_keys("cat")
            time.sleep(0.3)
            session.send_special_key("ctrl-c")
            time.sleep(0.3)
            session.send_keys(f"touch {proof_file}")
            time.sleep(0.3)
            assert proof_file.exists()
        finally:
            session.kill()