Viewing files at commit a3ea065926c9Commit detailsView latest

Browse files
tests/test_classify.py 67 lines · 2.8 KB · python
1
"""Tests for command classification and error-text pattern matching."""
2
3
from __future__ import annotations
4
5
import sys
6
from pathlib import Path
7
8
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))
9
10
import unittest
11
12
from oakbench.classify import UNKNOWN_COMMAND_PATTERN, classify_command, unwrap_shell_command
13
14
15
class UnwrapTest(unittest.TestCase):
16
    def test_unwraps_login_shell(self) -> None:
17
        self.assertEqual(unwrap_shell_command("/bin/zsh -lc 'git status'"), "git status")
18
        self.assertEqual(unwrap_shell_command("/bin/zsh -c 'git status'"), "git status")
19
        self.assertEqual(unwrap_shell_command("bash -lc 'oak diff'"), "oak diff")
20
21
    def test_passthrough(self) -> None:
22
        self.assertEqual(unwrap_shell_command("git status"), "git status")
23
24
25
class ClassifyTest(unittest.TestCase):
26
    def test_vcs_detection(self) -> None:
27
        self.assertIn("vcs", classify_command("git status --short"))
28
        self.assertIn("vcs", classify_command("/bin/zsh -lc 'oak commit --no-verify'"))
29
        self.assertNotIn("vcs", classify_command("ls -la"))
30
31
    def test_test_detection(self) -> None:
32
        self.assertIn("test", classify_command("python3 -m unittest discover -s tests"))
33
        self.assertIn("test", classify_command("python3 validate_configs.py"))
34
35
    def test_search_and_read(self) -> None:
36
        self.assertIn("search", classify_command("rg -n pattern ."))
37
        self.assertIn("search", classify_command("grep -rnE pattern src"))
38
        self.assertIn("search", classify_command("/usr/bin/grep -rnE pattern src"))
39
        self.assertIn("read", classify_command("sed -n '1,120p' app/pricing.py"))
40
        self.assertIn("read", classify_command("/bin/cat app/pricing.py"))
41
42
    def test_help_detection(self) -> None:
43
        # Help/discovery calls signal the agent does not know the tool zero-shot.
44
        self.assertIn("help", classify_command("oak --help"))
45
        self.assertIn("help", classify_command("git help commit"))
46
        self.assertIn("help", classify_command("man git-status"))
47
        self.assertNotIn("help", classify_command("ls -lh"))
48
49
50
class UnknownCommandPatternTest(unittest.TestCase):
51
    def test_matches_real_error_shapes(self) -> None:
52
        samples = [
53
            "git: 'stats' is not a git command. See 'git --help'.",
54
            "error: unrecognized subcommand 'stage-all-now'",
55
            "error: unknown option '--porcelain'",
56
            "zsh: no such command: oka",
57
        ]
58
        for sample in samples:
59
            self.assertIsNotNone(UNKNOWN_COMMAND_PATTERN.search(sample), sample)
60
61
    def test_does_not_match_ordinary_failures(self) -> None:
62
        for sample in ("nothing to commit, working tree clean", "merge conflict in app/pricing.py"):
63
            self.assertIsNone(UNKNOWN_COMMAND_PATTERN.search(sample), sample)
64
65
66
if __name__ == "__main__":
67
    unittest.main()