catalog_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. // 47, not 46: the vendor's VIA definition stops at 45, and the board takes
  18. // one ID more, which the tool names itself.
  19. if got := len(catalog.Names(via.ChannelRgbMatrix)); got != 47 {
  20. t.Errorf("backlight effects = %d, want 47", got)
  21. }
  22. if got := len(catalog.Names(via.ChannelRgblight)); got != 7 {
  23. t.Errorf("logo effects = %d, want 7", got)
  24. }
  25. if got := len(catalog.Names(via.ChannelAudio)); got != 7 {
  26. t.Errorf("side effects = %d, want 7", got)
  27. }
  28. }
  29. // The last name in the vendor's list and the one the tool added on top of it,
  30. // pinned at the boundary, because a list that silently grows or shrinks would
  31. // shift every ID above the change.
  32. func TestCatalogEndsWithTheVendorListThenTheToolName(t *testing.T) {
  33. catalog := impact80(t)
  34. tests := []struct {
  35. id uint8
  36. want string
  37. }{
  38. {45, "riverflow"},
  39. {46, "freeze"},
  40. {47, unknownEffectName},
  41. }
  42. for _, tt := range tests {
  43. if got := catalog.EffectName(via.ChannelRgbMatrix, tt.id); got != tt.want {
  44. t.Errorf("EffectName(rgb_matrix, %d) = %q, want %q", tt.id, got, tt.want)
  45. }
  46. }
  47. if id, ok := catalog.EffectID(via.ChannelRgbMatrix, "freeze"); !ok || id != 46 {
  48. t.Errorf("EffectID(rgb_matrix, \"freeze\") = %d, %t, want 46, true", id, ok)
  49. }
  50. }
  51. // The freeze is a tool name, so it must not leak into the channels the vendor
  52. // did not put it on. The board's other two channels have no such ID.
  53. func TestFreezeIsBacklightOnly(t *testing.T) {
  54. catalog := impact80(t)
  55. for _, ch := range []via.Channel{via.ChannelRgblight, via.ChannelAudio} {
  56. if id, ok := catalog.EffectID(ch, "freeze"); ok {
  57. t.Errorf("EffectID(%v, \"freeze\") = %d, true, want not found", ch, id)
  58. }
  59. }
  60. }
  61. func TestCatalogIsEmptyForAnUnknownBoard(t *testing.T) {
  62. if _, ok := CatalogFor(0x6666, 0x0001); ok {
  63. t.Error("CatalogFor(0x6666, 0x0001) = found, want none")
  64. }
  65. }
  66. func TestCatalogIDResolvesNamesAndAliases(t *testing.T) {
  67. catalog := impact80(t)
  68. tests := []struct {
  69. channel via.Channel
  70. name string
  71. want uint8
  72. }{
  73. {via.ChannelRgblight, "light", 5},
  74. {via.ChannelRgblight, "solid", 5},
  75. {via.ChannelRgblight, "breathe", 4},
  76. {via.ChannelRgbMatrix, "solid_color", 1},
  77. {via.ChannelRgbMatrix, "rainbow_moving_chevron", 17},
  78. }
  79. for _, tt := range tests {
  80. got, ok := catalog.EffectID(tt.channel, tt.name)
  81. if !ok {
  82. t.Errorf("EffectID(%d, %q) not found", tt.channel, tt.name)
  83. continue
  84. }
  85. if got != tt.want {
  86. t.Errorf("EffectID(%d, %q) = %d, want %d", tt.channel, tt.name, got, tt.want)
  87. }
  88. }
  89. }
  90. func TestCatalogEffectNameReportsUnknownForAnUnknownID(t *testing.T) {
  91. catalog := impact80(t)
  92. if got := catalog.EffectName(via.ChannelRgbMatrix, 200); got != "unknown" {
  93. t.Errorf("EffectName(3, 200) = %q, want %q", got, "unknown")
  94. }
  95. }
  96. func TestCatalogDefaultEffectPerChannel(t *testing.T) {
  97. catalog := impact80(t)
  98. if got, ok := catalog.DefaultEffect(via.ChannelRgblight); !ok || got != 4 {
  99. t.Errorf("DefaultEffect(2) = %d, %t, want 4, true", got, ok)
  100. }
  101. if got, ok := catalog.DefaultEffect(via.ChannelRgbMatrix); !ok || got != 5 {
  102. t.Errorf("DefaultEffect(3) = %d, %t, want 5, true", got, ok)
  103. }
  104. }
  105. func TestResolveEffectWithoutACatalogRefuses(t *testing.T) {
  106. _, _, err := ResolveEffect(nil, "wave", []via.Channel{via.ChannelRgblight}, false)
  107. if err == nil {
  108. t.Fatal("ResolveEffect(nil, ...) expected an error, got nil")
  109. }
  110. if !strings.Contains(err.Error(), "mode") {
  111. t.Errorf("error = %q, want it to point at mode", err)
  112. }
  113. }
  114. // ID 5 is light on channel 2 and rainbow_beacon on channel 3, so the same name
  115. // cannot be applied to both.
  116. func TestResolveEffectSkipsAChannelThatDoesNotSupportTheName(t *testing.T) {
  117. catalog := impact80(t)
  118. targets, skipped, err := ResolveEffect(catalog, "light", []via.Channel{via.ChannelRgblight, via.ChannelRgbMatrix}, false)
  119. if err != nil {
  120. t.Fatalf("ResolveEffect() error = %v", err)
  121. }
  122. if len(targets) != 1 || targets[0].Channel != via.ChannelRgblight || targets[0].ID != 5 {
  123. t.Errorf("targets = %+v, want one target on channel 2 with id 5", targets)
  124. }
  125. if len(skipped) != 1 || skipped[0] != "rgb_matrix" {
  126. t.Errorf("skipped = %v, want [rgb_matrix]", skipped)
  127. }
  128. }
  129. // Asking for the unsupported channel by name is an error, not a skip: a command
  130. // that quietly did nothing would look like a command that worked.
  131. func TestResolveEffectRejectsTheOnlyChannelWhenItDoesNotSupportTheName(t *testing.T) {
  132. catalog := impact80(t)
  133. _, _, err := ResolveEffect(catalog, "light", []via.Channel{via.ChannelRgbMatrix}, true)
  134. if err == nil {
  135. t.Fatal("ResolveEffect() expected an error, want light rejected on the backlight")
  136. }
  137. }
  138. func TestResolveEffectRejectsANameNoChannelKnows(t *testing.T) {
  139. catalog := impact80(t)
  140. if _, _, err := ResolveEffect(catalog, "nope", []via.Channel{via.ChannelRgblight}, true); err == nil {
  141. t.Fatal("ResolveEffect() expected an error for an unknown name")
  142. }
  143. }
  144. // "static" is a documented alias. It must resolve through every caller, not
  145. // only the one that rewrites the string before calling: the backlight catalog
  146. // spells its solid effect solid_color, so the alias chain needs both steps.
  147. func TestResolveEffectAcceptsTheStaticAliasOnEveryPath(t *testing.T) {
  148. catalog := impact80(t)
  149. for _, ch := range []via.Channel{via.ChannelRgblight, via.ChannelRgbMatrix, via.ChannelAudio} {
  150. targets, _, err := ResolveEffect(catalog, "static", []via.Channel{ch}, true)
  151. if err != nil {
  152. t.Errorf("ResolveEffect(static on channel %d) error = %v", ch, err)
  153. continue
  154. }
  155. if len(targets) != 1 {
  156. t.Errorf("ResolveEffect(static on channel %d) = %d targets, want 1", ch, len(targets))
  157. }
  158. }
  159. }
  160. // Whether a command may skip an unsupported effect is the caller's knowledge,
  161. // not something the channel count can tell it. A board with a single lighting
  162. // channel and no --zone must still behave like a default command.
  163. func TestResolveEffectDistinguishesExplicitFromDefault(t *testing.T) {
  164. catalog := impact80(t)
  165. both := []via.Channel{via.ChannelRgblight, via.ChannelRgbMatrix}
  166. // Default command, one channel cannot do it: skipped, not refused.
  167. targets, skipped, err := ResolveEffect(catalog, "rainbow_moving_chevron", both, false)
  168. if err != nil {
  169. t.Errorf("default command error = %v, want the unsupported channel skipped", err)
  170. }
  171. if len(skipped) != 1 || skipped[0] != "rgblight" {
  172. t.Errorf("skipped = %v, want [rgblight]", skipped)
  173. }
  174. if len(targets) != 1 || targets[0].Channel != via.ChannelRgbMatrix {
  175. t.Errorf("targets = %+v, want the backlight only", targets)
  176. }
  177. // Same name, named channel only: refused.
  178. if _, _, err := ResolveEffect(catalog, "rainbow_moving_chevron", []via.Channel{via.ChannelRgblight}, true); err == nil {
  179. t.Error("explicit request error = nil, want it refused")
  180. }
  181. // One channel reported by a board that has the name elsewhere: skipped with a
  182. // warning rather than called unknown, and never silently applied.
  183. targets, skipped, err = ResolveEffect(catalog, "rainbow_moving_chevron", []via.Channel{via.ChannelRgblight}, false)
  184. if err != nil {
  185. t.Errorf("single-channel default command error = %v, want a skip", err)
  186. }
  187. if len(targets) != 0 || len(skipped) != 1 {
  188. t.Errorf("targets = %+v, skipped = %v, want nothing applied and one skip", targets, skipped)
  189. }
  190. }
  191. // A name the board has but the named channel does not is not an unknown effect:
  192. // the user named a real effect and put it on the wrong channel, and that is a
  193. // different mistake with a different fix.
  194. func TestResolveEffectSaysTheChannelDoesNotSupportTheName(t *testing.T) {
  195. catalog := impact80(t)
  196. _, _, err := ResolveEffect(catalog, "rainbow_moving_chevron", []via.Channel{via.ChannelRgblight}, true)
  197. if err == nil {
  198. t.Fatal("ResolveEffect() expected an error, got nil")
  199. }
  200. if !strings.Contains(err.Error(), "not supported on rgblight") {
  201. t.Errorf("error = %q, want it to name the channel that cannot do it", err)
  202. }
  203. }
  204. // And a name no channel of the board has stays an unknown effect.
  205. func TestResolveEffectKeepsUnknownEffectForANameTheBoardLacks(t *testing.T) {
  206. catalog := impact80(t)
  207. _, _, err := ResolveEffect(catalog, "nope", []via.Channel{via.ChannelRgblight}, true)
  208. if err == nil {
  209. t.Fatal("ResolveEffect() expected an error, got nil")
  210. }
  211. if !strings.Contains(err.Error(), "unknown effect") {
  212. t.Errorf("error = %q, want it to stay an unknown effect", err)
  213. }
  214. }