package appcontext import ( "sync" "testing" "github.com/AFASystems/presence/internal/pkg/common/appcontext" "github.com/AFASystems/presence/internal/pkg/model" ) func TestNewAppState(t *testing.T) { state := appcontext.NewAppState() if state == nil { t.Fatal("NewAppState returned nil") } // Default settings settings := state.GetSettingsValue() if settings.CurrentAlgorithm != "filter" { t.Errorf("Expected CurrentAlgorithm 'filter', got %s", settings.CurrentAlgorithm) } if state.GetBeaconCount() != 0 { t.Errorf("Expected 0 beacons, got %d", state.GetBeaconCount()) } } func TestBeaconLookup_AddAndExists(t *testing.T) { state := appcontext.NewAppState() state.AddBeaconToLookup("AA:BB:CC:DD:EE:FF", "beacon-1") val, exists := state.BeaconExists("AA:BB:CC:DD:EE:FF") if !exists { t.Error("Expected beacon to exist after AddBeaconToLookup") } if val != "beacon-1" { t.Errorf("Expected value 'beacon-1', got %s", val) } } func TestBeaconLookup_Remove(t *testing.T) { state := appcontext.NewAppState() state.AddBeaconToLookup("AA:BB:CC:DD:EE:FF", "beacon-1") state.RemoveBeaconFromLookup("AA:BB:CC:DD:EE:FF") _, exists := state.BeaconExists("AA:BB:CC:DD:EE:FF") if exists { t.Error("Expected beacon to not exist after RemoveBeaconFromLookup") } } func TestBeaconLookup_CleanLookup(t *testing.T) { state := appcontext.NewAppState() state.AddBeaconToLookup("AA:BB:CC:DD:EE:FF", "beacon-1") state.AddBeaconToLookup("11:22:33:44:55:66", "beacon-2") state.CleanLookup() _, exists1 := state.BeaconExists("AA:BB:CC:DD:EE:FF") _, exists2 := state.BeaconExists("11:22:33:44:55:66") if exists1 || exists2 { t.Error("Expected all beacons to be removed after CleanLookup") } } func TestBeacon_GetAndUpdate(t *testing.T) { state := appcontext.NewAppState() beacon := model.Beacon{ ID: "test-beacon", Name: "Test", } state.UpdateBeacon("test-beacon", beacon) got, exists := state.GetBeacon("test-beacon") if !exists { t.Error("Expected beacon to exist") } if got.Name != "Test" { t.Errorf("Expected name 'Test', got %s", got.Name) } } func TestBeaconEvent_GetAndUpdate(t *testing.T) { state := appcontext.NewAppState() event := model.BeaconEvent{ ID: "beacon-1", Type: "iBeacon", Battery: 85, } state.UpdateBeaconEvent("beacon-1", event) got, exists := state.GetBeaconEvent("beacon-1") if !exists { t.Error("Expected event to exist") } if got.Type != "iBeacon" || got.Battery != 85 { t.Errorf("Expected type iBeacon battery 85, got %s %d", got.Type, got.Battery) } } func TestGetAllBeacons(t *testing.T) { state := appcontext.NewAppState() state.UpdateBeacon("b1", model.Beacon{ID: "b1"}) state.UpdateBeacon("b2", model.Beacon{ID: "b2"}) all := state.GetAllBeacons() if len(all) != 2 { t.Errorf("Expected 2 beacons, got %d", len(all)) } } func TestUpdateSettings(t *testing.T) { state := appcontext.NewAppState() state.UpdateSettings(map[string]any{ "current_algorithm": "ai", "location_confidence": int64(5), }) settings := state.GetSettingsValue() if settings.CurrentAlgorithm != "ai" { t.Errorf("Expected CurrentAlgorithm 'ai', got %s", settings.CurrentAlgorithm) } if settings.LocationConfidence != 5 { t.Errorf("Expected LocationConfidence 5, got %d", settings.LocationConfidence) } } func TestBeaconLookup_ConcurrentAccess(t *testing.T) { state := appcontext.NewAppState() var wg sync.WaitGroup for i := 0; i < 100; i++ { wg.Add(1) go func(n int) { defer wg.Done() mac := "AA:BB:CC:DD:EE:FF" id := "beacon-1" state.AddBeaconToLookup(mac, id) state.BeaconExists(mac) }(i) } wg.Wait() state.CleanLookup() _, exists := state.BeaconExists("AA:BB:CC:DD:EE:FF") if exists { t.Error("Expected lookup to be empty after CleanLookup") } }