def test_selection_logic(): # Helper to test logic def get_selected_version_index(versions): found_best = False version_index = -1 for i, v in enumerate(versions, start=0): if v.get("best", False): version_index = i found_best = True break if not found_best: # Default to the last one if not found version_index = len(versions) - 1 return version_index # Case 1: No versions # Case 2: Versions, none best -> Last one versions_none_best = [ {"title": "v1"}, {"title": "v2"} ] assert get_selected_version_index(versions_none_best) == 1, "Should select last one (index 1)" # Case 3: Versions, first is best -> First one versions_first_best = [ {"title": "v1", "best": True}, {"title": "v2"} ] assert get_selected_version_index(versions_first_best) == 0, "Should select best one (index 0)" # Case 4: Versions, second is best -> Second one versions_second_best = [ {"title": "v1"}, {"title": "v2", "best": True}, {"title": "v3"} ] assert get_selected_version_index(versions_second_best) == 1, "Should select best one (index 1)" print("All tests passed!") if __name__ == "__main__": test_selection_logic()