brightness_summary_test.go 3.5 KB

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