chrome-agent 0.15.0

Browser automation for AI agents. Single binary, zero deps, CDP direct to Chrome.
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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
//! Rendering an accessibility tree into the compact text a snapshot is.
//!
//! Split from `snapshot.rs` for the 1000-line file cap. Everything here is pure: it takes
//! `&[AXNode]` and produces text plus the uid maps, with no CDP call and no I/O — which is
//! what lets `snapshot::take_views` render ONE reading twice, once at full depth for the
//! diff baseline and once through the caller's `--filter`/`--max-depth`/`--uid`.

use std::collections::HashMap;

use crate::cdp::types::AXNode;
use crate::element_ref::ElementRef;
use crate::snapshot::Redaction;

/// Format `AXNode` list into indented text + uid map.
///
/// CDP returns a flat list of `AXNodes` with parent/child relationships
/// via `parentId` and `childIds`. We reconstruct the tree and format it.
///
/// When `focus_uid` is set, we first do a full pass to assign uids (so
/// the numbering matches a normal inspect), then find the node whose uid
/// matches and re-render only that subtree from depth 0.
///
/// `preassigned` maps an `AXNode` id to the `e{n}` uid a previous rendering of the SAME tree
/// gave it. Anonymous uids come from a counter walked in traversal order, so a rendering
/// that skips nodes renumbers the ones it keeps; passing the full rendering's assignment in
/// keeps the printed uid and the stored one the same node. Returns its own assignment as the
/// third element so a caller can feed it forward.
pub fn format_ax_tree(
    nodes: &[AXNode],
    verbose: bool,
    max_depth: Option<usize>,
    focus_uid: Option<&str>,
    role_filter: Option<&[&str]>,
    redaction: &Redaction,
    preassigned: Option<&HashMap<String, String>>,
) -> (String, HashMap<String, ElementRef>, HashMap<String, String>) {
    // Build lookup: nodeId → AXNode
    let node_by_id: HashMap<&str, &AXNode> = nodes
        .iter()
        .map(|n| (n.node_id.as_str(), n))
        .collect();

    // Find root (node with no parentId, or first node)
    let root_id = nodes
        .iter()
        .find(|n| n.parent_id.is_none())
        .map(|n| n.node_id.as_str());

    let Some(root_id) = root_id else {
        return (String::new(), HashMap::new(), HashMap::new());
    };

    if let Some(focus) = focus_uid {
        // First pass: assign uids without max_depth to find the target node
        let mut uid_map_full = HashMap::new();
        let mut uid_counter: u32 = 0;
        let mut discard = String::new();
        // Map uid → AXNode nodeId so we can find the subtree root
        let mut uid_to_node_id: HashMap<String, String> = HashMap::new();
        let mut anon_full: HashMap<String, String> = HashMap::new();
        format_node_with_tracking(
            root_id,
            &node_by_id,
            0,
            verbose,
            None, // no depth limit for uid assignment
            &mut uid_counter,
            &mut uid_map_full,
            &mut discard,
            &mut uid_to_node_id,
            redaction,
            preassigned,
            &mut anon_full,
        );

        // Find the AXNode nodeId for the focus uid
        let focus_node_id = uid_to_node_id.get(focus);
        if let Some(focus_node_id) = focus_node_id {
            // Second pass: render only the subtree
            let mut uid_map = HashMap::new();
            let mut output = String::new();
            let mut uid_counter2: u32 = 0;
            let mut tracking2: HashMap<String, String> = HashMap::new();
            let mut anon2: HashMap<String, String> = HashMap::new();
            // The subtree inherits the assignment of whichever rendering came first — the
            // caller's, or failing that this function's own full pass above. A subtree walked
            // from depth 0 would otherwise restart the counter and hand `e1` to a node the
            // stored map knows under another name.
            let inherited = preassigned.unwrap_or(&anon_full);
            format_node_with_tracking(
                focus_node_id,
                &node_by_id,
                0, // reset depth to 0
                verbose,
                max_depth,
                &mut uid_counter2,
                &mut uid_map,
                &mut output,
                &mut tracking2,
                redaction,
                Some(inherited),
                &mut anon2,
            );
            return (apply_role_filter(output, role_filter, max_depth), uid_map, anon2);
        }

        // uid not found — return the diagnostic verbatim. Do NOT route it
        // through the role filter: the message begins with "uid=" and would be
        // stripped as a non-matching node, producing silent empty output — the
        // exact confusion the filter's own empty-guard (below) tries to prevent.
        return (
            format!("uid={focus} not found in accessibility tree\n"),
            uid_map_full,
            anon_full,
        );
    }

    // Normal (no focus_uid) path
    let mut uid_map = HashMap::new();
    let mut output = String::new();
    let mut uid_counter: u32 = 0;
    let mut anon: HashMap<String, String> = HashMap::new();
    format_node(
        root_id,
        &node_by_id,
        0,
        verbose,
        max_depth,
        &mut uid_counter,
        &mut uid_map,
        &mut output,
        redaction,
        preassigned,
        &mut anon,
    );

    // Post-filter by role if requested
    let output = apply_role_filter(output, role_filter, max_depth);

    (output, uid_map, anon)
}

