File size: 8,765 Bytes
6aa6c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""Offline selector checks; native construction and model calls are forbidden."""
from contextlib import redirect_stderr
import importlib
import io
import json
import os
from pathlib import Path
import subprocess
import sys
import tempfile
import unittest
from unittest.mock import Mock, patch

from benchkit.sim_server import cli
from benchkit.sim_server.checks.test_benchmark_conventions import ENTRIES


ROOT = Path(__file__).resolve().parents[3]


class BenchmarkCliTests(unittest.TestCase):
    def test_routes_match_independent_service_fixture_and_catalog(self):
        expected = {identity: f"benchmarks.simBenchmarks.{directory}.server"
                    for directory, _, identity, *_ in ENTRIES}
        self.assertEqual(len(expected), 13)
        self.assertEqual(cli.SERVICE_MODULES, expected)
        self.assertEqual(cli.ALIASES, {"robocasa_365": "robocasa365"})
        catalog = json.loads((ROOT / "benchkit/catalog/benchmarks.json").read_text())
        catalog_ids = {row["id"]: row["directory"] for row in catalog["benchmarks"]}
        for identity, module in expected.items():
            catalog_id = "robocasa_365" if identity == "robocasa365" else identity
            self.assertEqual(catalog_ids[catalog_id], module.rsplit(".", 1)[0].replace(".", "/"))

    def test_every_route_and_alias_forwards_unchanged_argv_and_return_value_once(self):
        for identity in (*cli.SERVICE_MODULES, *cli.ALIASES):
            for selector in (["--benchmark", identity], [f"--benchmark={identity}"]):
                with self.subTest(identity=identity, selector=selector):
                    arguments = ["--parameters", "/a path/config.json", "--seed", "-9",
                                 "--output-dir=/a path/result", "--token-env", "MY_SERVICE_TOKEN",
                                 "--port", "0"]
                    original = ["serve", *arguments[:2], *selector, *arguments[2:]]
                    service = Mock()
                    service.main.return_value = 17
                    with patch.object(cli, "import_module", return_value=service) as load:
                        self.assertEqual(cli.main(original), 17)
                    canonical = cli.ALIASES.get(identity, identity)
                    load.assert_called_once_with(cli.SERVICE_MODULES[canonical])
                    service.main.assert_called_once_with(arguments)
                    self.assertEqual(original, ["serve", *arguments[:2], *selector, *arguments[2:]])

    def test_invalid_routing_is_rejected_before_import(self):
        cases = [[], ["serve"], ["list", "--seed", "1"], ["serve", "--benchmark"],
                 ["serve", "--bench", "libero"], ["serve", "--bench=libero"],
                 ["serve", "--benchmark", "libero", "--bench", "robodojo"],
                 ["serve", "--benchmark", "libero", "--benchmark", "libero"],
                 ["serve", "--benchmark=libero", "--benchmark=robodojo"],
                 ["serve", "--benchmark", "robocasa365", "--benchmark=robocasa_365"]]
        cases += [["serve", "--benchmark", identity, "--help"] for identity in (
            "unknown", "vln_ce", "smoke", "simple", "LIBERO", "../RoboDojo",
            "benchmarks.simBenchmarks.LIBERO.server")]
        for arguments in cases:
            with self.subTest(arguments=arguments), patch.object(cli, "import_module") as load:
                with redirect_stderr(io.StringIO()), self.assertRaises(SystemExit) as error:
                    cli.main(arguments)
                self.assertEqual(error.exception.code, 2)
                load.assert_not_called()

    def test_list_and_generic_help_do_not_import_services_in_fresh_process(self):
        for arguments in (["list"], ["--help"], ["list", "--help"], ["serve", "--help"], ["serve", "-h"]):
            with self.subTest(arguments=arguments):
                code = f"""
import sys
from benchkit.sim_server.cli import main
try:
    result = main({arguments!r})
except SystemExit as exc:
    result = exc.code
assert result == 0, result
for name in ('benchmarks', 'AgentPolicies', 'numpy', 'torch', 'isaacsim', 'mujoco',
             'openai', 'anthropic', 'benchkit.campaigns.workflow', 'benchkit.registry',
             'benchkit.sim_server.launch', 'benchkit.sim_server.server'):
    assert not any(key == name or key.startswith(name + '.') for key in sys.modules), name
