test_hms_errors.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """Tests for HMS error code translations."""
  2. import pytest
  3. from backend.app.services.hms_errors import HMS_ERROR_DESCRIPTIONS, get_error_description
  4. class TestHMSErrorDescriptions:
  5. """Tests for the HMS error descriptions dictionary."""
  6. def test_dictionary_is_not_empty(self):
  7. """Verify the error descriptions dictionary has entries."""
  8. assert len(HMS_ERROR_DESCRIPTIONS) > 0
  9. def test_dictionary_has_expected_count(self):
  10. """Verify we have the expected number of error codes."""
  11. # Should have 853 error codes from the frontend
  12. assert len(HMS_ERROR_DESCRIPTIONS) == 853
  13. def test_all_keys_are_valid_format(self):
  14. """Verify all keys follow the XXXX_YYYY format."""
  15. import re
  16. pattern = re.compile(r"^[0-9A-F]{4}_[0-9A-F]{4}$")
  17. for code in HMS_ERROR_DESCRIPTIONS:
  18. assert pattern.match(code), f"Invalid error code format: {code}"
  19. def test_all_values_are_non_empty_strings(self):
  20. """Verify all descriptions are non-empty strings."""
  21. for code, description in HMS_ERROR_DESCRIPTIONS.items():
  22. assert isinstance(description, str), f"Description for {code} is not a string"
  23. assert len(description) > 0, f"Description for {code} is empty"
  24. class TestGetErrorDescription:
  25. """Tests for the get_error_description function."""
  26. def test_returns_description_for_known_code(self):
  27. """Verify known error codes return their descriptions."""
  28. # 0300_400C = "The task was canceled."
  29. result = get_error_description("0300_400C")
  30. assert result == "The task was canceled."
  31. def test_returns_description_for_ams_error(self):
  32. """Verify AMS error codes return their descriptions."""
  33. # 0700_8010 = AMS assist motor overloaded
  34. result = get_error_description("0700_8010")
  35. assert "AMS assist motor" in result
  36. def test_returns_none_for_unknown_code(self):
  37. """Verify unknown error codes return None."""
  38. result = get_error_description("XXXX_YYYY")
  39. assert result is None
  40. def test_handles_lowercase_input(self):
  41. """Verify function handles lowercase input."""
  42. result = get_error_description("0300_400c")
  43. assert result == "The task was canceled."
  44. def test_handles_mixed_case_input(self):
  45. """Verify function handles mixed case input."""
  46. result = get_error_description("0300_400C")
  47. assert result == "The task was canceled."
  48. def test_common_error_codes_have_descriptions(self):
  49. """Verify common error codes have descriptions."""
  50. common_codes = [
  51. "0300_4000", # Z axis homing failed
  52. "0300_4006", # Nozzle clogged
  53. "0300_8004", # Filament ran out
  54. "0500_4001", # Failed to connect to Bambu Cloud
  55. "0700_8010", # AMS assist motor overloaded
  56. ]
  57. for code in common_codes:
  58. result = get_error_description(code)
  59. assert result is not None, f"Missing description for common code: {code}"