/// Post-process rendered snapshot text, keeping only lines whose role matches
/// `role_filter` (with alias expansion). Returns `output` unchanged when no
/// filter is requested. When the filter matches nothing but a `max_depth` was
/// set, returns a hint instead of silent empty output.
///
/// Applied on every rendering path — including the `focus_uid` subtree — so
/// `inspect --uid nN --filter button` scopes to both the subtree and the role.
fn apply_role_filter(output: String, role_filter: Option<&[&str]>, max_depth: Option<usize>) -> String {
    let Some(roles) = role_filter else {
        return output;
    };
    // Expand role aliases so agents don't need to know exact ARIA role names
    let expanded: Vec<String> = roles.iter().flat_map(|&r| {
        let mut v = vec![(*r).to_string()];
        match r.to_lowercase().as_str() {
            "textbox" => { v.push("searchbox".into()); v.push("combobox".into()); }
            "input" => {
                for r in ["textbox", "searchbox", "combobox", "checkbox", "radio", "slider", "spinbutton", "switch"] {
                    v.push(r.into());
                }
            }
            "button" => { v.push("menuitem".into()); }
            _ => {}
        }
        v
    }).collect();
    let filtered: String = output
        .lines()
        .filter(|line| {
            let trimmed = line.trim();
            if let Some(after_uid) = trimmed.strip_prefix("uid=")
                && let Some(rest) = after_uid.split_once(' ') {
                    let role = rest.1.split([' ', '"']).next().unwrap_or("");
                    return expanded.iter().any(|r| r.eq_ignore_ascii_case(role));
                }
            false
        })
        .fold(String::new(), |mut acc, line| {
            acc.push_str(line.trim_start());
            acc.push('\n');
            acc
        });
    // Warn if filter matched nothing — likely the matching elements are deeper
    // than max_depth. This prevents silent empty output that confuses agents.
    if filtered.is_empty() && max_depth.is_some() {
        format!("No elements matching filter {:?} found within --max-depth {}. Try increasing depth or removing --max-depth.\n",
            roles, max_depth.unwrap_or(0))
    } else {
        filtered
    }
}

fn format_node(
    node_id: &str,
    nodes: &HashMap<&str, &AXNode>,
    depth: usize,
    verbose: bool,
    max_depth: Option<usize>,
    uid_counter: &mut u32,
    uid_map: &mut HashMap<String, ElementRef>,
    output: &mut String,
    redaction: &Redaction,
    preassigned: Option<&HashMap<String, String>>,
    anon: &mut HashMap<String, String>,
) {
    let mut discard: HashMap<String, String> = HashMap::new();
    format_node_with_tracking(
        node_id, nodes, depth, verbose, max_depth, uid_counter, uid_map, output, &mut discard,
        redaction, preassigned, anon,
    );
}

