Files
bambuddy/backend/tests/unit/services/test_archive_display_stem.py
T
maziggy 01a7e6ee93 fix(archive,vp): strip .gcode.3mf properly + sync review/archive name (#1152)
@smandon retested the original #1152 fix on the latest daily and surfaced
  two distinct holes:

  1. ``Path(name).stem`` only strips the *last* suffix, so Bambu Studio's
     default ``Plate_1.gcode.3mf`` exports landed in the archive UI as
     ``Plate_1.gcode`` — never the bare ``Plate_1`` the user expected.

  2. The pending-uploads review card always showed the raw FTP filename,
     while the eventual ``PrintArchive.print_name`` resolved from the 3MF's
     embedded title (or, with the toggle on ``filename``, the stripped stem).
     Net effect: same upload showed two different names depending on which
     view you were looking at, with no way for the toggle to flip both
     views in lockstep.

  Three changes:

  - ``resolve_display_stem`` helper in ``services/archive.py`` strips
    ``.gcode.3mf`` / ``.3mf`` / ``.gcode`` (case-insensitive). Applied at
    the archive-creation site so ``Plate_1.gcode.3mf`` → ``Plate_1`` for
    every flow that produces a ``PrintArchive`` row.

  - ``PendingUpload.metadata_print_name`` (new nullable column) is
    populated at FTP-receive time by peeking at the 3MF's embedded title
    via the existing ``ThreeMFParser``. Read happens once per upload —
    the list endpoint then doesn't have to reopen each 3MF on every
    render. Parser failures are swallowed and the column stays NULL;
    the response model gracefully falls back to the stripped filename.

  - ``PendingUploadResponse.display_name`` is a computed field that
    mirrors ``archive_print``'s exact precedence — ``filename`` toggle
    → stripped stem; ``metadata`` toggle (default) → cached title or
    stripped stem. The frontend's review card reads it (with
    ``upload.filename`` as a defensive fallback) and surfaces the raw
    FTP filename via tooltip so users can still inspect what arrived.

  Migration is one idempotent ``ALTER TABLE pending_uploads ADD COLUMN
  metadata_print_name VARCHAR(255)`` (Postgres/SQLite-safe). Pre-migration
  rows have NULL and degrade to filename-stem behaviour without any
  operator action.

  Tests: 14 unit tests in ``test_archive_display_stem.py`` covering the
  canonical normalisation rules (Bambu Studio default name, mixed case,
  dots-in-the-middle, edge cases like ``.gcode.3mf``-only, full-path
  inputs); 6 integration tests in ``test_pending_upload_display_name.py``
  pinning the response contract (default toggle uses metadata title when
  present, falls back to stripped stem when absent, ``filename`` toggle
  overrides metadata, ``filename`` toggle still strips the double suffix,
  ``GET /{id}`` exposes the same field, whitespace-only metadata behaves
  like absent); 3 frontend tests in ``PendingUploadsPanel.test.tsx``
  pinning the review card's render path (resolved name shown, fallback
  to filename when display_name is empty, raw filename available via
  tooltip). Full backend suite: 3598 passed; frontend build clean; no
  regressions in any flow that previously processed ``.3mf`` /
  ``.gcode`` / non-3D filenames.
2026-05-01 12:34:56 +02:00

52 lines
2.3 KiB
Python

"""Tests for resolve_display_stem — Bambu Studio filename normalisation (#1152).
Bambu Studio's "Send to printer" dialog typically writes ``Plate_1.gcode.3mf``
(a sliced gcode payload wrapped in a 3MF container). ``Path(name).stem`` only
strips the last suffix and leaves ``Plate_1.gcode``, which then surfaces in
the archive UI as a confusing ``Plate_1.gcode`` rather than ``Plate_1``.
Pin the canonicalisation rules so a future refactor can't silently regress
this path. We don't need a dedicated test for ``archive_print``'s consumption
of the helper — the existing test suite covers that flow end-to-end via the
integration tests and a behaviour change there would surface as a different
``archive.print_name`` value.
"""
import pytest
from backend.app.services.archive import resolve_display_stem
@pytest.mark.parametrize(
("filename", "expected"),
[
# The headline case: Bambu Studio's default name for a sliced 3MF.
("Plate_1.gcode.3mf", "Plate_1"),
# User-renamed file with the double-suffix pattern.
("MyAwesomeBenchy.gcode.3mf", "MyAwesomeBenchy"),
# Plain .3mf (already-clean export from Bambu Studio's Save As).
("Benchy.3mf", "Benchy"),
# Standalone gcode upload — rare but supported.
("standalone.gcode", "standalone"),
# Mixed-case suffix — many slicers / OSes preserve user-typed case.
("UPPERCASE.GCODE.3MF", "UPPERCASE"),
("mixed.GCode.3mf", "mixed"),
# Names that contain dots in the middle should keep them.
("my.cool.model.gcode.3mf", "my.cool.model"),
("v1.2.3-prototype.3mf", "v1.2.3-prototype"),
# No recognised suffix → fall through to Path.stem.
("Cura_export.zip", "Cura_export"),
("README.md", "README"),
# Edge: just the suffix with nothing in front. Strip honestly — the
# caller is responsible for sanity-checking empty stems.
(".gcode.3mf", ""),
(".3mf", ""),
# Path components must not leak in. The helper takes a filename, but
# callers occasionally pass a full path string.
("/some/dir/Plate_1.gcode.3mf", "Plate_1"),
("subdir/MyModel.3mf", "MyModel"),
],
)
def test_resolve_display_stem(filename: str, expected: str) -> None:
assert resolve_display_stem(filename) == expected