Spaces:
Running
Running
File size: 16,344 Bytes
9fcee62 8146dee 9fcee62 8146dee 9fcee62 | 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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 | """
Comprehensive tests for CLI parser module to improve coverage.
"""
import argparse
import pytest
from orka.cli.parser import create_parser, setup_subcommands
class TestCreateParser:
"""Test the create_parser function."""
def test_create_parser_basic(self):
"""Test basic parser creation."""
parser = create_parser()
assert isinstance(parser, argparse.ArgumentParser)
assert parser.description == "OrKa - Orchestrator Kit for Agents"
assert parser.formatter_class == argparse.RawDescriptionHelpFormatter
def test_create_parser_global_options(self):
"""Test that global options are properly configured."""
parser = create_parser()
# Test verbose option
args = parser.parse_args(["-v"])
assert args.verbose is True
args = parser.parse_args(["--verbose"])
assert args.verbose is True
args = parser.parse_args([])
assert args.verbose is False
def test_create_parser_json_option(self):
"""Test JSON output option."""
parser = create_parser()
# Test JSON option
args = parser.parse_args(["--json"])
assert args.json is True
args = parser.parse_args([])
assert args.json is False
def test_create_parser_combined_options(self):
"""Test combining global options."""
parser = create_parser()
args = parser.parse_args(["-v", "--json"])
assert args.verbose is True
assert args.json is True
args = parser.parse_args(["--verbose", "--json"])
assert args.verbose is True
assert args.json is True
class TestSetupSubcommands:
"""Test the setup_subcommands function."""
def test_setup_subcommands_basic(self):
"""Test basic subcommand setup."""
parser = create_parser()
parser_with_subs = setup_subcommands(parser)
assert parser_with_subs is parser # Should return the same parser
assert hasattr(parser_with_subs, "_subparsers")
def test_run_command_setup(self):
"""Test run command configuration."""
parser = create_parser()
setup_subcommands(parser)
# Test run command with required arguments
args = parser.parse_args(["run", "config.yaml", "test_input"])
assert args.command == "run"
assert args.config == "config.yaml"
assert args.input == "test_input"
assert hasattr(args, "func")
def test_run_command_missing_arguments(self):
"""Test run command with missing arguments."""
parser = create_parser()
setup_subcommands(parser)
# Test missing config argument
with pytest.raises(SystemExit):
parser.parse_args(["run"])
# Test missing input argument
with pytest.raises(SystemExit):
parser.parse_args(["run", "config.yaml"])
def test_memory_command_setup(self):
"""Test memory command configuration."""
parser = create_parser()
setup_subcommands(parser)
# Test memory command without subcommand
args = parser.parse_args(["memory"])
assert args.command == "memory"
assert args.memory_command is None
def test_memory_stats_command(self):
"""Test memory stats subcommand."""
parser = create_parser()
setup_subcommands(parser)
# Test stats without backend
args = parser.parse_args(["memory", "stats"])
assert args.command == "memory"
assert args.memory_command == "stats"
assert args.backend is None
assert hasattr(args, "func")
# Test stats with backend
args = parser.parse_args(["memory", "stats", "--backend", "redis"])
assert args.backend == "redis"
args = parser.parse_args(["memory", "stats", "--backend", "redisstack"])
assert args.backend == "redisstack"
def test_memory_stats_invalid_backend(self):
"""Test memory stats with invalid backend."""
parser = create_parser()
setup_subcommands(parser)
# Test invalid backend
with pytest.raises(SystemExit):
parser.parse_args(["memory", "stats", "--backend", "invalid"])
def test_memory_cleanup_command(self):
"""Test memory cleanup subcommand."""
parser = create_parser()
setup_subcommands(parser)
# Test cleanup without options
args = parser.parse_args(["memory", "cleanup"])
assert args.command == "memory"
assert args.memory_command == "cleanup"
assert args.backend is None
assert args.dry_run is False
assert hasattr(args, "func")
# Test cleanup with backend
args = parser.parse_args(["memory", "cleanup", "--backend", "redis"])
assert args.backend == "redis"
# Test cleanup with dry-run
args = parser.parse_args(["memory", "cleanup", "--dry-run"])
assert args.dry_run is True
# Test cleanup with both options
args = parser.parse_args(["memory", "cleanup", "--backend", "redisstack", "--dry-run"])
assert args.backend == "redisstack"
assert args.dry_run is True
def test_memory_cleanup_invalid_backend(self):
"""Test memory cleanup with invalid backend."""
parser = create_parser()
setup_subcommands(parser)
# Test invalid backend
with pytest.raises(SystemExit):
parser.parse_args(["memory", "cleanup", "--backend", "invalid"])
def test_memory_configure_command(self):
"""Test memory configure subcommand."""
parser = create_parser()
setup_subcommands(parser)
# Test configure without backend
args = parser.parse_args(["memory", "configure"])
assert args.command == "memory"
assert args.memory_command == "configure"
assert args.backend is None
assert hasattr(args, "func")
# Test configure with backend
args = parser.parse_args(["memory", "configure", "--backend", "redisstack"])
assert args.backend == "redisstack"
def test_memory_configure_invalid_backend(self):
"""Test memory configure with invalid backend."""
parser = create_parser()
setup_subcommands(parser)
# Test invalid backend
with pytest.raises(SystemExit):
parser.parse_args(["memory", "configure", "--backend", "invalid"])
def test_memory_watch_command(self):
"""Test memory watch subcommand."""
parser = create_parser()
setup_subcommands(parser)
# Test watch without options
args = parser.parse_args(["memory", "watch"])
assert args.command == "memory"
assert args.memory_command == "watch"
assert args.backend is None
assert args.interval == 5
assert args.no_clear is False
assert args.compact is False
assert args.use_rich is False
assert args.fallback is False
assert hasattr(args, "func")
def test_memory_watch_with_backend(self):
"""Test memory watch with backend option."""
parser = create_parser()
setup_subcommands(parser)
# Test each backend
for backend in ["redis", "redisstack"]:
args = parser.parse_args(["memory", "watch", "--backend", backend])
assert args.backend == backend
def test_memory_watch_with_interval(self):
"""Test memory watch with interval option."""
parser = create_parser()
setup_subcommands(parser)
# Test integer interval
args = parser.parse_args(["memory", "watch", "--interval", "10"])
assert args.interval == 10.0
# Test float interval
args = parser.parse_args(["memory", "watch", "--interval", "2.5"])
assert args.interval == 2.5
def test_memory_watch_invalid_interval(self):
"""Test memory watch with invalid interval."""
parser = create_parser()
setup_subcommands(parser)
# Test invalid interval
with pytest.raises(SystemExit):
parser.parse_args(["memory", "watch", "--interval", "invalid"])
def test_memory_watch_boolean_options(self):
"""Test memory watch boolean options."""
parser = create_parser()
setup_subcommands(parser)
# Test no-clear option
args = parser.parse_args(["memory", "watch", "--no-clear"])
assert args.no_clear is True
# Test compact option
args = parser.parse_args(["memory", "watch", "--compact"])
assert args.compact is True
# Test use-rich option
args = parser.parse_args(["memory", "watch", "--use-rich"])
assert args.use_rich is True
# Test fallback option
args = parser.parse_args(["memory", "watch", "--fallback"])
assert args.fallback is True
def test_memory_watch_all_options(self):
"""Test memory watch with all options combined."""
parser = create_parser()
setup_subcommands(parser)
args = parser.parse_args(
[
"memory",
"watch",
"--backend",
"redis",
"--interval",
"3.0",
"--no-clear",
"--compact",
"--use-rich",
"--fallback",
],
)
assert args.backend == "redis"
assert args.interval == 3.0
assert args.no_clear is True
assert args.compact is True
assert args.use_rich is True
assert args.fallback is True
def test_memory_watch_invalid_backend(self):
"""Test memory watch with invalid backend."""
parser = create_parser()
setup_subcommands(parser)
# Test invalid backend
with pytest.raises(SystemExit):
parser.parse_args(["memory", "watch", "--backend", "invalid"])
def test_invalid_command(self):
"""Test parsing with invalid command."""
parser = create_parser()
setup_subcommands(parser)
# Test invalid command
with pytest.raises(SystemExit):
parser.parse_args(["invalid_command"])
def test_invalid_memory_subcommand(self):
"""Test parsing with invalid memory subcommand."""
parser = create_parser()
setup_subcommands(parser)
# Test invalid memory subcommand
with pytest.raises(SystemExit):
parser.parse_args(["memory", "invalid_subcommand"])
def test_global_options_with_subcommands(self):
"""Test global options combined with subcommands."""
parser = create_parser()
setup_subcommands(parser)
# Test global options with run command
args = parser.parse_args(["-v", "--json", "run", "config.yaml", "input"])
assert args.verbose is True
assert args.json is True
assert args.command == "run"
assert args.config == "config.yaml"
assert args.input == "input"
# Test global options with memory command
args = parser.parse_args(["--verbose", "memory", "stats", "--backend", "redis"])
assert args.verbose is True
assert args.json is False
assert args.command == "memory"
assert args.memory_command == "stats"
assert args.backend == "redis"
def test_help_output(self):
"""Test help output doesn't crash."""
parser = create_parser()
setup_subcommands(parser)
# Test main help
with pytest.raises(SystemExit):
parser.parse_args(["--help"])
# Test run help
with pytest.raises(SystemExit):
parser.parse_args(["run", "--help"])
# Test memory help
with pytest.raises(SystemExit):
parser.parse_args(["memory", "--help"])
# Test memory stats help
with pytest.raises(SystemExit):
parser.parse_args(["memory", "stats", "--help"])
# Test memory cleanup help
with pytest.raises(SystemExit):
parser.parse_args(["memory", "cleanup", "--help"])
# Test memory configure help
with pytest.raises(SystemExit):
parser.parse_args(["memory", "configure", "--help"])
# Test memory watch help
with pytest.raises(SystemExit):
parser.parse_args(["memory", "watch", "--help"])
class TestParserIntegration:
"""Test parser integration scenarios."""
def test_parser_integration_create_and_setup(self):
"""Test creating parser and setting up subcommands together."""
parser = create_parser()
parser_with_subs = setup_subcommands(parser)
# Verify it's the same parser object
assert parser is parser_with_subs
# Test that both global and subcommand options work
args = parser.parse_args(["-v", "--json", "memory", "watch", "--interval", "1.5"])
assert args.verbose is True
assert args.json is True
assert args.command == "memory"
assert args.memory_command == "watch"
assert args.interval == 1.5
def test_parser_function_assignments(self):
"""Test that functions are properly assigned to subcommands."""
parser = create_parser()
setup_subcommands(parser)
# Test run command function
args = parser.parse_args(["run", "test.yaml", "input"])
assert hasattr(args, "func")
assert args.func.__name__ == "run_orchestrator"
# Test memory stats function
args = parser.parse_args(["memory", "stats"])
assert hasattr(args, "func")
assert args.func.__name__ == "memory_stats"
# Test memory cleanup function
args = parser.parse_args(["memory", "cleanup"])
assert hasattr(args, "func")
assert args.func.__name__ == "memory_cleanup"
# Test memory configure function
args = parser.parse_args(["memory", "configure"])
assert hasattr(args, "func")
assert args.func.__name__ == "memory_configure"
# Test memory watch function
args = parser.parse_args(["memory", "watch"])
assert hasattr(args, "func")
assert args.func.__name__ == "memory_watch"
def test_parser_edge_cases(self):
"""Test parser edge cases."""
parser = create_parser()
setup_subcommands(parser)
# Test empty args - may or may not exit depending on argparse configuration
try:
args = parser.parse_args([])
# If it doesn't exit, check that command is None or empty
assert args.command is None
except SystemExit:
# This is also acceptable behavior
pass
# Test minimum valid run command
args = parser.parse_args(["run", "a", "b"])
assert args.config == "a"
assert args.input == "b"
# Test minimum valid memory command
args = parser.parse_args(["memory", "stats"])
assert args.memory_command == "stats"
def test_parser_namespace_isolation(self):
"""Test that different subcommands don't interfere with each other."""
parser = create_parser()
setup_subcommands(parser)
# Parse run command
run_args = parser.parse_args(["run", "config.yaml", "input"])
assert not hasattr(run_args, "backend")
assert not hasattr(run_args, "dry_run")
assert not hasattr(run_args, "interval")
# Parse memory stats command
stats_args = parser.parse_args(["memory", "stats", "--backend", "redis"])
assert not hasattr(stats_args, "config")
assert not hasattr(stats_args, "input")
assert not hasattr(stats_args, "dry_run")
assert not hasattr(stats_args, "interval")
# Parse memory cleanup command
cleanup_args = parser.parse_args(["memory", "cleanup", "--dry-run"])
assert not hasattr(cleanup_args, "config")
assert not hasattr(cleanup_args, "input")
assert not hasattr(cleanup_args, "interval")
# Parse memory watch command
watch_args = parser.parse_args(["memory", "watch", "--interval", "2"])
assert not hasattr(watch_args, "config")
assert not hasattr(watch_args, "input")
assert not hasattr(watch_args, "dry_run")
|