package rgb import ( "strings" "testing" "netdome.biz/paul/qmk-rgb/internal/via" ) func impact80(t *testing.T) *Catalog { t.Helper() catalog, ok := CatalogFor(0x36B0, 0x309F) if !ok { t.Fatal("CatalogFor(0x36b0, 0x309f) = not found, want the Impact 80 catalog") } return catalog } func TestCatalogHasTheVendorEffectFamilies(t *testing.T) { catalog := impact80(t) if got := len(catalog.Names(via.ChannelRgbMatrix)); got != 46 { t.Errorf("backlight effects = %d, want 46", got) } if got := len(catalog.Names(via.ChannelRgblight)); got != 7 { t.Errorf("logo effects = %d, want 7", got) } if got := len(catalog.Names(via.ChannelAudio)); got != 7 { t.Errorf("side effects = %d, want 7", got) } } func TestCatalogIsEmptyForAnUnknownBoard(t *testing.T) { if _, ok := CatalogFor(0x6666, 0x0001); ok { t.Error("CatalogFor(0x6666, 0x0001) = found, want none") } } func TestCatalogIDResolvesNamesAndAliases(t *testing.T) { catalog := impact80(t) tests := []struct { channel via.Channel name string want uint8 }{ {via.ChannelRgblight, "light", 5}, {via.ChannelRgblight, "solid", 5}, {via.ChannelRgblight, "breathe", 4}, {via.ChannelRgbMatrix, "solid_color", 1}, {via.ChannelRgbMatrix, "rainbow_moving_chevron", 17}, } for _, tt := range tests { got, ok := catalog.EffectID(tt.channel, tt.name) if !ok { t.Errorf("EffectID(%d, %q) not found", tt.channel, tt.name) continue } if got != tt.want { t.Errorf("EffectID(%d, %q) = %d, want %d", tt.channel, tt.name, got, tt.want) } } } func TestCatalogEffectNameReportsUnknownForAnUnknownID(t *testing.T) { catalog := impact80(t) if got := catalog.EffectName(via.ChannelRgbMatrix, 200); got != "unknown" { t.Errorf("EffectName(3, 200) = %q, want %q", got, "unknown") } } func TestCatalogDefaultEffectPerChannel(t *testing.T) { catalog := impact80(t) if got, ok := catalog.DefaultEffect(via.ChannelRgblight); !ok || got != 4 { t.Errorf("DefaultEffect(2) = %d, %t, want 4, true", got, ok) } if got, ok := catalog.DefaultEffect(via.ChannelRgbMatrix); !ok || got != 5 { t.Errorf("DefaultEffect(3) = %d, %t, want 5, true", got, ok) } } func TestResolveEffectWithoutACatalogRefuses(t *testing.T) { _, _, err := ResolveEffect(nil, "wave", []via.Channel{via.ChannelRgblight}, false) if err == nil { t.Fatal("ResolveEffect(nil, ...) expected an error, got nil") } if !strings.Contains(err.Error(), "mode") { t.Errorf("error = %q, want it to point at mode", err) } } // ID 5 is light on channel 2 and rainbow_beacon on channel 3, so the same name // cannot be applied to both. func TestResolveEffectSkipsAChannelThatDoesNotSupportTheName(t *testing.T) { catalog := impact80(t) targets, skipped, err := ResolveEffect(catalog, "light", []via.Channel{via.ChannelRgblight, via.ChannelRgbMatrix}, false) if err != nil { t.Fatalf("ResolveEffect() error = %v", err) } if len(targets) != 1 || targets[0].Channel != via.ChannelRgblight || targets[0].ID != 5 { t.Errorf("targets = %+v, want one target on channel 2 with id 5", targets) } if len(skipped) != 1 || skipped[0] != "rgb_matrix" { t.Errorf("skipped = %v, want [rgb_matrix]", skipped) } } // Asking for the unsupported channel by name is an error, not a skip: a command // that quietly did nothing would look like a command that worked. func TestResolveEffectRejectsTheOnlyChannelWhenItDoesNotSupportTheName(t *testing.T) { catalog := impact80(t) _, _, err := ResolveEffect(catalog, "light", []via.Channel{via.ChannelRgbMatrix}, true) if err == nil { t.Fatal("ResolveEffect() expected an error, want light rejected on the backlight") } } func TestResolveEffectRejectsANameNoChannelKnows(t *testing.T) { catalog := impact80(t) if _, _, err := ResolveEffect(catalog, "nope", []via.Channel{via.ChannelRgblight}, true); err == nil { t.Fatal("ResolveEffect() expected an error for an unknown name") } } // "static" is a documented alias. It must resolve through every caller, not // only the one that rewrites the string before calling: the backlight catalog // spells its solid effect solid_color, so the alias chain needs both steps. func TestResolveEffectAcceptsTheStaticAliasOnEveryPath(t *testing.T) { catalog := impact80(t) for _, ch := range []via.Channel{via.ChannelRgblight, via.ChannelRgbMatrix, via.ChannelAudio} { targets, _, err := ResolveEffect(catalog, "static", []via.Channel{ch}, true) if err != nil { t.Errorf("ResolveEffect(static on channel %d) error = %v", ch, err) continue } if len(targets) != 1 { t.Errorf("ResolveEffect(static on channel %d) = %d targets, want 1", ch, len(targets)) } } } // Whether a command may skip an unsupported effect is the caller's knowledge, // not something the channel count can tell it. A board with a single lighting // channel and no --zone must still behave like a default command. func TestResolveEffectDistinguishesExplicitFromDefault(t *testing.T) { catalog := impact80(t) both := []via.Channel{via.ChannelRgblight, via.ChannelRgbMatrix} // Default command, one channel cannot do it: skipped, not refused. targets, skipped, err := ResolveEffect(catalog, "rainbow_moving_chevron", both, false) if err != nil { t.Errorf("default command error = %v, want the unsupported channel skipped", err) } if len(skipped) != 1 || skipped[0] != "rgblight" { t.Errorf("skipped = %v, want [rgblight]", skipped) } if len(targets) != 1 || targets[0].Channel != via.ChannelRgbMatrix { t.Errorf("targets = %+v, want the backlight only", targets) } // Same name, named channel only: refused. if _, _, err := ResolveEffect(catalog, "rainbow_moving_chevron", []via.Channel{via.ChannelRgblight}, true); err == nil { t.Error("explicit request error = nil, want it refused") } // A name no channel of this board has is unknown either way. if _, _, err := ResolveEffect(catalog, "rainbow_moving_chevron", []via.Channel{via.ChannelRgblight}, false); err == nil { t.Error("single-channel default command error = nil, want an unknown name reported") } }