computeruse-rs 2.0.0

A Playwright-style SDK for automating desktop GUI applications
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
use super::*;

#[test]
fn test_basic_role_selector() {
    let selector = Selector::from("role:Button");
    match selector {
        Selector::Role { role, name } => {
            assert_eq!(role, "Button");
            assert_eq!(name, None);
        }
        _ => panic!("Expected Role selector"),
    }
}

#[test]
fn test_role_with_name() {
    // Use && syntax instead of deprecated | syntax
    let selector = Selector::from("role:Button && name:Calculate");
    match selector {
        Selector::And(selectors) => {
            assert_eq!(selectors.len(), 2);
            match &selectors[0] {
                Selector::Role { role, .. } => assert_eq!(role, "Button"),
                _ => panic!("Expected Role selector"),
            }
            match &selectors[1] {
                Selector::Name(name) => assert_eq!(name, "Calculate"),
                _ => panic!("Expected Name selector"),
            }
        }
        _ => panic!("Expected And selector"),
    }
}

#[test]
fn test_and_selector() {
    let selector = Selector::from("role:Button && name:Calculate");
    match selector {
        Selector::And(selectors) => {
            assert_eq!(selectors.len(), 2);
            // First should be role:Button
            match &selectors[0] {
                Selector::Role { role, .. } => assert_eq!(role, "Button"),
                _ => panic!("Expected Role selector"),
            }
            // Second should be name:Calculate
            match &selectors[1] {
                Selector::Name(name) => assert_eq!(name, "Calculate"),
                _ => panic!("Expected Name selector"),
            }
        }
        _ => panic!("Expected And selector, got: {selector:?}"),
    }
}

#[test]
fn test_or_selector() {
    let selector = Selector::from("role:Button || role:Link");
    match selector {
        Selector::Or(selectors) => {
            assert_eq!(selectors.len(), 2);
            match &selectors[0] {
                Selector::Role { role, .. } => assert_eq!(role, "Button"),
                _ => panic!("Expected Role selector"),
            }
            match &selectors[1] {
                Selector::Role { role, .. } => assert_eq!(role, "Link"),
                _ => panic!("Expected Role selector"),
            }
        }
        _ => panic!("Expected Or selector"),
    }
}

#[test]
fn test_parentheses_with_and() {
    let selector = Selector::from("(role:Window && name:Calculator)");
    match selector {
        Selector::And(selectors) => {
            assert_eq!(selectors.len(), 2);
            match &selectors[0] {
                Selector::Role { role, .. } => assert_eq!(role, "Window"),
                _ => panic!("Expected Role selector"),
            }
            match &selectors[1] {
                Selector::Name(name) => assert_eq!(name, "Calculator"),
                _ => panic!("Expected Name selector"),
            }
        }
        _ => panic!("Expected And selector"),
    }
}

#[test]
fn test_chain_selector() {
    let selector = Selector::from("role:Window >> role:Button");
    match selector {
        Selector::Chain(selectors) => {
            assert_eq!(selectors.len(), 2);
            match &selectors[0] {
                Selector::Role { role, .. } => assert_eq!(role, "Window"),
                _ => panic!("Expected Role selector"),
            }
            match &selectors[1] {
                Selector::Role { role, .. } => assert_eq!(role, "Button"),
                _ => panic!("Expected Role selector"),
            }
        }
        _ => panic!("Expected Chain selector"),
    }
}

#[test]
fn test_chain_with_parentheses_and_boolean() {
    // This is the problematic case
    let selector = Selector::from("(role:Window && name:Calculator) >> role:Button");
    match selector {
        Selector::Chain(selectors) => {
            assert_eq!(selectors.len(), 2);
            // First part should be an AND selector
            match &selectors[0] {
                Selector::And(and_parts) => {
                    assert_eq!(and_parts.len(), 2);
                    match &and_parts[0] {
                        Selector::Role { role, .. } => assert_eq!(role, "Window"),
                        _ => panic!("Expected Role selector in AND"),
                    }
                    match &and_parts[1] {
                        Selector::Name(name) => assert_eq!(name, "Calculator"),
                        _ => panic!("Expected Name selector in AND"),
                    }
                }
                _ => panic!("Expected And selector as first part of chain"),
            }
            // Second part should be role:Button
            match &selectors[1] {
                Selector::Role { role, .. } => assert_eq!(role, "Button"),
                _ => panic!("Expected Role selector as second part of chain"),
            }
        }
        _ => panic!("Expected Chain selector, got: {selector:?}"),
    }
}

