catalog_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package rgb
  2. import (
  3. "path/filepath"
  4. "strings"
  5. "testing"
  6. "netdome.biz/paul/qmk-rgb/internal/via"
  7. )
  8. // impact80 is the catalog the vendored definition file produces, because that
  9. // file is where every effect name now comes from.
  10. func impact80(t *testing.T) *Catalog {
  11. t.Helper()
  12. def, err := LoadDefinition(filepath.Join("..", "..", "definitions", "impact80.json"))
  13. if err != nil {
  14. t.Fatalf("LoadDefinition(definitions/impact80.json) error = %v", err)
  15. }
  16. return def.Catalog
  17. }
  18. func TestCatalogHasTheVendorEffectFamilies(t *testing.T) {
  19. catalog := impact80(t)
  20. // The vendor's definition is the whole catalog, so its counts are the
  21. // catalog's counts and there is nothing beside it.
  22. if got := len(catalog.Names(via.ChannelRgbMatrix)); got != 46 {
  23. t.Errorf("backlight effects = %d, want 46", got)
  24. }
  25. if got := len(catalog.Names(via.ChannelRgblight)); got != 7 {
  26. t.Errorf("logo effects = %d, want 7", got)
  27. }
  28. if got := len(catalog.Names(via.ChannelAudio)); got != 7 {
  29. t.Errorf("side effects = %d, want 7", got)
  30. }
  31. }
  32. // The board takes one effect ID more than its definition names, and the tool has
  33. // no source for that one's name. It says "unknown" rather than inventing a label,
  34. // which is the whole rule: a name the vendor does not publish is a name the tool
  35. // would be making up.
  36. func TestEffectTheDefinitionDoesNotNameIsUnknown(t *testing.T) {
  37. catalog := impact80(t)
  38. tests := []struct {
  39. id uint8
  40. want string
  41. }{
  42. {45, "riverflow"},
  43. {46, unknownEffectName},
  44. {47, unknownEffectName},
  45. }
  46. for _, tt := range tests {
  47. if got := catalog.EffectName(via.ChannelRgbMatrix, tt.id); got != tt.want {
  48. t.Errorf("EffectName(rgb_matrix, %d) = %q, want %q", tt.id, got, tt.want)
  49. }
  50. }
  51. if _, ok := catalog.EffectID(via.ChannelRgbMatrix, "freeze"); ok {
  52. t.Error("EffectID(rgb_matrix, \"freeze\") = found, want not found: no source names that ID")
  53. }
  54. }
  55. // Nothing is compiled in for any board, so a board the data directory does not
  56. // describe has no names at all and is driven through raw effect IDs.
  57. func TestThereIsNoCatalogWithoutADefinition(t *testing.T) {
  58. if catalog := NewCatalog("", nil); len(catalog.Effects(via.ChannelRgbMatrix)) != 0 {
  59. t.Error("a catalog with no entries has effects, want none")
  60. }
  61. if _, ok := impact80(t).EffectID(via.ChannelRgbMatrix, "riverflow"); !ok {
  62. t.Error("the vendored definition does not name riverflow, want it to")
  63. }
  64. }
  65. func TestCatalogIDResolvesNamesAndAliases(t *testing.T) {
  66. catalog := impact80(t)
  67. tests := []struct {
  68. channel via.Channel
  69. name string
  70. want uint8
  71. }{
  72. {via.ChannelRgblight, "light", 5},
  73. {via.ChannelRgblight, "solid", 5},
  74. {via.ChannelRgblight, "breathe", 4},
  75. {via.ChannelRgbMatrix, "solid_color", 1},
  76. {via.ChannelRgbMatrix, "rainbow_moving_chevron", 17},
  77. }
  78. for _, tt := range tests {
  79. got, ok := catalog.EffectID(tt.channel, tt.name)
  80. if !ok {
  81. t.Errorf("EffectID(%d, %q) not found", tt.channel, tt.name)
  82. continue
  83. }
  84. if got != tt.want {
  85. t.Errorf("EffectID(%d, %q) = %d, want %d", tt.channel, tt.name, got, tt.want)
  86. }
  87. }
  88. }
  89. func TestCatalogEffectNameReportsUnknownForAnUnknownID(t *testing.T) {
  90. catalog := impact80(t)
  91. if got := catalog.EffectName(via.ChannelRgbMatrix, 200); got != "unknown" {
  92. t.Errorf("EffectName(3, 200) = %q, want %q", got, "unknown")
  93. }
  94. }
  95. func TestCatalogDefaultEffectPerChannel(t *testing.T) {
  96. catalog := impact80(t)
  97. // The first effect the board's own list names that is not the off entry.
  98. if got, ok := catalog.DefaultEffect(via.ChannelRgblight); !ok || got != 1 {
  99. t.Errorf("DefaultEffect(2) = %d, %t, want 1, true", got, ok)
  100. }
  101. if got, ok := catalog.DefaultEffect(via.ChannelRgbMatrix); !ok || got != 1 {
  102. t.Errorf("DefaultEffect(3) = %d, %t, want 1, 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(), "effect <index>") {
  111. t.Errorf("error = %q, want it to point at the ID form", 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 named 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. }