test_print_start_time_3327.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.core.config import settings
  10. from backend.app.services.archive import ArchiveService
  11. from backend.tests.unit.test_archive_print_plate import _three_mf, printer_id # noqa: F401
  12. from backend.tests.unit.test_print_start_no_second_download_3308 import ( # noqa: F401
  13. FILE,
  14. _isolated,
  15. _listed,
  16. _print_start,
  17. _served_on_second_try,
  18. _times_out_at_root,
  19. )
  20. @pytest.fixture
  21. def dirs(tmp_path, monkeypatch):
  22. """The data directory; the archive directory is the print-start harness's
  23. (``_isolated``), so it is patched once and put back once."""
  24. monkeypatch.setattr(settings, "base_dir", tmp_path)
  25. return tmp_path
  26. def _timed(transfer):
  27. """*transfer*, noting when the first download attempt began."""
  28. began: list[datetime] = []
  29. async def timed(remote_path, local_path):
  30. began.append(datetime.now(timezone.utc))
  31. return await transfer(remote_path, local_path)
  32. return timed, began
  33. class TestOnPrintStart:
  34. @pytest.mark.asyncio
  35. async def test_the_archive_starts_when_the_print_was_seen(self):
  36. before = datetime.now(timezone.utc)
  37. transfer, began = _timed(_served_on_second_try(f"/cache/{FILE}"))
  38. run = await _print_start(transfer=transfer, listing=_listed(FILE))
  39. started_at = run.service.archive_print.await_args.kwargs["started_at"]
  40. assert before <= started_at <= began[0]
  41. @pytest.mark.asyncio
  42. async def test_so_does_the_fallback_archive(self):
  43. before = datetime.now(timezone.utc)
  44. transfer, began = _timed(_times_out_at_root)
  45. run = await _print_start(transfer=transfer, listing=_listed(FILE))
  46. assert before <= run.fallback.started_at <= began[0]
  47. class TestArchivePrint:
  48. @pytest.mark.asyncio
  49. async def test_a_given_start_is_kept(self, db_session, dirs, printer_id): # noqa: F811
  50. start = datetime(2026, 10, 15, 2, 48, 54, tzinfo=timezone.utc)
  51. archive = await ArchiveService(db_session).archive_print(
  52. printer_id, _three_mf(dirs / "p.3mf", [1]), print_data={"status": "printing"}, started_at=start
  53. )
  54. assert archive.started_at.replace(tzinfo=timezone.utc) == start
  55. @pytest.mark.asyncio
  56. async def test_without_one_it_is_now(self, db_session, dirs, printer_id): # noqa: F811
  57. before = datetime.now(timezone.utc)
  58. archive = await ArchiveService(db_session).archive_print(
  59. printer_id, _three_mf(dirs / "p.3mf", [1]), print_data={"status": "printing"}
  60. )
  61. assert archive.started_at.replace(tzinfo=timezone.utc) >= before.replace(microsecond=0)
  62. @pytest.mark.asyncio
  63. async def test_a_finished_archive_has_none(self, db_session, dirs, printer_id): # noqa: F811
  64. archive = await ArchiveService(db_session).archive_print(
  65. printer_id,
  66. _three_mf(dirs / "p.3mf", [1]),
  67. print_data={"status": "completed"},
  68. started_at=datetime(2026, 10, 15, tzinfo=timezone.utc),
  69. )
  70. assert archive.started_at is None