from typer.testing import CliRunner

from scripts.dwarf import app

runner = CliRunner()


def test_convert_rejects_jobs_below_one(tmp_path):
    root_file = tmp_path / "shard.root"
    root_file.touch()
    result = runner.invoke(app, ["convert", str(root_file), "--jobs", "0"])
    assert result.exit_code != 0
    assert "--jobs must be >= 1" in result.output


def test_convert_rejects_output_with_multiple_files(tmp_path):
    a = tmp_path / "a.root"
    b = tmp_path / "b.root"
    a.touch()
    b.touch()
    result = runner.invoke(app, ["convert", str(a), str(b), "--output", "out.parquet"])
    assert result.exit_code != 0
    assert "--output can only be used with a single input file" in result.output


def test_convert_rejects_output_with_parallel_jobs(tmp_path):
    root_file = tmp_path / "shard.root"
    root_file.touch()
    result = runner.invoke(
        app, ["convert", str(root_file), "--output", "out.parquet", "--jobs", "2"]
    )
    assert result.exit_code != 0
    assert "--output cannot be combined with --jobs > 1" in result.output


def test_convert_default_jobs_is_one():
    result = runner.invoke(app, ["convert", "--help"])
    assert result.exit_code == 0
    assert "default: 1" in result.output


def test_bump_gen_requires_reason():
    result = runner.invoke(app, ["bump-gen"])
    assert result.exit_code != 0
    assert "reason" in result.output.lower()


def test_create_manifest_requires_exactly_one_of_output_or_pool(tmp_path):
    f = tmp_path / "a.parquet"
    f.touch()
    result = runner.invoke(app, ["create-manifest", str(f)])
    assert result.exit_code != 0
    assert "exactly one of --output or --pool is required" in result.output


def test_create_manifest_requires_type_with_pool(tmp_path):
    f = tmp_path / "a.parquet"
    f.touch()
    result = runner.invoke(app, ["create-manifest", "--pool", "pbwo4", str(f)])
    assert result.exit_code != 0
    assert "--type is required when --pool is given" in result.output


def test_status_reports_missing_root(tmp_path):
    missing = tmp_path / "does-not-exist"
    result = runner.invoke(app, ["status", "--root", str(missing)])
    assert result.exit_code != 0
    assert "is not a directory" in result.output
