test_printer_offline_notification.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. """Tests for the connected → disconnected edge that fires the
  2. `on_printer_offline` notification (#1752).
  3. The provider toggle, schema, and dispatcher already existed; what was missing
  4. was a caller that fires `notification_service.on_printer_offline` when a
  5. printer goes offline. These tests pin both layers:
  6. * `_maybe_notify_printer_offline` — the debounced background task. Must
  7. fire when the printer is still offline at the end of the window, and
  8. must NOT fire if the printer reconnected during the window.
  9. * Edge detection inside `on_printer_status_change` — schedules the task
  10. only on the True → False transition, cancels any pending task on
  11. reconnect, and stays silent on startup (no prior connected state).
  12. """
  13. import asyncio
  14. from types import SimpleNamespace
  15. from unittest.mock import AsyncMock, MagicMock, patch
  16. import pytest
  17. from backend.app import main as main_module
  18. from backend.tests._fixtures.background_tasks import MAIN_TARGET, discarding_spawn_patch
  19. def _spawn_patch():
  20. """Patch `spawn_background_task` so the coroutine handed to it is closed.
  21. `on_printer_status_change` builds `reconcile_stale_active_prints(...)` as
  22. a call argument, so the coroutine object is constructed whether or not the
  23. replacement schedules it — see
  24. `backend/tests/_fixtures/background_tasks.py` for what parking it in a
  25. mock instead does to an unrelated test.
  26. """
  27. return discarding_spawn_patch(MAIN_TARGET)
  28. def _state(connected: bool, state: str = "IDLE") -> SimpleNamespace:
  29. """Minimal PrinterState stub.
  30. `state="IDLE"` is a *known* state, so on `connected=True` this does trip
  31. the reconcile-edge branch in `on_printer_status_change` — that is why
  32. every handler test patches the spawn helper via `_spawn_patch()`. These
  33. tests assert on the offline-notification edge only; reconciliation
  34. behaviour is pinned separately in
  35. `test_reconcile_stale_active_prints.py`. The remaining fields just let
  36. the handler thread through without extra DB / WS work.
  37. """
  38. return SimpleNamespace(
  39. connected=connected,
  40. state=state,
  41. progress=0,
  42. layer_num=0,
  43. temperatures={},
  44. raw_data={},
  45. stg_cur=0,
  46. cooling_fan_speed=0,
  47. big_fan1_speed=0,
  48. big_fan2_speed=0,
  49. chamber_light="",
  50. active_extruder=0,
  51. tray_now=0,
  52. door_open=False,
  53. subtask_name="",
  54. ams_filament_backup=None,
  55. )
  56. @pytest.fixture(autouse=True)
  57. def _reset_edge_state():
  58. """Clear the module-level edge dicts between tests so one test's
  59. True-edge doesn't leak into the next."""
  60. main_module._printer_last_connected.clear()
  61. for task in list(main_module._printer_offline_notify_tasks.values()):
  62. if not task.done():
  63. task.cancel()
  64. main_module._printer_offline_notify_tasks.clear()
  65. main_module._printer_reconciled_since_connect.clear()
  66. main_module._last_status_broadcast.clear()
  67. yield
  68. main_module._printer_last_connected.clear()
  69. for task in list(main_module._printer_offline_notify_tasks.values()):
  70. if not task.done():
  71. task.cancel()
  72. main_module._printer_offline_notify_tasks.clear()
  73. class TestMaybeNotifyPrinterOffline:
  74. """The debounced background task — fires notification at the end of the
  75. window only if the printer is still offline."""
  76. @pytest.mark.asyncio
  77. async def test_fires_notification_when_still_offline_after_debounce(self):
  78. printer = SimpleNamespace(id=1, name="Workshop")
  79. scalar = MagicMock()
  80. scalar.scalar_one_or_none.return_value = printer
  81. db = AsyncMock()
  82. db.execute = AsyncMock(return_value=scalar)
  83. session_cm = MagicMock()
  84. session_cm.__aenter__ = AsyncMock(return_value=db)
  85. session_cm.__aexit__ = AsyncMock(return_value=False)
  86. with (
  87. patch("backend.app.main.asyncio.sleep", new=AsyncMock()),
  88. patch("backend.app.main.printer_manager") as mock_pm,
  89. patch("backend.app.main.async_session", return_value=session_cm),
  90. patch("backend.app.main.notification_service") as mock_notif,
  91. ):
  92. mock_pm.is_connected.return_value = False
  93. mock_notif.on_printer_offline = AsyncMock()
  94. await main_module._maybe_notify_printer_offline(printer_id=1)
  95. mock_notif.on_printer_offline.assert_awaited_once_with(1, "Workshop", db)
  96. @pytest.mark.asyncio
  97. async def test_does_not_fire_when_printer_reconnected_during_debounce(self):
  98. with (
  99. patch("backend.app.main.asyncio.sleep", new=AsyncMock()),
  100. patch("backend.app.main.printer_manager") as mock_pm,
  101. patch("backend.app.main.notification_service") as mock_notif,
  102. ):
  103. mock_pm.is_connected.return_value = True
  104. mock_notif.on_printer_offline = AsyncMock()
  105. await main_module._maybe_notify_printer_offline(printer_id=1)
  106. mock_notif.on_printer_offline.assert_not_awaited()
  107. @pytest.mark.asyncio
  108. async def test_does_not_fire_when_printer_missing_from_db(self):
  109. scalar = MagicMock()
  110. scalar.scalar_one_or_none.return_value = None
  111. db = AsyncMock()
  112. db.execute = AsyncMock(return_value=scalar)
  113. session_cm = MagicMock()
  114. session_cm.__aenter__ = AsyncMock(return_value=db)
  115. session_cm.__aexit__ = AsyncMock(return_value=False)
  116. with (
  117. patch("backend.app.main.asyncio.sleep", new=AsyncMock()),
  118. patch("backend.app.main.printer_manager") as mock_pm,
  119. patch("backend.app.main.async_session", return_value=session_cm),
  120. patch("backend.app.main.notification_service") as mock_notif,
  121. ):
  122. mock_pm.is_connected.return_value = False
  123. mock_notif.on_printer_offline = AsyncMock()
  124. await main_module._maybe_notify_printer_offline(printer_id=1)
  125. mock_notif.on_printer_offline.assert_not_awaited()
  126. @pytest.mark.asyncio
  127. async def test_clears_task_entry_after_run(self):
  128. with (
  129. patch("backend.app.main.asyncio.sleep", new=AsyncMock()),
  130. patch("backend.app.main.printer_manager") as mock_pm,
  131. ):
  132. mock_pm.is_connected.return_value = True # No notification path
  133. main_module._printer_offline_notify_tasks[1] = MagicMock()
  134. await main_module._maybe_notify_printer_offline(printer_id=1)
  135. assert 1 not in main_module._printer_offline_notify_tasks
  136. class TestOfflineEdgeDetection:
  137. """Edge detection inside `on_printer_status_change` — only the
  138. True → False transition schedules a task. Reconnects cancel pending
  139. tasks. Startup-with-disconnected does not fire."""
  140. @staticmethod
  141. def _patch_handler_deps():
  142. """Patch out the heavy side-effects of `on_printer_status_change`
  143. (MQTT relay, WebSocket broadcast, state serializer) so we can focus
  144. on edge state."""
  145. ws_mgr = MagicMock()
  146. ws_mgr.send_printer_status = AsyncMock()
  147. relay = MagicMock()
  148. relay.on_printer_status = AsyncMock()
  149. pm = MagicMock()
  150. pm.get_printer.return_value = None # Skip the relay payload branch.
  151. pm.get_model.return_value = ""
  152. return ws_mgr, relay, pm
  153. @pytest.mark.asyncio
  154. async def test_first_call_connected_does_not_schedule(self):
  155. ws_mgr, relay, pm = self._patch_handler_deps()
  156. with (
  157. patch("backend.app.main.ws_manager", ws_mgr),
  158. patch("backend.app.main.mqtt_relay", relay),
  159. patch("backend.app.main.printer_manager", pm),
  160. _spawn_patch(),
  161. patch("backend.app.main.printer_state_to_dict", return_value={}),
  162. ):
  163. await main_module.on_printer_status_change(1, _state(connected=True))
  164. assert 1 not in main_module._printer_offline_notify_tasks
  165. assert main_module._printer_last_connected[1] is True
  166. @pytest.mark.asyncio
  167. async def test_first_call_disconnected_does_not_schedule(self):
  168. """Startup with an already-offline printer must not fire — there's
  169. no prior True observation, so we have no edge to trigger on."""
  170. ws_mgr, relay, pm = self._patch_handler_deps()
  171. with (
  172. patch("backend.app.main.ws_manager", ws_mgr),
  173. patch("backend.app.main.mqtt_relay", relay),
  174. patch("backend.app.main.printer_manager", pm),
  175. _spawn_patch(),
  176. patch("backend.app.main.printer_state_to_dict", return_value={}),
  177. ):
  178. await main_module.on_printer_status_change(1, _state(connected=False))
  179. assert 1 not in main_module._printer_offline_notify_tasks
  180. assert main_module._printer_last_connected[1] is False
  181. @pytest.mark.asyncio
  182. async def test_connected_to_disconnected_schedules_task(self):
  183. ws_mgr, relay, pm = self._patch_handler_deps()
  184. with (
  185. patch("backend.app.main.ws_manager", ws_mgr),
  186. patch("backend.app.main.mqtt_relay", relay),
  187. patch("backend.app.main.printer_manager", pm),
  188. _spawn_patch(),
  189. patch("backend.app.main._maybe_notify_printer_offline", new=AsyncMock()),
  190. patch("backend.app.main.printer_state_to_dict", return_value={}),
  191. ):
  192. await main_module.on_printer_status_change(1, _state(connected=True))
  193. await main_module.on_printer_status_change(1, _state(connected=False))
  194. task = main_module._printer_offline_notify_tasks.get(1)
  195. assert task is not None
  196. task.cancel()
  197. @pytest.mark.asyncio
  198. async def test_reconnect_cancels_pending_task(self):
  199. ws_mgr, relay, pm = self._patch_handler_deps()
  200. with (
  201. patch("backend.app.main.ws_manager", ws_mgr),
  202. patch("backend.app.main.mqtt_relay", relay),
  203. patch("backend.app.main.printer_manager", pm),
  204. _spawn_patch(),
  205. patch("backend.app.main._maybe_notify_printer_offline", new=AsyncMock()),
  206. patch("backend.app.main.printer_state_to_dict", return_value={}),
  207. ):
  208. await main_module.on_printer_status_change(1, _state(connected=True))
  209. await main_module.on_printer_status_change(1, _state(connected=False))
  210. scheduled = main_module._printer_offline_notify_tasks.get(1)
  211. assert scheduled is not None
  212. await main_module.on_printer_status_change(1, _state(connected=True))
  213. # Yield so the cancellation propagates through the event loop.
  214. await asyncio.sleep(0)
  215. assert 1 not in main_module._printer_offline_notify_tasks
  216. assert scheduled.cancelled() or scheduled.done()
  217. @pytest.mark.asyncio
  218. async def test_repeated_disconnected_does_not_reschedule(self):
  219. """A second False observation while a task is already pending must
  220. not replace the in-flight task — otherwise the debounce clock
  221. resets on every status callback and the notification never fires."""
  222. ws_mgr, relay, pm = self._patch_handler_deps()
  223. with (
  224. patch("backend.app.main.ws_manager", ws_mgr),
  225. patch("backend.app.main.mqtt_relay", relay),
  226. patch("backend.app.main.printer_manager", pm),
  227. _spawn_patch(),
  228. patch("backend.app.main._maybe_notify_printer_offline", new=AsyncMock()),
  229. patch("backend.app.main.printer_state_to_dict", return_value={}),
  230. ):
  231. await main_module.on_printer_status_change(1, _state(connected=True))
  232. await main_module.on_printer_status_change(1, _state(connected=False))
  233. first_task = main_module._printer_offline_notify_tasks.get(1)
  234. await main_module.on_printer_status_change(1, _state(connected=False))
  235. second_task = main_module._printer_offline_notify_tasks.get(1)
  236. assert first_task is second_task
  237. if first_task is not None:
  238. first_task.cancel()
  239. class TestProgressMilestoneSessionHygiene:
  240. """The progress-milestone notification path must capture the camera
  241. snapshot WITHOUT holding a DB session (issue #2572): a ~15s RTSP grab
  242. across an open session pinned a pooled connection per milestone, per
  243. printer. The read (printer name) and the send (provider lookups) each get
  244. their own short session; the snapshot happens in between with none held."""
  245. @staticmethod
  246. def _printing_state(progress: int):
  247. st = _state(connected=True, state="RUNNING")
  248. st.progress = progress
  249. st.remaining_time = 30
  250. st.gcode_file = "benchy.gcode"
  251. return st
  252. @pytest.mark.asyncio
  253. async def test_milestone_captures_snapshot_outside_session_and_notifies(self):
  254. main_module._last_progress_milestone.clear()
  255. printer = SimpleNamespace(id=1, name="Workshop")
  256. db = AsyncMock()
  257. db.execute = AsyncMock(return_value=MagicMock(scalar_one_or_none=MagicMock(return_value=printer)))
  258. # Stateful session that tracks how many sessions are currently open.
  259. open_sessions = {"count": 0}
  260. class _SessionCM:
  261. async def __aenter__(self):
  262. open_sessions["count"] += 1
  263. return db
  264. async def __aexit__(self, *exc):
  265. open_sessions["count"] -= 1
  266. return False
  267. snap_calls = []
  268. async def _snap(printer_id, prn, _logger):
  269. # The whole point of the fix (#2572): the ~15s camera grab must NOT
  270. # run while a DB session is held. On the old code the snapshot sat
  271. # inside the milestone session, so this would be 1.
  272. assert open_sessions["count"] == 0, "camera snapshot ran while a DB session was held"
  273. snap_calls.append((printer_id, prn))
  274. return b"jpeg-bytes"
  275. ws_mgr = MagicMock()
  276. ws_mgr.send_printer_status = AsyncMock()
  277. relay = MagicMock()
  278. relay.on_printer_status = AsyncMock()
  279. pm = MagicMock()
  280. pm.get_printer.return_value = None
  281. pm.get_model.return_value = ""
  282. with (
  283. patch("backend.app.main.ws_manager", ws_mgr),
  284. patch("backend.app.main.mqtt_relay", relay),
  285. patch("backend.app.main.printer_manager", pm),
  286. _spawn_patch(),
  287. patch("backend.app.main.printer_state_to_dict", return_value={}),
  288. patch("backend.app.main.async_session", side_effect=lambda: _SessionCM()),
  289. patch("backend.app.main._capture_snapshot_for_notification", new=_snap),
  290. patch("backend.app.main.notification_service") as mock_notif,
  291. ):
  292. mock_notif.on_print_progress = AsyncMock()
  293. await main_module.on_printer_status_change(1, self._printing_state(25))
  294. # Snapshot ran (with the detached printer) and outside any session.
  295. assert snap_calls == [(1, printer)]
  296. # The notification fired carrying that image (send legitimately holds a session).
  297. mock_notif.on_print_progress.assert_awaited_once()
  298. assert mock_notif.on_print_progress.await_args.kwargs["image_data"] == b"jpeg-bytes"
  299. # Every session opened was also closed — none leaked past the handler.
  300. assert open_sessions["count"] == 0
  301. main_module._last_progress_milestone.clear()