catalog_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package rgb
  2. import (
  3. "strings"
  4. "testing"
  5. "netdome.biz/paul/qmk-rgb/internal/via"
  6. )
  7. func impact80(t *testing.T) *Catalog {
  8. t.Helper()
  9. catalog, ok := CatalogFor(0x36B0, 0x309F)
  10. if !ok {
  11. t.Fatal("CatalogFor(0x36b0, 0x309f) = not found, want the Impact 80 catalog")
  12. }
  13. return catalog
  14. }
  15. func TestCatalogHasTheVendorEffectFamilies(t *testing.T) {
  16. catalog := impact80(t)
  17. if got := len(catalog.Names(via.ChannelRgbMatrix)); got != 46 {
  18. t.Errorf("backlight effects = %d, want 46", got)
  19. }
  20. if got := len(catalog.Names(via.ChannelRgblight)); got != 7 {
  21. t.Errorf("logo effects = %d, want 7", got)
  22. }
  23. if got := len(catalog.Names(via.ChannelAudio)); got != 7 {
  24. t.Errorf("side effects = %d, want 7", got)
  25. }
  26. }
  27. func TestCatalogIsEmptyForAnUnknownBoard(t *testing.T) {
  28. if _, ok := CatalogFor(0x6666, 0x0001); ok {
  29. t.Error("CatalogFor(0x6666, 0x0001) = found, want none")
  30. }
  31. }
  32. func TestCatalogIDResolvesNamesAndAliases(t *testing.T) {
  33. catalog := impact80(t)
  34. tests := []struct {
  35. channel via.Channel
  36. name string
  37. want uint8
  38. }{
  39. {via.ChannelRgblight, "light", 5},
  40. {via.ChannelRgblight, "solid", 5},
  41. {via.ChannelRgblight, "breathe", 4},
  42. {via.ChannelRgbMatrix, "solid_color", 1},
  43. {via.ChannelRgbMatrix, "rainbow_moving_chevron", 17},
  44. }
  45. for _, tt := range tests {
  46. got, ok := catalog.EffectID(tt.channel, tt.name)
  47. if !ok {
  48. t.Errorf("EffectID(%d, %q) not found", tt.channel, tt.name)
  49. continue
  50. }
  51. if got != tt.want {
  52. t.Errorf("EffectID(%d, %q) = %d, want %d", tt.channel, tt.name, got, tt.want)
  53. }
  54. }
  55. }
  56. func TestCatalogEffectNameReportsUnknownForAnUnknownID(t *testing.T) {
  57. catalog := impact80(t)
  58. if got := catalog.EffectName(via.ChannelRgbMatrix, 200); got != "unknown" {
  59. t.Errorf("EffectName(3, 200) = %q, want %q", got, "unknown")
  60. }
  61. }
  62. func TestCatalogDefaultEffectPerChannel(t *testing.T) {
  63. catalog := impact80(t)
  64. if got, ok := catalog.DefaultEffect(via.ChannelRgblight); !ok || got != 4 {
  65. t.Errorf("DefaultEffect(2) = %d, %t, want 4, true", got, ok)
  66. }
  67. if got, ok := catalog.DefaultEffect(via.ChannelRgbMatrix); !ok || got != 5 {
  68. t.Errorf("DefaultEffect(3) = %d, %t, want 5, true", got, ok)
  69. }
  70. }
  71. func TestResolveEffectWithoutACatalogRefuses(t *testing.T) {
  72. _, _, err := ResolveEffect(nil, "wave", []via.Channel{via.ChannelRgblight}, false)
  73. if err == nil {
  74. t.Fatal("ResolveEffect(nil, ...) expected an error, got nil")
  75. }
  76. if !strings.Contains(err.Error(), "mode") {
  77. t.Errorf("error = %q, want it to point at mode", err)
  78. }
  79. }
  80. // ID 5 is light on channel 2 and rainbow_beacon on channel 3, so the same name
  81. // cannot be applied to both.
  82. func TestResolveEffectSkipsAChannelThatDoesNotSupportTheName(t *testing.T) {
  83. catalog := impact80(t)
  84. targets, skipped, err := ResolveEffect(catalog, "light", []via.Channel{via.ChannelRgblight, via.ChannelRgbMatrix}, false)
  85. if err != nil {
  86. t.Fatalf("ResolveEffect() error = %v", err)
  87. }
  88. if len(targets) != 1 || targets[0].Channel != via.ChannelRgblight || targets[0].ID != 5 {
  89. t.Errorf("targets = %+v, want one target on channel 2 with id 5", targets)
  90. }
  91. if len(skipped) != 1 || skipped[0] != "rgb_matrix" {
  92. t.Errorf("skipped = %v, want [rgb_matrix]", skipped)
  93. }
  94. }
  95. // Asking for the unsupported channel by name is an error, not a skip: a command
  96. // that quietly did nothing would look like a command that worked.
  97. func TestResolveEffectRejectsTheOnlyChannelWhenItDoesNotSupportTheName(t *testing.T) {
  98. catalog := impact80(t)
  99. _, _, err := ResolveEffect(catalog, "light", []via.Channel{via.ChannelRgbMatrix}, true)
  100. if err == nil {
  101. t.Fatal("ResolveEffect() expected an error, want light rejected on the backlight")
  102. }
  103. }
  104. func TestResolveEffectRejectsANameNoChannelKnows(t *testing.T) {
  105. catalog := impact80(t)
  106. if _, _, err := ResolveEffect(catalog, "nope", []via.Channel{via.ChannelRgblight}, true); err == nil {
  107. t.Fatal("ResolveEffect() expected an error for an unknown name")
  108. }
  109. }
  110. // "static" is a documented alias. It must resolve through every caller, not
  111. // only the one that rewrites the string before calling: the backlight catalog
  112. // spells its solid effect solid_color, so the alias chain needs both steps.
  113. func TestResolveEffectAcceptsTheStaticAliasOnEveryPath(t *testing.T) {
  114. catalog := impact80(t)
  115. for _, ch := range []via.Channel{via.ChannelRgblight, via.ChannelRgbMatrix, via.ChannelAudio} {
  116. targets, _, err := ResolveEffect(catalog, "static", []via.Channel{ch}, true)
  117. if err != nil {
  118. t.Errorf("ResolveEffect(static on channel %d) error = %v", ch, err)
  119. continue
  120. }
  121. if len(targets) != 1 {
  122. t.Errorf("ResolveEffect(static on channel %d) = %d targets, want 1", ch, len(targets))
  123. }
  124. }
  125. }
  126. // Whether a command may skip an unsupported effect is the caller's knowledge,
  127. // not something the channel count can tell it. A board with a single lighting
  128. // channel and no --zone must still behave like a default command.
  129. func TestResolveEffectDistinguishesExplicitFromDefault(t *testing.T) {
  130. catalog := impact80(t)
  131. both := []via.Channel{via.ChannelRgblight, via.ChannelRgbMatrix}
  132. // Default command, one channel cannot do it: skipped, not refused.
  133. targets, skipped, err := ResolveEffect(catalog, "rainbow_moving_chevron", both, false)
  134. if err != nil {
  135. t.Errorf("default command error = %v, want the unsupported channel skipped", err)
  136. }
  137. if len(skipped) != 1 || skipped[0] != "rgblight" {
  138. t.Errorf("skipped = %v, want [rgblight]", skipped)
  139. }
  140. if len(targets) != 1 || targets[0].Channel != via.ChannelRgbMatrix {
  141. t.Errorf("targets = %+v, want the backlight only", targets)
  142. }
  143. // Same name, named channel only: refused.
  144. if _, _, err := ResolveEffect(catalog, "rainbow_moving_chevron", []via.Channel{via.ChannelRgblight}, true); err == nil {
  145. t.Error("explicit request error = nil, want it refused")
  146. }
  147. // A name no channel of this board has is unknown either way.
  148. if _, _, err := ResolveEffect(catalog, "rainbow_moving_chevron", []via.Channel{via.ChannelRgblight}, false); err == nil {
  149. t.Error("single-channel default command error = nil, want an unknown name reported")
  150. }
  151. }