keyboard_info_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "strings"
  7. "testing"
  8. intdevice "netdome.biz/paul/qmk-rgb/internal/device"
  9. )
  10. type deviceInfoOutput struct {
  11. Devices []intdevice.Device `json:"devices"`
  12. Total int `json:"total"`
  13. }
  14. func stubDiscovery(t *testing.T, devices []intdevice.Device) {
  15. t.Helper()
  16. orig := discoverAll
  17. discoverAll = func() ([]intdevice.Device, error) { return devices, nil }
  18. t.Cleanup(func() { discoverAll = orig })
  19. }
  20. // runRealKeyboardInfo executes the shipped command so a re-added
  21. // human-readable banner is caught, not just changes inside a helper.
  22. func runRealKeyboardInfo(t *testing.T) (stdout, stderr string, err error) {
  23. t.Helper()
  24. var out, errOut bytes.Buffer
  25. origOut, origErr := keyboardInfoCmd.OutOrStdout(), keyboardInfoCmd.ErrOrStderr()
  26. keyboardInfoCmd.SetOut(&out)
  27. keyboardInfoCmd.SetErr(&errOut)
  28. t.Cleanup(func() {
  29. keyboardInfoCmd.SetOut(origOut)
  30. keyboardInfoCmd.SetErr(origErr)
  31. })
  32. err = keyboardInfoCmd.RunE(keyboardInfoCmd, nil)
  33. return out.String(), errOut.String(), err
  34. }
  35. func TestKeyboardInfoWritesOnlyJSON(t *testing.T) {
  36. stubDiscovery(t, []intdevice.Device{
  37. {Index: 1, Path: "/dev/hidraw7", VendorID: 0x36b0, ProductID: 0x309f, Name: "Wobkey Impact 80", Known: true},
  38. })
  39. stdout, stderr, err := runRealKeyboardInfo(t)
  40. if err != nil {
  41. t.Fatalf("keyboard info returned error: %v", err)
  42. }
  43. if stderr != "" {
  44. t.Errorf("keyboard info wrote %q to stderr, want no human-readable banner next to the JSON", stderr)
  45. }
  46. var got deviceInfoOutput
  47. if err := json.Unmarshal([]byte(stdout), &got); err != nil {
  48. t.Fatalf("stdout is not valid JSON: %v (output %q)", err, stdout)
  49. }
  50. if got.Total != 1 {
  51. t.Errorf("total = %d, want 1", got.Total)
  52. }
  53. if len(got.Devices) != 1 {
  54. t.Fatalf("devices = %d entries, want 1", len(got.Devices))
  55. }
  56. if got.Devices[0].Index != 1 {
  57. t.Errorf("devices[0].index = %d, want 1", got.Devices[0].Index)
  58. }
  59. if got.Devices[0].Path != "/dev/hidraw7" {
  60. t.Errorf("devices[0].path = %q, want %q", got.Devices[0].Path, "/dev/hidraw7")
  61. }
  62. }
  63. func TestKeyboardInfoEmitsIndentedJSON(t *testing.T) {
  64. stubDiscovery(t, []intdevice.Device{
  65. {Index: 1, Path: "/dev/hidraw7", Name: "Wobkey Impact 80", Known: true},
  66. })
  67. stdout, _, err := runRealKeyboardInfo(t)
  68. if err != nil {
  69. t.Fatalf("keyboard info returned error: %v", err)
  70. }
  71. if !strings.Contains(stdout, "\n \"devices\": [") {
  72. t.Errorf("stdout = %q, want two-space indented JSON like the other commands emit", stdout)
  73. }
  74. if !strings.HasSuffix(stdout, "\n") {
  75. t.Errorf("stdout = %q, want a trailing newline", stdout)
  76. }
  77. }
  78. func TestKeyboardInfoEmitsEmptyArrayNotNull(t *testing.T) {
  79. stubDiscovery(t, nil)
  80. stdout, _, err := runRealKeyboardInfo(t)
  81. if err != nil {
  82. t.Fatalf("keyboard info returned error: %v", err)
  83. }
  84. if !strings.Contains(stdout, `"devices": []`) {
  85. t.Errorf("stdout = %q, want an empty array so consumers can iterate unconditionally", stdout)
  86. }
  87. }
  88. func TestKeyboardInfoPropagatesDiscoveryError(t *testing.T) {
  89. orig := discoverAll
  90. discoverAll = func() ([]intdevice.Device, error) { return nil, errStub }
  91. t.Cleanup(func() { discoverAll = orig })
  92. _, _, err := runRealKeyboardInfo(t)
  93. if err == nil {
  94. t.Fatal("keyboard info expected discovery error, got nil")
  95. }
  96. if !strings.Contains(err.Error(), "enumerate") {
  97. t.Errorf("error = %v, want it to wrap the discovery failure", err)
  98. }
  99. }
  100. var errStub = errors.New("enumerate: stub failure")