speed_verify_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package main
  2. import (
  3. "bytes"
  4. "strings"
  5. "testing"
  6. intrgb "netdome.biz/paul/qmk-rgb/internal/rgb"
  7. "netdome.biz/paul/qmk-rgb/internal/via"
  8. )
  9. func runSpeed(t *testing.T, applied map[via.Channel]uint8, zoneFlag string, arg string) (stdout, stderr string) {
  10. t.Helper()
  11. proto := &verifyingProtocol{applied: applied}
  12. originalOpen := openRGBProtocol
  13. originalZone := targetZone
  14. t.Cleanup(func() {
  15. openRGBProtocol = originalOpen
  16. targetZone = originalZone
  17. })
  18. openRGBProtocol = func() (rgbProtocol, error) { return proto, nil }
  19. targetZone = zoneFlag
  20. var out, errOut bytes.Buffer
  21. cmd := NewSpeedCmd()
  22. cmd.SetOut(&out)
  23. cmd.SetErr(&errOut)
  24. cmd.SetArgs([]string{arg})
  25. if err := cmd.Execute(); err != nil {
  26. t.Fatalf("speed returned error: %v", err)
  27. }
  28. return out.String(), errOut.String()
  29. }
  30. // The Impact 80 collapses every speed above 0 to 4 on the logo and side
  31. // channels, so `speed 60` claiming success there is the same lie brightness
  32. // had.
  33. func TestSpeedReportsPlainMessageWhenAllZonesMatch(t *testing.T) {
  34. stdout, stderr := runSpeed(t, map[via.Channel]uint8{
  35. via.ChannelRgblight: 60,
  36. via.ChannelRgbMatrix: 60,
  37. via.ChannelAudio: 60,
  38. }, "", "60")
  39. if strings.TrimSpace(stdout) != "Speed set to 60" {
  40. t.Errorf("stdout = %q, want the exact success message", stdout)
  41. }
  42. if stderr != "" {
  43. t.Errorf("stderr = %q, want nothing when the request was met", stderr)
  44. }
  45. }
  46. func TestSpeedSummarisesAppliedValuesOnMismatch(t *testing.T) {
  47. stdout, _ := runSpeed(t, map[via.Channel]uint8{
  48. via.ChannelRgblight: 4,
  49. via.ChannelRgbMatrix: 60,
  50. via.ChannelAudio: 4,
  51. }, "", "60")
  52. for _, want := range []string{"logo 4", "backlight 60", "side 4", "requested 60"} {
  53. if !strings.Contains(stdout, want) {
  54. t.Errorf("stdout = %q, want it to contain %q", stdout, want)
  55. }
  56. }
  57. if strings.Contains(stdout, "set to 60") {
  58. t.Errorf("stdout = %q, must not claim 60 was set when logo and side hold 4", stdout)
  59. }
  60. if strings.Count(strings.TrimSpace(stdout), "\n") != 0 {
  61. t.Errorf("stdout = %q, want exactly one line", stdout)
  62. }
  63. }
  64. func TestSpeedNamesOnlySelectedZones(t *testing.T) {
  65. stdout, _ := runSpeed(t, map[via.Channel]uint8{via.ChannelAudio: 4}, "side", "60")
  66. if !strings.Contains(stdout, "side 4") {
  67. t.Errorf("stdout = %q, want side reported", stdout)
  68. }
  69. for _, unwanted := range []string{"logo", "backlight"} {
  70. if strings.Contains(stdout, unwanted) {
  71. t.Errorf("stdout = %q, must not mention %s when only side was selected", stdout, unwanted)
  72. }
  73. }
  74. }
  75. // speed 0 is a genuine value on every channel, not a collapsed one.
  76. func TestSpeedZeroIsReportedAsApplied(t *testing.T) {
  77. stdout, _ := runSpeed(t, map[via.Channel]uint8{
  78. via.ChannelRgblight: 0,
  79. via.ChannelRgbMatrix: 0,
  80. via.ChannelAudio: 0,
  81. }, "", "0")
  82. if strings.TrimSpace(stdout) != "Speed set to 0" {
  83. t.Errorf("stdout = %q, want the success message for a value the keyboard kept", stdout)
  84. }
  85. }
  86. // setValueVerified must be parameter-agnostic so both commands share it.
  87. func TestSetValueVerifiedReadsBackTheRequestedParam(t *testing.T) {
  88. proto := &verifyingProtocol{
  89. applied: map[via.Channel]uint8{via.ChannelRgbMatrix: 159},
  90. }
  91. results, err := setValueVerified(proto, []intrgb.Zone{intrgb.ZoneBacklight},
  92. uint8(intrgb.Speed), 100)
  93. if err != nil {
  94. t.Fatalf("setValueVerified() error = %v", err)
  95. }
  96. if len(results) != 1 {
  97. t.Fatalf("results = %d, want 1", len(results))
  98. }
  99. if results[0].Applied != 159 || results[0].Requested != 100 {
  100. t.Errorf("results[0] = %+v, want Requested 100 Applied 159", results[0])
  101. }
  102. }