profile_load_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. dir := t.TempDir()
  14. profile := Profile{
  15. Name: "three",
  16. Version: 1,
  17. Zones: map[string]*ZoneSettings{
  18. "logo": {Enabled: true, Effect: "light", Brightness: 100, Speed: 1, Color: "00ff"},
  19. "backlight": {Enabled: true, Effect: "solid_color", Brightness: 100, Speed: 1, Color: "00ff"},
  20. "side": {Enabled: true, Effect: "wave", Brightness: 100, Speed: 1, Color: "00ff"},
  21. },
  22. }
  23. originalDir := profilesDir
  24. t.Cleanup(func() { profilesDir = originalDir })
  25. profilesDir = dir
  26. if err := profile.Save(); err != nil {
  27. t.Fatalf("save profile: %v", err)
  28. }
  29. tests := []struct {
  30. name string
  31. zoneFlag string
  32. wantApplied int
  33. }{
  34. {"no flag applies all three", "", 3},
  35. {"logo applies one", "logo", 1},
  36. {"backlight applies one", "backlight", 1},
  37. {"side applies one", "side", 1},
  38. }
  39. for _, tt := range tests {
  40. t.Run(tt.name, func(t *testing.T) {
  41. proto := &verifyingProtocol{}
  42. restore := stubTargetForProfileTest(t, proto, tt.zoneFlag, impact80Display())
  43. defer restore()
  44. cmd := NewProfileLoadCmd()
  45. cmd.SetOut(&bytes.Buffer{})
  46. cmd.SetErr(&bytes.Buffer{})
  47. cmd.SetArgs([]string{"three"})
  48. if err := cmd.Execute(); err != nil {
  49. t.Fatalf("load returned error: %v", err)
  50. }
  51. if got := effectWrites(proto); got != tt.wantApplied {
  52. t.Errorf("effect writes = %d, want %d", got, tt.wantApplied)
  53. }
  54. })
  55. }
  56. }
  57. // effectWrites counts the effect writes, one per applied channel.
  58. func effectWrites(proto *verifyingProtocol) int {
  59. count := 0
  60. for _, r := range proto.reports {
  61. if r.param == uint8(intrgb.EffectID) {
  62. count++
  63. }
  64. }
  65. return count
  66. }