test_design_settings.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. """Tests for carrying a 3MF designer's process tweaks across a re-slice (#2622).
  2. The layout asserted here — ``different_settings_to_system`` being
  3. ``[process, *filaments, printer]`` — was verified against real BambuStudio files
  4. at 2, 3 and 4 filament slots before this was written. Getting the index wrong
  5. would carry ``machine_start_gcode`` from the designer's printer onto the user's,
  6. so the parser refuses any file whose array length disagrees with its own filament
  7. count rather than guessing.
  8. """
  9. import io
  10. import json
  11. import zipfile
  12. from backend.app.services.design_settings import (
  13. DesignOverride,
  14. apply_design_overrides,
  15. extract_design_process_overrides,
  16. is_printer_coupled,
  17. overrides_from_config,
  18. )
  19. def _config(**overrides) -> dict:
  20. """A minimal project_settings.config with two filament slots."""
  21. base = {
  22. "print_settings_id": "0.20mm Standard @BBL A1",
  23. "printer_settings_id": "Bambu Lab A1 0.4 nozzle",
  24. "filament_settings_id": ["Bambu PLA Basic @BBL A1", "Bambu PLA Matte @BBL A1"],
  25. "wall_loops": "5",
  26. "sparse_infill_density": "100%",
  27. "initial_layer_print_height": "0.1",
  28. "outer_wall_speed": "200",
  29. "machine_start_gcode": "G28 ; designer printer",
  30. "different_settings_to_system": [
  31. "wall_loops;sparse_infill_density;initial_layer_print_height;outer_wall_speed",
  32. "",
  33. "",
  34. "machine_start_gcode",
  35. ],
  36. }
  37. base.update(overrides)
  38. return base
  39. def _3mf(config: dict | None, *, include_config: bool = True) -> bytes:
  40. buffer = io.BytesIO()
  41. with zipfile.ZipFile(buffer, "w") as zf:
  42. zf.writestr("3D/3dmodel.model", "<model/>")
  43. if include_config:
  44. zf.writestr("Metadata/project_settings.config", json.dumps(config))
  45. return buffer.getvalue()
  46. class TestClassification:
  47. def test_geometry_and_quality_keys_are_portable(self):
  48. for key in (
  49. "wall_loops",
  50. "sparse_infill_density",
  51. "sparse_infill_pattern",
  52. "initial_layer_print_height",
  53. "layer_height",
  54. "enable_support",
  55. "brim_type",
  56. "seam_position",
  57. "ironing_type",
  58. ):
  59. assert is_printer_coupled(key) is False, key
  60. def test_kinematic_and_thermal_keys_are_printer_coupled(self):
  61. for key in (
  62. "outer_wall_speed",
  63. "inner_wall_speed",
  64. "internal_solid_infill_speed",
  65. "travel_acceleration",
  66. "default_acceleration",
  67. "default_jerk",
  68. "overhang_fan_speed",
  69. "nozzle_temperature",
  70. "prime_tower_max_speed",
  71. "prime_tower_width",
  72. "prime_tower_rib_wall",
  73. "prime_tower_infill_gap",
  74. "enable_prime_tower",
  75. "independent_support_layer_height",
  76. "precise_z_height",
  77. ):
  78. assert is_printer_coupled(key) is True, key
  79. class TestExtraction:
  80. def test_reads_the_process_slot_and_classifies_each_key(self):
  81. overrides = extract_design_process_overrides(_3mf(_config()))
  82. assert [o.key for o in overrides] == [
  83. "initial_layer_print_height",
  84. "outer_wall_speed",
  85. "sparse_infill_density",
  86. "wall_loops",
  87. ]
  88. by_key = {o.key: o for o in overrides}
  89. assert by_key["wall_loops"].value == "5"
  90. assert by_key["sparse_infill_density"].value == "100%"
  91. assert by_key["wall_loops"].printer_coupled is False
  92. assert by_key["outer_wall_speed"].printer_coupled is True
  93. def test_never_surfaces_the_printer_slot(self):
  94. # machine_start_gcode is listed in the printer entry, not the process
  95. # one. Carrying it would push the designer's start G-code onto another
  96. # machine — the exact failure the length check exists to prevent.
  97. overrides = extract_design_process_overrides(_3mf(_config()))
  98. assert "machine_start_gcode" not in {o.key for o in overrides}
  99. def test_rejects_an_array_whose_length_contradicts_the_filament_count(self):
  100. # Three entries for two filaments: the layout is not the one we know,
  101. # so index 0 might not be the process slot. Refuse rather than guess.
  102. cfg = _config(different_settings_to_system=["wall_loops", "", ""])
  103. assert extract_design_process_overrides(_3mf(cfg)) == []
  104. def test_skips_keys_absent_from_the_flattened_config(self):
  105. cfg = _config(different_settings_to_system=["wall_loops;renamed_in_a_later_slicer", "", "", ""])
  106. assert [o.key for o in extract_design_process_overrides(_3mf(cfg))] == ["wall_loops"]
  107. def test_empty_process_entry_yields_nothing(self):
  108. cfg = _config(different_settings_to_system=["", "", "", "machine_start_gcode"])
  109. assert extract_design_process_overrides(_3mf(cfg)) == []
  110. def test_files_without_the_field_yield_nothing(self):
  111. cfg = _config()
  112. del cfg["different_settings_to_system"]
  113. assert extract_design_process_overrides(_3mf(cfg)) == []
  114. def test_files_without_project_settings_yield_nothing(self):
  115. assert extract_design_process_overrides(_3mf(None, include_config=False)) == []
  116. def test_malformed_input_yields_nothing(self):
  117. assert extract_design_process_overrides(b"not a zip") == []
  118. assert overrides_from_config("not a dict") == []
  119. assert overrides_from_config({"different_settings_to_system": "not a list"}) == []
  120. def test_tolerates_a_missing_filament_list(self):
  121. # No filament_settings_id to cross-check against — index 0 is still the
  122. # documented process slot, so parse it rather than bailing out.
  123. cfg = _config()
  124. del cfg["filament_settings_id"]
  125. assert [o.key for o in extract_design_process_overrides(_3mf(cfg))] == [
  126. "initial_layer_print_height",
  127. "outer_wall_speed",
  128. "sparse_infill_density",
  129. "wall_loops",
  130. ]
  131. class TestApply:
  132. def _overrides(self) -> list[DesignOverride]:
  133. return extract_design_process_overrides(_3mf(_config()))
  134. def test_writes_only_the_selected_keys(self):
  135. process = json.dumps({"inherits": "0.20mm Standard @BBL X1C", "from": "system", "wall_loops": "2"})
  136. patched = json.loads(apply_design_overrides(process, self._overrides(), ["wall_loops"]))
  137. assert patched["wall_loops"] == "5"
  138. # Not selected — the picked preset's own value must survive.
  139. assert "sparse_infill_density" not in patched
  140. assert "outer_wall_speed" not in patched
  141. # The inherits stub is what makes the patch win over the flattened
  142. # parent inside the sidecar; it must not be disturbed.
  143. assert patched["inherits"] == "0.20mm Standard @BBL X1C"
  144. assert patched["from"] == "system"
  145. def test_a_key_the_source_never_flagged_is_ignored(self):
  146. process = json.dumps({"inherits": "x"})
  147. patched = json.loads(apply_design_overrides(process, self._overrides(), ["layer_height"]))
  148. assert "layer_height" not in patched
  149. def test_printer_coupled_keys_apply_when_explicitly_selected(self):
  150. process = json.dumps({"inherits": "x"})
  151. patched = json.loads(apply_design_overrides(process, self._overrides(), ["outer_wall_speed"]))
  152. assert patched["outer_wall_speed"] == "200"
  153. def test_no_selection_is_a_no_op(self):
  154. process = json.dumps({"inherits": "x"})
  155. assert apply_design_overrides(process, self._overrides(), []) == process
  156. def test_unparseable_process_json_is_returned_untouched(self):
  157. assert apply_design_overrides("{not json", self._overrides(), ["wall_loops"]) == "{not json"