rgb_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. originalKeyboard := keyboardFor
  36. t.Cleanup(func() { keyboardFor = originalKeyboard })
  37. keyboardFor = func(uint16, uint16) (intdevice.Keyboard, bool, error) {
  38. return intdevice.Keyboard{Channels: impact80Display()}, true, nil
  39. }
  40. }
  41. func TestDeviceFlagIsInheritedByCommands(t *testing.T) {
  42. stubOneImpact80(t)
  43. ran := false
  44. cmd := newRootCommand()
  45. cmd.AddCommand(&cobra.Command{
  46. Use: "probe",
  47. Run: func(*cobra.Command, []string) { ran = true },
  48. })
  49. cmd.SetArgs([]string{"--device", "1", "probe"})
  50. if err := cmd.Execute(); err != nil {
  51. t.Fatalf("Execute() error = %v", err)
  52. }
  53. if !ran {
  54. t.Fatal("child command did not run")
  55. }
  56. if targetDevice != "1" {
  57. t.Errorf("targetDevice = %q, want %q", targetDevice, "1")
  58. }
  59. }
  60. func TestResolveChannelsDefaultsToEveryDetectedChannel(t *testing.T) {
  61. proto := &verifyingProtocol{}
  62. got, err := resolveChannels(proto, targetDeviceData{})
  63. if err != nil {
  64. t.Fatalf("resolveChannels() unexpected error: %v", err)
  65. }
  66. want := []via.Channel{via.ChannelRgblight, via.ChannelRgbMatrix, via.ChannelAudio}
  67. if !reflect.DeepEqual(got, want) {
  68. t.Errorf("resolveChannels() = %v, want %v", got, want)
  69. }
  70. }
  71. func TestResolveChannelsRejectsAChannelTheKeyboardDoesNotHave(t *testing.T) {
  72. proto := &verifyingProtocol{channels: []via.Channel{via.ChannelRgblight}}
  73. target := targetDeviceData{Requested: []via.Channel{via.ChannelAudio}}
  74. if _, err := resolveChannels(proto, target); err == nil {
  75. t.Fatal("resolveChannels() expected an error, want the missing channel reported")
  76. }
  77. }
  78. func TestResolveChannelsRejectsAShadowedDisplayName(t *testing.T) {
  79. proto := &verifyingProtocol{channels: []via.Channel{via.ChannelBacklight, via.ChannelRgbMatrix}}
  80. target := targetDeviceData{Display: map[uint16]string{3: "backlight"}}
  81. if _, err := resolveChannels(proto, target); err == nil {
  82. t.Fatal("resolveChannels() expected an error, want the shadowed name reported")
  83. }
  84. }
  85. func TestResolveChannelsReportsWhenNoChannelIsPresent(t *testing.T) {
  86. proto := &verifyingProtocol{channels: []via.Channel{}}
  87. if _, err := resolveChannels(proto, targetDeviceData{}); err == nil {
  88. t.Fatal("resolveChannels() expected an error when the keyboard has no channels")
  89. }
  90. }
  91. func TestZoneFlagIsInheritedByCommands(t *testing.T) {
  92. stubOneImpact80(t)
  93. ran := false
  94. cmd := newRootCommand()
  95. cmd.AddCommand(&cobra.Command{
  96. Use: "probe",
  97. Run: func(*cobra.Command, []string) { ran = true },
  98. })
  99. cmd.SetArgs([]string{"--zone", "backlight", "probe"})
  100. if err := cmd.Execute(); err != nil {
  101. t.Fatalf("Execute() error = %v", err)
  102. }
  103. if !ran {
  104. t.Fatal("child command did not run")
  105. }
  106. if targetZone != "backlight" {
  107. t.Errorf("targetZone = %q, want %q", targetZone, "backlight")
  108. }
  109. }
  110. func TestZoneValidationPrecedesDeviceOpening(t *testing.T) {
  111. originalTargetZone := targetZone
  112. t.Cleanup(func() { targetZone = originalTargetZone })
  113. targetZone = "matrix"
  114. _, _, _, err := openTarget()
  115. if err == nil {
  116. t.Fatal("openTarget() expected zone error, got nil")
  117. }
  118. if !strings.Contains(err.Error(), "unknown zone") {
  119. t.Errorf("openTarget() error = %v, want unknown zone error", err)
  120. }
  121. }
  122. func TestZoneValidationRunsBeforeCommandOpener(t *testing.T) {
  123. originalTargetDevice := targetDevice
  124. originalTargetZone := targetZone
  125. t.Cleanup(func() {
  126. targetDevice = originalTargetDevice
  127. targetZone = originalTargetZone
  128. })
  129. cmd := newRootCommand()
  130. openerCalled := false
  131. cmd.AddCommand(&cobra.Command{
  132. Use: "probe",
  133. RunE: func(*cobra.Command, []string) error {
  134. openerCalled = true
  135. _, _, _, err := openTarget()
  136. return err
  137. },
  138. })
  139. cmd.SetArgs([]string{"--zone", "matrix", "probe"})
  140. err := cmd.Execute()
  141. if err == nil {
  142. t.Fatal("Execute() expected zone error, got nil")
  143. }
  144. if !strings.Contains(err.Error(), "unknown zone") {
  145. t.Errorf("Execute() error = %v, want unknown zone error", err)
  146. }
  147. if openerCalled {
  148. t.Fatal("child command invoked openTarget before zone validation")
  149. }
  150. }