test_hms_error_summary.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """Tests for main._format_hms_error_summary — the helper that turns MQTT hms_errors
  2. into a human-readable PrintQueueItem.error_message on pre-print failures (#1111)."""
  3. def _format(hms_errors):
  4. from backend.app.main import _format_hms_error_summary
  5. return _format_hms_error_summary(hms_errors)
  6. def test_returns_none_for_empty_list():
  7. assert _format([]) is None
  8. assert _format(None or []) is None
  9. def test_formats_known_nozzle_mismatch_code():
  10. """0500_4038 is the nozzle-size-mismatch code from the HMS table — the common
  11. trigger for issue #1111."""
  12. summary = _format([{"code": "0x4038", "attr": 0x05000000, "module": 0x5, "severity": 1}])
  13. assert summary is not None
  14. assert "0500_4038" in summary
  15. assert "nozzle diameter" in summary.lower()
  16. def test_formats_unknown_code_as_bare_short_code():
  17. summary = _format([{"code": "0x9999", "attr": 0x99990000, "module": 0x99, "severity": 1}])
  18. assert summary == "[9999_9999]"
  19. def test_joins_multiple_errors_with_semicolons():
  20. summary = _format(
  21. [
  22. {"code": "0x4038", "attr": 0x05000000, "module": 0x5, "severity": 1},
  23. {"code": "0x9999", "attr": 0x99990000, "module": 0x99, "severity": 1},
  24. ]
  25. )
  26. assert summary is not None
  27. assert "; " in summary
  28. assert summary.count("[") == 2
  29. def test_tolerates_malformed_entry_and_skips_it():
  30. summary = _format(
  31. [
  32. {"code": "not-hex", "attr": "also-not-int"},
  33. {"code": "0x4038", "attr": 0x05000000, "module": 0x5, "severity": 1},
  34. ]
  35. )
  36. assert summary is not None
  37. assert "0500_4038" in summary
  38. def test_all_malformed_returns_none():
  39. assert _format([{"code": "not-hex", "attr": "also-not-int"}]) is None
  40. def test_masks_a_32_bit_code_into_a_four_digit_label():
  41. """An `hms[]` entry's code carries the alert level in its high 16 bits. The
  42. label used to be formatted from the unmasked value, producing "0500_3000A" —
  43. five digits in a group that has four, so it matched no catalogue key and was
  44. not a code anyone could look up either."""
  45. summary = _format([{"code": "0x3000a", "attr": 0x05000200, "module": 5, "severity": 2}])
  46. assert summary == "[0500_000A]"
  47. def test_masking_lets_a_32_bit_code_resolve_its_description():
  48. """0500_4038 is the nozzle-size mismatch. Arriving as an `hms[]` entry with
  49. an alert-level group, it went undescribed purely because of the formatting
  50. above; now it reads the same as when it arrives via print_error."""
  51. summary = _format([{"code": "0x00024038", "attr": 0x05000200, "module": 5, "severity": 2}])
  52. assert summary is not None
  53. assert summary.startswith("[0500_4038] ")
  54. assert "nozzle diameter" in summary.lower()
  55. def test_accepts_an_integer_code():
  56. """`_hms_short_code` takes both shapes; the raw MQTT payload carries ints."""
  57. assert _format([{"code": 0x4038, "attr": 0x05000000, "module": 5, "severity": 1}]).startswith("[0500_4038] ")