Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

107 рядки
2.8 KiB

  1. package model
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/AFASystems/presence/internal/pkg/model"
  6. )
  7. func TestBeaconEvent_Hash(t *testing.T) {
  8. e := model.BeaconEvent{
  9. ID: "beacon-1",
  10. Name: "beacon-1",
  11. Type: "iBeacon",
  12. Battery: 85,
  13. Event: 1,
  14. }
  15. hash := e.Hash()
  16. if len(hash) == 0 {
  17. t.Error("Expected non-empty hash")
  18. }
  19. // Same event should produce same hash
  20. hash2 := e.Hash()
  21. if string(hash) != string(hash2) {
  22. t.Error("Hash should be deterministic")
  23. }
  24. }
  25. func TestBeaconEvent_Hash_BatteryRounded(t *testing.T) {
  26. e1 := model.BeaconEvent{ID: "1", Battery: 84, Event: 1}
  27. e2 := model.BeaconEvent{ID: "1", Battery: 89, Event: 1}
  28. hash1 := e1.Hash()
  29. hash2 := e2.Hash()
  30. // Battery is rounded to nearest 10, so 84 and 89 should produce same hash
  31. if string(hash1) != string(hash2) {
  32. t.Error("Battery rounding should make 84 and 89 produce same hash")
  33. }
  34. }
  35. func TestBeaconEvent_ToJSON(t *testing.T) {
  36. e := model.BeaconEvent{
  37. ID: "beacon-1",
  38. Name: "Test",
  39. Type: "iBeacon",
  40. Battery: 100,
  41. }
  42. data, err := e.ToJSON()
  43. if err != nil {
  44. t.Fatalf("ToJSON failed: %v", err)
  45. }
  46. var decoded model.BeaconEvent
  47. if err := json.Unmarshal(data, &decoded); err != nil {
  48. t.Fatalf("Failed to unmarshal: %v", err)
  49. }
  50. if decoded.ID != e.ID || decoded.Battery != e.Battery {
  51. t.Errorf("Decoded mismatch: got %+v", decoded)
  52. }
  53. }
  54. func TestParserRegistry_RegisterAndUnregister(t *testing.T) {
  55. registry := &model.ParserRegistry{ParserList: make(map[string]model.BeaconParser)}
  56. config := model.Config{
  57. Name: "test-parser",
  58. Min: 4,
  59. Max: 20,
  60. Pattern: []string{"0x02", "0x01"},
  61. Configs: map[string]model.ParserConfig{
  62. "battery": {Length: 1, Offset: 2, Order: "littleendian"},
  63. },
  64. }
  65. registry.Register("test-parser", config)
  66. if len(registry.ParserList) != 1 {
  67. t.Errorf("Expected 1 parser, got %d", len(registry.ParserList))
  68. }
  69. registry.Unregister("test-parser")
  70. if len(registry.ParserList) != 0 {
  71. t.Errorf("Expected 0 parsers after Unregister, got %d", len(registry.ParserList))
  72. }
  73. }
  74. func TestParserRegistry_UpdateOverwrites(t *testing.T) {
  75. registry := &model.ParserRegistry{ParserList: make(map[string]model.BeaconParser)}
  76. config1 := model.Config{Name: "p1", Min: 2, Max: 10, Pattern: []string{"0x02"}}
  77. config2 := model.Config{Name: "p1", Min: 5, Max: 15, Pattern: []string{"0x03"}}
  78. registry.Register("p1", config1)
  79. registry.Register("p1", config2)
  80. if len(registry.ParserList) != 1 {
  81. t.Errorf("Expected 1 parser after update, got %d", len(registry.ParserList))
  82. }
  83. }
  84. func TestConfig_GetPatternBytes(t *testing.T) {
  85. config := model.Config{Pattern: []string{"0xFF", "0x4C", "0x00"}}
  86. bytes := config.GetPatternBytes()
  87. if len(bytes) != 3 {
  88. t.Fatalf("Expected 3 bytes, got %d", len(bytes))
  89. }
  90. if bytes[0] != 0xFF || bytes[1] != 0x4C || bytes[2] != 0x00 {
  91. t.Errorf("Expected [0xFF, 0x4C, 0x00], got %v", bytes)
  92. }
  93. }