Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

108 lignes
2.9 KiB

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