test_hms_errors.py 3.0 KB

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