axterminator 0.7.1

macOS GUI testing framework with background testing, sub-millisecond element access, and self-healing locators
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
//! Accessibility element wrapper

#![allow(clippy::useless_conversion)]

use std::time::Duration;

use crate::accessibility::{
    self, actions, attributes, get_attribute, perform_action, AXUIElementRef,
};
use crate::error::{AXError, AXResult};
use crate::ActionMode;

/// Wrapper for an accessibility element
#[derive(Debug)]
pub struct AXElement {
    /// Raw accessibility element reference
    pub(crate) element: AXUIElementRef,
    /// Cached role
    pub(crate) role: Option<String>,
    /// Cached title
    pub(crate) title: Option<String>,
}

// Manual Clone implementation that properly retains the element
impl Clone for AXElement {
    fn clone(&self) -> Self {
        // CRITICAL: Retain the element so both copies own a reference
        let _ = accessibility::retain_cf(self.element.cast());
        Self {
            element: self.element,
            role: self.role.clone(),
            title: self.title.clone(),
        }
    }
}

unsafe impl Send for AXElement {}
unsafe impl Sync for AXElement {}

// Pure-Rust accessors — always compiled, used by the CLI, MCP server, and tests.
impl AXElement {
    /// Get the element's role (e.g., "`AXButton`", "`AXTextField`")
    pub fn role(&self) -> Option<String> {
        self.get_string_attribute(attributes::AX_ROLE)
    }

    /// Get the element's title
    pub fn title(&self) -> Option<String> {
        self.get_string_attribute(attributes::AX_TITLE)
    }

    /// Get the element's value
    pub fn value(&self) -> Option<String> {
        self.get_string_attribute(attributes::AX_VALUE)
    }

    /// Get the element's description
    pub fn description(&self) -> Option<String> {
        self.get_string_attribute(attributes::AX_DESCRIPTION)
    }

    /// Get the element's label
    pub fn label(&self) -> Option<String> {
        self.get_string_attribute(attributes::AX_LABEL)
    }

    /// Get the element's identifier
    pub fn identifier(&self) -> Option<String> {
        self.get_string_attribute(attributes::AX_IDENTIFIER)
    }

    /// Check if the element is enabled
    pub fn enabled(&self) -> bool {
        self.get_bool_attribute(attributes::AX_ENABLED)
            .unwrap_or(false)
    }

    /// Check if the element currently has keyboard focus.
    pub fn focused(&self) -> bool {
        self.get_bool_attribute(attributes::AX_FOCUSED)
            .unwrap_or(false)
    }

    /// Check if the element exists in the UI
    pub fn exists(&self) -> bool {
        self.role().is_some()
    }

    /// Get the element's bounds (x, y, width, height)
    pub fn bounds(&self) -> Option<(f64, f64, f64, f64)> {
        let position = accessibility::get_position_attribute(self.element)?;
        let size = accessibility::get_size_attribute(self.element)?;
        Some((position.x, position.y, size.width, size.height))
    }

    /// Get the element's direct children.
    ///
    /// Returns an empty `Vec` when the element has no children or the
    /// accessibility API reports an error (e.g. the element is a leaf node).
    pub fn children(&self) -> Vec<AXElement> {
        accessibility::get_children(self.element)
            .unwrap_or_default()
            .into_iter()
            .map(AXElement::new)
            .collect()
    }
}

impl AXElement {
    /// Create a new element wrapper
    #[must_use]
    pub fn new(element: AXUIElementRef) -> Self {
        Self {
            element,
            role: None,
            title: None,
        }
    }

    // -----------------------------------------------------------------------
    // Rust-native action methods — safe to call from the MCP server and CLI binary.
    // -----------------------------------------------------------------------

    /// Click — returns `AXResult`.
    pub fn click_native(&self, mode: ActionMode) -> AXResult<()> {
        self.perform_click_native(mode)
    }

