diff --git a/tests/backend/app/modelling/test_trigger_run_router.py b/tests/backend/app/modelling/test_trigger_run_router.py index ae9e020be..d383fae1d 100644 --- a/tests/backend/app/modelling/test_trigger_run_router.py +++ b/tests/backend/app/modelling/test_trigger_run_router.py @@ -83,6 +83,37 @@ def api(db_engine: Engine) -> Api: return harness +def _trigger(api: Api, task: Task, scenario_ids: list[int]) -> Any: + return api.client.post( + "/v1/modelling/trigger-run", + json={ + "task_id": str(task.id), + "portfolio_id": PORTFOLIO_ID, + "scenario_ids": scenario_ids, + "filters": {}, + }, + ) + + +def test_trigger_run_rejects_a_task_that_already_has_subtasks(api: Api) -> None: + """A blind retry or double-submit must not double the fan-out (ADR-0055): + the app checks the task's progress instead of re-POSTing.""" + # arrange — a task that has already been distributed + task = api.seed_task() + scenario = api.seed_scenario() + api.seed_property() + assert _trigger(api, task, [scenario]).status_code == 202 + already_sent = len(api.sent_bodies) + + # act + response = _trigger(api, task, [scenario]) + + # assert — refused, nothing new created or sent + assert response.status_code == 409 + assert len(api.sent_bodies) == already_sent + assert len(api.subtasks_for(task)) == 1 + + def test_trigger_run_fans_out_one_subtask_and_message_per_scenario_batch( api: Api, ) -> None: