test_plate_filament_preset.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """Each plate filament carries the slicer preset it was sliced with (SN-C9).
  2. The plate's type is only "PLA"; the colour catalogue is keyed by product line
  3. ("PLA Matte"), where the same hex has another name. The queue page names a
  4. colour from these plate listings, so each needs the preset id (tray_info_idx)
  5. that names the line. All three listings are copies of one parser.
  6. """
  7. import io
  8. import zipfile
  9. from pathlib import Path
  10. from unittest.mock import AsyncMock, patch
  11. import pytest
  12. pytestmark = [pytest.mark.asyncio, pytest.mark.integration]
  13. def _sliced_3mf() -> bytes:
  14. buf = io.BytesIO()
  15. with zipfile.ZipFile(buf, "w") as zf:
  16. zf.writestr(
  17. "Metadata/slice_info.config",
  18. "<?xml version='1.0'?><config><plate><metadata key='index' value='1'/>"
  19. "<filament id='1' tray_info_idx='GFA01' type='PLA' color='#000000' used_g='5' used_m='1.6'/>"
  20. "<filament id='2' type='PLA' color='#FFFFFF' used_g='3' used_m='1'/>"
  21. "</plate></config>",
  22. )
  23. zf.writestr("Metadata/plate_1.gcode", "; plate 1")
  24. return buf.getvalue()
  25. def _presets(response) -> list[str]:
  26. assert response.status_code == 200, response.text
  27. return [f["tray_info_idx"] for f in response.json()["plates"][0]["filaments"]]
  28. @pytest.fixture
  29. def base_dir(monkeypatch, tmp_path) -> Path:
  30. from backend.app.core.config import settings
  31. monkeypatch.setattr(settings, "base_dir", tmp_path)
  32. return tmp_path
  33. async def test_archive_plates(async_client, archive_factory, printer_factory, base_dir):
  34. (base_dir / "matte.3mf").write_bytes(_sliced_3mf())
  35. printer = await printer_factory()
  36. archive = await archive_factory(printer.id, filename="matte.3mf", file_path="matte.3mf")
  37. response = await async_client.get(f"/api/v1/archives/{archive.id}/plates")
  38. assert _presets(response) == ["GFA01", ""]
  39. async def test_library_file_plates(async_client, db_session, base_dir):
  40. from backend.app.models.library import LibraryFile
  41. path = base_dir / "matte.3mf"
  42. path.write_bytes(_sliced_3mf())
  43. lib_file = LibraryFile(filename="matte.3mf", file_path=str(path), file_type="3mf", file_size=1)
  44. db_session.add(lib_file)
  45. await db_session.commit()
  46. response = await async_client.get(f"/api/v1/library/files/{lib_file.id}/plates")
  47. assert _presets(response) == ["GFA01", ""]
  48. async def test_printer_file_plates(async_client, printer_factory):
  49. printer = await printer_factory()
  50. with patch("backend.app.api.routes.printers._download_printer_file_bytes", AsyncMock(return_value=_sliced_3mf())):
  51. response = await async_client.get(
  52. f"/api/v1/printers/{printer.id}/files/plates", params={"path": "/cache/matte.3mf"}
  53. )
  54. assert _presets(response) == ["GFA01", ""]