rgb_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package main
  2. import (
  3. "reflect"
  4. "strings"
  5. "testing"
  6. "github.com/spf13/cobra"
  7. intrgb "netdome.biz/paul/qmk-rgb/internal/rgb"
  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. func TestDeviceFlagIsInheritedByCommands(t *testing.T) {
  31. originalTargetDevice := targetDevice
  32. t.Cleanup(func() { targetDevice = originalTargetDevice })
  33. const wantPath = "hid-device-0"
  34. ran := false
  35. cmd := newRootCommand()
  36. cmd.AddCommand(&cobra.Command{
  37. Use: "probe",
  38. Run: func(*cobra.Command, []string) { ran = true },
  39. })
  40. cmd.SetArgs([]string{"--device", wantPath, "probe"})
  41. if err := cmd.Execute(); err != nil {
  42. t.Fatalf("Execute() error = %v", err)
  43. }
  44. if !ran {
  45. t.Fatal("child command did not run")
  46. }
  47. if targetDevice != wantPath {
  48. t.Errorf("targetDevice = %q, want %q", targetDevice, wantPath)
  49. }
  50. }
  51. func TestSelectedZonesDefaultsToAll(t *testing.T) {
  52. originalTargetZone := targetZone
  53. t.Cleanup(func() { targetZone = originalTargetZone })
  54. targetZone = ""
  55. got, err := selectedZones()
  56. if err != nil {
  57. t.Fatalf("selectedZones() unexpected error: %v", err)
  58. }
  59. want := []intrgb.Zone{intrgb.ZoneLogo, intrgb.ZoneBacklight, intrgb.ZoneSide}
  60. if !reflect.DeepEqual(got, want) {
  61. t.Errorf("selectedZones() = %v, want %v", got, want)
  62. }
  63. }
  64. func TestSelectedZonesRejectsUnknownZone(t *testing.T) {
  65. originalTargetZone := targetZone
  66. t.Cleanup(func() { targetZone = originalTargetZone })
  67. targetZone = "matrix"
  68. got, err := selectedZones()
  69. if err == nil {
  70. t.Fatal("selectedZones() expected error, got nil")
  71. }
  72. if got != nil {
  73. t.Errorf("selectedZones() = %v, want nil", got)
  74. }
  75. }
  76. func TestSelectedZonesExplicitZones(t *testing.T) {
  77. originalTargetZone := targetZone
  78. t.Cleanup(func() { targetZone = originalTargetZone })
  79. cases := []struct {
  80. name string
  81. want intrgb.Zone
  82. }{
  83. {"logo", intrgb.ZoneLogo},
  84. {"backlight", intrgb.ZoneBacklight},
  85. {"side", intrgb.ZoneSide},
  86. }
  87. for _, tc := range cases {
  88. t.Run(tc.name, func(t *testing.T) {
  89. targetZone = tc.name
  90. got, err := selectedZones()
  91. if err != nil {
  92. t.Fatalf("selectedZones() unexpected error: %v", err)
  93. }
  94. want := []intrgb.Zone{tc.want}
  95. if !reflect.DeepEqual(got, want) {
  96. t.Errorf("selectedZones() = %v, want %v", got, want)
  97. }
  98. })
  99. }
  100. }
  101. func TestZoneChannels(t *testing.T) {
  102. got := zoneChannels(intrgb.AllZones())
  103. want := []via.Channel{2, 3, 4}
  104. if !reflect.DeepEqual(got, want) {
  105. t.Errorf("zoneChannels() = %v, want %v", got, want)
  106. }
  107. }
  108. func TestZoneFlagIsInheritedByCommands(t *testing.T) {
  109. originalTargetDevice := targetDevice
  110. originalTargetZone := targetZone
  111. t.Cleanup(func() {
  112. targetDevice = originalTargetDevice
  113. targetZone = originalTargetZone
  114. })
  115. const wantPath = "hid-device-0"
  116. ran := false
  117. cmd := newRootCommand()
  118. cmd.AddCommand(&cobra.Command{
  119. Use: "probe",
  120. Run: func(*cobra.Command, []string) { ran = true },
  121. })
  122. cmd.SetArgs([]string{"--device", wantPath, "--zone", "backlight", "probe"})
  123. if err := cmd.Execute(); err != nil {
  124. t.Fatalf("Execute() error = %v", err)
  125. }
  126. if !ran {
  127. t.Fatal("child command did not run")
  128. }
  129. if targetDevice != wantPath {
  130. t.Errorf("targetDevice = %q, want %q", targetDevice, wantPath)
  131. }
  132. if targetZone != "backlight" {
  133. t.Errorf("targetZone = %q, want %q", targetZone, "backlight")
  134. }
  135. }
  136. func TestZoneValidationPrecedesDeviceOpening(t *testing.T) {
  137. originalTargetZone := targetZone
  138. t.Cleanup(func() { targetZone = originalTargetZone })
  139. targetZone = "matrix"
  140. _, err := OpenDevice()
  141. if err == nil {
  142. t.Fatal("OpenDevice() expected zone error, got nil")
  143. }
  144. if !strings.Contains(err.Error(), "unknown zone") {
  145. t.Errorf("OpenDevice() error = %v, want unknown zone error", err)
  146. }
  147. }
  148. func TestZoneValidationRunsBeforeCommandOpener(t *testing.T) {
  149. originalTargetDevice := targetDevice
  150. originalTargetZone := targetZone
  151. t.Cleanup(func() {
  152. targetDevice = originalTargetDevice
  153. targetZone = originalTargetZone
  154. })
  155. cmd := newRootCommand()
  156. openerCalled := false
  157. cmd.AddCommand(&cobra.Command{
  158. Use: "probe",
  159. RunE: func(*cobra.Command, []string) error {
  160. openerCalled = true
  161. _, err := OpenDevice()
  162. return err
  163. },
  164. })
  165. cmd.SetArgs([]string{"--zone", "matrix", "probe"})
  166. err := cmd.Execute()
  167. if err == nil {
  168. t.Fatal("Execute() expected zone error, got nil")
  169. }
  170. if !strings.Contains(err.Error(), "unknown zone") {
  171. t.Errorf("Execute() error = %v, want unknown zone error", err)
  172. }
  173. if openerCalled {
  174. t.Fatal("child command invoked OpenDevice before zone validation")
  175. }
  176. }