accessibility_test.go
1 package tools 2 3 import ( 4 "context" 5 "strings" 6 "testing" 7 ) 8 9 func TestAccessibility_Info(t *testing.T) { 10 tool := &AccessibilityTool{client: &AXClient{}} 11 info := tool.Info() 12 if info.Name != "accessibility" { 13 t.Errorf("expected name 'accessibility', got %q", info.Name) 14 } 15 if len(info.Required) != 1 || info.Required[0] != "action" { 16 t.Errorf("expected required [action], got %v", info.Required) 17 } 18 props, ok := info.Parameters["properties"].(map[string]any) 19 if !ok { 20 t.Fatal("expected properties map in parameters") 21 } 22 for _, key := range []string{"action", "app", "max_depth", "filter", "ref", "value"} { 23 if _, exists := props[key]; !exists { 24 t.Errorf("expected property %q in schema", key) 25 } 26 } 27 } 28 29 func TestAccessibility_RequiresApproval(t *testing.T) { 30 tool := &AccessibilityTool{client: &AXClient{}} 31 if tool.RequiresApproval() { 32 t.Error("expected RequiresApproval to return false") 33 } 34 } 35 36 func TestAccessibility_InvalidJSON(t *testing.T) { 37 tool := &AccessibilityTool{client: &AXClient{}} 38 result, err := tool.Run(context.Background(), `not valid json`) 39 if err != nil { 40 t.Fatalf("unexpected error: %v", err) 41 } 42 if !result.IsError { 43 t.Error("expected error result for invalid JSON") 44 } 45 } 46 47 func TestAccessibility_MissingAction(t *testing.T) { 48 tool := &AccessibilityTool{client: &AXClient{}} 49 result, err := tool.Run(context.Background(), `{}`) 50 if err != nil { 51 t.Fatalf("unexpected error: %v", err) 52 } 53 if !result.IsError { 54 t.Error("expected error result for missing action") 55 } 56 if !strings.Contains(result.Content, "missing required parameter: action") { 57 t.Errorf("expected missing action error, got: %s", result.Content) 58 } 59 } 60 61 func TestAccessibility_UnknownAction(t *testing.T) { 62 tool := &AccessibilityTool{client: &AXClient{}} 63 result, err := tool.Run(context.Background(), `{"action": "fly"}`) 64 if err != nil { 65 t.Fatalf("unexpected error: %v", err) 66 } 67 if !result.IsError { 68 t.Error("expected error result for unknown action") 69 } 70 if !strings.Contains(result.Content, "unknown action") { 71 t.Errorf("expected 'unknown action' in error, got: %s", result.Content) 72 } 73 } 74 75 func TestAccessibility_ClickMissingRef(t *testing.T) { 76 tool := &AccessibilityTool{client: &AXClient{}} 77 result, err := tool.Run(context.Background(), `{"action": "click"}`) 78 if err != nil { 79 t.Fatalf("unexpected error: %v", err) 80 } 81 if !result.IsError { 82 t.Error("expected error result for click without ref") 83 } 84 } 85 86 func TestAccessibility_ClickUnknownRef(t *testing.T) { 87 tool := &AccessibilityTool{client: &AXClient{}} 88 tool.refs = map[string]refEntry{"e1": {path: "window[0]", pid: 1}} 89 result, err := tool.Run(context.Background(), `{"action": "click", "ref": "e99"}`) 90 if err != nil { 91 t.Fatalf("unexpected error: %v", err) 92 } 93 if !result.IsError { 94 t.Error("expected error for unknown ref") 95 } 96 if !strings.Contains(result.Content, "unknown ref") { 97 t.Errorf("expected 'unknown ref' error, got: %s", result.Content) 98 } 99 } 100 101 func TestAccessibility_SetValueMissingValue(t *testing.T) { 102 tool := &AccessibilityTool{client: &AXClient{}} 103 tool.refs = map[string]refEntry{"e1": {path: "window[0]/AXTextField[0]", role: "AXTextField", pid: 1}} 104 result, err := tool.Run(context.Background(), `{"action": "set_value", "ref": "e1"}`) 105 if err != nil { 106 t.Fatalf("unexpected error: %v", err) 107 } 108 if !result.IsError { 109 t.Error("expected error for set_value without value") 110 } 111 } 112 113 func TestAccessibility_NilClient(t *testing.T) { 114 tool := &AccessibilityTool{} // no client 115 result, err := tool.Run(context.Background(), `{"action": "read_tree"}`) 116 if err != nil { 117 t.Fatalf("unexpected error: %v", err) 118 } 119 if !result.IsError { 120 t.Error("expected error for nil client") 121 } 122 }