#[test]
fn test_complex_chain_with_multiple_boolean() {
    let selector = Selector::from("(role:Window && name:Calculator) >> (role:Button && name:Plus)");
    match selector {
        Selector::Chain(selectors) => {
            assert_eq!(selectors.len(), 2);
            // Both parts should be AND selectors
            match &selectors[0] {
                Selector::And(and_parts) => {
                    assert_eq!(and_parts.len(), 2);
                }
                _ => panic!("Expected And selector as first part"),
            }
            match &selectors[1] {
                Selector::And(and_parts) => {
                    assert_eq!(and_parts.len(), 2);
                }
                _ => panic!("Expected And selector as second part"),
            }
        }
        _ => panic!("Expected Chain selector"),
    }
}

#[test]
fn test_nativeid_selector() {
    let selector = Selector::from("nativeid:button-plus");
    match selector {
        Selector::NativeId(id) => {
            assert_eq!(id, "button-plus");
        }
        _ => panic!("Expected NativeId selector"),
    }
}

#[test]
fn test_chain_with_nativeid() {
    let selector = Selector::from("(role:Window && name:Calculator) >> nativeid:button-plus");
    match selector {
        Selector::Chain(selectors) => {
            assert_eq!(selectors.len(), 2);
            // First part should be AND
            match &selectors[0] {
                Selector::And(_) => {}
                _ => panic!("Expected And selector as first part"),
            }
            // Second part should be nativeid
            match &selectors[1] {
                Selector::NativeId(id) => assert_eq!(id, "button-plus"),
                _ => panic!("Expected NativeId selector as second part"),
            }
        }
        _ => panic!("Expected Chain selector"),
    }
}

#[test]
fn test_nth_selector() {
    let selector = Selector::from("role:Button >> nth:2");
    match selector {
        Selector::Chain(selectors) => {
            assert_eq!(selectors.len(), 2);
            match &selectors[1] {
                Selector::Nth(n) => assert_eq!(*n, 2),
                _ => panic!("Expected Nth selector"),
            }
        }
        _ => panic!("Expected Chain selector"),
    }
}

#[test]
fn test_not_selector() {
    let selector = Selector::from("!name:Cancel");
    match selector {
        Selector::Not(inner) => match inner.as_ref() {
            Selector::Name(name) => assert_eq!(name, "Cancel"),
            _ => panic!("Expected Name selector inside Not"),
        },
        _ => panic!("Expected Not selector"),
    }
}

#[test]
fn test_complex_boolean_expression() {
    let selector = Selector::from("(role:Button && name:OK) || (role:Link && name:Submit)");
    match selector {
        Selector::Or(or_parts) => {
            assert_eq!(or_parts.len(), 2);
            // Both parts should be AND selectors
            match &or_parts[0] {
                Selector::And(and_parts) => assert_eq!(and_parts.len(), 2),
                _ => panic!("Expected And selector in first OR part"),
            }
            match &or_parts[1] {
                Selector::And(and_parts) => assert_eq!(and_parts.len(), 2),
                _ => panic!("Expected And selector in second OR part"),
            }
        }
        _ => panic!("Expected Or selector at top level"),
    }
}

#[test]
fn test_text_selector() {
    let selector = Selector::from("text:Calculate");
    match selector {
        Selector::Text(text) => assert_eq!(text, "Calculate"),
        _ => panic!("Expected Text selector"),
    }
}

#[test]
fn test_text_selector_with_special_chars() {
    // Test that text: selector works with parentheses, exclamation marks, etc.
    let selector = Selector::from("text:Test 1: DEFAULT (focus+click) SUCCESS!");
    match selector {
        Selector::Text(text) => assert_eq!(text, "Test 1: DEFAULT (focus+click) SUCCESS!"),
        _ => panic!("Expected Text selector, got {selector:?}"),
    }
}

#[test]
fn test_text_selector_with_comma() {
    let selector = Selector::from("text:Hello, world!");
    match selector {
        Selector::Text(text) => assert_eq!(text, "Hello, world!"),
        _ => panic!("Expected Text selector, got {selector:?}"),
    }
}