    /// Double-click — returns `AXResult`.
    pub fn double_click_native(&self, mode: ActionMode) -> AXResult<()> {
        self.perform_click_native(mode)?;
        std::thread::sleep(Duration::from_millis(50));
        self.perform_click_native(mode)
    }

    /// Right-click — returns `AXResult`.
    pub fn right_click_native(&self, mode: ActionMode) -> AXResult<()> {
        self.perform_show_menu(mode)
    }

    /// Type text — returns `AXResult`.
    pub fn type_text_native(&self, text: &str, mode: ActionMode) -> AXResult<()> {
        if mode == ActionMode::Background {
            return Err(AXError::BackgroundNotSupported(
                "Text input requires FOCUS mode".into(),
            ));
        }
        self.perform_type_text(text)
    }

    /// Set value — returns `AXResult`.
    pub fn set_value_native(&self, value: &str) -> AXResult<()> {
        self.perform_set_value(value)
    }

    /// Screenshot — returns `AXResult`.
    pub fn screenshot_native(&self) -> AXResult<Vec<u8>> {
        self.capture_element_screenshot()
    }

    /// Get a string attribute value
    fn get_string_attribute(&self, attribute: &str) -> Option<String> {
        accessibility::get_string_attribute_value(self.element, attribute)
    }

    /// Get a boolean attribute value
    fn get_bool_attribute(&self, attribute: &str) -> Option<bool> {
        accessibility::get_bool_attribute_value(self.element, attribute)
    }

    /// Perform click action — returns `AXResult`.
    fn perform_click_native(&self, mode: ActionMode) -> AXResult<()> {
        match mode {
            ActionMode::Background => perform_action(self.element, actions::AX_PRESS),
            ActionMode::Focus => {
                self.bring_to_focus_internal()?;
                perform_action(self.element, actions::AX_PRESS)
            }
        }
    }

    /// Perform show menu action (right-click)
    fn perform_show_menu(&self, mode: ActionMode) -> AXResult<()> {
        if mode == ActionMode::Focus {
            self.bring_to_focus_internal()?;
        }
        perform_action(self.element, actions::AX_SHOW_MENU)
    }

    /// Type text into the element (BACKGROUND - no focus stealing!)
    ///
    /// Uses `CGEventPostToPid` to send keyboard events directly to the
    /// target application without stealing focus from the current app.
    fn perform_type_text(&self, text: &str) -> AXResult<()> {
        use core_graphics::event_source::{CGEventSource, CGEventSourceStateID};

        // Get the PID of the element's owning application
        let pid = accessibility::get_element_pid(self.element)?;

        // Create event source
        let source = CGEventSource::new(CGEventSourceStateID::HIDSystemState)
            .map_err(|()| AXError::ActionFailed("Failed to create event source".into()))?;

        // Type each character directly to the target PID
        for ch in text.chars() {
            self.type_character_to_pid(ch, &source, pid)?;
        }

        Ok(())
    }

    /// Type a single character to a specific PID (BACKGROUND mode)
    fn type_character_to_pid(
        &self,
        ch: char,
        source: &core_graphics::event_source::CGEventSource,
        pid: i32,
    ) -> AXResult<()> {
        use core_graphics::event::CGEvent;

        // Convert character to virtual key code and determine if shift is needed
        let (key_code, needs_shift) = char_to_keycode(ch);

        // Press shift if needed
        if needs_shift {
            if let Ok(shift_down) = CGEvent::new_keyboard_event(source.clone(), 56, true) {
                shift_down.post_to_pid(pid);
                std::thread::sleep(Duration::from_millis(10));
            }
        }

        // Key down
        if let Ok(key_down) = CGEvent::new_keyboard_event(source.clone(), key_code, true) {
            // Set the Unicode character
            key_down.set_string_from_utf16_unchecked(&[ch as u16]);
            key_down.post_to_pid(pid);
            std::thread::sleep(Duration::from_millis(10));

            // Key up
            if let Ok(key_up) = CGEvent::new_keyboard_event(source.clone(), key_code, false) {
                key_up.set_string_from_utf16_unchecked(&[ch as u16]);
                key_up.post_to_pid(pid);
                std::thread::sleep(Duration::from_millis(10));
            }
        }

        // Release shift if needed
        if needs_shift {
            if let Ok(shift_up) = CGEvent::new_keyboard_event(source.clone(), 56, false) {
                shift_up.post_to_pid(pid);
                std::thread::sleep(Duration::from_millis(10));
            }
        }

        Ok(())
    }

