def test_env_runner_load_state_no_file(mock_task, mock_agent):
    # Assign
    env_runner = EnvRunner(mock_task, mock_agent, max_iterations=10)
    env_runner.logger = mock.MagicMock()

    # Act
    env_runner.load_state(file_prefix='saved_state')

    # Assert
    env_runner.logger.warning.assert_called_once_with(
        "Couldn't load state. Forcing restart.")
    mock_agent.load_state.assert_not_called()
def test_env_runner_load_state(mock_task, mock_agent, mock_os):
    # Assign
    env_runner = EnvRunner(mock_task, mock_agent, max_iterations=10)
    mock_os.listdir.return_value = [
        'saved_state_e10.json', 'saved_state_e999.json', 'other.file'
    ]
    mocked_state = '{"episode": 10, "epsilon": 0.2, "score": 0.3, "average_score": -0.1}'

    # Act
    with mock.patch('builtins.open',
                    mock.mock_open(read_data=mocked_state)) as mock_file:
        env_runner.load_state(file_prefix='saved_state')
        mock_file.assert_called_once_with(
            f'{env_runner.state_dir}/saved_state_e999.json', 'r')

    # Assert
    mock_agent.load_state.assert_called_once()
    assert env_runner.episode == 10
    assert env_runner.epsilon == 0.2
    assert len(env_runner.all_scores) == 1
    assert env_runner.all_scores[0] == 0.3