"""Tests for --workers flag in mine-blocks command."""

from __future__ import annotations



def test_workers_flag_parsing():
    """Test that --workers flag is parsed correctly."""
    from mining.cli.miner import _build_arg_parser
    
    parser = _build_arg_parser()
    
    # Test with explicit workers value
    args = parser.parse_args([
        "mine-blocks",
        "--address", "anim1test123",
        "--count", "5",
        "--workers", "8"
    ])
    
    assert args.cmd == "mine-blocks"
    assert args.address == "anim1test123"
    assert args.count == 5
    assert args.workers == 8


def test_workers_flag_default():
    """Test that --workers defaults to auto (0)."""
    from mining.cli.miner import _build_arg_parser
    
    parser = _build_arg_parser()
    
    # Test without workers flag
    args = parser.parse_args([
        "mine-blocks",
        "--address", "anim1test123",
        "--count", "5"
    ])
    
    # Should default to auto (0)
    assert args.workers == 0


def test_workers_flag_validation():
    """Test that --workers validates worker count."""
    from mining.cli.miner import _build_arg_parser
    
    parser = _build_arg_parser()
    
    # Test with positive worker count
    args = parser.parse_args([
        "mine-blocks",
        "--address", "anim1test123",
        "--count", "5",
        "--workers", "16"
    ])
    assert args.workers == 16
    
    # Test with worker count of 1
    args = parser.parse_args([
        "mine-blocks",
        "--address", "anim1test123",
        "--count", "5",
        "--workers", "1"
    ])
    assert args.workers == 1


def test_threads_in_start_command():
    """Test that --threads exists in start command (baseline verification)."""
    from mining.cli.miner import _build_arg_parser
    
    parser = _build_arg_parser()
    
    # Test start command with threads
    args = parser.parse_args([
        "start",
        "--threads", "4"
    ])
    
    assert args.cmd == "start"
    assert args.threads == 4


def test_workers_parameter_in_args():
    """Test that workers parameter is available in parsed arguments for RPC call."""
    from mining.cli.miner import _build_arg_parser
    from argparse import Namespace
    
    parser = _build_arg_parser()
    
    # Parse mine-blocks command with workers
    args = parser.parse_args([
        "mine-blocks",
        "--address", "anim1test123",
        "--count", "5",
        "--workers", "8",
        "--rpc-url", "http://127.0.0.1:8547"
    ])
    
    # Verify all parameters are present
    assert hasattr(args, "cmd")
    assert hasattr(args, "address")
    assert hasattr(args, "count")
    assert hasattr(args, "workers")
    assert hasattr(args, "rpc_url")
    
    # Verify values
    assert args.cmd == "mine-blocks"
    assert args.address == "anim1test123"
    assert args.count == 5
    assert args.workers == 8
    assert args.rpc_url == "http://127.0.0.1:8547"
    
    # Test that workers can be used to construct RPC params
    rpc_params = {
        "count": args.count,
        "address": args.address,
        "workers": args.workers
    }
    
    assert rpc_params["workers"] == 8
    assert rpc_params["count"] == 5
    assert rpc_params["address"] == "anim1test123"


if __name__ == "__main__":
    # Run tests directly
    test_workers_flag_parsing()
    print("✓ Workers flag parsing test passed")
    
    test_workers_flag_default()
    print("✓ Workers flag default test passed")
    
    test_workers_flag_validation()
    print("✓ Workers flag validation test passed")
    
    test_threads_in_start_command()
    print("✓ Threads in start command test passed")
    
    test_workers_parameter_in_args()
    print("✓ Workers parameter in args test passed")
    
    print("\n✓ All tests passed!")