    /// Set the element's value directly
    fn perform_set_value(&self, value: &str) -> AXResult<()> {
        accessibility::set_string_attribute_value(self.element, attributes::AX_VALUE, value)
    }

    /// Bring the element to focus (internal version returning `AXResult`)
    fn bring_to_focus_internal(&self) -> AXResult<()> {
        // Set AXFocused attribute to true
        accessibility::set_bool_attribute_value(self.element, attributes::AX_FOCUSED, true)?;

        // Get the window and raise it
        if let Ok(window) = self.get_window() {
            let _ = perform_action(window, actions::AX_RAISE);
        }

        Ok(())
    }

    /// Get the window containing this element
    fn get_window(&self) -> AXResult<AXUIElementRef> {
        // Walk up the parent chain to find a window
        let mut current = self.element;
        loop {
            if let Some(role) =
                accessibility::get_string_attribute_value(current, attributes::AX_ROLE)
            {
                if role == "AXWindow" {
                    return Ok(current);
                }
            }

            // Get parent
            match get_attribute(current, attributes::AX_PARENT) {
                Ok(parent_ref) if !parent_ref.is_null() => {
                    current = parent_ref as AXUIElementRef;
                }
                _ => break,
            }
        }

        Err(AXError::ElementNotFound("window".into()))
    }

    /// Capture screenshot of the element
    fn capture_element_screenshot(&self) -> AXResult<Vec<u8>> {
        use std::process::Command;

        // Get element bounds
        let (x, y, width, height) = self
            .bounds()
            .ok_or_else(|| AXError::ActionFailed("Could not get element bounds".into()))?;

        // Use CGWindowListCreateImage to capture the region
        // For simplicity, use screencapture command with region
        let temp_path = format!(
            "/tmp/axterminator_element_screenshot_{}.png",
            std::process::id()
        );

        let output = Command::new("screencapture")
            .args([
                "-R",
                &format!(
                    "{},{},{},{}",
                    x as i32, y as i32, width as i32, height as i32
                ),
                "-x",
                &temp_path,
            ])
            .output()
            .map_err(|e| AXError::SystemError(e.to_string()))?;

        if !output.status.success() {
            return Err(AXError::ActionFailed("Screenshot failed".into()));
        }

        let data = std::fs::read(&temp_path).map_err(|e| AXError::SystemError(e.to_string()))?;
        let _ = std::fs::remove_file(&temp_path);

        Ok(data)
    }

    /// Find a child element with timeout
    fn find_child(&self, query: &str, timeout: Option<Duration>) -> AXResult<AXElement> {
        use std::time::Instant;

        let start = Instant::now();
        let timeout = timeout.unwrap_or(Duration::from_millis(100));

        loop {
            match self.search_child(query) {
                Ok(element) => return Ok(element),
                Err(_) if start.elapsed() >= timeout => {
                    return Err(AXError::ElementNotFound(query.to_string()));
                }
                Err(_) => {
                    std::thread::sleep(Duration::from_millis(50));
                }
            }
        }
    }

    /// Search for a child element (single attempt)
    fn search_child(&self, query: &str) -> AXResult<AXElement> {
        let children = accessibility::get_children(self.element)?;
        if query.contains(':') {
            let parts: Vec<&str> = query.splitn(2, ':').collect();
            let attr = parts[0].trim();
            let value = parts[1].trim();
            self.search_in_elements(&children, attr, value)
        } else {
            self.search_in_elements_any_text(&children, query)
        }
    }

