profile_load_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package main
  2. import (
  3. "bytes"
  4. "testing"
  5. intrgb "netdome.biz/paul/qmk-rgb/internal/rgb"
  6. )
  7. // README.md: "With `--zone`, commands target exactly the selected channel."
  8. // A profile key names a channel, so a load applies only the keys that resolve to
  9. // a selected channel. The keys themselves are resolved by resolveZoneName, which
  10. // channels_test.go covers; what matters here is that a key is never applied to a
  11. // channel it does not name.
  12. func TestLoadAppliesOnlyTheKeysTheSelectionNames(t *testing.T) {
  13. t.Cleanup(vendoredDefinitions(t))
  14. dir := t.TempDir()
  15. profile := Profile{
  16. Name: "three",
  17. Version: 1,
  18. Zones: map[string]*ZoneSettings{
  19. "logo": {Enabled: true, Effect: "light", Brightness: 100, Speed: 1, Color: "00ff"},
  20. "backlight": {Enabled: true, Effect: "solid_color", Brightness: 100, Speed: 1, Color: "00ff"},
  21. "side": {Enabled: true, Effect: "wave", Brightness: 100, Speed: 1, Color: "00ff"},
  22. },
  23. }
  24. originalDir := profilesDir
  25. t.Cleanup(func() { profilesDir = originalDir })
  26. profilesDir = dir
  27. if err := profile.Save(); err != nil {
  28. t.Fatalf("save profile: %v", err)
  29. }
  30. tests := []struct {
  31. name string
  32. zoneFlag string
  33. wantApplied int
  34. }{
  35. {"no flag applies all three", "", 3},
  36. {"logo applies one", "logo", 1},
  37. {"backlight applies one", "backlight", 1},
  38. {"side applies one", "side", 1},
  39. }
  40. for _, tt := range tests {
  41. t.Run(tt.name, func(t *testing.T) {
  42. proto := &verifyingProtocol{}
  43. restore := stubTargetForProfileTest(t, proto, tt.zoneFlag, impact80Display())
  44. defer restore()
  45. cmd := NewProfileLoadCmd()
  46. cmd.SetOut(&bytes.Buffer{})
  47. cmd.SetErr(&bytes.Buffer{})
  48. cmd.SetArgs([]string{"three"})
  49. if err := cmd.Execute(); err != nil {
  50. t.Fatalf("load returned error: %v", err)
  51. }
  52. if got := effectWrites(proto); got != tt.wantApplied {
  53. t.Errorf("effect writes = %d, want %d", got, tt.wantApplied)
  54. }
  55. })
  56. }
  57. }
  58. // effectWrites counts the effect writes, one per applied channel.
  59. func effectWrites(proto *verifyingProtocol) int {
  60. count := 0
  61. for _, r := range proto.reports {
  62. if r.param == uint8(intrgb.EffectID) {
  63. count++
  64. }
  65. }
  66. return count
  67. }
  68. // A key the selection leaves out is the user's own choice, and saying the
  69. // keyboard lacks it is false. Only a key that resolves to a channel the
  70. // keyboard does not have is reported.
  71. func TestLoadIsQuietAboutAKeyTheSelectionExcludes(t *testing.T) {
  72. t.Cleanup(vendoredDefinitions(t))
  73. dir := t.TempDir()
  74. originalDir := profilesDir
  75. t.Cleanup(func() { profilesDir = originalDir })
  76. profilesDir = dir
  77. profile := Profile{
  78. Name: "three",
  79. Version: 1,
  80. Zones: map[string]*ZoneSettings{
  81. "logo": {Enabled: true, Effect: "light", Brightness: 100, Speed: 1, Color: "00ff"},
  82. "backlight": {Enabled: true, Effect: "solid_color", Brightness: 100, Speed: 1, Color: "00ff"},
  83. },
  84. }
  85. if err := profile.Save(); err != nil {
  86. t.Fatalf("save profile: %v", err)
  87. }
  88. proto := &verifyingProtocol{}
  89. t.Cleanup(stubTargetForProfileTest(t, proto, "logo", impact80Display()))
  90. var stderr bytes.Buffer
  91. cmd := NewProfileLoadCmd()
  92. cmd.SetOut(&bytes.Buffer{})
  93. cmd.SetErr(&stderr)
  94. cmd.SetArgs([]string{"three"})
  95. if err := cmd.Execute(); err != nil {
  96. t.Fatalf("load returned error: %v", err)
  97. }
  98. if stderr.Len() != 0 {
  99. t.Errorf("stderr = %q, want nothing for a channel the selection excluded", stderr.String())
  100. }
  101. if got := effectWrites(proto); got != 1 {
  102. t.Errorf("effect writes = %d, want 1", got)
  103. }
  104. }