"""Unimplemented recipes and invalid research constraints cannot publish artifacts."""

from types import SimpleNamespace

import pytest

from pallet import cli
from pallet.experiments.train import train_yolo


@pytest.mark.parametrize("recipe", ["official_default"])
def test_direct_training_rejects_incomplete_recipe_before_output(tmp_path, recipe):
    output = tmp_path / "run"
    with pytest.raises(RuntimeError, match="not_runnable"):
        train_yolo([], [], None, None, output, recipe=recipe)
    assert not output.exists()


@pytest.mark.parametrize("recipe", ["official_default"])
def test_cli_rejects_incomplete_recipe_before_reading_data_or_writing_run(tmp_path, recipe, monkeypatch):
    def unexpected(*args, **kwargs):
        raise AssertionError("data access occurred before the recipe gate")

    monkeypatch.setattr(cli, "load_jsonl", unexpected)
    output = tmp_path / "run"
    args = cli.build_parser().parse_args([
        "train", "--annotations", str(tmp_path / "missing.jsonl"),
        "--output", str(output), "--recipe", recipe,
    ])
    with pytest.raises(RuntimeError, match="not_runnable"):
        args.func(args)
    assert not output.exists()


def test_pilot_recipe_is_the_unchanged_cli_default():
    args = cli.build_parser().parse_args(["train", "--annotations", "labels.jsonl"])
    assert args.recipe == "project_adamw_pilot"


def test_rf_constrained_invalid_config_rejected_before_artifacts(tmp_path):
    output = tmp_path/'rf'
    with pytest.raises(ValueError, match='requires size=640'):
        train_yolo([], [], None, None, output, family='rfdetr-small',
                   size=64, recipe='official_research_constrained')
    assert not output.exists()


def test_constrained_invalid_config_rejected_before_artifacts(tmp_path):
    output = tmp_path/'invalid'
    with pytest.raises(ValueError, match='requires size=640'):
        train_yolo([], [], None, None, output, size=64,
                   recipe='official_research_constrained')
    assert not output.exists()
