25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

149 satır
3.7 KiB

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