    /// Recursively search in element list
    fn search_in_elements(
        &self,
        elements: &[AXUIElementRef],
        attr: &str,
        value: &str,
    ) -> AXResult<AXElement> {
        for &element in elements {
            // Check if this element matches
            if let Some(attr_value) = accessibility::get_string_attribute_value(element, attr) {
                if attr_value.contains(value) {
                    return Ok(AXElement::new(element));
                }
            }

            // Search in children
            if let Ok(children) = accessibility::get_children(element) {
                if let Ok(found) = self.search_in_elements(&children, attr, value) {
                    return Ok(found);
                }
            }
        }

        Err(AXError::ElementNotFound(format!("{attr}:{value}")))
    }

    /// Recursively search elements, matching query against ANY text-bearing attribute.
    fn search_in_elements_any_text(
        &self,
        elements: &[AXUIElementRef],
        query: &str,
    ) -> AXResult<AXElement> {
        let text_attrs = [
            attributes::AX_TITLE,
            attributes::AX_DESCRIPTION,
            attributes::AX_VALUE,
            attributes::AX_LABEL,
            attributes::AX_IDENTIFIER,
        ];

        for &element in elements {
            let matches = text_attrs.iter().any(|attr| {
                accessibility::get_string_attribute_value(element, attr)
                    .is_some_and(|v| v.contains(query))
            });

            if matches {
                return Ok(AXElement::new(element));
            }

            if let Ok(children) = accessibility::get_children(element) {
                if let Ok(found) = self.search_in_elements_any_text(&children, query) {
                    return Ok(found);
                }
            }
        }

        Err(AXError::ElementNotFound(query.to_string()))
    }
}

/// Convert a character to macOS virtual key code
///
/// Returns (`key_code`, `needs_shift`)
fn char_to_keycode(ch: char) -> (u16, bool) {
    match ch {
        'a' | 'A' => (0, ch.is_uppercase()),
        'b' | 'B' => (11, ch.is_uppercase()),
        'c' | 'C' => (8, ch.is_uppercase()),
        'd' | 'D' => (2, ch.is_uppercase()),
        'e' | 'E' => (14, ch.is_uppercase()),
        'f' | 'F' => (3, ch.is_uppercase()),
        'g' | 'G' => (5, ch.is_uppercase()),
        'h' | 'H' => (4, ch.is_uppercase()),
        'i' | 'I' => (34, ch.is_uppercase()),
        'j' | 'J' => (38, ch.is_uppercase()),
        'k' | 'K' => (40, ch.is_uppercase()),
        'l' | 'L' => (37, ch.is_uppercase()),
        'm' | 'M' => (46, ch.is_uppercase()),
        'n' | 'N' => (45, ch.is_uppercase()),
        'o' | 'O' => (31, ch.is_uppercase()),
        'p' | 'P' => (35, ch.is_uppercase()),
        'q' | 'Q' => (12, ch.is_uppercase()),
        'r' | 'R' => (15, ch.is_uppercase()),
        's' | 'S' => (1, ch.is_uppercase()),
        't' | 'T' => (17, ch.is_uppercase()),
        'u' | 'U' => (32, ch.is_uppercase()),
        'v' | 'V' => (9, ch.is_uppercase()),
        'w' | 'W' => (13, ch.is_uppercase()),
        'x' | 'X' => (7, ch.is_uppercase()),
        'y' | 'Y' => (16, ch.is_uppercase()),
        'z' | 'Z' => (6, ch.is_uppercase()),
        '0' => (29, false),
        '1' => (18, false),
        '2' => (19, false),
        '3' => (20, false),
        '4' => (21, false),
        '5' => (23, false),
        '6' => (22, false),
        '7' => (26, false),
        '8' => (28, false),
        '9' => (25, false),
        ')' => (29, true),
        '!' => (18, true),
        '@' => (19, true),
        '#' => (20, true),
        '$' => (21, true),
        '%' => (23, true),
        '^' => (22, true),
        '&' => (26, true),
        '*' => (28, true),
        '(' => (25, true),
        ' ' => (49, false),
        '-' | '_' => (27, ch == '_'),
        '=' | '+' => (24, ch == '+'),
        '[' | '{' => (33, ch == '{'),
        ']' | '}' => (30, ch == '}'),
        '\\' | '|' => (42, ch == '|'),
        ';' | ':' => (41, ch == ':'),
        '\'' | '"' => (39, ch == '"'),
        ',' | '<' => (43, ch == '<'),
        '.' | '>' => (47, ch == '>'),
        '/' | '?' => (44, ch == '?'),
        '`' | '~' => (50, ch == '~'),
        '\n' | '\r' => (36, false), // Return
        '\t' => (48, false),        // Tab
        _ => (49, false),           // Default to space
    }
}

