test_printer_offline_notification.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. nozzles=[],
  45. raw_data={},
  46. stg_cur=0,
  47. # Real PrinterState always carries these; the status-broadcast dedup
  48. # key reads them so a Filament Track Switch rebind reaches the card.
  49. fila_switch=None,
  50. ams_switch_inlet={},
  51. extruder_slots={},
  52. cooling_fan_speed=0,
  53. big_fan1_speed=0,
  54. big_fan2_speed=0,
  55. chamber_light="",
  56. active_extruder=0,
  57. tray_now=0,
  58. door_open=False,
  59. subtask_name="",
  60. ams_filament_backup=None,
  61. )
  62. @pytest.fixture(autouse=True)
  63. def _reset_edge_state():
  64. """Clear the module-level edge dicts between tests so one test's
  65. True-edge doesn't leak into the next."""
  66. main_module._printer_last_connected.clear()
  67. for task in list(main_module._printer_offline_notify_tasks.values()):
  68. if not task.done():
  69. task.cancel()
  70. main_module._printer_offline_notify_tasks.clear()
  71. main_module._printer_reconciled_since_connect.clear()
  72. main_module._last_status_broadcast.clear()
  73. yield
  74. main_module._printer_last_connected.clear()
  75. for task in list(main_module._printer_offline_notify_tasks.values()):
  76. if not task.done():
  77. task.cancel()
  78. main_module._printer_offline_notify_tasks.clear()
  79. class TestMaybeNotifyPrinterOffline:
  80. """The debounced background task — fires notification at the end of the
  81. window only if the printer is still offline."""
  82. @pytest.mark.asyncio
  83. async def test_fires_notification_when_still_offline_after_debounce(self):
  84. printer = SimpleNamespace(id=1, name="Workshop")
  85. scalar = MagicMock()
  86. scalar.scalar_one_or_none.return_value = printer
  87. db = AsyncMock()
  88. db.execute = AsyncMock(return_value=scalar)
  89. session_cm = MagicMock()
  90. session_cm.__aenter__ = AsyncMock(return_value=db)
  91. session_cm.__aexit__ = AsyncMock(return_value=False)
  92. with (
  93. patch("backend.app.main.asyncio.sleep", new=AsyncMock()),
  94. patch("backend.app.main.printer_manager") as mock_pm,
  95. patch("backend.app.main.async_session", return_value=session_cm),
  96. patch("backend.app.main.notification_service") as mock_notif,
  97. ):
  98. mock_pm.is_connected.return_value = False
  99. mock_notif.on_printer_offline = AsyncMock()
  100. await main_module._maybe_notify_printer_offline(printer_id=1)
  101. mock_notif.on_printer_offline.assert_awaited_once_with(1, "Workshop", db)
  102. @pytest.mark.asyncio
  103. async def test_does_not_fire_when_printer_reconnected_during_debounce(self):
  104. with (
  105. patch("backend.app.main.asyncio.sleep", new=AsyncMock()),
  106. patch("backend.app.main.printer_manager") as mock_pm,
  107. patch("backend.app.main.notification_service") as mock_notif,
  108. ):
  109. mock_pm.is_connected.return_value = True
  110. mock_notif.on_printer_offline = AsyncMock()
  111. await main_module._maybe_notify_printer_offline(printer_id=1)
  112. mock_notif.on_printer_offline.assert_not_awaited()
  113. @pytest.mark.asyncio
  114. async def test_does_not_fire_when_printer_missing_from_db(self):
  115. scalar = MagicMock()
  116. scalar.scalar_one_or_none.return_value = None
  117. db = AsyncMock()
  118. db.execute = AsyncMock(return_value=scalar)
  119. session_cm = MagicMock()
  120. session_cm.__aenter__ = AsyncMock(return_value=db)
  121. session_cm.__aexit__ = AsyncMock(return_value=False)
  122. with (
  123. patch("backend.app.main.asyncio.sleep", new=AsyncMock()),
  124. patch("backend.app.main.printer_manager") as mock_pm,
  125. patch("backend.app.main.async_session", return_value=session_cm),
  126. patch("backend.app.main.notification_service") as mock_notif,
  127. ):
  128. mock_pm.is_connected.return_value = False
  129. mock_notif.on_printer_offline = AsyncMock()
  130. await main_module._maybe_notify_printer_offline(printer_id=1)
  131. mock_notif.on_printer_offline.assert_not_awaited()
  132. @pytest.mark.asyncio
  133. async def test_clears_task_entry_after_run(self):
  134. with (
  135. patch("backend.app.main.asyncio.sleep", new=AsyncMock()),
  136. patch("backend.app.main.printer_manager") as mock_pm,
  137. ):
  138. mock_pm.is_connected.return_value = True # No notification path
  139. main_module._printer_offline_notify_tasks[1] = MagicMock()
  140. await main_module._maybe_notify_printer_offline(printer_id=1)
  141. assert 1 not in main_module._printer_offline_notify_tasks
  142. class TestOfflineEdgeDetection:
  143. """Edge detection inside `on_printer_status_change` — only the
  144. True → False transition schedules a task. Reconnects cancel pending
  145. tasks. Startup-with-disconnected does not fire."""
  146. @staticmethod
  147. def _patch_handler_deps():
  148. """Patch out the heavy side-effects of `on_printer_status_change`
  149. (MQTT relay, WebSocket broadcast, state serializer) so we can focus
  150. on edge state."""
  151. ws_mgr = MagicMock()
  152. ws_mgr.send_printer_status = AsyncMock()
  153. relay = MagicMock()
  154. relay.on_printer_status = AsyncMock()
  155. pm = MagicMock()
  156. pm.get_printer.return_value = None # Skip the relay payload branch.
  157. pm.get_model.return_value = ""
  158. return ws_mgr, relay, pm
  159. @pytest.mark.asyncio
  160. async def test_first_call_connected_does_not_schedule(self):
  161. ws_mgr, relay, pm = self._patch_handler_deps()
  162. with (
  163. patch("backend.app.main.ws_manager", ws_mgr),
  164. patch("backend.app.main.mqtt_relay", relay),
  165. patch("backend.app.main.printer_manager", pm),
  166. _spawn_patch(),
  167. patch("backend.app.main.printer_state_to_dict", return_value={}),
  168. ):
  169. await main_module.on_printer_status_change(1, _state(connected=True))
  170. assert 1 not in main_module._printer_offline_notify_tasks
  171. assert main_module._printer_last_connected[1] is True
  172. @pytest.mark.asyncio
  173. async def test_first_call_disconnected_does_not_schedule(self):
  174. """Startup with an already-offline printer must not fire — there's
  175. no prior True observation, so we have no edge to trigger on."""
  176. ws_mgr, relay, pm = self._patch_handler_deps()
  177. with (
  178. patch("backend.app.main.ws_manager", ws_mgr),
  179. patch("backend.app.main.mqtt_relay", relay),
  180. patch("backend.app.main.printer_manager", pm),
  181. _spawn_patch(),
  182. patch("backend.app.main.printer_state_to_dict", return_value={}),
  183. ):
  184. await main_module.on_printer_status_change(1, _state(connected=False))
  185. assert 1 not in main_module._printer_offline_notify_tasks
  186. assert main_module._printer_last_connected[1] is False
  187. @pytest.mark.asyncio
  188. async def test_connected_to_disconnected_schedules_task(self):
  189. ws_mgr, relay, pm = self._patch_handler_deps()
  190. with (
  191. patch("backend.app.main.ws_manager", ws_mgr),
  192. patch("backend.app.main.mqtt_relay", relay),
  193. patch("backend.app.main.printer_manager", pm),
  194. _spawn_patch(),
  195. patch("backend.app.main._maybe_notify_printer_offline", new=AsyncMock()),
  196. patch("backend.app.main.printer_state_to_dict", return_value={}),
  197. ):
  198. await main_module.on_printer_status_change(1, _state(connected=True))
  199. await main_module.on_printer_status_change(1, _state(connected=False))
  200. task = main_module._printer_offline_notify_tasks.get(1)
  201. assert task is not None
  202. task.cancel()
  203. @pytest.mark.asyncio
  204. async def test_reconnect_cancels_pending_task(self):
  205. ws_mgr, relay, pm = self._patch_handler_deps()
  206. with (
  207. patch("backend.app.main.ws_manager", ws_mgr),
  208. patch("backend.app.main.mqtt_relay", relay),
  209. patch("backend.app.main.printer_manager", pm),
  210. _spawn_patch(),
  211. patch("backend.app.main._maybe_notify_printer_offline", new=AsyncMock()),
  212. patch("backend.app.main.printer_state_to_dict", return_value={}),
  213. ):
  214. await main_module.on_printer_status_change(1, _state(connected=True))
  215. await main_module.on_printer_status_change(1, _state(connected=False))
  216. scheduled = main_module._printer_offline_notify_tasks.get(1)
  217. assert scheduled is not None
  218. await main_module.on_printer_status_change(1, _state(connected=True))
  219. # Yield so the cancellation propagates through the event loop.
  220. await asyncio.sleep(0)
  221. assert 1 not in main_module._printer_offline_notify_tasks
  222. assert scheduled.cancelled() or scheduled.done()
  223. @pytest.mark.asyncio
  224. async def test_repeated_disconnected_does_not_reschedule(self):
  225. """A second False observation while a task is already pending must
  226. not replace the in-flight task — otherwise the debounce clock
  227. resets on every status callback and the notification never fires."""
  228. ws_mgr, relay, pm = self._patch_handler_deps()
  229. with (
  230. patch("backend.app.main.ws_manager", ws_mgr),
  231. patch("backend.app.main.mqtt_relay", relay),
  232. patch("backend.app.main.printer_manager", pm),
  233. _spawn_patch(),
  234. patch("backend.app.main._maybe_notify_printer_offline", new=AsyncMock()),
  235. patch("backend.app.main.printer_state_to_dict", return_value={}),
  236. ):
  237. await main_module.on_printer_status_change(1, _state(connected=True))
  238. await main_module.on_printer_status_change(1, _state(connected=False))
  239. first_task = main_module._printer_offline_notify_tasks.get(1)
  240. await main_module.on_printer_status_change(1, _state(connected=False))
  241. second_task = main_module._printer_offline_notify_tasks.get(1)
  242. assert first_task is second_task
  243. if first_task is not None:
  244. first_task.cancel()
  245. class TestProgressMilestoneSessionHygiene:
  246. """The progress-milestone notification path must capture the camera
  247. snapshot WITHOUT holding a DB session (issue #2572): a ~15s RTSP grab
  248. across an open session pinned a pooled connection per milestone, per
  249. printer. The read (printer name) and the send (provider lookups) each get
  250. their own short session; the snapshot happens in between with none held."""
  251. @staticmethod
  252. def _printing_state(progress: int):
  253. st = _state(connected=True, state="RUNNING")
  254. st.progress = progress
  255. st.remaining_time = 30
  256. st.gcode_file = "benchy.gcode"
  257. return st
  258. @pytest.mark.asyncio
  259. async def test_milestone_captures_snapshot_outside_session_and_notifies(self):
  260. main_module._last_progress_milestone.clear()
  261. printer = SimpleNamespace(id=1, name="Workshop")
  262. db = AsyncMock()
  263. db.execute = AsyncMock(return_value=MagicMock(scalar_one_or_none=MagicMock(return_value=printer)))
  264. # Stateful session that tracks how many sessions are currently open.
  265. open_sessions = {"count": 0}
  266. class _SessionCM:
  267. async def __aenter__(self):
  268. open_sessions["count"] += 1
  269. return db
  270. async def __aexit__(self, *exc):
  271. open_sessions["count"] -= 1
  272. return False
  273. snap_calls = []
  274. async def _snap(printer_id, prn, _logger):
  275. # The whole point of the fix (#2572): the ~15s camera grab must NOT
  276. # run while a DB session is held. On the old code the snapshot sat
  277. # inside the milestone session, so this would be 1.
  278. assert open_sessions["count"] == 0, "camera snapshot ran while a DB session was held"
  279. snap_calls.append((printer_id, prn))
  280. return b"jpeg-bytes"
  281. ws_mgr = MagicMock()
  282. ws_mgr.send_printer_status = AsyncMock()
  283. relay = MagicMock()
  284. relay.on_printer_status = AsyncMock()
  285. pm = MagicMock()
  286. pm.get_printer.return_value = None
  287. pm.get_model.return_value = ""
  288. with (
  289. patch("backend.app.main.ws_manager", ws_mgr),
  290. patch("backend.app.main.mqtt_relay", relay),
  291. patch("backend.app.main.printer_manager", pm),
  292. _spawn_patch(),
  293. patch("backend.app.main.printer_state_to_dict", return_value={}),
  294. patch("backend.app.main.async_session", side_effect=lambda: _SessionCM()),
  295. patch("backend.app.main._capture_snapshot_for_notification", new=_snap),
  296. patch("backend.app.main.notification_service") as mock_notif,
  297. ):
  298. mock_notif.on_print_progress = AsyncMock()
  299. await main_module.on_printer_status_change(1, self._printing_state(25))
  300. # Snapshot ran (with the detached printer) and outside any session.
  301. assert snap_calls == [(1, printer)]
  302. # The notification fired carrying that image (send legitimately holds a session).
  303. mock_notif.on_print_progress.assert_awaited_once()
  304. assert mock_notif.on_print_progress.await_args.kwargs["image_data"] == b"jpeg-bytes"
  305. # Every session opened was also closed — none leaked past the handler.
  306. assert open_sessions["count"] == 0
  307. main_module._last_progress_milestone.clear()