#[test]
fn test_id_selector_with_hash() {
    let selector = Selector::from("#button-123");
    match selector {
        Selector::Id(id) => assert_eq!(id, "button-123"),
        _ => panic!("Expected Id selector"),
    }
}

#[test]
fn test_visible_selector() {
    let selector = Selector::from("visible:true");
    match selector {
        Selector::Visible(v) => assert!(v),
        _ => panic!("Expected Visible selector"),
    }
}

#[test]
fn test_classname_selector() {
    let selector = Selector::from("classname:btn-primary");
    match selector {
        Selector::ClassName(class) => assert_eq!(class, "btn-primary"),
        _ => panic!("Expected ClassName selector"),
    }
}

#[test]
fn test_comma_as_or() {
    let selector = Selector::from("role:Button, role:Link");
    match selector {
        Selector::Or(selectors) => {
            assert_eq!(selectors.len(), 2);
        }
        _ => panic!("Expected Or selector from comma"),
    }
}

#[test]
fn test_invalid_selector() {
    // Test various invalid patterns
    // Empty selector after &&
    let selector = Selector::from("role:Button &&");
    match selector {
        Selector::Invalid(msg) => {
            assert!(
                msg.contains("Parse error")
                    || msg.contains("Unknown selector")
                    || msg.contains("Expected selector")
            );
        }
        _ => panic!("Expected Invalid selector for trailing &&"),
    }

    // Unbalanced parentheses
    let selector2 = Selector::from("(role:Button && name:Test");
    match selector2 {
        Selector::Invalid(msg) => {
            assert!(msg.contains("Unmatched") || msg.contains("parenthes"));
        }
        _ => panic!("Expected Invalid selector for unbalanced parens"),
    }
}

#[test]
fn test_best_plan_pro_selector() {
    // Test the actual problematic selector from Best Plan Pro
    let selector = Selector::from("(role:Window && name:Best Plan Pro) >> nativeid:dob");
    match selector {
        Selector::Chain(selectors) => {
            assert_eq!(selectors.len(), 2);
            // First part: parenthesized AND
            match &selectors[0] {
                Selector::And(and_parts) => {
                    assert_eq!(and_parts.len(), 2);
                    match &and_parts[0] {
                        Selector::Role { role, .. } => assert_eq!(role, "Window"),
                        _ => panic!("Expected Role selector"),
                    }
                    match &and_parts[1] {
                        Selector::Name(name) => assert_eq!(name, "Best Plan Pro"),
                        _ => panic!("Expected Name selector"),
                    }
                }
                _ => panic!("Expected And selector"),
            }
            // Second part: nativeid
            match &selectors[1] {
                Selector::NativeId(id) => assert_eq!(id, "dob"),
                _ => panic!("Expected NativeId selector"),
            }
        }
        Selector::Invalid(msg) => {
            panic!("Selector parsing failed with: {msg}");
        }
        _ => panic!("Expected Chain selector, got: {selector:?}"),
    }
}

#[test]
fn test_chained_and_with_role_and_nativeid() {
    // Test: "(role:Window && name:Best Plan Pro) >> (role:Edit && nativeid:dob)"
    let selector =
        Selector::from("(role:Window && name:Best Plan Pro) >> (role:Edit && nativeid:dob)");
    match selector {
        Selector::Chain(selectors) => {
            assert_eq!(selectors.len(), 2);

            // First part: (role:Window && name:Best Plan Pro)
            match &selectors[0] {
                Selector::And(and_parts) => {
                    assert_eq!(and_parts.len(), 2);
                    match &and_parts[0] {
                        Selector::Role { role, .. } => assert_eq!(role, "Window"),
                        _ => panic!("Expected Role selector in first AND"),
                    }
                    match &and_parts[1] {
                        Selector::Name(name) => assert_eq!(name, "Best Plan Pro"),
                        _ => panic!("Expected Name selector in first AND"),
                    }
                }
                _ => panic!("Expected first chain element to be And selector"),
            }

            // Second part: (role:Edit && nativeid:dob)
            match &selectors[1] {
                Selector::And(and_parts) => {
                    assert_eq!(and_parts.len(), 2);
                    match &and_parts[0] {
                        Selector::Role { role, .. } => assert_eq!(role, "Edit"),
                        _ => panic!("Expected Role selector in second AND"),
                    }
                    match &and_parts[1] {
                        Selector::NativeId(id) => assert_eq!(id, "dob"),
                        _ => panic!("Expected NativeId selector in second AND"),
                    }
                }
                _ => panic!("Expected second chain element to be And selector"),
            }
        }
        Selector::Invalid(msg) => {
            panic!("Selector parsing failed with: {msg}");
        }
        _ => panic!("Expected Chain selector, got: {selector:?}"),
    }
}

