"""Unit tests for extdef.py. Run from the exp/ folder: python -m pytest unit_tests -v""" import hashlib import json import pytest import extdef from extdef import ( MAX_TOTAL_ROUNDS, build_session_data, generate_session_data, instruction_example, make_ctr_values, others_ctr_points, round_values, validate_session_params, TEAM_COLOR_HIGH, TEAM_COLOR_LOW, color_vars, team_color, ) # SHA-256 of the data the OLD extdef.py generated (seed 59, 10 groups of 9, 6 blocks x 20 rounds, # noise +-10). Recorded before the refactor; proves the new generator draws the same numbers. LEGACY_SHA256 = '8ee07328e1f1e3bdb6696716281118deca5731958e6fd544c85e96cd062c3a83' def _small(seed=1, n_groups=2, group_size=7, n_blocks=3, rounds=4, noise=10): return generate_session_data(n_groups, group_size, n_blocks, rounds, noise, seed) # ── make_ctr_values ─────────────────────────────────────────────────────────── def test_ctr_values_for_nine_match_the_old_design(): assert make_ctr_values(9) == [10, 20, 30, 40, 50, 60, 70, 80, 90] def test_ctr_values_for_ten(): assert make_ctr_values(10) == [9, 18, 27, 36, 45, 55, 64, 73, 82, 91] @pytest.mark.parametrize('size', range(7, 14)) def test_ctr_values_are_distinct_increasing_and_inside_the_axis(size): vals = make_ctr_values(size) assert len(vals) == size assert all(isinstance(v, int) for v in vals) assert vals == sorted(set(vals)) assert 0 < vals[0] and vals[-1] < 100 # ── generate_session_data ───────────────────────────────────────────────────── def test_generator_reproduces_the_legacy_data_exactly(): data = generate_session_data(10, 9, 6, 20, 10, 59) blob = json.dumps( {'ctr': data['ctr_points'], 'noise': data['noise_vals'], 'pol': data['policies']}, sort_keys=True, ) assert hashlib.sha256(blob.encode()).hexdigest() == LEGACY_SHA256 def test_generator_shapes_and_params(): data = _small() assert data['params'] == dict( n_groups=2, group_size=7, n_blocks=3, rounds_per_block=4, noise_range=10, seed=1, total_rounds=12, ) assert len(data['ctr_points']) == 2 for g in range(2): assert len(data['ctr_points'][g]) == 3 assert len(data['noise_vals'][g]) == 3 and len(data['noise_vals'][g][0]) == 4 assert len(data['policies'][g]) == 3 and len(data['policies'][g][0]) == 4 for b in range(3): # every center value appears exactly once per group per block assert sorted(data['ctr_points'][g][b]) == make_ctr_values(7) for r in range(4): assert len(data['noise_vals'][g][b][r]) == 7 assert all(-10 <= n <= 10 for n in data['noise_vals'][g][b][r]) def test_generator_is_deterministic_per_seed(): assert _small(seed=5) == _small(seed=5) assert _small(seed=5) != _small(seed=6) def test_generator_data_survives_a_json_round_trip(): # session.vars is stored as JSON, so the data must not change when round-tripped data = _small() assert json.loads(json.dumps(data)) == data @pytest.mark.parametrize('size', range(7, 14)) def test_policies_always_valid(size): # policy A strictly below policy B, both inside 0..100, for many seeds and the largest groups for seed in range(40): data = generate_session_data(1, size, 2, 10, 10, seed) for block in data['policies'][0]: for policy_a, policy_b in block: assert 0 <= policy_a < policy_b <= 100 def test_ideal_points_can_leave_the_axis(): # Documents (does not change) existing behaviour: with 13 players the lowest center is 7, so # noise can push an ideal point below 0. Nothing may crash because of it. lows = [] for seed in range(60): data = generate_session_data(1, 13, 2, 10, 10, seed) for g_block, n_block in zip(data['ctr_points'][0], data['noise_vals'][0]): for rnd in n_block: lows.append(min(c + n for c, n in zip(g_block, rnd))) assert min(lows) < 0 # ── validate_session_params ─────────────────────────────────────────────────── def test_validate_accepts_the_boundaries(): validate_session_params(7, 7, 1, 1, 0) validate_session_params(26, 13, 6, 30, 10) # 2 groups of 13, exactly 180 rounds @pytest.mark.parametrize('args, setting', [ ((6, 6, 6, 10, 10), 'group_size'), ((14, 14, 6, 10, 10), 'group_size'), ((10, 7, 6, 10, 10), 'multiple'), # 10 participants cannot fill groups of 7 ((4, 7, 6, 10, 10), 'multiple'), # fewer participants than one group ((9, 9, 0, 10, 10), 'n_blocks'), ((9, 9, 6, 0, 10), 'rounds_per_block'), ((9, 9, 7, 30, 10), str(MAX_TOTAL_ROUNDS)), # 210 rounds > 180 ((9, 9, 6, 10, -1), 'noise_range'), ]) def test_validate_rejects_bad_settings_naming_the_setting(args, setting): with pytest.raises(ValueError, match=setting): validate_session_params(*args) def test_build_session_data_reads_the_config_and_validates(): config = dict(group_size=7, n_blocks=2, rounds_per_block=3, noise_range=10, seed=3) data = build_session_data(config, 14) assert data['params']['n_groups'] == 2 and data['params']['total_rounds'] == 6 with pytest.raises(ValueError, match='multiple'): build_session_data(config, 15) # ── round_values / others_ctr_points ────────────────────────────────────────── def test_round_values_maps_round_numbers_to_blocks(): data = _small(rounds=4) assert round_values(data, 0, 0, 1)[:2] == (0, 0) assert round_values(data, 0, 0, 4)[:2] == (0, 3) assert round_values(data, 0, 0, 5)[:2] == (1, 0) assert round_values(data, 1, 3, 12)[:2] == (2, 3) def test_round_values_combines_center_noise_and_policies(): data = _small() block, rib, ctr, ideal, pol_a, pol_b = round_values(data, 1, 2, 6) # block 1, round 1 assert (block, rib) == (1, 1) assert ctr == data['ctr_points'][1][1][2] assert ideal == ctr + data['noise_vals'][1][1][1][2] assert [pol_a, pol_b] == data['policies'][1][1][1] def test_others_ctr_points_excludes_only_the_player(): data = _small() others = others_ctr_points(data, 0, 3, 1) mine = data['ctr_points'][0][1][3] assert len(others) == 6 assert sorted(others + [mine]) == make_ctr_values(7) # ── instruction_example ─────────────────────────────────────────────────────── def test_instruction_example_for_nine_matches_the_old_hard_coded_picture(): ex = instruction_example(9, 10) assert (ex['ex_ctr'], ex['ex_lo'], ex['ex_hi']) == (50, 40, 60) assert (ex['ex_other_ctr'], ex['ex_other_lo'], ex['ex_other_hi']) == (30, 20, 40) assert ex['ex_best'] == 47 assert json.loads(ex['ex_unlabeled_json']) == [10, 20, 40, 60, 70, 80, 90] assert json.loads(ex['ex_others_json']) == [10, 20, 30, 40, 60, 70, 80, 90] @pytest.mark.parametrize('size', range(7, 14)) @pytest.mark.parametrize('noise', [0, 2, 10]) def test_instruction_example_is_consistent_for_every_size(size, noise): ex = instruction_example(size, noise) vals = make_ctr_values(size) others = json.loads(ex['ex_others_json']) unlabeled = json.loads(ex['ex_unlabeled_json']) assert ex['ex_ctr'] in vals and ex['ex_ctr'] not in others assert sorted(others + [ex['ex_ctr']]) == vals assert ex['ex_other_ctr'] in others and ex['ex_other_ctr'] not in unlabeled assert sorted(unlabeled + [ex['ex_other_ctr']]) == sorted(others) assert ex['ex_hi'] - ex['ex_ctr'] == noise == ex['ex_ctr'] - ex['ex_lo'] assert ex['ex_lo'] <= ex['ex_best'] <= ex['ex_hi'] # ── team colors ─────────────────────────────────────────────────────────────── def test_team_color_splits_at_fifty(): assert team_color(10) == team_color(50) == TEAM_COLOR_LOW == '#009E73' # green: 50 or below assert team_color(51) == team_color(90) == TEAM_COLOR_HIGH == '#E69F00' # orange: above 50 def test_color_vars_off_keeps_the_original_neutral_look(): cv = color_vars(30, team_colors=False) assert cv['team_colors'] is False assert cv['color_low'] == cv['color_high'] == '#999999' # others stay gray assert (cv['c_ctr'], cv['c_best'], cv['c_dot']) == ('#1a6b1a', '#29ab2b', '#000000') def test_color_vars_on_colors_the_player_and_the_others_by_team(): low, high = color_vars(30, True), color_vars(70, True) assert low['team_colors'] is True assert (low['color_low'], low['color_high'], low['team_split']) == ('#009E73', '#E69F00', 50) assert low['c_ctr'] == low['c_best'] == low['c_dot'] == '#009E73' assert high['c_ctr'] == high['c_best'] == high['c_dot'] == '#E69F00' def test_instruction_example_reports_the_example_team_colors(): ex = instruction_example(9, 10, team_colors=True) assert ex['ex_other_color'] == '#009E73' # the example other center is 30 assert ex['ex_ctr'] == 50 and ex['c_ctr'] == '#009E73' # 50 is on the green side plain = instruction_example(9, 10) assert plain['ex_other_color'] == '#999999' and plain['c_ctr'] == '#1a6b1a'