@@ -267,3 +267,54 @@ def test_post_expedition_report(tmp_path):
267267 assert problem .message in content , (
268268 "Problem messages in report should match those of selected problems."
269269 )
270+
271+
272+ def test_instrument_problems_only_selected_when_instruments_present (tmp_path ):
273+ expedition = _make_simple_expedition (num_waypoints = 3 , no_instruments = True )
274+ instruments_in_expedition = expedition .get_instruments ()
275+ assert len (instruments_in_expedition ) == 0 , "Expedition should have no instruments"
276+
277+ simulator = ProblemSimulator (expedition , str (tmp_path ))
278+ problems = simulator .select_problems (
279+ instruments_in_expedition , difficulty_level = "hard"
280+ )
281+
282+ has_instrument_problems = any (
283+ isinstance (cls , InstrumentProblem ) for cls in problems ["problem_class" ]
284+ )
285+ assert not has_instrument_problems , (
286+ "Should not select instrument problems when no instruments are present"
287+ )
288+
289+
290+ def test_instrument_not_present_doesnt_select_instrument_problem (tmp_path ):
291+ expedition = _make_simple_expedition (num_waypoints = 3 , no_instruments = True )
292+
293+ # prescribe instruments at waypoints, for this test case each should only be present at one waypoint
294+ expedition .schedule .waypoints [0 ].instrument = [InstrumentType .CTD ]
295+ expedition .schedule .waypoints [1 ].instrument = [
296+ InstrumentType .ARGO_FLOAT ,
297+ InstrumentType .DRIFTER ,
298+ ]
299+
300+ instruments_in_expedition = expedition .get_instruments ()
301+ simulator = ProblemSimulator (expedition , str (tmp_path ))
302+
303+ # run many iterations of randomly selecting problems and check that if an instrument problem is selected, the associated instrument is actually present at the selected waypoint
304+ for _ in range (int (1e4 )):
305+ problems = simulator .select_problems (
306+ instruments_in_expedition , difficulty_level = "hard"
307+ )
308+
309+ for problem , wp_i in zip (
310+ problems ["problem_class" ], problems ["waypoint_i" ], strict = False
311+ ):
312+ if isinstance (problem , InstrumentProblem ):
313+ wp_instruments = expedition .schedule .waypoints [wp_i ].instrument
314+ assert problem .instrument_type in wp_instruments , (
315+ "Instrument problem should only be selected if the instrument is present at the selected waypoint"
316+ )
317+
318+ # any incompatible waypoint x instrument problem combinations should have been replaced by a general problem
319+ else :
320+ assert isinstance (problem , GeneralProblem )
0 commit comments