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) // 47, not 46: the vendor's VIA definition stops at 45, and the board takes // one ID more, which the tool names itself. if got := len(catalog.Names(via.ChannelRgbMatrix)); got != 47 { t.Errorf("backlight effects = %d, want 47", 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) } } // The last name in the vendor's list and the one the tool added on top of it, // pinned at the boundary, because a list that silently grows or shrinks would // shift every ID above the change. func TestCatalogEndsWithTheVendorListThenTheToolName(t *testing.T) { catalog := impact80(t) tests := []struct { id uint8 want string }{ {45, "riverflow"}, {46, "freeze"}, {47, unknownEffectName}, } for _, tt := range tests { if got := catalog.EffectName(via.ChannelRgbMatrix, tt.id); got != tt.want { t.Errorf("EffectName(rgb_matrix, %d) = %q, want %q", tt.id, got, tt.want) } } if id, ok := catalog.EffectID(via.ChannelRgbMatrix, "freeze"); !ok || id != 46 { t.Errorf("EffectID(rgb_matrix, \"freeze\") = %d, %t, want 46, true", id, ok) } } // The freeze is a tool name, so it must not leak into the channels the vendor // did not put it on. The board's other two channels have no such ID. func TestFreezeIsBacklightOnly(t *testing.T) { catalog := impact80(t) for _, ch := range []via.Channel{via.ChannelRgblight, via.ChannelAudio} { if id, ok := catalog.EffectID(ch, "freeze"); ok { t.Errorf("EffectID(%v, \"freeze\") = %d, true, want not found", ch, id) } } } 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") } // One channel reported by a board that has the name elsewhere: skipped with a // warning rather than called unknown, and never silently applied. targets, skipped, err = ResolveEffect(catalog, "rainbow_moving_chevron", []via.Channel{via.ChannelRgblight}, false) if err != nil { t.Errorf("single-channel default command error = %v, want a skip", err) } if len(targets) != 0 || len(skipped) != 1 { t.Errorf("targets = %+v, skipped = %v, want nothing applied and one skip", targets, skipped) } } // A name the board has but the named channel does not is not an unknown effect: // the user named a real effect and put it on the wrong channel, and that is a // different mistake with a different fix. func TestResolveEffectSaysTheChannelDoesNotSupportTheName(t *testing.T) { catalog := impact80(t) _, _, err := ResolveEffect(catalog, "rainbow_moving_chevron", []via.Channel{via.ChannelRgblight}, true) if err == nil { t.Fatal("ResolveEffect() expected an error, got nil") } if !strings.Contains(err.Error(), "not supported on rgblight") { t.Errorf("error = %q, want it to name the channel that cannot do it", err) } } // And a name no channel of the board has stays an unknown effect. func TestResolveEffectKeepsUnknownEffectForANameTheBoardLacks(t *testing.T) { catalog := impact80(t) _, _, err := ResolveEffect(catalog, "nope", []via.Channel{via.ChannelRgblight}, true) if err == nil { t.Fatal("ResolveEffect() expected an error, got nil") } if !strings.Contains(err.Error(), "unknown effect") { t.Errorf("error = %q, want it to stay an unknown effect", err) } }