test_led_effects.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env python3
  2. """Test script for per-pattern LED effect functionality."""
  3. import os
  4. import sys
  5. import json
  6. # Add the parent directory to the path
  7. sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
  8. from modules.core.cache_manager import (
  9. get_pattern_led_effect,
  10. set_pattern_led_effect,
  11. clear_pattern_led_effect,
  12. load_metadata_cache,
  13. save_metadata_cache
  14. )
  15. def test_led_effect_storage():
  16. """Test storing and retrieving LED effects for patterns."""
  17. print("Testing LED effect storage...")
  18. # Test pattern
  19. test_pattern = "test_pattern.thr"
  20. # Test effect settings
  21. test_effect = {
  22. "effect_id": 8,
  23. "palette_id": 0,
  24. "speed": 150,
  25. "intensity": 200,
  26. "color1": "#ff0000",
  27. "color2": "#00ff00",
  28. "color3": "#0000ff"
  29. }
  30. # Test 1: Set playing effect
  31. print("\n1. Setting playing effect for pattern...")
  32. success = set_pattern_led_effect(test_pattern, "playing", test_effect)
  33. print(f" Result: {'✓ Success' if success else '✗ Failed'}")
  34. # Test 2: Retrieve playing effect
  35. print("\n2. Retrieving playing effect for pattern...")
  36. retrieved_effect = get_pattern_led_effect(test_pattern, "playing")
  37. if retrieved_effect == test_effect:
  38. print(f" Result: ✓ Success - Effect matches")
  39. print(f" Retrieved: {json.dumps(retrieved_effect, indent=2)}")
  40. else:
  41. print(f" Result: ✗ Failed - Effect doesn't match")
  42. print(f" Expected: {json.dumps(test_effect, indent=2)}")
  43. print(f" Retrieved: {json.dumps(retrieved_effect, indent=2)}")
  44. # Test 3: Set idle effect
  45. test_idle_effect = {
  46. "effect_id": 0,
  47. "palette_id": 0,
  48. "speed": 128,
  49. "intensity": 128,
  50. "color1": "#ffffff",
  51. "color2": "#000000",
  52. "color3": "#0000ff"
  53. }
  54. print("\n3. Setting idle effect for pattern...")
  55. success = set_pattern_led_effect(test_pattern, "idle", test_idle_effect)
  56. print(f" Result: {'✓ Success' if success else '✗ Failed'}")
  57. # Test 4: Retrieve both effects
  58. print("\n4. Verifying both effects are stored...")
  59. playing = get_pattern_led_effect(test_pattern, "playing")
  60. idle = get_pattern_led_effect(test_pattern, "idle")
  61. if playing == test_effect and idle == test_idle_effect:
  62. print(f" Result: ✓ Success - Both effects stored correctly")
  63. else:
  64. print(f" Result: ✗ Failed - Effects don't match")
  65. # Test 5: Clear specific effect
  66. print("\n5. Clearing playing effect...")
  67. success = clear_pattern_led_effect(test_pattern, "playing")
  68. print(f" Result: {'✓ Success' if success else '✗ Failed'}")
  69. playing = get_pattern_led_effect(test_pattern, "playing")
  70. idle = get_pattern_led_effect(test_pattern, "idle")
  71. if playing is None and idle == test_idle_effect:
  72. print(f" Result: ✓ Success - Playing cleared, idle remains")
  73. else:
  74. print(f" Result: ✗ Failed")
  75. print(f" Playing (should be None): {playing}")
  76. print(f" Idle (should exist): {idle}")
  77. # Test 6: Clear all effects
  78. print("\n6. Clearing all effects...")
  79. success = clear_pattern_led_effect(test_pattern)
  80. print(f" Result: {'✓ Success' if success else '✗ Failed'}")
  81. playing = get_pattern_led_effect(test_pattern, "playing")
  82. idle = get_pattern_led_effect(test_pattern, "idle")
  83. if playing is None and idle is None:
  84. print(f" Result: ✓ Success - All effects cleared")
  85. else:
  86. print(f" Result: ✗ Failed - Effects still exist")
  87. print(f" Playing: {playing}")
  88. print(f" Idle: {idle}")
  89. print("\n✓ LED effect storage tests completed!")
  90. if __name__ == "__main__":
  91. test_led_effect_storage()