package main import ( "bytes" "strings" "testing" intrgb "netdome.biz/paul/qmk-rgb/internal/rgb" "netdome.biz/paul/qmk-rgb/internal/via" ) func runSpeed(t *testing.T, applied map[via.Channel]uint8, zoneFlag string, arg string) (stdout, stderr string) { t.Helper() proto := &verifyingProtocol{applied: applied} originalOpen := openRGBProtocol originalZone := targetZone t.Cleanup(func() { openRGBProtocol = originalOpen targetZone = originalZone }) openRGBProtocol = func() (rgbProtocol, error) { return proto, nil } targetZone = zoneFlag var out, errOut bytes.Buffer cmd := NewSpeedCmd() cmd.SetOut(&out) cmd.SetErr(&errOut) cmd.SetArgs([]string{arg}) if err := cmd.Execute(); err != nil { t.Fatalf("speed returned error: %v", err) } return out.String(), errOut.String() } // The Impact 80 collapses every speed above 0 to 4 on the logo and side // channels, so `speed 60` claiming success there is the same lie brightness // had. func TestSpeedReportsPlainMessageWhenAllZonesMatch(t *testing.T) { stdout, stderr := runSpeed(t, map[via.Channel]uint8{ via.ChannelRgblight: 60, via.ChannelRgbMatrix: 60, via.ChannelAudio: 60, }, "", "60") if strings.TrimSpace(stdout) != "Speed set to 60" { t.Errorf("stdout = %q, want the exact success message", stdout) } if stderr != "" { t.Errorf("stderr = %q, want nothing when the request was met", stderr) } } func TestSpeedSummarisesAppliedValuesOnMismatch(t *testing.T) { stdout, _ := runSpeed(t, map[via.Channel]uint8{ via.ChannelRgblight: 4, via.ChannelRgbMatrix: 60, via.ChannelAudio: 4, }, "", "60") for _, want := range []string{"logo 4", "backlight 60", "side 4", "requested 60"} { if !strings.Contains(stdout, want) { t.Errorf("stdout = %q, want it to contain %q", stdout, want) } } if strings.Contains(stdout, "set to 60") { t.Errorf("stdout = %q, must not claim 60 was set when logo and side hold 4", stdout) } if strings.Count(strings.TrimSpace(stdout), "\n") != 0 { t.Errorf("stdout = %q, want exactly one line", stdout) } } func TestSpeedNamesOnlySelectedZones(t *testing.T) { stdout, _ := runSpeed(t, map[via.Channel]uint8{via.ChannelAudio: 4}, "side", "60") if !strings.Contains(stdout, "side 4") { t.Errorf("stdout = %q, want side reported", stdout) } for _, unwanted := range []string{"logo", "backlight"} { if strings.Contains(stdout, unwanted) { t.Errorf("stdout = %q, must not mention %s when only side was selected", stdout, unwanted) } } } // speed 0 is a genuine value on every channel, not a collapsed one. func TestSpeedZeroIsReportedAsApplied(t *testing.T) { stdout, _ := runSpeed(t, map[via.Channel]uint8{ via.ChannelRgblight: 0, via.ChannelRgbMatrix: 0, via.ChannelAudio: 0, }, "", "0") if strings.TrimSpace(stdout) != "Speed set to 0" { t.Errorf("stdout = %q, want the success message for a value the keyboard kept", stdout) } } // setValueVerified must be parameter-agnostic so both commands share it. func TestSetValueVerifiedReadsBackTheRequestedParam(t *testing.T) { proto := &verifyingProtocol{ applied: map[via.Channel]uint8{via.ChannelRgbMatrix: 159}, } results, err := setValueVerified(proto, []intrgb.Zone{intrgb.ZoneBacklight}, uint8(intrgb.Speed), 100) if err != nil { t.Fatalf("setValueVerified() error = %v", err) } if len(results) != 1 { t.Fatalf("results = %d, want 1", len(results)) } if results[0].Applied != 159 || results[0].Requested != 100 { t.Errorf("results[0] = %+v, want Requested 100 Applied 159", results[0]) } }