|
|
@@ -1,7 +1,9 @@
|
|
|
package rgb
|
|
|
|
|
|
import (
|
|
|
+ "bytes"
|
|
|
"reflect"
|
|
|
+ "strings"
|
|
|
"testing"
|
|
|
|
|
|
intrgb "github.com/wobkey/rgb/internal/rgb"
|
|
|
@@ -95,3 +97,76 @@ func TestResolveEffectTargetsStaticCompatibility(t *testing.T) {
|
|
|
t.Errorf("resolveEffectTargets() skipped = %v, want none", skipped)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+func executeEffectCommand(t *testing.T, protocol *fakeZoneProtocol, target string, args ...string) (string, string, error) {
|
|
|
+ t.Helper()
|
|
|
+ originalOpen := openRGBProtocol
|
|
|
+ originalZone := targetZone
|
|
|
+ openRGBProtocol = func() (rgbProtocol, error) {
|
|
|
+ return protocol, nil
|
|
|
+ }
|
|
|
+ targetZone = target
|
|
|
+ t.Cleanup(func() {
|
|
|
+ openRGBProtocol = originalOpen
|
|
|
+ targetZone = originalZone
|
|
|
+ })
|
|
|
+
|
|
|
+ var stdout bytes.Buffer
|
|
|
+ var stderr bytes.Buffer
|
|
|
+ cmd := NewEffectCmd()
|
|
|
+ cmd.SetOut(&stdout)
|
|
|
+ cmd.SetErr(&stderr)
|
|
|
+ cmd.SilenceErrors = true
|
|
|
+ cmd.SilenceUsage = true
|
|
|
+ cmd.SetArgs(args)
|
|
|
+ err := cmd.Execute()
|
|
|
+ return stdout.String(), stderr.String(), err
|
|
|
+}
|
|
|
+
|
|
|
+func TestEffectCommandWarnsForSkippedZones(t *testing.T) {
|
|
|
+ protocol := &fakeZoneProtocol{failAt: -1}
|
|
|
+ stdout, stderr, err := executeEffectCommand(t, protocol, "", "rainbow_moving_chevron")
|
|
|
+ if err != nil {
|
|
|
+ t.Fatalf("Execute() unexpected error: %v", err)
|
|
|
+ }
|
|
|
+ if !strings.Contains(stderr, "effect not supported on zone(s): logo, side") {
|
|
|
+ t.Errorf("stderr = %q, want skipped-zone warning", stderr)
|
|
|
+ }
|
|
|
+ if !strings.Contains(stdout, `Effect set to "rainbow_moving_chevron"`) {
|
|
|
+ t.Errorf("stdout = %q, want success output", stdout)
|
|
|
+ }
|
|
|
+ if len(protocol.reports) != 1 {
|
|
|
+ t.Fatalf("reports = %v, want one Backlight report", protocol.reports)
|
|
|
+ }
|
|
|
+ if protocol.reports[0].channel != 3 || protocol.reports[0].param != 2 || protocol.reports[0].value != 17 {
|
|
|
+ t.Errorf("report = %+v, want Backlight Effect 17", protocol.reports[0])
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestEffectCommandRejectsBeforeOpeningProtocol(t *testing.T) {
|
|
|
+ protocol := &fakeZoneProtocol{failAt: -1}
|
|
|
+ stdout, _, err := executeEffectCommand(t, protocol, "logo", "rainbow_moving_chevron")
|
|
|
+ if err == nil {
|
|
|
+ t.Fatal("Execute() expected error, got nil")
|
|
|
+ }
|
|
|
+ if len(protocol.reports) != 0 {
|
|
|
+ t.Errorf("reports = %v, want no writes before rejection", protocol.reports)
|
|
|
+ }
|
|
|
+ if stdout != "" {
|
|
|
+ t.Errorf("stdout = %q, want no success output", stdout)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestEffectCommandSuppressesSuccessAfterLaterWriteFailure(t *testing.T) {
|
|
|
+ protocol := &fakeZoneProtocol{failAt: 1}
|
|
|
+ stdout, _, err := executeEffectCommand(t, protocol, "", "breathing")
|
|
|
+ if err == nil {
|
|
|
+ t.Fatal("Execute() expected write error, got nil")
|
|
|
+ }
|
|
|
+ if len(protocol.reports) != 1 {
|
|
|
+ t.Fatalf("reports = %v, want one report before failure", protocol.reports)
|
|
|
+ }
|
|
|
+ if strings.Contains(stdout, "Effect set") {
|
|
|
+ t.Errorf("stdout = %q, want success output suppressed", stdout)
|
|
|
+ }
|
|
|
+}
|