|
- package model
-
- import (
- "encoding/json"
- "testing"
-
- "github.com/AFASystems/presence/internal/pkg/model"
- )
-
- func TestBeaconEvent_Hash(t *testing.T) {
- e := model.BeaconEvent{
- ID: "beacon-1",
- Name: "beacon-1",
- Type: "iBeacon",
- Battery: 85,
- Event: 1,
- }
- hash := e.Hash()
- if len(hash) == 0 {
- t.Error("Expected non-empty hash")
- }
-
- // Same event should produce same hash
- hash2 := e.Hash()
- if string(hash) != string(hash2) {
- t.Error("Hash should be deterministic")
- }
- }
-
- func TestBeaconEvent_Hash_BatteryRounded(t *testing.T) {
- e1 := model.BeaconEvent{ID: "1", Battery: 84, Event: 1}
- e2 := model.BeaconEvent{ID: "1", Battery: 89, Event: 1}
- hash1 := e1.Hash()
- hash2 := e2.Hash()
- // Battery is rounded to nearest 10, so 84 and 89 should produce same hash
- if string(hash1) != string(hash2) {
- t.Error("Battery rounding should make 84 and 89 produce same hash")
- }
- }
-
- func TestBeaconEvent_ToJSON(t *testing.T) {
- e := model.BeaconEvent{
- ID: "beacon-1",
- Name: "Test",
- Type: "iBeacon",
- Battery: 100,
- }
- data, err := e.ToJSON()
- if err != nil {
- t.Fatalf("ToJSON failed: %v", err)
- }
- var decoded model.BeaconEvent
- if err := json.Unmarshal(data, &decoded); err != nil {
- t.Fatalf("Failed to unmarshal: %v", err)
- }
- if decoded.ID != e.ID || decoded.Battery != e.Battery {
- t.Errorf("Decoded mismatch: got %+v", decoded)
- }
- }
-
- func TestParserRegistry_RegisterAndUnregister(t *testing.T) {
- registry := &model.ParserRegistry{ParserList: make(map[string]model.BeaconParser)}
- config := model.Config{
- Name: "test-parser",
- Min: 4,
- Max: 20,
- Pattern: []string{"0x02", "0x01"},
- Configs: map[string]model.ParserConfig{
- "battery": {Length: 1, Offset: 2, Order: "littleendian"},
- },
- }
-
- registry.Register("test-parser", config)
- if len(registry.ParserList) != 1 {
- t.Errorf("Expected 1 parser, got %d", len(registry.ParserList))
- }
-
- registry.Unregister("test-parser")
- if len(registry.ParserList) != 0 {
- t.Errorf("Expected 0 parsers after Unregister, got %d", len(registry.ParserList))
- }
- }
-
- func TestParserRegistry_UpdateOverwrites(t *testing.T) {
- registry := &model.ParserRegistry{ParserList: make(map[string]model.BeaconParser)}
- config1 := model.Config{Name: "p1", Min: 2, Max: 10, Pattern: []string{"0x02"}}
- config2 := model.Config{Name: "p1", Min: 5, Max: 15, Pattern: []string{"0x03"}}
-
- registry.Register("p1", config1)
- registry.Register("p1", config2)
-
- if len(registry.ParserList) != 1 {
- t.Errorf("Expected 1 parser after update, got %d", len(registry.ParserList))
- }
- }
-
- func TestConfig_GetPatternBytes(t *testing.T) {
- config := model.Config{Pattern: []string{"0xFF", "0x4C", "0x00"}}
- bytes := config.GetPatternBytes()
- if len(bytes) != 3 {
- t.Fatalf("Expected 3 bytes, got %d", len(bytes))
- }
- if bytes[0] != 0xFF || bytes[1] != 0x4C || bytes[2] != 0x00 {
- t.Errorf("Expected [0xFF, 0x4C, 0x00], got %v", bytes)
- }
- }
|