| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- package main
- import (
- "bytes"
- "strings"
- "testing"
- "netdome.biz/paul/qmk-rgb/internal/via"
- )
- func runBrightness(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 := NewBrightnessCmd()
- cmd.SetOut(&out)
- cmd.SetErr(&errOut)
- cmd.SetArgs([]string{arg})
- if err := cmd.Execute(); err != nil {
- t.Fatalf("brightness returned error: %v", err)
- }
- return out.String(), errOut.String()
- }
- // A success message that says "set to 200" while the keyboard holds 160 is a
- // contradiction. Where every zone applied the request, the plain message is
- // accurate and stays.
- func TestBrightnessReportsPlainMessageWhenAllZonesMatch(t *testing.T) {
- stdout, stderr := runBrightness(t, map[via.Channel]uint8{via.ChannelRgblight: 200}, "logo", "200")
- if strings.TrimSpace(stdout) != "Brightness set to 200" {
- t.Errorf("stdout = %q, want the exact success message", stdout)
- }
- if stderr != "" {
- t.Errorf("stderr = %q, want nothing on stderr when the request was met", stderr)
- }
- }
- // Where a zone differs, one line must state what each zone actually holds.
- func TestBrightnessSummarisesAppliedValuesOnMismatch(t *testing.T) {
- stdout, _ := runBrightness(t, map[via.Channel]uint8{
- via.ChannelRgblight: 160,
- via.ChannelRgbMatrix: 255,
- via.ChannelAudio: 160,
- }, "", "200")
- for _, want := range []string{"logo 160", "backlight 255", "side 160", "requested 200"} {
- if !strings.Contains(stdout, want) {
- t.Errorf("stdout = %q, want it to contain %q", stdout, want)
- }
- }
- if strings.Contains(stdout, "set to 200") {
- t.Errorf("stdout = %q, must not claim 200 was set when it was not", stdout)
- }
- if strings.Count(strings.TrimSpace(stdout), "\n") != 0 {
- t.Errorf("stdout = %q, want exactly one line", stdout)
- }
- }
- func TestBrightnessSummarisesASingleMismatchingZone(t *testing.T) {
- // backlight lives on the RGBMatrix channel, not RGBLight.
- stdout, _ := runBrightness(t, map[via.Channel]uint8{via.ChannelRgbMatrix: 159}, "backlight", "100")
- if !strings.Contains(stdout, "backlight 159") || !strings.Contains(stdout, "requested 100") {
- t.Errorf("stdout = %q, want the single zone's applied value and the request", stdout)
- }
- if strings.Contains(stdout, "logo") || strings.Contains(stdout, "side") {
- t.Errorf("stdout = %q, want only the selected zone named", stdout)
- }
- }
- // The summary must not invent zones the user did not select.
- func TestBrightnessSummaryNamesOnlySelectedZones(t *testing.T) {
- proto := &verifyingProtocol{applied: map[via.Channel]uint8{via.ChannelAudio: 160}}
- originalOpen := openRGBProtocol
- originalZone := targetZone
- t.Cleanup(func() {
- openRGBProtocol = originalOpen
- targetZone = originalZone
- })
- openRGBProtocol = func() (rgbProtocol, error) { return proto, nil }
- targetZone = "side"
- var out bytes.Buffer
- cmd := NewBrightnessCmd()
- cmd.SetOut(&out)
- cmd.SetErr(&out)
- cmd.SetArgs([]string{"200"})
- if err := cmd.Execute(); err != nil {
- t.Fatalf("brightness returned error: %v", err)
- }
- if !strings.Contains(out.String(), "side 160") {
- t.Errorf("stdout = %q, want side reported", out.String())
- }
- for _, unwanted := range []string{"logo", "backlight"} {
- if strings.Contains(out.String(), unwanted) {
- t.Errorf("stdout = %q, must not mention %s when only side was selected", out.String(), unwanted)
- }
- }
- }
|