speed_verify_test.go 3.4 KB

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