impl Drop for AXElement {
    fn drop(&mut self) {
        accessibility::release_cf(self.element.cast());
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::process::Command;

    /// Helper to check if accessibility is enabled
    fn check_accessibility() -> bool {
        accessibility::check_accessibility_enabled()
    }

    #[test]
    fn test_char_to_keycode_lowercase() {
        let (code, shift) = char_to_keycode('a');
        assert_eq!(code, 0);
        assert!(!shift);

        let (code, shift) = char_to_keycode('z');
        assert_eq!(code, 6);
        assert!(!shift);
    }

    #[test]
    fn test_char_to_keycode_uppercase() {
        let (code, shift) = char_to_keycode('A');
        assert_eq!(code, 0);
        assert!(shift);

        let (code, shift) = char_to_keycode('Z');
        assert_eq!(code, 6);
        assert!(shift);
    }

    #[test]
    fn test_char_to_keycode_numbers() {
        let (code, shift) = char_to_keycode('0');
        assert_eq!(code, 29);
        assert!(!shift);

        let (code, shift) = char_to_keycode('5');
        assert_eq!(code, 23);
        assert!(!shift);
    }

    #[test]
    fn test_char_to_keycode_symbols() {
        let (code, shift) = char_to_keycode('!');
        assert_eq!(code, 18);
        assert!(shift);

        let (code, shift) = char_to_keycode('@');
        assert_eq!(code, 19);
        assert!(shift);

        let (code, shift) = char_to_keycode(' ');
        assert_eq!(code, 49);
        assert!(!shift);
    }

    #[test]
    fn test_char_to_keycode_special() {
        let (code, shift) = char_to_keycode('\n');
        assert_eq!(code, 36);
        assert!(!shift);

        let (code, shift) = char_to_keycode('\t');
        assert_eq!(code, 48);
        assert!(!shift);
    }

    #[test]
    fn test_element_creation() {
        if !check_accessibility() {
            println!("Skipping: Accessibility not enabled");
            return;
        }

        // Create a system-wide element
        let sys = accessibility::create_system_wide_element();
        assert!(sys.is_ok());

        if let Ok(element_ref) = sys {
            let element = AXElement::new(element_ref);
            // Element should exist
            assert!(element.role().is_some());
        }
    }

    #[test]
    fn test_get_string_attribute() {
        if !check_accessibility() {
            println!("Skipping: Accessibility not enabled");
            return;
        }

        // Try to get an app (Finder should always be running)
        let output = Command::new("pgrep")
            .arg("-x")
            .arg("Finder")
            .output()
            .expect("Failed to run pgrep");

        if let Ok(pid_str) = String::from_utf8(output.stdout) {
            if let Ok(pid) = pid_str.trim().parse::<i32>() {
                if let Ok(app_ref) = accessibility::create_application_element(pid) {
                    let element = AXElement::new(app_ref);

                    // Finder should have a role
                    let role = element.role();
                    assert!(role.is_some());
                    assert_eq!(role.unwrap(), "AXApplication");
                }
            }
        }
    }

    #[test]
    fn test_get_bool_attribute() {
        if !check_accessibility() {
            println!("Skipping: Accessibility not enabled");
            return;
        }

        let output = Command::new("pgrep")
            .arg("-x")
            .arg("Finder")
            .output()
            .expect("Failed to run pgrep");

        if let Ok(pid_str) = String::from_utf8(output.stdout) {
            if let Ok(pid) = pid_str.trim().parse::<i32>() {
                if let Ok(app_ref) = accessibility::create_application_element(pid) {
                    let element = AXElement::new(app_ref);

                    // Check enabled attribute (should return a boolean)
                    let enabled = element.enabled();
                    // Value doesn't matter, just that it returns without panic
                    let _ = enabled;
                }
            }
        }
    }

    #[test]
    fn test_bounds() {
        if !check_accessibility() {
            println!("Skipping: Accessibility not enabled");
            return;
        }

        let output = Command::new("pgrep")
            .arg("-x")
            .arg("Finder")
            .output()
            .expect("Failed to run pgrep");

        if let Ok(pid_str) = String::from_utf8(output.stdout) {
            if let Ok(pid) = pid_str.trim().parse::<i32>() {
                if let Ok(app_ref) = accessibility::create_application_element(pid) {
                    let element = AXElement::new(app_ref);

                    // Try to get windows
                    if let Ok(children) = accessibility::get_children(element.element) {
                        for child in children.iter().take(5) {
                            let child_elem = AXElement::new(*child);
                            if let Some(role) = child_elem.role() {
                                if role == "AXWindow" {
                                    // Window should have bounds
                                    if let Some((_x, _y, w, h)) = child_elem.bounds() {
                                        assert!(w > 0.0);
                                        assert!(h > 0.0);
                                        return;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    #[test]
    fn test_element_exists() {
        if !check_accessibility() {
            println!("Skipping: Accessibility not enabled");
            return;
        }

        let output = Command::new("pgrep")
            .arg("-x")
            .arg("Finder")
            .output()
            .expect("Failed to run pgrep");

        if let Ok(pid_str) = String::from_utf8(output.stdout) {
            if let Ok(pid) = pid_str.trim().parse::<i32>() {
                if let Ok(app_ref) = accessibility::create_application_element(pid) {
                    let element = AXElement::new(app_ref);
                    assert!(element.exists());
                }
            }
        }
    }

    #[test]
    fn test_search_child_parsing() {
        if !check_accessibility() {
            println!("Skipping: Accessibility not enabled");
            return;
        }

        // Test query parsing
        let query = "AXTitle:Save";
        assert!(query.contains(':'));

        let parts: Vec<&str> = query.splitn(2, ':').collect();
        assert_eq!(parts[0], "AXTitle");
        assert_eq!(parts[1], "Save");

        // Test simple query
        let query = "Save";
        assert!(!query.contains(':'));
    }

    #[test]
    fn test_perform_set_value_structure() {
        // This test verifies the structure is correct
        // Actual functionality requires a real text field element
        if !check_accessibility() {
            println!("Skipping: Accessibility not enabled");
            return;
        }

        // Test that CFString can be created
        use core_foundation::base::TCFType;
        use core_foundation::string::CFString;
        let test_value = CFString::new("test");
        assert!(!test_value.as_concrete_TypeRef().is_null());
    }

    #[test]
    fn test_type_character_mapping_completeness() {
        // Ensure all common characters have mappings
        let test_chars = "abcdefghijklmnopqrstuvwxyz\
                          ABCDEFGHIJKLMNOPQRSTUVWXYZ\
                          0123456789\
                          !@#$%^&*()\
                          -=[]\\;',./\
                          _+{}|:\"<>?\
                          `~ \n\t";

        for ch in test_chars.chars() {
            let (code, _shift) = char_to_keycode(ch);
            // All characters should map to valid key codes (0-127)
            assert!(code < 128);
        }
    }
}