package xiaomi import "testing" func TestFilterByHome(t *testing.T) { f := FilterByHome("home1") if !f(&DeviceInfo{HomeID: "home1"}) { t.Error("FilterByHome(home1) should match home1") } if f(&DeviceInfo{HomeID: "home2"}) { t.Error("FilterByHome(home1) should not match home2") } if f(&DeviceInfo{HomeID: ""}) { t.Error("FilterByHome(home1) should not match empty") } } func TestFilterByRoom(t *testing.T) { f := FilterByRoom("room1") if !f(&DeviceInfo{RoomID: "room1"}) { t.Error("FilterByRoom(room1) should match room1") } if f(&DeviceInfo{RoomID: "room2"}) { t.Error("FilterByRoom(room1) should not match room2") } } func TestFilterByOnline(t *testing.T) { f := FilterByOnline() if !f(&DeviceInfo{Online: true}) { t.Error("FilterByOnline should match online=true") } if f(&DeviceInfo{Online: false}) { t.Error("FilterByOnline should not match online=false") } } func TestFilterByModel(t *testing.T) { f := FilterByModel("xiaomi.light.v1") if !f(&DeviceInfo{Model: "xiaomi.light.v1"}) { t.Error("FilterByModel should match exact model") } if f(&DeviceInfo{Model: "xiaomi.light.v2"}) { t.Error("FilterByModel should not match different model") } } func TestDeviceFilterComposition(t *testing.T) { tests := []struct { name string device *DeviceInfo filters []DeviceFilter want bool }{ { "all pass", &DeviceInfo{HomeID: "home1", RoomID: "room1", Online: true}, []DeviceFilter{FilterByHome("home1"), FilterByRoom("room1"), FilterByOnline()}, true, }, { "one fails", &DeviceInfo{HomeID: "home1", RoomID: "room2", Online: true}, []DeviceFilter{FilterByHome("home1"), FilterByRoom("room1"), FilterByOnline()}, false, }, { "no filters", &DeviceInfo{HomeID: "home1"}, nil, true, // empty filters = no-op }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { ok := true for _, f := range tt.filters { if !f(tt.device) { ok = false break } } if ok != tt.want { t.Errorf("got %v, want %v", ok, tt.want) } }) } }