"""The layer probe opens one branch, or none."""

from util.layer_probe import choose_layer_experiment


def _rows(scores, crackle_time):
    return [
        {'score': score, 'crackle_time': time}
        for score, time in zip(scores, crackle_time)
    ]


def test_middle_layer_wins_when_probe_score_beats_the_final_block():
    scores = [60.0] * 12
    scores[6] = 70.0
    scores[11] = 66.0
    choice = choose_layer_experiment(_rows(scores, [1.0] * 12))
    assert choice['decision'] == 'layer'
    assert choice['feature_layer'] == 6
    assert choice['feature_layer_aux'] == -1


def test_concat_when_final_layer_is_best_and_crackle_peaks_earlier():
    scores = [50.0] * 12
    scores[11] = 66.0
    times = [1.0] * 12
    times[4] = 1.20
    times[11] = 1.00
    choice = choose_layer_experiment(_rows(scores, times))
    assert choice['decision'] == 'concat'
    assert choice['feature_layer'] == 11
    assert choice['feature_layer_aux'] == 4


def test_stop_when_the_score_gap_and_the_time_peak_are_both_small():
    scores = [60.0] * 12
    scores[6] = 60.2
    scores[11] = 60.0
    times = [1.0] * 12
    times[4] = 1.10
    times[11] = 1.00
    choice = choose_layer_experiment(_rows(scores, times))
    assert choice['decision'] == 'stop'
    assert choice['feature_layer'] == 11
    assert choice['feature_layer_aux'] == -1


def test_tied_scores_keep_the_final_layer_and_can_still_concat():
    scores = [66.0] * 12
    times = [1.0] * 12
    times[3] = 1.20
    choice = choose_layer_experiment(_rows(scores, times))
    assert choice['decision'] == 'concat'
    assert choice['feature_layer_aux'] == 3
