brightness_summary_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package main
  2. import (
  3. "bytes"
  4. "strings"
  5. "testing"
  6. "netdome.biz/paul/qmk-rgb/internal/via"
  7. )
  8. func runBrightness(t *testing.T, applied map[via.Channel]uint8, zone string, arg string) (stdout, stderr string) {
  9. t.Helper()
  10. proto := &verifyingProtocol{applied: applied}
  11. t.Cleanup(impact80Target(t, proto))
  12. var out, errOut bytes.Buffer
  13. cmd := NewBrightnessCmd()
  14. cmd.SetOut(&out)
  15. cmd.SetErr(&errOut)
  16. cmd.SetArgs([]string{zone, arg})
  17. if err := cmd.Execute(); err != nil {
  18. t.Fatalf("brightness returned error: %v", err)
  19. }
  20. return out.String(), errOut.String()
  21. }
  22. // A success message that says "set to 200" while the keyboard holds 160 is a
  23. // contradiction. Where every zone applied the request, the plain message is
  24. // accurate and stays.
  25. func TestBrightnessReportsPlainMessageWhenAllZonesMatch(t *testing.T) {
  26. stdout, stderr := runBrightness(t, map[via.Channel]uint8{via.ChannelRgblight: 200}, "logo", "200")
  27. if strings.TrimSpace(stdout) != "Brightness set to 200" {
  28. t.Errorf("stdout = %q, want the exact success message", stdout)
  29. }
  30. if stderr != "" {
  31. t.Errorf("stderr = %q, want nothing on stderr when the request was met", stderr)
  32. }
  33. }
  34. // Where a zone differs, one line must state what each zone actually holds.
  35. func TestBrightnessSummarisesAppliedValuesOnMismatch(t *testing.T) {
  36. stdout, _ := runBrightness(t, map[via.Channel]uint8{
  37. via.ChannelRgblight: 160,
  38. via.ChannelRgbMatrix: 255,
  39. via.ChannelAudio: 160,
  40. }, "all", "200")
  41. for _, want := range []string{"logo 160", "backlight 255", "side 160", "requested 200"} {
  42. if !strings.Contains(stdout, want) {
  43. t.Errorf("stdout = %q, want it to contain %q", stdout, want)
  44. }
  45. }
  46. if strings.Contains(stdout, "set to 200") {
  47. t.Errorf("stdout = %q, must not claim 200 was set when it was not", stdout)
  48. }
  49. if strings.Count(strings.TrimSpace(stdout), "\n") != 0 {
  50. t.Errorf("stdout = %q, want exactly one line", stdout)
  51. }
  52. }
  53. func TestBrightnessSummarisesASingleMismatchingZone(t *testing.T) {
  54. // backlight lives on the RGBMatrix channel, not RGBLight.
  55. stdout, _ := runBrightness(t, map[via.Channel]uint8{via.ChannelRgbMatrix: 159}, "backlight", "100")
  56. if !strings.Contains(stdout, "backlight 159") || !strings.Contains(stdout, "requested 100") {
  57. t.Errorf("stdout = %q, want the single zone's applied value and the request", stdout)
  58. }
  59. if strings.Contains(stdout, "logo") || strings.Contains(stdout, "side") {
  60. t.Errorf("stdout = %q, want only the selected zone named", stdout)
  61. }
  62. }
  63. // The summary must not invent zones the user did not select.
  64. func TestBrightnessSummaryNamesOnlySelectedZones(t *testing.T) {
  65. proto := &verifyingProtocol{applied: map[via.Channel]uint8{via.ChannelAudio: 160}}
  66. t.Cleanup(impact80Target(t, proto))
  67. var out bytes.Buffer
  68. cmd := NewBrightnessCmd()
  69. cmd.SetOut(&out)
  70. cmd.SetErr(&out)
  71. cmd.SetArgs([]string{"side", "200"})
  72. if err := cmd.Execute(); err != nil {
  73. t.Fatalf("brightness returned error: %v", err)
  74. }
  75. if !strings.Contains(out.String(), "side 160") {
  76. t.Errorf("stdout = %q, want side reported", out.String())
  77. }
  78. for _, unwanted := range []string{"logo", "backlight"} {
  79. if strings.Contains(out.String(), unwanted) {
  80. t.Errorf("stdout = %q, must not mention %s when only side was selected", out.String(), unwanted)
  81. }
  82. }
  83. }