info_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "reflect"
  7. "strings"
  8. "testing"
  9. "netdome.biz/paul/qmk-rgb/internal/via"
  10. )
  11. type infoKey struct {
  12. channel via.Channel
  13. param uint8
  14. }
  15. // DetectChannels lets the info command's target seam reuse this fake.
  16. func (f *fakeInfoProtocol) DetectChannels() ([]via.Channel, error) {
  17. return impact80Channels(), nil
  18. }
  19. type fakeInfoProtocol struct {
  20. values map[infoKey][]byte
  21. errors map[infoKey]error
  22. calls []infoKey
  23. }
  24. func (f *fakeInfoProtocol) GetValue(channel via.Channel, param uint8) ([]byte, error) {
  25. key := infoKey{channel: channel, param: param}
  26. f.calls = append(f.calls, key)
  27. if err := f.errors[key]; err != nil {
  28. return nil, err
  29. }
  30. return append([]byte(nil), f.values[key]...), nil
  31. }
  32. func (f *fakeInfoProtocol) SetValue(via.Channel, uint8, uint8) error {
  33. return errors.New("unexpected SetValue call")
  34. }
  35. func (f *fakeInfoProtocol) SetColor(via.Channel, uint8, uint8) error {
  36. return errors.New("unexpected SetColor call")
  37. }
  38. func (f *fakeInfoProtocol) Close() error {
  39. return nil
  40. }
  41. func TestInfoSuccessfulRecord(t *testing.T) {
  42. t.Cleanup(vendoredDefinitions(t))
  43. protocol := &fakeInfoProtocol{
  44. values: map[infoKey][]byte{
  45. {channel: 3, param: 1}: {160},
  46. {channel: 3, param: 2}: {17},
  47. {channel: 3, param: 3}: {2},
  48. {channel: 3, param: 4}: {85, 255},
  49. {channel: 2, param: 1}: {80},
  50. {channel: 2, param: 2}: {4},
  51. {channel: 2, param: 3}: {1},
  52. {channel: 2, param: 4}: {0, 128},
  53. },
  54. errors: make(map[infoKey]error),
  55. }
  56. zones := []via.Channel{via.ChannelRgbMatrix, via.ChannelRgblight}
  57. got, err := readInfo(protocol, zones, impact80Display(), impact80Catalog(t))
  58. if err != nil {
  59. t.Fatalf("readInfo() unexpected error: %v", err)
  60. }
  61. want := infoOutput{
  62. Enabled: true,
  63. Mode: "rainbow_moving_chevron",
  64. Brightness: 160,
  65. Speed: 2,
  66. Zones: []zoneInfo{
  67. {
  68. Zone: "backlight",
  69. Channel: 3,
  70. Enabled: true,
  71. Effect: "rainbow_moving_chevron",
  72. EffectID: 17,
  73. Brightness: 160,
  74. Speed: 2,
  75. Color: infoColor{Hue: 85, Saturation: 255},
  76. },
  77. {
  78. Zone: "logo",
  79. Channel: 2,
  80. Enabled: true,
  81. Effect: "breathe",
  82. EffectID: 4,
  83. Brightness: 80,
  84. Speed: 1,
  85. Color: infoColor{Hue: 0, Saturation: 128},
  86. },
  87. },
  88. }
  89. if !reflect.DeepEqual(got, want) {
  90. t.Fatalf("readInfo() = %+v, want %+v", got, want)
  91. }
  92. data, err := json.Marshal(got)
  93. if err != nil {
  94. t.Fatalf("json.Marshal() error = %v", err)
  95. }
  96. wantJSON := `{"enabled":true,"mode":"rainbow_moving_chevron","brightness":160,"speed":2,"zones":[{"zone":"backlight","channel":3,"enabled":true,"effect":"rainbow_moving_chevron","effectId":17,"brightness":160,"speed":2,"color":{"hue":85,"saturation":255}},{"zone":"logo","channel":2,"enabled":true,"effect":"breathe","effectId":4,"brightness":80,"speed":1,"color":{"hue":0,"saturation":128}}]}`
  97. if string(data) != wantJSON {
  98. t.Errorf("JSON = %s, want %s", data, wantJSON)
  99. }
  100. }
  101. func TestInfoTextOutputIsOneLinePerChannelAndNoHeader(t *testing.T) {
  102. t.Cleanup(vendoredDefinitions(t))
  103. // Three channels running three different effects, which is the case a header
  104. // could not honestly summarise: it used to print the first channel's effect
  105. // above all of them, so `breathe` appeared twice and read as a mode the whole
  106. // keyboard was in.
  107. protocol := &fakeInfoProtocol{
  108. values: map[infoKey][]byte{
  109. {channel: 2, param: 1}: {75},
  110. {channel: 2, param: 2}: {4},
  111. {channel: 2, param: 3}: {1},
  112. {channel: 2, param: 4}: {3, 255},
  113. {channel: 3, param: 1}: {255},
  114. {channel: 3, param: 2}: {5},
  115. {channel: 3, param: 3}: {4},
  116. {channel: 3, param: 4}: {3, 255},
  117. {channel: 4, param: 1}: {160},
  118. {channel: 4, param: 2}: {1},
  119. {channel: 4, param: 3}: {1},
  120. {channel: 4, param: 4}: {8, 255},
  121. },
  122. errors: make(map[infoKey]error),
  123. }
  124. t.Cleanup(stubOpenTarget(t, protocol, impact80Display(), impact80Channels(), 0x36B0, 0x309F))
  125. var stdout bytes.Buffer
  126. cmd := NewInfoCmd()
  127. cmd.SetOut(&stdout)
  128. cmd.SilenceErrors = true
  129. cmd.SilenceUsage = true
  130. cmd.SetArgs(nil)
  131. if err := cmd.Execute(); err != nil {
  132. t.Fatalf("Execute() error = %v", err)
  133. }
  134. want := " logo on breathe brightness 75 speed 1 color hsv:3,255\n" +
  135. " backlight on breathing brightness 255 speed 4 color hsv:3,255\n" +
  136. " side on wave brightness 160 speed 1 color hsv:8,255\n"
  137. if got := stdout.String(); got != want {
  138. t.Errorf("stdout =\n%q\nwant\n%q", got, want)
  139. }
  140. }
  141. func TestInfoErrorRecord(t *testing.T) {
  142. t.Cleanup(vendoredDefinitions(t))
  143. protocol := &fakeInfoProtocol{
  144. values: map[infoKey][]byte{
  145. {channel: 2, param: 1}: {120},
  146. {channel: 2, param: 2}: {4},
  147. {channel: 2, param: 3}: {3},
  148. {channel: 2, param: 4}: {10, 20},
  149. {channel: 3, param: 1}: {90},
  150. {channel: 3, param: 2}: {5},
  151. {channel: 3, param: 3}: {4},
  152. {channel: 3, param: 4}: {30, 40},
  153. },
  154. errors: map[infoKey]error{
  155. {channel: 4, param: 1}: errors.New("side unavailable"),
  156. },
  157. }
  158. zones := []via.Channel{via.ChannelRgblight, via.ChannelAudio, via.ChannelRgbMatrix}
  159. got, err := readInfo(protocol, zones, impact80Display(), impact80Catalog(t))
  160. if err == nil {
  161. t.Fatal("readInfo() expected error, got nil")
  162. }
  163. want := infoOutput{
  164. Enabled: true,
  165. Mode: "breathe",
  166. Brightness: 120,
  167. Speed: 3,
  168. Zones: []zoneInfo{
  169. {
  170. Zone: "logo",
  171. Channel: 2,
  172. Enabled: true,
  173. Effect: "breathe",
  174. EffectID: 4,
  175. Brightness: 120,
  176. Speed: 3,
  177. Color: infoColor{Hue: 10, Saturation: 20},
  178. },
  179. {
  180. Zone: "side",
  181. Channel: 4,
  182. Error: "side unavailable",
  183. },
  184. {
  185. // The vendor spells this channel's breathing effect differently
  186. // from the other two channels': "breathing" here and "breathe"
  187. // there. Both are the vendor's own, and the tool reports each
  188. // channel as its definition writes it.
  189. Zone: "backlight",
  190. Channel: 3,
  191. Enabled: true,
  192. Effect: "breathing",
  193. EffectID: 5,
  194. Brightness: 90,
  195. Speed: 4,
  196. Color: infoColor{Hue: 30, Saturation: 40},
  197. },
  198. },
  199. }
  200. if !reflect.DeepEqual(got, want) {
  201. t.Fatalf("readInfo() = %+v, want %+v", got, want)
  202. }
  203. data, err := json.Marshal(got)
  204. if err != nil {
  205. t.Fatalf("json.Marshal() error = %v", err)
  206. }
  207. var envelope struct {
  208. Zones []map[string]json.RawMessage `json:"zones"`
  209. }
  210. if err := json.Unmarshal(data, &envelope); err != nil {
  211. t.Fatalf("json.Unmarshal() error = %v", err)
  212. }
  213. if len(envelope.Zones) != 3 {
  214. t.Fatalf("zones = %d, want 3", len(envelope.Zones))
  215. }
  216. if _, ok := envelope.Zones[1]["error"]; !ok {
  217. t.Errorf("error zone JSON = %v, want error key", envelope.Zones[1])
  218. }
  219. }
  220. func TestInfoCommandPrintsJSONBeforeReturningError(t *testing.T) {
  221. withJSON(t)
  222. protocol := &fakeInfoProtocol{
  223. values: map[infoKey][]byte{
  224. {channel: 2, param: 1}: {120},
  225. {channel: 2, param: 2}: {4},
  226. {channel: 2, param: 3}: {3},
  227. {channel: 2, param: 4}: {10, 20},
  228. {channel: 3, param: 1}: {90},
  229. {channel: 3, param: 2}: {5},
  230. {channel: 3, param: 3}: {4},
  231. {channel: 3, param: 4}: {30, 40},
  232. },
  233. errors: map[infoKey]error{
  234. {channel: 4, param: 1}: errors.New("side unavailable"),
  235. },
  236. }
  237. t.Cleanup(stubOpenTarget(t, protocol, impact80Display(), impact80Channels(), 0x36B0, 0x309F))
  238. var stdout bytes.Buffer
  239. var stderr bytes.Buffer
  240. cmd := NewInfoCmd()
  241. cmd.SetOut(&stdout)
  242. cmd.SetErr(&stderr)
  243. cmd.SilenceErrors = true
  244. cmd.SilenceUsage = true
  245. cmd.SetArgs(nil)
  246. err := cmd.Execute()
  247. if err == nil {
  248. t.Fatal("Execute() expected query error, got nil")
  249. }
  250. if !strings.Contains(stdout.String(), `"zones"`) || !strings.Contains(stdout.String(), `"error"`) {
  251. t.Errorf("stdout = %q, want JSON containing zones and error", stdout.String())
  252. }
  253. if stderr.Len() != 0 {
  254. t.Errorf("stderr = %q, want no Cobra usage/error noise", stderr.String())
  255. }
  256. }