|
- package utils
-
- import (
- "testing"
-
- "github.com/AFASystems/presence/internal/pkg/common/utils"
- "github.com/AFASystems/presence/internal/pkg/model"
- )
-
- func TestParseADFast_Empty(t *testing.T) {
- result := utils.ParseADFast([]byte{})
- if len(result) != 0 {
- t.Errorf("Expected empty result, got %d structures", len(result))
- }
- }
-
- func TestParseADFast_SingleStructure(t *testing.T) {
- // Length 2, type 0x01, data 0x06 -> [0,3)
- data := []byte{0x02, 0x01, 0x06}
- result := utils.ParseADFast(data)
- if len(result) != 1 {
- t.Fatalf("Expected 1 structure, got %d", len(result))
- }
- if result[0][0] != 0 || result[0][1] != 3 {
- t.Errorf("Expected [0,3), got [%d,%d)", result[0][0], result[0][1])
- }
- }
-
- func TestParseADFast_MultipleStructures(t *testing.T) {
- // First: len=2 (bytes 0-2), Second: len=3 (bytes 3-6)
- data := []byte{0x02, 0x01, 0x06, 0x03, 0xFF, 0x4C, 0x00}
- result := utils.ParseADFast(data)
- if len(result) != 2 {
- t.Fatalf("Expected 2 structures, got %d", len(result))
- }
- if result[0][0] != 0 || result[0][1] != 3 {
- t.Errorf("First structure: expected [0,3), got [%d,%d)", result[0][0], result[0][1])
- }
- if result[1][0] != 3 || result[1][1] != 7 {
- t.Errorf("Second structure: expected [3,7), got [%d,%d)", result[1][0], result[1][1])
- }
- }
-
- func TestParseADFast_ZeroLengthBreaks(t *testing.T) {
- data := []byte{0x00, 0x01, 0x02}
- result := utils.ParseADFast(data)
- if len(result) != 0 {
- t.Errorf("Expected 0 structures (zero length breaks), got %d", len(result))
- }
- }
-
- func TestRemoveFlagBytes_WithFlags(t *testing.T) {
- // AD structure: len=2, type=0x01 (flags), data=0x06
- // Remaining: 0x03, 0xFF, 0x4C
- data := []byte{0x02, 0x01, 0x06, 0x03, 0xFF, 0x4C, 0x00}
- result := utils.RemoveFlagBytes(data)
- if len(result) != 4 {
- t.Errorf("Expected 4 bytes after flag removal, got %d", len(result))
- }
- if result[0] != 0x03 && result[1] != 0xFF {
- t.Errorf("Expected flag bytes removed, got %v", result)
- }
- }
-
- func TestRemoveFlagBytes_WithoutFlags(t *testing.T) {
- data := []byte{0x02, 0xFF, 0x4C, 0x00} // type 0xFF, not 0x01
- result := utils.RemoveFlagBytes(data)
- if len(result) != 4 {
- t.Errorf("Expected unchanged 4 bytes, got %d", len(result))
- }
- }
-
- func TestRemoveFlagBytes_TooShort(t *testing.T) {
- data := []byte{0x01}
- result := utils.RemoveFlagBytes(data)
- if len(result) != 1 {
- t.Errorf("Expected 1 byte, got %d", len(result))
- }
- }
-
- func TestCalculateDistance(t *testing.T) {
- tests := []struct {
- name string
- rssi int64
- txPower string
- }{
- {"typical beacon", -65, "C5"}, // -59 in two's complement
- {"weak signal", -90, "C5"},
- {"strong signal", -40, "C5"},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- adv := model.BeaconAdvertisement{RSSI: tt.rssi, TXPower: tt.txPower}
- d := utils.CalculateDistance(adv)
- if d < 0 {
- t.Errorf("Distance should be non-negative, got %f", d)
- }
- })
- }
- }
-
- func TestLoopADStructures_NoParsers(t *testing.T) {
- registry := &model.ParserRegistry{ParserList: make(map[string]model.BeaconParser)}
- data := []byte{0x02, 0x01, 0x06}
- indices := utils.ParseADFast(data)
- event := utils.LoopADStructures(data, indices, "beacon-1", registry)
- if event.ID != "" {
- t.Errorf("Expected empty event with no parsers, got ID %s", event.ID)
- }
- }
|