device_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package device
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "github.com/wobkey/rgb/internal/hid"
  7. )
  8. func TestLoadKeyboards(t *testing.T) {
  9. // Create a temporary keyboards.json for testing
  10. tmpDir := t.TempDir()
  11. tmpFile := filepath.Join(tmpDir, "keyboards.json")
  12. testData := `[
  13. {"name": "Test Keyboard 1", "vendorId": 22222, "productId": 1, "protocol": "via"},
  14. {"name": "Test Keyboard 2", "vendorId": 33333, "productId": 2, "protocol": "viapro"}
  15. ]`
  16. if err := os.WriteFile(tmpFile, []byte(testData), 0644); err != nil {
  17. t.Fatalf("failed to write test file: %v", err)
  18. }
  19. // Save original function and override search path for testing
  20. origFind := findKeyboardsJSON
  21. findKeyboardsJSON = func() (string, error) {
  22. return tmpFile, nil
  23. }
  24. defer func() { findKeyboardsJSON = origFind }()
  25. keyboards, err := LoadKeyboards()
  26. if err != nil {
  27. t.Fatalf("LoadKeyboards() returned error: %v", err)
  28. }
  29. if len(keyboards) != 2 {
  30. t.Errorf("LoadKeyboards() returned %d keyboards, want 2", len(keyboards))
  31. }
  32. if keyboards[0].Name != "Test Keyboard 1" {
  33. t.Errorf("First keyboard name = %q, want %q", keyboards[0].Name, "Test Keyboard 1")
  34. }
  35. if keyboards[0].VendorID != 22222 {
  36. t.Errorf("First keyboard VID = %d, want %d", keyboards[0].VendorID, 22222)
  37. }
  38. if keyboards[1].ProductID != 2 {
  39. t.Errorf("Second keyboard PID = %d, want %d", keyboards[1].ProductID, 2)
  40. }
  41. }
  42. func TestLoadKeyboardsInvalid(t *testing.T) {
  43. // Test with invalid JSON
  44. tmpDir := t.TempDir()
  45. tmpFile := filepath.Join(tmpDir, "keyboards.json")
  46. invalidData := `[this is not valid json`
  47. if err := os.WriteFile(tmpFile, []byte(invalidData), 0644); err != nil {
  48. t.Fatalf("failed to write test file: %v", err)
  49. }
  50. origFind := findKeyboardsJSON
  51. findKeyboardsJSON = func() (string, error) {
  52. return tmpFile, nil
  53. }
  54. defer func() { findKeyboardsJSON = origFind }()
  55. _, err := LoadKeyboards()
  56. if err == nil {
  57. t.Error("LoadKeyboards() expected error for invalid JSON, got nil")
  58. }
  59. }
  60. func TestMatchKeyboardsSelectsRawHIDInterface(t *testing.T) {
  61. keyboards := []Keyboard{{
  62. Name: "Wobkey Impact 80",
  63. VendorID: 0x36b0,
  64. ProductID: 0x309f,
  65. }}
  66. devices := []hid.DeviceInfo{
  67. {Path: "/dev/hidraw3", VendorID: 0x36b0, ProductID: 0x309f},
  68. {Path: "/dev/hidraw4", VendorID: 0x36b0, ProductID: 0x309f, RawHID: true},
  69. {Path: "/dev/hidraw5", VendorID: 0x36b0, ProductID: 0x309f},
  70. }
  71. got := matchKeyboards(keyboards, devices)
  72. if len(got) != 1 {
  73. t.Fatalf("matchKeyboards() returned %d devices, want 1", len(got))
  74. }
  75. if got[0].Path != "/dev/hidraw4" {
  76. t.Errorf("matchKeyboards() path = %q, want /dev/hidraw4", got[0].Path)
  77. }
  78. }
  79. func TestLoadKeyboardsEmptyFile(t *testing.T) {
  80. tmpDir := t.TempDir()
  81. tmpFile := filepath.Join(tmpDir, "keyboards.json")
  82. if err := os.WriteFile(tmpFile, []byte("[]"), 0644); err != nil {
  83. t.Fatalf("failed to write test file: %v", err)
  84. }
  85. origFind := findKeyboardsJSON
  86. findKeyboardsJSON = func() (string, error) {
  87. return tmpFile, nil
  88. }
  89. defer func() { findKeyboardsJSON = origFind }()
  90. keyboards, err := LoadKeyboards()
  91. if err != nil {
  92. t.Fatalf("LoadKeyboards() returned error: %v", err)
  93. }
  94. if len(keyboards) != 0 {
  95. t.Errorf("LoadKeyboards() returned %d keyboards, want 0", len(keyboards))
  96. }
  97. }