def test_load_scenario(tmpdir):

    data_dir = Path(tmpdir)
    Settings.DATA_DIR = data_dir

    mock_sim_controls = mock.Mock()
    mock_aircraft_controls = mock.Mock()
    proxy_simulator_controls = ProxySimulatorControls(mock_sim_controls,
                                                      mock_aircraft_controls)

    # Test error when no sector set

    res = proxy_simulator_controls.load_scenario(_TEST_SCENARIO)
    assert res == "No sector set"

    # First test uploading a new scenario

    # Test error handling from reset

    mock_sim_controls.reset.return_value = "Sim error (reset)"
    err = proxy_simulator_controls.load_sector(_TEST_SECTOR)
    assert err == "Sim error (reset)"

    # Test error handling from load_scenario

    proxy_simulator_controls.sector = _TEST_SECTOR
    mock_sim_controls.reset.return_value = None
    mock_sim_controls.load_scenario.return_value = "Sim error (load_scenario)"
    err = proxy_simulator_controls.load_scenario(_TEST_SCENARIO)
    assert err == "Sim error (load_scenario)"

    # Test valid response

    mock_sim_controls.load_scenario.return_value = None
    err = proxy_simulator_controls.load_scenario(_TEST_SCENARIO)
    assert not err

    # Assert scenario saved to file

    with open(data_dir / "scenarios" / f"{_TEST_SCENARIO.name}.json") as f:
        assert json.load(f) == _TEST_SCENARIO.content

    # Test load of existing scenario by name

    existing_scn = Scenario(_TEST_SCENARIO.name, None)
    err = proxy_simulator_controls.load_scenario(existing_scn)
    assert not err
def test_store_data(tmpdir):

    mock_sim_controls = mock.create_autospec(spec=AbstractSimulatorControls)
    mock_aircraft_controls = mock.create_autospec(spec=ProxyAircraftControls)
    proxy_simulator_controls = ProxySimulatorControls(mock_sim_controls,
                                                      mock_aircraft_controls)

    # Set the scenario and sector

    proxy_simulator_controls.sector = _TEST_SECTOR
    mock_sim_controls.reset.return_value = None
    mock_sim_controls.load_scenario.return_value = None
    err = proxy_simulator_controls.load_scenario(_TEST_SCENARIO)
    assert not err

    # Assert exceptions are caught

    Settings.DATA_DIR = "fake"
    err = proxy_simulator_controls.store_data()
    assert err.startswith("Error storing data: ")

    # Assert .last_sector created

    data_dir = Path(tmpdir)
    Settings.DATA_DIR = data_dir

    err = proxy_simulator_controls.store_data()
    assert not err

    with open(tmpdir / "sectors" / ".last_sector") as f:
        assert f.read() == "test-sector\n"

    # Assert .last_scenario created

    with open(tmpdir / "scenarios" / ".last_scenario") as f:
        assert f.read() == "test-scenario\n"