"""
                result = subprocess.run([sys.executable, "-B", "-S", "-c", code], cwd=ROOT,
                                        capture_output=True, text=True, timeout=30)
                self.assertEqual(result.returncode, 0, result.stderr)
                self.assertNotIn("listening", result.stdout)
                if arguments == ["list"]:
                    self.assertIn("Development services", result.stdout)
                    self.assertIn("repository-relative", result.stdout)
                    self.assertIn("robocasa365 (alias: robocasa_365)", result.stdout)
                    self.assertNotIn("vln_ce", result.stdout)

    def test_selected_help_imports_only_selected_service_without_native_or_model(self):
        for directory, _, identity, *_ in ENTRIES:
            with self.subTest(benchmark=identity):
                code = f"""
import sys
from benchkit.sim_server.cli import main
try:
    main(['serve', '--benchmark', {identity!r}, '--help'])
except SystemExit as exc:
    assert exc.code == 0, exc.code
else:
    raise AssertionError('selected launcher must handle help')
prefix = 'benchmarks.simBenchmarks.'
assert all(name.startswith(prefix + {directory!r} + '.') or name == prefix + {directory!r}
           for name in sys.modules if name.startswith(prefix)), sorted(sys.modules)
for name in ('AgentPolicies', 'numpy', 'torch', 'isaacsim', 'isaaclab', 'mujoco', 'robosuite',
             'libero', 'simpler_env', 'metaworld', 'sapien', 'omnigibson', 'calvin_env',
             'pybullet', 'openai', 'anthropic', 'benchkit.campaigns.workflow'):
    assert not any(key == name or key.startswith(name + '.') for key in sys.modules), name
"""
                result = subprocess.run([sys.executable, "-B", "-S", "-c", code], cwd=ROOT,
                                        capture_output=True, text=True, timeout=30,
                                        env={key: value for key, value in os.environ.items()
                                             if key != "RSI_SIM_SERVER_TOKEN"})
                self.assertEqual(result.returncode, 0, result.stderr)
                for option in ("--parameters", "--seed", "--output-dir", "--token-env"):
                    self.assertIn(option, result.stdout)
                self.assertNotIn("listening", result.stdout)

    def test_dojo_complete_panel_and_token_validation_still_precede_factory_and_storage(self):
        service = importlib.import_module("benchmarks.simBenchmarks.RoboDojo.server")
        for suffix, expected in ((["--episodes", "1"], "complete named panel"),
                                 ([], "URL-safe token"), (["--full"], "unrecognized arguments")):
            with self.subTest(suffix=suffix), tempfile.TemporaryDirectory() as directory:
                output = Path(directory) / "server"
                arguments = ["serve", "--benchmark", "robodojo", "--seed", "20260907",
                             "--output-dir", str(output), *suffix]
                with patch.dict(os.environ, {"RSI_SIM_SERVER_TOKEN": ""}), \
                        patch.object(service, "make_robodojo") as factory, \
                        redirect_stderr(io.StringIO()) as stderr, self.assertRaises(SystemExit) as error:
                    cli.main(arguments)
                self.assertEqual(error.exception.code, 2)
                self.assertIn(expected, stderr.getvalue())
                factory.assert_not_called()
                self.assertFalse(output.exists())

    def test_module_entrypoint_and_packaged_console_definition(self):
        configuration = (ROOT / "pyproject.toml").read_text()
        self.assertIn('rsi-bench = "benchkit.sim_server.cli:main"', configuration)
        result = subprocess.run([sys.executable, "-B", "-m", "benchkit.sim_server.cli", "list"],
                                cwd=ROOT, capture_output=True, text=True, timeout=30)
        self.assertEqual(result.returncode, 0, result.stderr)
        self.assertIn("simple_humanoid", result.stdout)
        smoke = importlib.import_module("benchkit.sim_server.__main__")
        with patch.object(smoke, "launch", return_value=0) as launch:
            self.assertEqual(smoke.main(["--help"]), 0)
        launch.assert_called_once_with(smoke.make_smoke_benchmark,
                                       supplemental_sources=smoke.SUPPLEMENTAL_FILES, argv=["--help"])


if __name__ == "__main__":
    unittest.main()