| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- package main
- import (
- "reflect"
- "strings"
- "testing"
- "github.com/spf13/cobra"
- intrgb "netdome.biz/paul/qmk-rgb/internal/rgb"
- "netdome.biz/paul/qmk-rgb/internal/via"
- )
- func TestLightingEnabledRequiresActiveEffectAndBrightness(t *testing.T) {
- cases := []struct {
- name string
- mode uint8
- brightness uint8
- want bool
- }{
- {"active", 1, 160, true},
- {"zero brightness", 1, 0, false},
- {"disabled effect", 0, 160, false},
- {"both zero", 0, 0, false},
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- if got := lightingEnabled(tc.mode, tc.brightness); got != tc.want {
- t.Errorf("lightingEnabled(%d, %d) = %t, want %t", tc.mode, tc.brightness, got, tc.want)
- }
- })
- }
- }
- func TestDeviceFlagIsInheritedByCommands(t *testing.T) {
- originalTargetDevice := targetDevice
- t.Cleanup(func() { targetDevice = originalTargetDevice })
- const wantPath = "hid-device-0"
- ran := false
- cmd := newRootCommand()
- cmd.AddCommand(&cobra.Command{
- Use: "probe",
- Run: func(*cobra.Command, []string) { ran = true },
- })
- cmd.SetArgs([]string{"--device", wantPath, "probe"})
- if err := cmd.Execute(); err != nil {
- t.Fatalf("Execute() error = %v", err)
- }
- if !ran {
- t.Fatal("child command did not run")
- }
- if targetDevice != wantPath {
- t.Errorf("targetDevice = %q, want %q", targetDevice, wantPath)
- }
- }
- func TestSelectedZonesDefaultsToAll(t *testing.T) {
- originalTargetZone := targetZone
- t.Cleanup(func() { targetZone = originalTargetZone })
- targetZone = ""
- got, err := selectedZones()
- if err != nil {
- t.Fatalf("selectedZones() unexpected error: %v", err)
- }
- want := []intrgb.Zone{intrgb.ZoneLogo, intrgb.ZoneBacklight, intrgb.ZoneSide}
- if !reflect.DeepEqual(got, want) {
- t.Errorf("selectedZones() = %v, want %v", got, want)
- }
- }
- func TestSelectedZonesRejectsUnknownZone(t *testing.T) {
- originalTargetZone := targetZone
- t.Cleanup(func() { targetZone = originalTargetZone })
- targetZone = "matrix"
- got, err := selectedZones()
- if err == nil {
- t.Fatal("selectedZones() expected error, got nil")
- }
- if got != nil {
- t.Errorf("selectedZones() = %v, want nil", got)
- }
- }
- func TestSelectedZonesExplicitZones(t *testing.T) {
- originalTargetZone := targetZone
- t.Cleanup(func() { targetZone = originalTargetZone })
- cases := []struct {
- name string
- want intrgb.Zone
- }{
- {"logo", intrgb.ZoneLogo},
- {"backlight", intrgb.ZoneBacklight},
- {"side", intrgb.ZoneSide},
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- targetZone = tc.name
- got, err := selectedZones()
- if err != nil {
- t.Fatalf("selectedZones() unexpected error: %v", err)
- }
- want := []intrgb.Zone{tc.want}
- if !reflect.DeepEqual(got, want) {
- t.Errorf("selectedZones() = %v, want %v", got, want)
- }
- })
- }
- }
- func TestZoneChannels(t *testing.T) {
- got := zoneChannels(intrgb.AllZones())
- want := []via.LEDType{2, 3, 4}
- if !reflect.DeepEqual(got, want) {
- t.Errorf("zoneChannels() = %v, want %v", got, want)
- }
- }
- func TestZoneFlagIsInheritedByCommands(t *testing.T) {
- originalTargetDevice := targetDevice
- originalTargetZone := targetZone
- t.Cleanup(func() {
- targetDevice = originalTargetDevice
- targetZone = originalTargetZone
- })
- const wantPath = "hid-device-0"
- ran := false
- cmd := newRootCommand()
- cmd.AddCommand(&cobra.Command{
- Use: "probe",
- Run: func(*cobra.Command, []string) { ran = true },
- })
- cmd.SetArgs([]string{"--device", wantPath, "--zone", "backlight", "probe"})
- if err := cmd.Execute(); err != nil {
- t.Fatalf("Execute() error = %v", err)
- }
- if !ran {
- t.Fatal("child command did not run")
- }
- if targetDevice != wantPath {
- t.Errorf("targetDevice = %q, want %q", targetDevice, wantPath)
- }
- if targetZone != "backlight" {
- t.Errorf("targetZone = %q, want %q", targetZone, "backlight")
- }
- }
- func TestZoneValidationPrecedesDeviceOpening(t *testing.T) {
- originalTargetZone := targetZone
- t.Cleanup(func() { targetZone = originalTargetZone })
- targetZone = "matrix"
- _, err := OpenDevice()
- if err == nil {
- t.Fatal("OpenDevice() expected zone error, got nil")
- }
- if !strings.Contains(err.Error(), "unknown zone") {
- t.Errorf("OpenDevice() error = %v, want unknown zone error", err)
- }
- }
- func TestZoneValidationRunsBeforeCommandOpener(t *testing.T) {
- originalTargetDevice := targetDevice
- originalTargetZone := targetZone
- t.Cleanup(func() {
- targetDevice = originalTargetDevice
- targetZone = originalTargetZone
- })
- cmd := newRootCommand()
- openerCalled := false
- cmd.AddCommand(&cobra.Command{
- Use: "probe",
- RunE: func(*cobra.Command, []string) error {
- openerCalled = true
- _, err := OpenDevice()
- return err
- },
- })
- cmd.SetArgs([]string{"--zone", "matrix", "probe"})
- err := cmd.Execute()
- if err == nil {
- t.Fatal("Execute() expected zone error, got nil")
- }
- if !strings.Contains(err.Error(), "unknown zone") {
- t.Errorf("Execute() error = %v, want unknown zone error", err)
- }
- if openerCalled {
- t.Fatal("child command invoked OpenDevice before zone validation")
- }
- }
|