mode_verify_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package main
  2. import (
  3. "errors"
  4. "strings"
  5. "testing"
  6. intrgb "netdome.biz/paul/qmk-rgb/internal/rgb"
  7. "netdome.biz/paul/qmk-rgb/internal/via"
  8. )
  9. // A raw index above what the board implements is not refused: the Impact 80
  10. // clamps it to the highest ID it holds, so the command would report an index the
  11. // keyboard never took. mode is the escape hatch for a board without a catalog,
  12. // which is exactly where an unverified success would hurt most.
  13. func TestModeCommandReportsClampedIndex(t *testing.T) {
  14. proto := &verifyingProtocol{applied: map[via.Channel]uint8{via.ChannelRgbMatrix: 46}}
  15. t.Cleanup(impact80Target(t, proto))
  16. var out, errOut strings.Builder
  17. cmd := NewModeCmd()
  18. cmd.SetOut(&out)
  19. cmd.SetErr(&errOut)
  20. cmd.SetArgs([]string{"99"})
  21. if err := cmd.Execute(); err != nil {
  22. t.Fatalf("mode returned error: %v", err)
  23. }
  24. stdout := out.String()
  25. if strings.Contains(stdout, "Mode set to index 99") {
  26. t.Errorf("stdout = %q, want no success claim for an index the keyboard clamped", stdout)
  27. }
  28. for _, want := range []string{"backlight", "46", "99"} {
  29. if !strings.Contains(stdout, want) {
  30. t.Errorf("stdout = %q, want it to name %q", stdout, want)
  31. }
  32. }
  33. }
  34. // An index the board takes must stay a plain success line, so the summary is
  35. // reserved for a real divergence.
  36. func TestModeCommandStaysQuietWhenApplied(t *testing.T) {
  37. proto := &verifyingProtocol{
  38. applied: map[via.Channel]uint8{
  39. via.ChannelRgblight: 5,
  40. via.ChannelRgbMatrix: 20,
  41. via.ChannelAudio: 5,
  42. },
  43. }
  44. t.Cleanup(impact80Target(t, proto))
  45. var out, errOut strings.Builder
  46. cmd := NewModeCmd()
  47. cmd.SetOut(&out)
  48. cmd.SetErr(&errOut)
  49. cmd.SetArgs([]string{"5"})
  50. if err := cmd.Execute(); err != nil {
  51. t.Fatalf("mode returned error: %v", err)
  52. }
  53. // Only the backlight stores 20, so two of three zones deviate and the
  54. // summary is expected; a silent run would mean the read-back is skipped.
  55. if out.Len() == 0 {
  56. t.Error("stdout = \"\", want a divergence summary")
  57. }
  58. }
  59. func TestSetModeVerifiedPropagatesReadError(t *testing.T) {
  60. proto := &verifyingProtocol{getErr: errors.New("read timeout")}
  61. _, err := setModeVerified(proto, []via.Channel{via.ChannelRgbMatrix}, impact80Display(), 46)
  62. if err == nil {
  63. t.Fatal("setModeVerified() expected read error, got nil")
  64. }
  65. }
  66. // The mode command writes the effect value ID, the same one effect writes.
  67. func TestSetModeVerifiedUsesTheEffectValueID(t *testing.T) {
  68. proto := &verifyingProtocol{applied: map[via.Channel]uint8{via.ChannelRgbMatrix: 46}}
  69. if _, err := setModeVerified(proto, []via.Channel{via.ChannelRgbMatrix}, impact80Display(), 46); err != nil {
  70. t.Fatalf("setModeVerified() error = %v", err)
  71. }
  72. if len(proto.reports) != 1 {
  73. t.Fatalf("reports = %d, want 1", len(proto.reports))
  74. }
  75. if got := proto.reports[0].param; got != uint8(intrgb.EffectID) {
  76. t.Errorf("param = 0x%02x, want 0x%02x", got, uint8(intrgb.EffectID))
  77. }
  78. }