info_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 TestInfoErrorRecord(t *testing.T) {
  102. t.Cleanup(vendoredDefinitions(t))
  103. protocol := &fakeInfoProtocol{
  104. values: map[infoKey][]byte{
  105. {channel: 2, param: 1}: {120},
  106. {channel: 2, param: 2}: {4},
  107. {channel: 2, param: 3}: {3},
  108. {channel: 2, param: 4}: {10, 20},
  109. {channel: 3, param: 1}: {90},
  110. {channel: 3, param: 2}: {5},
  111. {channel: 3, param: 3}: {4},
  112. {channel: 3, param: 4}: {30, 40},
  113. },
  114. errors: map[infoKey]error{
  115. {channel: 4, param: 1}: errors.New("side unavailable"),
  116. },
  117. }
  118. zones := []via.Channel{via.ChannelRgblight, via.ChannelAudio, via.ChannelRgbMatrix}
  119. got, err := readInfo(protocol, zones, impact80Display(), impact80Catalog(t))
  120. if err == nil {
  121. t.Fatal("readInfo() expected error, got nil")
  122. }
  123. want := infoOutput{
  124. Enabled: true,
  125. Mode: "breathe",
  126. Brightness: 120,
  127. Speed: 3,
  128. Zones: []zoneInfo{
  129. {
  130. Zone: "logo",
  131. Channel: 2,
  132. Enabled: true,
  133. Effect: "breathe",
  134. EffectID: 4,
  135. Brightness: 120,
  136. Speed: 3,
  137. Color: infoColor{Hue: 10, Saturation: 20},
  138. },
  139. {
  140. Zone: "side",
  141. Channel: 4,
  142. Error: "side unavailable",
  143. },
  144. {
  145. // The vendor spells this channel's breathing effect differently
  146. // from the other two channels': "breathing" here and "breathe"
  147. // there. Both are the vendor's own, and the tool reports each
  148. // channel as its definition writes it.
  149. Zone: "backlight",
  150. Channel: 3,
  151. Enabled: true,
  152. Effect: "breathing",
  153. EffectID: 5,
  154. Brightness: 90,
  155. Speed: 4,
  156. Color: infoColor{Hue: 30, Saturation: 40},
  157. },
  158. },
  159. }
  160. if !reflect.DeepEqual(got, want) {
  161. t.Fatalf("readInfo() = %+v, want %+v", got, want)
  162. }
  163. data, err := json.Marshal(got)
  164. if err != nil {
  165. t.Fatalf("json.Marshal() error = %v", err)
  166. }
  167. var envelope struct {
  168. Zones []map[string]json.RawMessage `json:"zones"`
  169. }
  170. if err := json.Unmarshal(data, &envelope); err != nil {
  171. t.Fatalf("json.Unmarshal() error = %v", err)
  172. }
  173. if len(envelope.Zones) != 3 {
  174. t.Fatalf("zones = %d, want 3", len(envelope.Zones))
  175. }
  176. if _, ok := envelope.Zones[1]["error"]; !ok {
  177. t.Errorf("error zone JSON = %v, want error key", envelope.Zones[1])
  178. }
  179. }
  180. func TestInfoCommandPrintsJSONBeforeReturningError(t *testing.T) {
  181. withJSON(t)
  182. protocol := &fakeInfoProtocol{
  183. values: map[infoKey][]byte{
  184. {channel: 2, param: 1}: {120},
  185. {channel: 2, param: 2}: {4},
  186. {channel: 2, param: 3}: {3},
  187. {channel: 2, param: 4}: {10, 20},
  188. {channel: 3, param: 1}: {90},
  189. {channel: 3, param: 2}: {5},
  190. {channel: 3, param: 3}: {4},
  191. {channel: 3, param: 4}: {30, 40},
  192. },
  193. errors: map[infoKey]error{
  194. {channel: 4, param: 1}: errors.New("side unavailable"),
  195. },
  196. }
  197. t.Cleanup(stubOpenTarget(t, protocol, impact80Display(), impact80Channels(), 0x36B0, 0x309F))
  198. var stdout bytes.Buffer
  199. var stderr bytes.Buffer
  200. cmd := NewInfoCmd()
  201. cmd.SetOut(&stdout)
  202. cmd.SetErr(&stderr)
  203. cmd.SilenceErrors = true
  204. cmd.SilenceUsage = true
  205. cmd.SetArgs(nil)
  206. err := cmd.Execute()
  207. if err == nil {
  208. t.Fatal("Execute() expected query error, got nil")
  209. }
  210. if !strings.Contains(stdout.String(), `"zones"`) || !strings.Contains(stdout.String(), `"error"`) {
  211. t.Errorf("stdout = %q, want JSON containing zones and error", stdout.String())
  212. }
  213. if stderr.Len() != 0 {
  214. t.Errorf("stderr = %q, want no Cobra usage/error noise", stderr.String())
  215. }
  216. }