device_test.go 2.5 KB

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