catalog_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. // The advice the error carries has to be an invocation that runs. The zone is
  106. // `effect`'s first argument and is required, so `effect <index>` on its own is
  107. // read as a channel name rather than as an effect ID.
  108. func TestResolveEffectWithoutACatalogRefuses(t *testing.T) {
  109. _, _, err := ResolveEffect(nil, "wave", []via.Channel{via.ChannelRgblight}, false)
  110. if err == nil {
  111. t.Fatal("ResolveEffect(nil, ...) expected an error, got nil")
  112. }
  113. if !strings.Contains(err.Error(), "effect <zone> <index>") {
  114. t.Errorf("error = %q, want it to point at the ID form", err)
  115. }
  116. if strings.Contains(err.Error(), "`effect <index>`") {
  117. t.Errorf("error = %q, want the zone spelled out: a bare `effect <index>` is read as a channel name", err)
  118. }
  119. }
  120. // ID 5 is light on channel 2 and rainbow_beacon on channel 3, so the same name
  121. // cannot be applied to both.
  122. func TestResolveEffectSkipsAChannelThatDoesNotSupportTheName(t *testing.T) {
  123. catalog := impact80(t)
  124. targets, skipped, err := ResolveEffect(catalog, "light", []via.Channel{via.ChannelRgblight, via.ChannelRgbMatrix}, false)
  125. if err != nil {
  126. t.Fatalf("ResolveEffect() error = %v", err)
  127. }
  128. if len(targets) != 1 || targets[0].Channel != via.ChannelRgblight || targets[0].ID != 5 {
  129. t.Errorf("targets = %+v, want one target on channel 2 with id 5", targets)
  130. }
  131. if len(skipped) != 1 || skipped[0] != "rgb_matrix" {
  132. t.Errorf("skipped = %v, want [rgb_matrix]", skipped)
  133. }
  134. }
  135. // Asking for the unsupported channel by name is an error, not a skip: a command
  136. // that quietly did nothing would look like a command that worked.
  137. func TestResolveEffectRejectsTheOnlyChannelWhenItDoesNotSupportTheName(t *testing.T) {
  138. catalog := impact80(t)
  139. _, _, err := ResolveEffect(catalog, "light", []via.Channel{via.ChannelRgbMatrix}, true)
  140. if err == nil {
  141. t.Fatal("ResolveEffect() expected an error, want light rejected on the backlight")
  142. }
  143. }
  144. func TestResolveEffectRejectsANameNoChannelKnows(t *testing.T) {
  145. catalog := impact80(t)
  146. if _, _, err := ResolveEffect(catalog, "nope", []via.Channel{via.ChannelRgblight}, true); err == nil {
  147. t.Fatal("ResolveEffect() expected an error for an unknown name")
  148. }
  149. }
  150. // "static" is a documented alias. It must resolve through every caller, not
  151. // only the one that rewrites the string before calling: the backlight catalog
  152. // spells its solid effect solid_color, so the alias chain needs both steps.
  153. func TestResolveEffectAcceptsTheStaticAliasOnEveryPath(t *testing.T) {
  154. catalog := impact80(t)
  155. for _, ch := range []via.Channel{via.ChannelRgblight, via.ChannelRgbMatrix, via.ChannelAudio} {
  156. targets, _, err := ResolveEffect(catalog, "static", []via.Channel{ch}, true)
  157. if err != nil {
  158. t.Errorf("ResolveEffect(static on channel %d) error = %v", ch, err)
  159. continue
  160. }
  161. if len(targets) != 1 {
  162. t.Errorf("ResolveEffect(static on channel %d) = %d targets, want 1", ch, len(targets))
  163. }
  164. }
  165. }
  166. // Whether a command may skip an unsupported effect is the caller's knowledge,
  167. // not something the channel count can tell it. A board with a single lighting
  168. // channel and no zone named must still behave like a default command.
  169. func TestResolveEffectDistinguishesExplicitFromDefault(t *testing.T) {
  170. catalog := impact80(t)
  171. both := []via.Channel{via.ChannelRgblight, via.ChannelRgbMatrix}
  172. // Default command, one channel cannot do it: skipped, not refused.
  173. targets, skipped, err := ResolveEffect(catalog, "rainbow_moving_chevron", both, false)
  174. if err != nil {
  175. t.Errorf("default command error = %v, want the unsupported channel skipped", err)
  176. }
  177. if len(skipped) != 1 || skipped[0] != "rgblight" {
  178. t.Errorf("skipped = %v, want [rgblight]", skipped)
  179. }
  180. if len(targets) != 1 || targets[0].Channel != via.ChannelRgbMatrix {
  181. t.Errorf("targets = %+v, want the backlight only", targets)
  182. }
  183. // Same name, named channel only: refused.
  184. if _, _, err := ResolveEffect(catalog, "rainbow_moving_chevron", []via.Channel{via.ChannelRgblight}, true); err == nil {
  185. t.Error("explicit request error = nil, want it refused")
  186. }
  187. // One channel reported by a board that has the name elsewhere: skipped with a
  188. // warning rather than called unknown, and never silently applied.
  189. targets, skipped, err = ResolveEffect(catalog, "rainbow_moving_chevron", []via.Channel{via.ChannelRgblight}, false)
  190. if err != nil {
  191. t.Errorf("single-channel default command error = %v, want a skip", err)
  192. }
  193. if len(targets) != 0 || len(skipped) != 1 {
  194. t.Errorf("targets = %+v, skipped = %v, want nothing applied and one skip", targets, skipped)
  195. }
  196. }
  197. // A name the board has but the named channel does not is not an unknown effect:
  198. // the user named a real effect and put it on the wrong channel, and that is a
  199. // different mistake with a different fix.
  200. func TestResolveEffectSaysTheChannelDoesNotSupportTheName(t *testing.T) {
  201. catalog := impact80(t)
  202. _, _, err := ResolveEffect(catalog, "rainbow_moving_chevron", []via.Channel{via.ChannelRgblight}, true)
  203. if err == nil {
  204. t.Fatal("ResolveEffect() expected an error, got nil")
  205. }
  206. if !strings.Contains(err.Error(), "not supported on rgblight") {
  207. t.Errorf("error = %q, want it to name the channel that cannot do it", err)
  208. }
  209. }
  210. // And a name no channel of the board has stays an unknown effect.
  211. func TestResolveEffectKeepsUnknownEffectForANameTheBoardLacks(t *testing.T) {
  212. catalog := impact80(t)
  213. _, _, err := ResolveEffect(catalog, "nope", []via.Channel{via.ChannelRgblight}, true)
  214. if err == nil {
  215. t.Fatal("ResolveEffect() expected an error, got nil")
  216. }
  217. if !strings.Contains(err.Error(), "unknown effect") {
  218. t.Errorf("error = %q, want it to stay an unknown effect", err)
  219. }
  220. }
  221. // A definition can be found and still name nothing: it refers to VIA's built-in
  222. // lighting menu, whose names are in VIA's own code and not in the file. The GMMK
  223. // Pro is one of these. Reporting an unknown effect there points at a typo in a name
  224. // the tool does not hold a single one of, and fetching again changes nothing.
  225. func TestResolveEffectSaysTheDefinitionNamesNoEffects(t *testing.T) {
  226. catalog := NewCatalog("GMMK Pro", nil)
  227. _, _, err := ResolveEffect(catalog, "breathing", []via.Channel{via.ChannelRgbMatrix}, true)
  228. if err == nil {
  229. t.Fatal("ResolveEffect() expected an error, got nil")
  230. }
  231. if !strings.Contains(err.Error(), "GMMK Pro") {
  232. t.Errorf("error = %q, want it to name the board", err)
  233. }
  234. if strings.Contains(err.Error(), "unknown effect") {
  235. t.Errorf("error = %q, want it not to blame the name the user typed", err)
  236. }
  237. if !strings.Contains(err.Error(), "effect <zone> <index>") {
  238. t.Errorf("error = %q, want it to point at the ID form, which is all a board with no names accepts", err)
  239. }
  240. }
  241. // The two ways of having no names are different and the message has to tell them
  242. // apart: one is fixed by fetching a definition, the other is not fixed by anything
  243. // the user can run.
  244. func TestResolveEffectTellsAMissingDefinitionFromOneThatNamesNothing(t *testing.T) {
  245. _, _, missing := ResolveEffect(nil, "breathing", []via.Channel{via.ChannelRgbMatrix}, true)
  246. _, _, empty := ResolveEffect(NewCatalog("GMMK Pro", nil), "breathing", []via.Channel{via.ChannelRgbMatrix}, true)
  247. if missing == nil || empty == nil {
  248. t.Fatal("both cases have to be errors")
  249. }
  250. if missing.Error() == empty.Error() {
  251. t.Errorf("both messages read %q, want them told apart", empty)
  252. }
  253. if !strings.Contains(missing.Error(), "keyboard fetch") {
  254. t.Errorf("missing-definition error = %q, want it to point at `keyboard fetch`", missing)
  255. }
  256. }
  257. // A catalog that names one channel names effects, so the empty-catalog message must
  258. // not swallow a real lookup on that channel.
  259. func TestHasNamesIsAboutTheWholeCatalog(t *testing.T) {
  260. var none *Catalog
  261. if none.HasNames() {
  262. t.Error("a nil catalog has names, want none")
  263. }
  264. if NewCatalog("GMMK Pro", nil).HasNames() {
  265. t.Error("a catalog with no entries has names, want none")
  266. }
  267. if !impact80(t).HasNames() {
  268. t.Error("the vendored definition has no names, want its effect list")
  269. }
  270. }