| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- 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.LEDType]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.LEDType]uint8{
- via.RGBLight: 60,
- via.RGBMatrix: 60,
- via.SideLight: 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.LEDType]uint8{
- via.RGBLight: 4,
- via.RGBMatrix: 60,
- via.SideLight: 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.LEDType]uint8{via.SideLight: 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.LEDType]uint8{
- via.RGBLight: 0,
- via.RGBMatrix: 0,
- via.SideLight: 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.LEDType]uint8{via.RGBMatrix: 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])
- }
- }
|