test_pending_upload_display_name.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. """Integration tests for #1152 follow-up — pending-upload review card name
  2. matches the eventual archive's print_name.
  3. Before this change the review card always showed the raw FTP filename while
  4. the archive's ``print_name`` was resolved from the 3MF metadata title (or, with
  5. the toggle on ``filename``, the stripped stem). That gave users two different
  6. names for the same item — they'd see *Plate_1.gcode* in review and *Some
  7. Creator's Title* in the archive grid.
  8. These tests pin the new contract:
  9. - ``PendingUploadResponse.display_name`` mirrors what archive_print will
  10. eventually write to ``PrintArchive.print_name``.
  11. - The toggle (``virtual_printer_archive_name_source``) flips both views in
  12. lockstep — never one without the other.
  13. - Filename normalisation (``Plate_1.gcode.3mf`` → ``Plate_1``) is applied
  14. consistently regardless of the toggle.
  15. """
  16. import pytest
  17. from httpx import AsyncClient
  18. from sqlalchemy.ext.asyncio import AsyncSession
  19. from backend.app.models.pending_upload import PendingUpload
  20. from backend.app.models.settings import Settings
  21. async def _set_archive_name_source(db: AsyncSession, value: str) -> None:
  22. """Write the virtual_printer_archive_name_source setting directly."""
  23. db.add(Settings(key="virtual_printer_archive_name_source", value=value))
  24. await db.commit()
  25. async def _seed_pending(
  26. db: AsyncSession,
  27. *,
  28. filename: str,
  29. metadata_print_name: str | None = None,
  30. ) -> int:
  31. pending = PendingUpload(
  32. filename=filename,
  33. file_path=f"/tmp/{filename}",
  34. file_size=42,
  35. source_ip="192.168.1.50",
  36. status="pending",
  37. metadata_print_name=metadata_print_name,
  38. )
  39. db.add(pending)
  40. await db.commit()
  41. await db.refresh(pending)
  42. return pending.id
  43. class TestDisplayNameResolution:
  44. """``GET /pending-uploads/`` resolves ``display_name`` to the same value
  45. ``archive_print`` would store on the eventual ``PrintArchive``."""
  46. @pytest.mark.asyncio
  47. @pytest.mark.integration
  48. async def test_default_toggle_uses_metadata_title_when_present(
  49. self, async_client: AsyncClient, db_session: AsyncSession
  50. ):
  51. """Default toggle is ``metadata`` — review card shows the embedded
  52. title rather than the FTP filename, matching what the archived
  53. PrintArchive.print_name will end up being."""
  54. await _seed_pending(
  55. db_session,
  56. filename="Plate_1.gcode.3mf",
  57. metadata_print_name="Custom Cool Benchy",
  58. )
  59. resp = await async_client.get("/api/v1/pending-uploads/")
  60. assert resp.status_code == 200
  61. rows = resp.json()
  62. assert len(rows) == 1
  63. assert rows[0]["display_name"] == "Custom Cool Benchy"
  64. # filename is still surfaced separately so the user can see what
  65. # actually arrived over FTP if they want to (tooltip).
  66. assert rows[0]["filename"] == "Plate_1.gcode.3mf"
  67. @pytest.mark.asyncio
  68. @pytest.mark.integration
  69. async def test_default_toggle_falls_back_to_stripped_stem_when_no_metadata(
  70. self, async_client: AsyncClient, db_session: AsyncSession
  71. ):
  72. """No embedded title (Bambu Studio's default plate export) — both
  73. review and archive end up showing the stripped filename stem."""
  74. await _seed_pending(
  75. db_session,
  76. filename="Plate_1.gcode.3mf",
  77. metadata_print_name=None,
  78. )
  79. resp = await async_client.get("/api/v1/pending-uploads/")
  80. assert resp.status_code == 200
  81. # ``Plate_1`` — both ``.gcode`` and ``.3mf`` stripped (#1152
  82. # follow-up: ``Path.stem`` only strips the last suffix and would
  83. # leave ``Plate_1.gcode``).
  84. assert resp.json()[0]["display_name"] == "Plate_1"
  85. @pytest.mark.asyncio
  86. @pytest.mark.integration
  87. async def test_filename_toggle_overrides_metadata_title(self, async_client: AsyncClient, db_session: AsyncSession):
  88. """When the operator opts into ``filename`` (so a user-renamed
  89. Bambu Studio job surfaces its renamed-at-send filename), the embedded
  90. creator-baked title is ignored. Review must follow the same toggle
  91. as the archive."""
  92. await _set_archive_name_source(db_session, "filename")
  93. await _seed_pending(
  94. db_session,
  95. filename="MyRenamedJob.3mf",
  96. metadata_print_name="Original Creator Title",
  97. )
  98. resp = await async_client.get("/api/v1/pending-uploads/")
  99. assert resp.status_code == 200
  100. assert resp.json()[0]["display_name"] == "MyRenamedJob"
  101. @pytest.mark.asyncio
  102. @pytest.mark.integration
  103. async def test_filename_toggle_strips_double_suffix(self, async_client: AsyncClient, db_session: AsyncSession):
  104. """Filename mode also drops .gcode.3mf — same normalisation as the
  105. archive's print_name, so the names line up exactly."""
  106. await _set_archive_name_source(db_session, "filename")
  107. await _seed_pending(db_session, filename="Plate_4.gcode.3mf", metadata_print_name=None)
  108. resp = await async_client.get("/api/v1/pending-uploads/")
  109. assert resp.status_code == 200
  110. assert resp.json()[0]["display_name"] == "Plate_4"
  111. @pytest.mark.asyncio
  112. @pytest.mark.integration
  113. async def test_get_one_returns_display_name(self, async_client: AsyncClient, db_session: AsyncSession):
  114. """The single-resource endpoint surfaces display_name too — UIs that
  115. load a pending upload by id (e.g. detail modals) get the same name."""
  116. upload_id = await _seed_pending(
  117. db_session,
  118. filename="X.gcode.3mf",
  119. metadata_print_name="Deep Detail Bear",
  120. )
  121. resp = await async_client.get(f"/api/v1/pending-uploads/{upload_id}")
  122. assert resp.status_code == 200
  123. assert resp.json()["display_name"] == "Deep Detail Bear"
  124. @pytest.mark.asyncio
  125. @pytest.mark.integration
  126. async def test_blank_metadata_title_falls_back_to_stem(self, async_client: AsyncClient, db_session: AsyncSession):
  127. """A whitespace-only metadata title behaves like absent metadata —
  128. guards against 3MFs with broken/empty Title fields surfacing as a
  129. blank review card."""
  130. await _seed_pending(
  131. db_session,
  132. filename="empty-title.gcode.3mf",
  133. metadata_print_name=" ",
  134. )
  135. resp = await async_client.get("/api/v1/pending-uploads/")
  136. assert resp.status_code == 200
  137. assert resp.json()[0]["display_name"] == "empty-title"