package uhf import ( "testing" "time" "github.com/stretchr/testify/require" ) func TestChannelEventsUseMonotonicIDs(t *testing.T) { channelEventMu.Lock() previousQueue := ChannelEventQueue previousNextID := channelEventNextID ChannelEventQueue = make([]ChannelEvent, 0, 50) channelEventNextID = 0 channelEventMu.Unlock() t.Cleanup(func() { channelEventMu.Lock() ChannelEventQueue = previousQueue channelEventNextID = previousNextID channelEventMu.Unlock() }) PushChannelEvent(ChannelEvent{RFIDTag: "A", Timestamp: 100}) PushChannelEvent(ChannelEvent{RFIDTag: "B", Timestamp: 100}) all := GetChannelEvents(0) require.Len(t, all, 2) require.Equal(t, uint64(1), all[0].ID) require.Equal(t, uint64(2), all[1].ID) afterFirst := GetChannelEvents(all[0].ID) require.Len(t, afterFirst, 1) require.Equal(t, "B", afterFirst[0].RFIDTag) } func TestDeviceHandlerAcceptReportDeduplicatesDeviceAndEPC(t *testing.T) { h := &DeviceHandler{lastReport: make(map[string]time.Time)} base := time.Unix(100, 0) report := &ReportData{DeviceCode: "RFID-01", Epcs: []string{"e20001"}} if !h.acceptReport(report, base) { t.Fatal("first report should be accepted") } if h.acceptReport(report, base.Add(reportDebounceWindow-time.Nanosecond)) { t.Fatal("duplicate report inside debounce window should be rejected") } if !h.acceptReport(report, base.Add(reportDebounceWindow)) { t.Fatal("report at the end of debounce window should be accepted") } } func TestDeviceHandlerAcceptReportAllowsDifferentEPCs(t *testing.T) { h := &DeviceHandler{lastReport: make(map[string]time.Time)} now := time.Unix(200, 0) if !h.acceptReport(&ReportData{DeviceCode: "RFID-01", Epcs: []string{"e20001"}}, now) { t.Fatal("first EPC should be accepted") } if !h.acceptReport(&ReportData{DeviceCode: "RFID-01", Epcs: []string{"e20002"}}, now) { t.Fatal("different EPC should be accepted") } if !h.acceptReport(&ReportData{DeviceCode: "RFID-02", Epcs: []string{"e20001"}}, now) { t.Fatal("same EPC from a different device should be accepted") } }