test_print_start_time_3327.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. """An archive's started_at is when the print started, not when it was written (#3327).
  2. The archive waits for the 3MF, which the printer serves slowly while it starts:
  3. the reporter's H2C archived its print a minute after the start. Completion takes
  4. the print time from started_at, so that minute was missing from it. The fallback
  5. archive, written only after every download attempt failed, waited longer still.
  6. """
  7. from datetime import datetime, timezone
  8. import pytest
  9. from backend.app.services.archive import ArchiveService
  10. from backend.tests.unit.test_archive_print_plate import _three_mf, dirs, printer_id # noqa: F401
  11. from backend.tests.unit.test_print_start_no_second_download_3308 import ( # noqa: F401
  12. FILE,
  13. _isolated,
  14. _listed,
  15. _print_start,
  16. _served_on_second_try,
  17. _times_out_at_root,
  18. )
  19. def _timed(transfer):
  20. """*transfer*, noting when the first download attempt began."""
  21. began: list[datetime] = []
  22. async def timed(remote_path, local_path):
  23. began.append(datetime.now(timezone.utc))
  24. return await transfer(remote_path, local_path)
  25. return timed, began
  26. class TestOnPrintStart:
  27. @pytest.mark.asyncio
  28. async def test_the_archive_starts_when_the_print_was_seen(self):
  29. before = datetime.now(timezone.utc)
  30. transfer, began = _timed(_served_on_second_try(f"/cache/{FILE}"))
  31. run = await _print_start(transfer=transfer, listing=_listed(FILE))
  32. started_at = run.service.archive_print.await_args.kwargs["started_at"]
  33. assert before <= started_at <= began[0]
  34. @pytest.mark.asyncio
  35. async def test_so_does_the_fallback_archive(self):
  36. before = datetime.now(timezone.utc)
  37. transfer, began = _timed(_times_out_at_root)
  38. run = await _print_start(transfer=transfer, listing=_listed(FILE))
  39. assert before <= run.fallback.started_at <= began[0]
  40. class TestArchivePrint:
  41. @pytest.mark.asyncio
  42. async def test_a_given_start_is_kept(self, db_session, dirs, printer_id): # noqa: F811
  43. start = datetime(2026, 10, 15, 2, 48, 54, tzinfo=timezone.utc)
  44. archive = await ArchiveService(db_session).archive_print(
  45. printer_id, _three_mf(dirs / "p.3mf", [1]), print_data={"status": "printing"}, started_at=start
  46. )
  47. assert archive.started_at.replace(tzinfo=timezone.utc) == start
  48. @pytest.mark.asyncio
  49. async def test_without_one_it_is_now(self, db_session, dirs, printer_id): # noqa: F811
  50. before = datetime.now(timezone.utc)
  51. archive = await ArchiveService(db_session).archive_print(
  52. printer_id, _three_mf(dirs / "p.3mf", [1]), print_data={"status": "printing"}
  53. )
  54. assert archive.started_at.replace(tzinfo=timezone.utc) >= before.replace(microsecond=0)
  55. @pytest.mark.asyncio
  56. async def test_a_finished_archive_has_none(self, db_session, dirs, printer_id): # noqa: F811
  57. archive = await ArchiveService(db_session).archive_print(
  58. printer_id,
  59. _three_mf(dirs / "p.3mf", [1]),
  60. print_data={"status": "completed"},
  61. started_at=datetime(2026, 10, 15, tzinfo=timezone.utc),
  62. )
  63. assert archive.started_at is None