package monitor import ( "testing" ) func TestParseMetrics(t *testing.T) { tests := []struct { name string mfrData []byte serviceData []byte wantMetrics *Metrics wantErr bool }{ { name: "Valid positive temp, normal CO2, normal battery", mfrData: []byte{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x99, 0x37, 0x00, 0x00, 0x03, 0x52, }, serviceData: []byte{ 0x00, 0x00, 0x5F, }, wantMetrics: &Metrics{ Temperature: 25.5, Humidity: 55, CO2: 850, Battery: 95, }, wantErr: false, }, { name: "Valid negative temp", mfrData: []byte{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x05, 0x2d, 0x00, 0x00, 0x01, 0x90, }, serviceData: []byte{ 0x00, 0x00, 0x32, }, wantMetrics: &Metrics{ Temperature: -5.2, Humidity: 45, CO2: 400, Battery: 50, }, wantErr: false, }, { name: "No manufacturer data (only battery from service data)", mfrData: nil, serviceData: []byte{ 0x00, 0x00, 0x64, }, wantMetrics: &Metrics{ Battery: 100, }, wantErr: false, }, { name: "Invalid CO2 value (too high ignored)", mfrData: []byte{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x94, 0x32, 0x00, 0x00, 0x27, 0x10, }, serviceData: []byte{ 0x00, 0x00, 0x40, }, wantMetrics: &Metrics{ Temperature: 20.0, Humidity: 50, CO2: 0, Battery: 64, }, wantErr: false, }, { name: "Empty inputs", mfrData: nil, serviceData: nil, wantMetrics: nil, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := ParseMetrics(tt.mfrData, tt.serviceData) if (err != nil) != tt.wantErr { t.Errorf("ParseMetrics() error = %v, wantErr %v", err, tt.wantErr) return } if tt.wantErr { return } if got.Temperature != tt.wantMetrics.Temperature { t.Errorf("Temperature = %v, want %v", got.Temperature, tt.wantMetrics.Temperature) } if got.Humidity != tt.wantMetrics.Humidity { t.Errorf("Humidity = %v, want %v", got.Humidity, tt.wantMetrics.Humidity) } if got.CO2 != tt.wantMetrics.CO2 { t.Errorf("CO2 = %v, want %v", got.CO2, tt.wantMetrics.CO2) } if got.Battery != tt.wantMetrics.Battery { t.Errorf("Battery = %v, want %v", got.Battery, tt.wantMetrics.Battery) } }) } } func TestIsSwitchBotCO2Advertisement(t *testing.T) { tests := []struct { name string hasServiceUUID bool mfrCompanyID uint16 mfrDataLen int want bool }{ {"Valid SwitchBot Monitor Pro (both UUID & Mfr ID)", true, 0x0969, 15, true}, {"Valid SwitchBot Monitor Pro (only Mfr ID & len)", false, 0x0969, 15, true}, {"Valid SwitchBot Monitor Pro (extended data)", false, 0x0969, 17, true}, {"Wrong Company ID and no Service UUID", false, 0x1234, 15, false}, {"Too short manufacturer data", false, 0x0969, 10, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := IsSwitchBotCO2Advertisement(tt.hasServiceUUID, tt.mfrCompanyID, tt.mfrDataLen); got != tt.want { t.Errorf("IsSwitchBotCO2Advertisement() = %v, want %v", got, tt.want) } }) } }