test_pause_warning_2984.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """The pause heads-up: its channel toggle, its setting, and the time on the status (#2984)."""
  2. from unittest.mock import MagicMock, patch
  3. import pytest
  4. from httpx import AsyncClient
  5. @pytest.mark.asyncio
  6. @pytest.mark.integration
  7. async def test_channel_toggle_saves_on_create_and_update(async_client: AsyncClient):
  8. data = {
  9. "name": "Pause heads-up",
  10. "provider_type": "ntfy",
  11. "config": {"server": "https://ntfy.sh", "topic": "test"},
  12. "on_print_pause_upcoming": True,
  13. }
  14. created = await async_client.post("/api/v1/notifications/", json=data)
  15. assert created.status_code == 200
  16. assert created.json()["on_print_pause_upcoming"] is True
  17. provider_id = created.json()["id"]
  18. updated = await async_client.patch(f"/api/v1/notifications/{provider_id}", json={"on_print_pause_upcoming": False})
  19. assert updated.status_code == 200
  20. assert updated.json()["on_print_pause_upcoming"] is False
  21. listed = await async_client.get("/api/v1/notifications/")
  22. assert next(p for p in listed.json() if p["id"] == provider_id)["on_print_pause_upcoming"] is False
  23. @pytest.mark.asyncio
  24. @pytest.mark.integration
  25. async def test_channel_toggle_off_by_default(async_client: AsyncClient):
  26. data = {"name": "Plain", "provider_type": "ntfy", "config": {"server": "https://ntfy.sh", "topic": "t"}}
  27. created = await async_client.post("/api/v1/notifications/", json=data)
  28. assert created.json()["on_print_pause_upcoming"] is False
  29. @pytest.mark.asyncio
  30. @pytest.mark.integration
  31. async def test_heads_up_minutes_setting(async_client: AsyncClient):
  32. assert (await async_client.get("/api/v1/settings")).json()["pause_warning_minutes"] == 5
  33. saved = await async_client.patch("/api/v1/settings", json={"pause_warning_minutes": 15})
  34. assert saved.status_code == 200
  35. assert (await async_client.get("/api/v1/settings")).json()["pause_warning_minutes"] == 15
  36. for bad in (0, 121):
  37. assert (await async_client.patch("/api/v1/settings", json={"pause_warning_minutes": bad})).status_code == 422
  38. @pytest.mark.asyncio
  39. @pytest.mark.integration
  40. async def test_heads_up_reads_the_saved_setting(async_client: AsyncClient, db_session):
  41. from backend.app.services.pause_eta import warning_minutes
  42. await async_client.patch("/api/v1/settings", json={"pause_warning_minutes": 20})
  43. assert await warning_minutes(db_session) == 20
  44. @pytest.mark.asyncio
  45. @pytest.mark.integration
  46. async def test_status_reports_the_next_pause(async_client: AsyncClient, printer_factory):
  47. from backend.app.services.bambu_mqtt import PrinterState
  48. from backend.app.utils.threemf_tools import PausePoint
  49. printer = await printer_factory()
  50. state = PrinterState()
  51. state.connected = True
  52. state.state = "RUNNING"
  53. state.layer_num = 1
  54. state.remaining_time = 110
  55. state.pause_points = [PausePoint(3, 78)]
  56. with patch("backend.app.api.routes.printers.printer_manager") as mock_pm:
  57. mock_pm.get_status = MagicMock(return_value=state)
  58. mock_pm.is_awaiting_plate_clear = MagicMock(return_value=False)
  59. response = await async_client.get(f"/api/v1/printers/{printer.id}/status")
  60. assert response.status_code == 200
  61. assert response.json()["next_pause"] == {"layer": 3, "minutes": 32}
  62. @pytest.mark.asyncio
  63. @pytest.mark.integration
  64. async def test_an_unparseable_stored_value_reads_back_as_the_default(async_client: AsyncClient, db_session):
  65. from backend.app.models.settings import Settings
  66. db_session.add(Settings(key="pause_warning_minutes", value="None"))
  67. await db_session.commit()
  68. response = await async_client.get("/api/v1/settings")
  69. assert response.status_code == 200
  70. assert response.json()["pause_warning_minutes"] == 5