test_scheduler_budget_reservation.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. """Budget-reservation lifecycle through the unified queue scheduler."""
  2. from contextlib import ExitStack
  3. from pathlib import Path
  4. from types import SimpleNamespace
  5. from unittest.mock import AsyncMock, MagicMock, patch
  6. import pytest
  7. from fastapi import HTTPException
  8. from sqlalchemy import func, select
  9. from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
  10. import backend.app.models # noqa: F401 - populate Base.metadata
  11. import backend.app.services.print_scheduler as scheduler_module
  12. from backend.app.core.database import Base
  13. from backend.app.models.archive import PrintArchive
  14. from backend.app.models.finance import BudgetReservation, CostCenter, UserWallet
  15. from backend.app.models.print_queue import PrintQueueItem
  16. from backend.app.models.printer import Printer
  17. from backend.app.models.settings import Settings
  18. from backend.app.models.user import User
  19. from backend.app.services.finance_budget import validate_print_budget
  20. from backend.app.services.print_scheduler import PrintScheduler
  21. from backend.tests._fixtures.background_tasks import discarding_spawn_patch
  22. pytestmark = pytest.mark.integration
  23. @pytest.fixture
  24. async def billing_dispatch_case(tmp_path):
  25. engine = create_async_engine("sqlite+aiosqlite:///:memory:")
  26. async with engine.begin() as conn:
  27. await conn.run_sync(Base.metadata.create_all)
  28. session_maker = async_sessionmaker(engine, expire_on_commit=False)
  29. base_dir = tmp_path / "billing-dispatch"
  30. archive_rel = Path("archives") / "job.3mf"
  31. archive_abs = base_dir / archive_rel
  32. archive_abs.parent.mkdir(parents=True)
  33. archive_abs.write_bytes(b"archive payload")
  34. async with session_maker() as db:
  35. db.add(Settings(key="billing_enabled", value="true"))
  36. user = User(username="scheduler-budget-admin", role="admin", is_active=True)
  37. cost_center = CostCenter(name="Scheduler Budget", is_active=True, monthly_budget=10.0)
  38. printer = Printer(
  39. name="Budget Printer",
  40. serial_number="BUDGET-SERIAL",
  41. ip_address="127.0.0.1",
  42. access_code="access-code",
  43. model="X1C",
  44. )
  45. db.add_all([user, cost_center, printer])
  46. await db.flush()
  47. archive = PrintArchive(
  48. printer_id=printer.id,
  49. filename="job.3mf",
  50. file_path=str(archive_rel),
  51. file_size=archive_abs.stat().st_size,
  52. status="completed",
  53. cost=4.0,
  54. created_by_id=user.id,
  55. cost_center_id=cost_center.id,
  56. )
  57. db.add(archive)
  58. await db.flush()
  59. item = PrintQueueItem(
  60. printer_id=printer.id,
  61. archive_id=archive.id,
  62. cost_center_id=cost_center.id,
  63. estimated_cost=4.0,
  64. created_by_id=user.id,
  65. status="pending",
  66. )
  67. db.add(item)
  68. await db.commit()
  69. ids = SimpleNamespace(
  70. user_id=user.id,
  71. cost_center_id=cost_center.id,
  72. printer_id=printer.id,
  73. archive_id=archive.id,
  74. item_id=item.id,
  75. )
  76. try:
  77. yield SimpleNamespace(session_maker=session_maker, base_dir=base_dir, ids=ids)
  78. finally:
  79. await engine.dispose()
  80. async def _dispatch(ctx, *, uploaded: bool = True, cancel_during_upload: bool = False):
  81. scheduler = PrintScheduler()
  82. start_print = MagicMock(return_value=True)
  83. async def upload(*_args, **_kwargs):
  84. if cancel_during_upload:
  85. async with ctx.session_maker() as other_db:
  86. item = await other_db.get(PrintQueueItem, ctx.ids.item_id)
  87. item.status = "cancelled"
  88. await other_db.commit()
  89. return uploaded
  90. patches = [
  91. patch.object(scheduler_module, "async_session", ctx.session_maker),
  92. patch.object(scheduler_module.settings, "base_dir", ctx.base_dir),
  93. patch("backend.app.services.print_scheduler.printer_manager.is_connected", MagicMock(return_value=True)),
  94. patch("backend.app.services.print_scheduler.printer_manager.get_status", MagicMock(return_value=None)),
  95. patch("backend.app.services.print_scheduler.printer_manager.start_print", start_print),
  96. patch("backend.app.services.print_scheduler.printer_manager.set_awaiting_plate_clear", MagicMock()),
  97. patch(
  98. "backend.app.services.print_scheduler.get_ftp_retry_settings",
  99. AsyncMock(return_value=(False, 0, 0, 1.0)),
  100. ),
  101. patch("backend.app.services.print_scheduler.delete_file_async", AsyncMock(return_value=True)),
  102. patch("backend.app.services.print_scheduler.upload_file_async", upload),
  103. patch("backend.app.services.print_scheduler.cache_3mf_download", MagicMock()),
  104. discarding_spawn_patch(),
  105. patch("backend.app.services.notification_service.notification_service.on_queue_job_started", AsyncMock()),
  106. patch("backend.app.services.notification_service.notification_service.on_queue_job_failed", AsyncMock()),
  107. patch("backend.app.services.mqtt_relay.mqtt_relay.on_queue_job_started", AsyncMock()),
  108. patch.object(scheduler, "_propagate_owner_to_printer_manager", AsyncMock()),
  109. patch.object(scheduler, "_power_off_if_needed", AsyncMock()),
  110. patch.object(scheduler, "_preheat_and_soak", AsyncMock()),
  111. ]
  112. with ExitStack() as stack:
  113. for patcher in patches:
  114. stack.enter_context(patcher)
  115. await scheduler._dispatch_one(ctx.ids.item_id)
  116. return start_print
  117. async def _reservation(ctx):
  118. async with ctx.session_maker() as db:
  119. return await db.scalar(
  120. select(BudgetReservation).where(
  121. BudgetReservation.source_type == "print_queue",
  122. BudgetReservation.source_id == ctx.ids.item_id,
  123. )
  124. )
  125. @pytest.mark.asyncio
  126. async def test_successful_scheduler_dispatch_keeps_one_active_reservation(billing_dispatch_case):
  127. start_print = await _dispatch(billing_dispatch_case)
  128. reservation = await _reservation(billing_dispatch_case)
  129. assert reservation is not None
  130. assert reservation.status == "active"
  131. assert reservation.amount == 4.0
  132. assert reservation.print_archive_id == billing_dispatch_case.ids.archive_id
  133. start_print.assert_called_once()
  134. async with billing_dispatch_case.session_maker() as db:
  135. item = await db.get(PrintQueueItem, billing_dispatch_case.ids.item_id)
  136. archive = await db.get(PrintArchive, billing_dispatch_case.ids.archive_id)
  137. assert item.billing_run_id is not None
  138. assert archive.billing_run_id == item.billing_run_id
  139. # The internal UUID is deliberately independent from Bambu's 31-bit
  140. # task/subtask identifier.
  141. assert len(item.billing_run_id) == 36
  142. # The printing queue row and its persisted reservation represent the same
  143. # €4 hold. A second €6 job must fit exactly; €6.01 must not.
  144. async with billing_dispatch_case.session_maker() as db:
  145. user = await db.get(User, billing_dispatch_case.ids.user_id)
  146. second = PrintQueueItem(
  147. printer_id=billing_dispatch_case.ids.printer_id,
  148. archive_id=billing_dispatch_case.ids.archive_id,
  149. cost_center_id=billing_dispatch_case.ids.cost_center_id,
  150. estimated_cost=6.0,
  151. created_by_id=user.id,
  152. status="pending",
  153. )
  154. db.add(second)
  155. await db.commit()
  156. await validate_print_budget(
  157. db,
  158. cost_center_id=second.cost_center_id,
  159. estimated_cost=6.0,
  160. current_user=user,
  161. exclude_queue_item_id=second.id,
  162. )
  163. with pytest.raises(HTTPException, match="exceeds available"):
  164. await validate_print_budget(
  165. db,
  166. cost_center_id=second.cost_center_id,
  167. estimated_cost=6.01,
  168. current_user=user,
  169. exclude_queue_item_id=second.id,
  170. )
  171. @pytest.mark.asyncio
  172. async def test_cost_center_without_budget_is_unlimited_regardless_of_wallet_balance(billing_dispatch_case):
  173. """Wallet balance is accounting data; only an explicit cost-center budget gates printing."""
  174. async with billing_dispatch_case.session_maker() as db:
  175. user = await db.get(User, billing_dispatch_case.ids.user_id)
  176. center = await db.get(CostCenter, billing_dispatch_case.ids.cost_center_id)
  177. center.monthly_budget = None
  178. center.total_budget = None
  179. wallet = UserWallet(user_id=user.id, balance=-100.0, currency="EUR")
  180. db.add(wallet)
  181. await db.commit()
  182. await validate_print_budget(
  183. db,
  184. cost_center_id=center.id,
  185. estimated_cost=1_000_000.0,
  186. current_user=user,
  187. )
  188. @pytest.mark.asyncio
  189. async def test_upload_failure_releases_scheduler_reservation(billing_dispatch_case):
  190. start_print = await _dispatch(billing_dispatch_case, uploaded=False)
  191. reservation = await _reservation(billing_dispatch_case)
  192. assert reservation is not None
  193. assert reservation.status == "released"
  194. assert reservation.released_at is not None
  195. start_print.assert_not_called()
  196. @pytest.mark.asyncio
  197. async def test_retried_dispatch_reuses_active_reservation(billing_dispatch_case):
  198. await _dispatch(billing_dispatch_case)
  199. # Simulate startup recovery after the process stopped with a persisted
  200. # reservation and the queue row was made dispatchable again.
  201. async with billing_dispatch_case.session_maker() as db:
  202. item = await db.get(PrintQueueItem, billing_dispatch_case.ids.item_id)
  203. item.status = "pending"
  204. item.started_at = None
  205. item.dispatching_at = None
  206. await db.commit()
  207. await _dispatch(billing_dispatch_case)
  208. async with billing_dispatch_case.session_maker() as db:
  209. reservations = (
  210. (
  211. await db.execute(
  212. select(BudgetReservation).where(
  213. BudgetReservation.source_type == "print_queue",
  214. BudgetReservation.source_id == billing_dispatch_case.ids.item_id,
  215. )
  216. )
  217. )
  218. .scalars()
  219. .all()
  220. )
  221. assert len(reservations) == 1
  222. assert reservations[0].status == "active"
  223. @pytest.mark.asyncio
  224. async def test_cancel_during_upload_releases_scheduler_reservation(billing_dispatch_case):
  225. start_print = await _dispatch(billing_dispatch_case, cancel_during_upload=True)
  226. reservation = await _reservation(billing_dispatch_case)
  227. assert reservation is not None
  228. assert reservation.status == "released"
  229. assert reservation.released_at is not None
  230. start_print.assert_not_called()
  231. async with billing_dispatch_case.session_maker() as db:
  232. item = await db.get(PrintQueueItem, billing_dispatch_case.ids.item_id)
  233. active_count = await db.scalar(
  234. select(func.count()).select_from(BudgetReservation).where(BudgetReservation.status == "active")
  235. )
  236. assert item.status == "cancelled"
  237. assert active_count == 0
  238. @pytest.mark.asyncio
  239. async def test_cleanup_session_does_not_rollback_failed_dispatch_status(billing_dispatch_case):
  240. scheduler = PrintScheduler()
  241. async def fail_after_reserving(db, item):
  242. db.add(
  243. BudgetReservation(
  244. cost_center_id=item.cost_center_id,
  245. amount=4.0,
  246. status="active",
  247. source_type="print_queue",
  248. source_id=item.id,
  249. print_archive_id=item.archive_id,
  250. )
  251. )
  252. await db.commit()
  253. scheduler._unconfirmed_budget_reservations.add(item.id)
  254. item.status = "failed"
  255. item.error_message = "dispatch failed after reservation"
  256. raise RuntimeError("simulated dispatch failure")
  257. with (
  258. patch.object(scheduler_module, "async_session", billing_dispatch_case.session_maker),
  259. patch.object(scheduler, "_start_print", fail_after_reserving),
  260. pytest.raises(RuntimeError, match="simulated dispatch failure"),
  261. ):
  262. await scheduler._dispatch_one(billing_dispatch_case.ids.item_id)
  263. async with billing_dispatch_case.session_maker() as db:
  264. item = await db.get(PrintQueueItem, billing_dispatch_case.ids.item_id)
  265. reservation = await db.scalar(
  266. select(BudgetReservation).where(
  267. BudgetReservation.source_type == "print_queue",
  268. BudgetReservation.source_id == billing_dispatch_case.ids.item_id,
  269. )
  270. )
  271. assert item.status == "failed"
  272. assert item.error_message == "dispatch failed after reservation"
  273. assert item.dispatching_at is None
  274. assert reservation.status == "released"