fn format_node_with_tracking(
    node_id: &str,
    nodes: &HashMap<&str, &AXNode>,
    depth: usize,
    verbose: bool,
    max_depth: Option<usize>,
    uid_counter: &mut u32,
    uid_map: &mut HashMap<String, ElementRef>,
    output: &mut String,
    uid_to_node_id: &mut HashMap<String, String>,
    redaction: &Redaction,
    preassigned: Option<&HashMap<String, String>>,
    anon: &mut HashMap<String, String>,
) {
    let Some(node) = nodes.get(node_id) else {
        return;
    };

    // Skip ignored nodes unless verbose
    if node.ignored && !verbose {
        // Still recurse into children — some ignored nodes have visible children
        if let Some(child_ids) = &node.child_ids {
            for child_id in child_ids {
                format_node_with_tracking(child_id, nodes, depth, verbose, max_depth, uid_counter, uid_map, output, uid_to_node_id, redaction, preassigned, anon);
            }
        }
        return;
    }

    let role = node.role_name().unwrap_or("");
    let mut name = node.name_value().unwrap_or("").to_string();

    // Skip noise roles unless verbose — these repeat parent content and waste tokens
    const NOISE_ROLES: &[&str] = &["none", "StaticText", "InlineTextBox"];
    if !verbose && NOISE_ROLES.contains(&role) {
        if let Some(child_ids) = &node.child_ids {
            for child_id in child_ids {
                format_node_with_tracking(child_id, nodes, depth, verbose, max_depth, uid_counter, uid_map, output, uid_to_node_id, redaction, preassigned, anon);
            }
        }
        return;
    }

    // If name is empty and we're filtering noise, pull text from StaticText children
    if !verbose && name.is_empty()
        && let Some(child_ids) = &node.child_ids {
            let texts: Vec<&str> = child_ids
                .iter()
                .filter_map(|cid| nodes.get(cid.as_str()))
                .filter(|n| n.role_name() == Some("StaticText"))
                .filter_map(|n| n.name_value())
                .collect();
            if !texts.is_empty() {
                name = texts.join(" ");
            }
        }

    // Skip generic containers with no name unless verbose
    if !verbose && role == "generic" && name.is_empty() {
        if let Some(child_ids) = &node.child_ids {
            for child_id in child_ids {
                format_node_with_tracking(child_id, nodes, depth, verbose, max_depth, uid_counter, uid_map, output, uid_to_node_id, redaction, preassigned, anon);
            }
        }
        return;
    }

    // Assign uid — stable (based on backendNodeId) when available, sequential fallback.
    // The fallback is positional, so a rendering that skips nodes would renumber the ones it
    // keeps: `preassigned` carries the numbering of the rendering that ran first over the
    // same tree, so the printed uid and the stored one name the same node.
    let uid = if let Some(backend_id) = node.backend_dom_node_id {
        let uid = format!("n{backend_id}");
        uid_map.insert(uid.clone(), ElementRef::backend_node(backend_id));
        uid
    } else if let Some(existing) = preassigned.and_then(|m| m.get(node_id)) {
        existing.clone()
    } else {
        *uid_counter += 1;
        let uid = format!("e{uid_counter}");
        anon.insert(node_id.to_string(), uid.clone());
        uid
    };

    // Track uid → AXNode nodeId for focus_uid lookup
    uid_to_node_id.insert(uid.clone(), node_id.to_string());

    // Build attribute string
    let indent = "  ".repeat(depth);
    output.push_str(&indent);
    output.push_str("uid=");
    output.push_str(&uid);

    if !role.is_empty() {
        output.push(' ');
        if role == "none" {
            output.push_str("ignored");
        } else {
            output.push_str(role);
        }
    }

    if !name.is_empty() {
        output.push_str(" \"");
        output.push_str(redaction.name(node.backend_dom_node_id, &name));
        output.push('"');
    }

    // Value (for inputs). The marker stays inside the quotes: `diff` reads a value through the
    // `value="` prefix, and an unquoted marker would read as a field that holds nothing —
    // turning every secret into a `values_lost` on the next comparison.
    if let Some(value_ax) = &node.value
        && let Some(val) = value_ax.value.as_ref().and_then(|v| v.as_str())
            && !val.is_empty() {
                output.push_str(" value=\"");
                output.push_str(redaction.value(node.backend_dom_node_id, val));
                output.push('"');
            }

    // Properties: focused, disabled, expanded, selected, level, checked
    if let Some(props) = &node.properties {
        for prop in props {
            let prop_val = prop.value.value.as_ref();
            match prop.name.as_str() {
                "focused" => {
                    if prop_val.and_then(serde_json::Value::as_bool).unwrap_or(false) {
                        output.push_str(" focused");
                    }
                }
                "disabled" => {
                    if prop_val.and_then(serde_json::Value::as_bool).unwrap_or(false) {
                        output.push_str(" disabled");
                    }
                }
                "expanded" => {
                    if prop_val.and_then(serde_json::Value::as_bool).unwrap_or(false) {
                        output.push_str(" expanded");
                    }
                }
                "selected" => {
                    if prop_val.and_then(serde_json::Value::as_bool).unwrap_or(false) {
                        output.push_str(" selected");
                    }
                }
                "checked" => {
                    if let Some(val) = prop_val.and_then(|v| v.as_str())
                        && val != "false" {
                            output.push_str(" checked=");
                            output.push_str(val);
                        }
                }
                "level" => {
                    if let Some(level) = prop_val.and_then(serde_json::Value::as_u64) {
                        output.push_str(&format!(" level={level}"));
                    }
                }
                "required" => {
                    if prop_val.and_then(serde_json::Value::as_bool).unwrap_or(false) {
                        output.push_str(" required");
                    }
                }
                "readonly" => {
                    if prop_val.and_then(serde_json::Value::as_bool).unwrap_or(false) {
                        output.push_str(" readonly");
                    }
                }
                _ => {
                    // Include all properties in verbose mode
                    if verbose
                        && let Some(val) = prop_val {
                            output.push(' ');
                            output.push_str(&prop.name);
                            output.push('=');
                            match val {
                                serde_json::Value::Bool(b) => output.push_str(&b.to_string()),
                                serde_json::Value::Number(n) => output.push_str(&n.to_string()),
                                serde_json::Value::String(s) => {
                                    output.push('"');
                                    output.push_str(redaction.name(node.backend_dom_node_id, s));
                                    output.push('"');
                                }
                                _ => output.push_str(&val.to_string()),
                            }
                        }
                }
            }
        }
    }

    output.push('\n');

    // Depth limit: skip children if we've reached max_depth
    if let Some(max) = max_depth
        && depth >= max {
            return;
        }

    // Recurse children
    if let Some(child_ids) = &node.child_ids {
        for child_id in child_ids {
            format_node_with_tracking(
                child_id,
                nodes,
                depth + 1,
                verbose,
                max_depth,
                uid_counter,
                uid_map,
                output,
                uid_to_node_id,
                redaction,
                preassigned,
                anon,
            );
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cdp::types::{AXValue, AXProperty};

    fn make_ax_value(s: &str) -> AXValue {
        AXValue {
            value: Some(serde_json::Value::String(s.into())),
        }
    }

    fn default_ax_node() -> AXNode {
        AXNode {
            node_id: String::new(),
            ignored: false,
            role: None,
            name: None,
            value: None,
            properties: None,
            child_ids: None,
            backend_dom_node_id: None,
            parent_id: None,
        }
    }

    fn make_bool_prop(name: &str, val: bool) -> AXProperty {
        AXProperty {
            name: name.into(),
            value: AXValue {
                value: Some(serde_json::Value::Bool(val)),
            },
        }
    }

    #[test]
    fn formats_simple_tree() {
        let nodes = vec![
            AXNode {
                node_id: "1".into(),
                ignored: false,
                role: Some(make_ax_value("heading")),
                name: Some(make_ax_value("Welcome")),
                value: None,
                properties: Some(vec![AXProperty {
                    name: "level".into(),
                    value: AXValue {
                        value: Some(serde_json::json!(1)),
                    },
                }]),
                child_ids: Some(vec![]),
                backend_dom_node_id: Some(10),
                parent_id: None,
            },
        ];

        let (text, uid_map, _) = format_ax_tree(&nodes, false, None, None, None, &Redaction::none(), None);
        assert!(text.contains("uid=n10 heading \"Welcome\" level=1"));
        assert!(uid_map.contains_key("n10"));
        assert_eq!(uid_map["n10"].backend_node_id(), Some(10));
    }

    #[test]
    fn input_alias_covers_all_input_roles() {
        // CLAUDE.md contract: "input→all input roles". A checkbox/radio/slider
        // silently dropped by --filter input looks like the page has no such
        // control at all.
        let output = "uid=n1 textbox \"Name\"\n\
                      uid=n2 checkbox \"Agree\"\n\
                      uid=n3 radio \"Choice A\"\n\
                      uid=n4 slider \"Volume\"\n\
                      uid=n5 spinbutton \"Qty\"\n\
                      uid=n6 switch \"Dark mode\"\n\
                      uid=n7 searchbox \"Search\"\n\
                      uid=n8 combobox \"Country\"\n\
                      uid=n9 button \"Go\"\n"
            .to_string();
        let filtered = apply_role_filter(output, Some(&["input"]), None);
        for role in ["textbox", "checkbox", "radio", "slider", "spinbutton", "switch", "searchbox", "combobox"] {
            assert!(filtered.contains(role), "input alias should keep role {role}, got:\n{filtered}");
        }
        assert!(!filtered.contains("uid=n9"), "input alias must not keep non-input roles, got:\n{filtered}");
    }

    #[test]
    fn skips_ignored_nodes() {
        let nodes = vec![
            AXNode {
                node_id: "1".into(),
                ignored: true,
                role: None,
                name: None,
                value: None,
                properties: None,
                child_ids: Some(vec!["2".into()]),
                backend_dom_node_id: None,
                parent_id: None,
            },
            AXNode {
                node_id: "2".into(),
                ignored: false,
                role: Some(make_ax_value("button")),
                name: Some(make_ax_value("Click me")),
                value: None,
                properties: Some(vec![make_bool_prop("focused", true)]),
                child_ids: Some(vec![]),
                backend_dom_node_id: Some(20),
                parent_id: Some("1".into()),
            },
        ];

        let (text, uid_map, _) = format_ax_tree(&nodes, false, None, None, None, &Redaction::none(), None);
        assert!(!text.contains("ignored"));
        assert!(text.contains("uid=n20 button \"Click me\" focused"));
        assert_eq!(uid_map.len(), 1);
    }

    #[test]
    fn max_depth_limits_output() {
        let nodes = vec![
            AXNode {
                node_id: "1".into(),
                role: Some(make_ax_value("heading")),
                name: Some(make_ax_value("Root")),
                child_ids: Some(vec!["2".into()]),
                parent_id: None,
                backend_dom_node_id: Some(1),
                ..default_ax_node()
            },
            AXNode {
                node_id: "2".into(),
                role: Some(make_ax_value("button")),
                name: Some(make_ax_value("Child")),
                child_ids: Some(vec!["3".into()]),
                parent_id: Some("1".into()),
                backend_dom_node_id: Some(2),
                ..default_ax_node()
            },
            AXNode {
                node_id: "3".into(),
                role: Some(make_ax_value("link")),
                name: Some(make_ax_value("Grand")),
                child_ids: Some(vec![]),
                parent_id: Some("2".into()),
                backend_dom_node_id: Some(3),
                ..default_ax_node()
            },
        ];
        let (text, _, _) = format_ax_tree(&nodes, false, Some(1), None, None, &Redaction::none(), None);
        assert!(text.contains("Root"));
        assert!(text.contains("Child"));
        assert!(!text.contains("Grand")); // depth 2 filtered
    }

    #[test]
    fn focus_uid_scopes_subtree() {
        let nodes = vec![
            AXNode {
                node_id: "1".into(),
                role: Some(make_ax_value("WebArea")),
                name: Some(make_ax_value("Page")),
                child_ids: Some(vec!["2".into(), "3".into()]),
                parent_id: None,
                backend_dom_node_id: Some(1),
                ..default_ax_node()
            },
            AXNode {
                node_id: "2".into(),
                role: Some(make_ax_value("heading")),
                name: Some(make_ax_value("Title")),
                child_ids: Some(vec![]),
                parent_id: Some("1".into()),
                backend_dom_node_id: Some(2),
                ..default_ax_node()
            },
            AXNode {
                node_id: "3".into(),
                role: Some(make_ax_value("button")),
                name: Some(make_ax_value("Submit")),
                child_ids: Some(vec![]),
                parent_id: Some("1".into()),
                backend_dom_node_id: Some(3),
                ..default_ax_node()
            },
        ];
        // n1=WebArea, n2=heading, n3=button — focus on n3
        let (text, _, _) = format_ax_tree(&nodes, false, None, Some("n3"), None, &Redaction::none(), None);
        assert!(text.contains("Submit"));
        assert!(!text.contains("Title"));
    }

    #[test]
    fn focus_uid_applies_role_filter() {
        // Regression (A10e): the focus_uid branch rendered the subtree but never
        // applied role_filter. `inspect --uid n1 --filter button` must return only
        // button-role descendants, not the whole subtree.
        let nodes = vec![
            AXNode {
                node_id: "1".into(),
                role: Some(make_ax_value("form")),
                name: Some(make_ax_value("Signup")),
                child_ids: Some(vec!["2".into(), "3".into()]),
                parent_id: None,
                backend_dom_node_id: Some(1),
                ..default_ax_node()
            },
            AXNode {
                node_id: "2".into(),
                role: Some(make_ax_value("heading")),
                name: Some(make_ax_value("Please sign up")),
                child_ids: Some(vec![]),
                parent_id: Some("1".into()),
                backend_dom_node_id: Some(2),
                ..default_ax_node()
            },
            AXNode {
                node_id: "3".into(),
                role: Some(make_ax_value("button")),
                name: Some(make_ax_value("Submit")),
                child_ids: Some(vec![]),
                parent_id: Some("1".into()),
                backend_dom_node_id: Some(3),
                ..default_ax_node()
            },
        ];
        // Focus the form (n1) AND filter to buttons: only the Submit button remains.
        let (text, _, _) = format_ax_tree(&nodes, false, None, Some("n1"), Some(&["button"]), &Redaction::none(), None);
        assert!(text.contains("uid=n3 button \"Submit\""));
        assert!(!text.contains("Please sign up")); // heading filtered out
        assert!(!text.contains("form")); // focus root also filtered (not a button)
    }

    #[test]
    fn focus_uid_not_found() {
        let nodes = vec![AXNode {
            node_id: "1".into(),
            role: Some(make_ax_value("heading")),
            name: Some(make_ax_value("Root")),
            child_ids: Some(vec![]),
            parent_id: None,
            backend_dom_node_id: Some(1),
            ..default_ax_node()
        }];
        let (text, _, _) = format_ax_tree(&nodes, false, None, Some("e99"), None, &Redaction::none(), None);
        assert!(text.contains("not found"));
    }

    #[test]
    fn bug_empty_tree() {
        let nodes: Vec<AXNode> = vec![];
        let (text, uid_map, _) = format_ax_tree(&nodes, false, None, None, None, &Redaction::none(), None);
        assert!(text.is_empty());
        assert!(uid_map.is_empty());
    }

    #[test]
    fn bug_all_ignored_nodes() {
        let nodes = vec![
            AXNode {
                node_id: "1".into(),
                ignored: true,
                child_ids: Some(vec!["2".into()]),
                parent_id: None,
                ..default_ax_node()
            },
            AXNode {
                node_id: "2".into(),
                ignored: true,
                child_ids: Some(vec![]),
                parent_id: Some("1".into()),
                ..default_ax_node()
            },
        ];
        let (text, uid_map, _) = format_ax_tree(&nodes, false, None, None, None, &Redaction::none(), None);
        // All nodes ignored = empty output
        assert!(text.is_empty());
        assert!(uid_map.is_empty());
    }

    #[test]
    fn bug_filter_no_match() {
        let nodes = vec![AXNode {
            node_id: "1".into(),
            role: Some(make_ax_value("heading")),
            name: Some(make_ax_value("Title")),
            child_ids: Some(vec![]),
            parent_id: None,
            backend_dom_node_id: Some(1),
            ..default_ax_node()
        }];
        // Filter for "button" but only heading exists
        let (text, _, _) = format_ax_tree(&nodes, false, None, None, Some(&["button"]), &Redaction::none(), None);
        assert!(text.is_empty() || !text.contains("heading"));
    }

    /// A tree whose anonymous nodes sit at different depths, so a truncated traversal
    /// reaches them in a different order than a full one.
    fn tree_with_anonymous_nodes() -> Vec<AXNode> {
        vec![
            AXNode {
                node_id: "1".into(),
                role: Some(make_ax_value("WebArea")),
                name: Some(make_ax_value("Page")),
                child_ids: Some(vec!["2".into(), "4".into()]),
                parent_id: None,
                backend_dom_node_id: Some(1),
                ..default_ax_node()
            },
            AXNode {
                node_id: "2".into(),
                role: Some(make_ax_value("region")),
                name: Some(make_ax_value("Deep branch")),
                child_ids: Some(vec!["3".into()]),
                parent_id: Some("1".into()),
                backend_dom_node_id: Some(2),
                ..default_ax_node()
            },
            // No backendDOMNodeId, and only reachable at depth 2.
            AXNode {
                node_id: "3".into(),
                role: Some(make_ax_value("banner")),
                name: Some(make_ax_value("Deep anonymous")),
                child_ids: Some(vec![]),
                parent_id: Some("2".into()),
                backend_dom_node_id: None,
                ..default_ax_node()
            },
            // No backendDOMNodeId, reachable at depth 1.
            AXNode {
                node_id: "4".into(),
                role: Some(make_ax_value("contentinfo")),
                name: Some(make_ax_value("Shallow anonymous")),
                child_ids: Some(vec![]),
                parent_id: Some("1".into()),
                backend_dom_node_id: None,
                ..default_ax_node()
            },
        ]
    }

    #[test]
    fn anonymous_uids_are_renumbered_by_a_truncated_traversal() {
        // The behaviour the preassignment exists to correct. `e{n}` comes from a counter
        // walked in traversal order: the deep node takes `e1` when the whole tree is walked
        // and never appears when the walk stops at depth 1, so the shallow node moves from
        // `e2` to `e1`. Two renderings of ONE tree, and `e1` naming two different nodes.
        let nodes = tree_with_anonymous_nodes();
        let (full, _, _) = format_ax_tree(&nodes, false, None, None, None, &Redaction::none(), None);
        assert!(full.contains("uid=e1 banner \"Deep anonymous\""), "got {full}");
        assert!(full.contains("uid=e2 contentinfo \"Shallow anonymous\""), "got {full}");

        let (shallow, _, _) =
            format_ax_tree(&nodes, false, Some(1), None, None, &Redaction::none(), None);
        assert!(shallow.contains("uid=e1 contentinfo \"Shallow anonymous\""), "got {shallow}");
    }

    #[test]
    fn a_preassigned_anonymous_uid_survives_a_truncated_traversal() {
        // What `take_views` relies on: the baseline is rendered first, the caller's reduced
        // view second, and the second inherits the first's numbering. Without it the uid
        // printed for a node and the uid stored for it are different nodes — the uid map
        // would resolve `e1` to the deep node while the output offered `e1` for the shallow
        // one.
        let nodes = tree_with_anonymous_nodes();
        let (_, _, anon) = format_ax_tree(&nodes, false, None, None, None, &Redaction::none(), None);
        let (shallow, _, _) =
            format_ax_tree(&nodes, false, Some(1), None, None, &Redaction::none(), Some(&anon));
        assert!(
            shallow.contains("uid=e2 contentinfo \"Shallow anonymous\""),
            "the reduced view must keep the number the full view gave this node, got {shallow}"
        );
        assert!(!shallow.contains("uid=e1"), "e1 belongs to the deep node, got {shallow}");
    }

}