import threading
from types import SimpleNamespace

from pallet import matrix


def test_matrix_passes_explicit_recipe_and_cache_to_train_and_evaluate(tmp_path,monkeypatch):
    labels=tmp_path/'labels.jsonl'
    labels.write_text('frozen labels')
    protocol=tmp_path/'protocol'
    protocol.mkdir()
    (protocol/'protocol.json').write_text('{}')
    cache=tmp_path/'sealed.json'
    cache.write_text('{}')
    config={'epochs':1,'image_size':640,'effective_batch':16,'protocol':str(protocol),
            'phase':'pilot','recipe':'official_research_constrained','cache_index':str(cache)}
    spec={'output':str(tmp_path/'run'),'family':'yolo26','seed':42,
          'holdout_domain':None,'flags':[]}
    commands=[]
    trained=False
    monkeypatch.setattr(matrix,'_is_completed',lambda *args:trained)
    def run(command,**kwargs):
        nonlocal trained
        commands.append(command)
        trained=True
        return SimpleNamespace(returncode=0)
    monkeypatch.setattr(matrix.subprocess,'run',run)
    result=matrix._run_one(spec,config,str(labels),str(tmp_path),'cpu',threading.Lock())
    assert result['commands_executed']==2
    assert commands[0][commands[0].index('--recipe')+1]=='official_research_constrained'
    for command in commands:
        assert command[command.index('--cache-index')+1]==str(cache)
    assert commands[1][commands[1].index('--split')+1]=='val'
