speed_verify_test.go 3.3 KB

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