rgb_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package main
  2. import (
  3. "reflect"
  4. "strings"
  5. "testing"
  6. "github.com/spf13/cobra"
  7. intdevice "netdome.biz/paul/qmk-rgb/internal/device"
  8. "netdome.biz/paul/qmk-rgb/internal/via"
  9. )
  10. func TestLightingEnabledRequiresActiveEffectAndBrightness(t *testing.T) {
  11. cases := []struct {
  12. name string
  13. mode uint8
  14. brightness uint8
  15. want bool
  16. }{
  17. {"active", 1, 160, true},
  18. {"zero brightness", 1, 0, false},
  19. {"disabled effect", 0, 160, false},
  20. {"both zero", 0, 0, false},
  21. }
  22. for _, tc := range cases {
  23. t.Run(tc.name, func(t *testing.T) {
  24. if got := lightingEnabled(tc.mode, tc.brightness); got != tc.want {
  25. t.Errorf("lightingEnabled(%d, %d) = %t, want %t", tc.mode, tc.brightness, got, tc.want)
  26. }
  27. })
  28. }
  29. }
  30. // stubOneImpact80 puts one keyboard in front of the root command's validation,
  31. // which now resolves the target before a subcommand runs.
  32. func stubOneImpact80(t *testing.T) {
  33. t.Helper()
  34. stubDiscovery(t, []intdevice.Device{{Index: 1, Path: "hid-device-0", VendorID: 0x36B0, ProductID: 0x309F}})
  35. }
  36. func TestDeviceFlagIsInheritedByCommands(t *testing.T) {
  37. stubOneImpact80(t)
  38. ran := false
  39. cmd := newRootCommand()
  40. cmd.AddCommand(&cobra.Command{
  41. Use: "probe",
  42. Run: func(*cobra.Command, []string) { ran = true },
  43. })
  44. cmd.SetArgs([]string{"--device", "1", "probe"})
  45. if err := cmd.Execute(); err != nil {
  46. t.Fatalf("Execute() error = %v", err)
  47. }
  48. if !ran {
  49. t.Fatal("child command did not run")
  50. }
  51. if targetDevice != "1" {
  52. t.Errorf("targetDevice = %q, want %q", targetDevice, "1")
  53. }
  54. }
  55. func TestResolveChannelsDefaultsToEveryDetectedChannel(t *testing.T) {
  56. proto := &verifyingProtocol{}
  57. got, err := resolveChannels(proto, targetDeviceData{})
  58. if err != nil {
  59. t.Fatalf("resolveChannels() unexpected error: %v", err)
  60. }
  61. want := []via.Channel{via.ChannelRgblight, via.ChannelRgbMatrix, via.ChannelAudio}
  62. if !reflect.DeepEqual(got, want) {
  63. t.Errorf("resolveChannels() = %v, want %v", got, want)
  64. }
  65. }
  66. func TestResolveChannelsRejectsAChannelTheKeyboardDoesNotHave(t *testing.T) {
  67. proto := &verifyingProtocol{channels: []via.Channel{via.ChannelRgblight}}
  68. target := targetDeviceData{Requested: []via.Channel{via.ChannelAudio}}
  69. if _, err := resolveChannels(proto, target); err == nil {
  70. t.Fatal("resolveChannels() expected an error, want the missing channel reported")
  71. }
  72. }
  73. func TestResolveChannelsRejectsAShadowedDisplayName(t *testing.T) {
  74. proto := &verifyingProtocol{channels: []via.Channel{via.ChannelBacklight, via.ChannelRgbMatrix}}
  75. target := targetDeviceData{Display: map[uint16]string{3: "backlight"}}
  76. if _, err := resolveChannels(proto, target); err == nil {
  77. t.Fatal("resolveChannels() expected an error, want the shadowed name reported")
  78. }
  79. }
  80. func TestResolveChannelsReportsWhenNoChannelIsPresent(t *testing.T) {
  81. proto := &verifyingProtocol{channels: []via.Channel{}}
  82. if _, err := resolveChannels(proto, targetDeviceData{}); err == nil {
  83. t.Fatal("resolveChannels() expected an error when the keyboard has no channels")
  84. }
  85. }
  86. func TestZoneFlagIsInheritedByCommands(t *testing.T) {
  87. stubOneImpact80(t)
  88. ran := false
  89. cmd := newRootCommand()
  90. cmd.AddCommand(&cobra.Command{
  91. Use: "probe",
  92. Run: func(*cobra.Command, []string) { ran = true },
  93. })
  94. cmd.SetArgs([]string{"--zone", "backlight", "probe"})
  95. if err := cmd.Execute(); err != nil {
  96. t.Fatalf("Execute() error = %v", err)
  97. }
  98. if !ran {
  99. t.Fatal("child command did not run")
  100. }
  101. if targetZone != "backlight" {
  102. t.Errorf("targetZone = %q, want %q", targetZone, "backlight")
  103. }
  104. }
  105. // Precedence, documented because two errors can apply at once: a board that
  106. // cannot be selected is reported first, since a zone name is resolved against
  107. // the board's own names.
  108. func TestNoKeyboardIsReportedBeforeAnUnknownZone(t *testing.T) {
  109. stubDiscovery(t, []intdevice.Device{})
  110. originalZone := targetZone
  111. t.Cleanup(func() { targetZone = originalZone })
  112. targetZone = "matx"
  113. _, _, _, err := openTarget()
  114. if err == nil {
  115. t.Fatal("openTarget() expected an error, got nil")
  116. }
  117. if !strings.Contains(err.Error(), "no QMK keyboard found") {
  118. t.Errorf("openTarget() error = %v, want the missing keyboard reported", err)
  119. }
  120. }