brightness_summary_test.go 3.4 KB

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