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.
 
 
 
 

148 lignes
3.7 KiB

  1. package appcontext
  2. import (
  3. "sync"
  4. "testing"
  5. "github.com/AFASystems/presence/internal/pkg/common/appcontext"
  6. )
  7. func TestNewAppState(t *testing.T) {
  8. state := appcontext.NewAppState()
  9. if state == nil {
  10. t.Fatal("NewAppState returned nil")
  11. }
  12. // Default settings
  13. settings := state.GetSettingsValue()
  14. if settings.CurrentAlgorithm != "filter" {
  15. t.Errorf("Expected CurrentAlgorithm 'filter', got %s", settings.CurrentAlgorithm)
  16. }
  17. if state.GetBeaconCount() != 0 {
  18. t.Errorf("Expected 0 beacons, got %d", state.GetBeaconCount())
  19. }
  20. }
  21. func TestBeaconLookup_AddAndExists(t *testing.T) {
  22. state := appcontext.NewAppState()
  23. state.AddBeaconToLookup("AA:BB:CC:DD:EE:FF", "beacon-1")
  24. val, exists := state.BeaconExists("AA:BB:CC:DD:EE:FF")
  25. if !exists {
  26. t.Error("Expected beacon to exist after AddBeaconToLookup")
  27. }
  28. if val != "beacon-1" {
  29. t.Errorf("Expected value 'beacon-1', got %s", val)
  30. }
  31. }
  32. func TestBeaconLookup_Remove(t *testing.T) {
  33. state := appcontext.NewAppState()
  34. state.AddBeaconToLookup("AA:BB:CC:DD:EE:FF", "beacon-1")
  35. state.RemoveBeaconFromLookup("AA:BB:CC:DD:EE:FF")
  36. _, exists := state.BeaconExists("AA:BB:CC:DD:EE:FF")
  37. if exists {
  38. t.Error("Expected beacon to not exist after RemoveBeaconFromLookup")
  39. }
  40. }
  41. func TestBeaconLookup_CleanLookup(t *testing.T) {
  42. state := appcontext.NewAppState()
  43. state.AddBeaconToLookup("AA:BB:CC:DD:EE:FF", "beacon-1")
  44. state.AddBeaconToLookup("11:22:33:44:55:66", "beacon-2")
  45. state.CleanLookup()
  46. _, exists1 := state.BeaconExists("AA:BB:CC:DD:EE:FF")
  47. _, exists2 := state.BeaconExists("11:22:33:44:55:66")
  48. if exists1 || exists2 {
  49. t.Error("Expected all beacons to be removed after CleanLookup")
  50. }
  51. }
  52. func TestBeacon_GetAndUpdate(t *testing.T) {
  53. state := appcontext.NewAppState()
  54. beacon := appcontext.Beacon{
  55. ID: "test-beacon",
  56. Name: "Test",
  57. }
  58. state.UpdateBeacon("test-beacon", beacon)
  59. got, exists := state.GetBeacon("test-beacon")
  60. if !exists {
  61. t.Error("Expected beacon to exist")
  62. }
  63. if got.Name != "Test" {
  64. t.Errorf("Expected name 'Test', got %s", got.Name)
  65. }
  66. }
  67. func TestBeaconEvent_GetAndUpdate(t *testing.T) {
  68. state := appcontext.NewAppState()
  69. event := appcontext.BeaconEvent{
  70. ID: "beacon-1",
  71. Type: "iBeacon",
  72. Battery: 85,
  73. }
  74. state.UpdateBeaconEvent("beacon-1", event)
  75. got, exists := state.GetBeaconEvent("beacon-1")
  76. if !exists {
  77. t.Error("Expected event to exist")
  78. }
  79. if got.Type != "iBeacon" || got.Battery != 85 {
  80. t.Errorf("Expected type iBeacon battery 85, got %s %d", got.Type, got.Battery)
  81. }
  82. }
  83. func TestGetAllBeacons(t *testing.T) {
  84. state := appcontext.NewAppState()
  85. state.UpdateBeacon("b1", appcontext.Beacon{ID: "b1"})
  86. state.UpdateBeacon("b2", appcontext.Beacon{ID: "b2"})
  87. all := state.GetAllBeacons()
  88. if len(all) != 2 {
  89. t.Errorf("Expected 2 beacons, got %d", len(all))
  90. }
  91. }
  92. func TestUpdateSettings(t *testing.T) {
  93. state := appcontext.NewAppState()
  94. state.UpdateSettings(map[string]any{
  95. "current_algorithm": "ai",
  96. "location_confidence": int64(5),
  97. })
  98. settings := state.GetSettingsValue()
  99. if settings.CurrentAlgorithm != "ai" {
  100. t.Errorf("Expected CurrentAlgorithm 'ai', got %s", settings.CurrentAlgorithm)
  101. }
  102. if settings.LocationConfidence != 5 {
  103. t.Errorf("Expected LocationConfidence 5, got %d", settings.LocationConfidence)
  104. }
  105. }
  106. func TestBeaconLookup_ConcurrentAccess(t *testing.T) {
  107. state := appcontext.NewAppState()
  108. var wg sync.WaitGroup
  109. for i := 0; i < 100; i++ {
  110. wg.Add(1)
  111. go func(n int) {
  112. defer wg.Done()
  113. mac := "AA:BB:CC:DD:EE:FF"
  114. id := "beacon-1"
  115. state.AddBeaconToLookup(mac, id)
  116. state.BeaconExists(mac)
  117. }(i)
  118. }
  119. wg.Wait()
  120. state.CleanLookup()
  121. _, exists := state.BeaconExists("AA:BB:CC:DD:EE:FF")
  122. if exists {
  123. t.Error("Expected lookup to be empty after CleanLookup")
  124. }
  125. }