#[test]
fn test_calculator_window_selector() {
    let selector = Selector::from("(role:Window && name:Calculator)");
    match selector {
        Selector::And(selectors) => {
            assert_eq!(selectors.len(), 2);
        }
        _ => panic!("Expected And selector"),
    }
}

#[test]
fn test_multiple_and_conditions() {
    let selector = Selector::from("role:Button && name:Plus && visible:true");
    match selector {
        Selector::And(selectors) => {
            assert_eq!(selectors.len(), 3);
            match &selectors[0] {
                Selector::Role { role, .. } => assert_eq!(role, "Button"),
                _ => panic!("Expected Role selector"),
            }
            match &selectors[1] {
                Selector::Name(name) => assert_eq!(name, "Plus"),
                _ => panic!("Expected Name selector"),
            }
            match &selectors[2] {
                Selector::Visible(v) => assert!(v),
                _ => panic!("Expected Visible selector"),
            }
        }
        _ => panic!("Expected And selector with 3 conditions"),
    }
}

#[test]
fn test_calculator_chain_with_parentheses_runtime() {
    // This is the exact selector pattern that failed at runtime with Calculator
    // Original error: Role: '(role', Name: Some("Window && name:Calculator)")
    let selector =
        Selector::from("(role:Window && name:Calculator) >> (role:Custom && nativeid:NavView)");
    match selector {
        Selector::Chain(selectors) => {
            assert_eq!(selectors.len(), 2);

            // First part: (role:Window && name:Calculator)
            match &selectors[0] {
                Selector::And(and_parts) => {
                    assert_eq!(and_parts.len(), 2);
                    match &and_parts[0] {
                        Selector::Role { role, .. } => assert_eq!(role, "Window"),
                        _ => panic!("Expected Role selector for Window, got: {:?}", and_parts[0]),
                    }
                    match &and_parts[1] {
                        Selector::Name(name) => assert_eq!(name, "Calculator"),
                        _ => panic!(
                            "Expected Name selector for Calculator, got: {:?}",
                            and_parts[1]
                        ),
                    }
                }
                _ => panic!(
                    "Expected And selector for first chain part, got: {:?}",
                    selectors[0]
                ),
            }

            // Second part: (role:Custom && nativeid:NavView)
            match &selectors[1] {
                Selector::And(and_parts) => {
                    assert_eq!(and_parts.len(), 2);
                    match &and_parts[0] {
                        Selector::Role { role, .. } => assert_eq!(role, "Custom"),
                        _ => panic!("Expected Role selector for Custom, got: {:?}", and_parts[0]),
                    }
                    match &and_parts[1] {
                        Selector::NativeId(id) => assert_eq!(id, "NavView"),
                        _ => panic!(
                            "Expected NativeId selector for NavView, got: {:?}",
                            and_parts[1]
                        ),
                    }
                }
                _ => panic!(
                    "Expected And selector for second chain part, got: {:?}",
                    selectors[1]
                ),
            }
        }
        Selector::Invalid(msg) => {
            panic!("Selector parsing failed with error: {msg}");
        }
        _ => panic!("Expected Chain selector, got: {selector:?}"),
    }
}

#[test]
fn test_nested_parentheses() {
    let selector = Selector::from("((role:Button && name:OK) || (role:Link && name:Submit))");
    match selector {
        Selector::Or(or_parts) => {
            assert_eq!(or_parts.len(), 2);
        }
        _ => panic!("Expected Or selector"),
    }
}

#[test]
fn test_chain_with_multiple_operators() {
    let selector = Selector::from("role:Window >> role:Group >> (role:Button && name:Calculate)");
    match selector {
        Selector::Chain(selectors) => {
            assert_eq!(selectors.len(), 3);
            // Third part should be AND
            match &selectors[2] {
                Selector::And(and_parts) => assert_eq!(and_parts.len(), 2),
                _ => panic!("Expected And selector as third part"),
            }
        }
        _ => panic!("Expected Chain selector"),
    }
}