bao_cdp 0.3.13

A Rust-native programmable browser runtime built on Servo and SpiderMonkey
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
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
// REQ-BRW-048 DevTools CDP domain e2e: the DOM/CSS query methods return the
// live page's real data through the production dispatch + bridge + servo
// stack. The client half runs handle_command against a real BridgeSender on
// a helper thread; the main thread spins servo and drains the bridge with
// bao_browser::handle_bridge_command — the run_with_bridge loop shape minus
// the WS transport (socket routing is covered by bao_browser's
// cdp_ws_command_face_tests).
//
// Covered faces:
//   CSS.getComputedStyleForNode — engine-computed values match the page's
//     stylesheet declarations (font-size / color)
//   CSS.getInlineStylesForNode — the style attribute's real declarations
//   CSS.getMatchedStylesForNode — author rules whose selectors match, with
//     their declarations (servo CSSOM: document.styleSheets.cssRules)
//   DOM.getFlattenedDocument — document-order flat node list, parentId
//     links, children arrays, CDP depth semantics
//   DOM.getNodeForOwner — RemoteObject objectId → canonical nodeId
//
// Canonical NodeId identity (devtools_dom): root document = 1, child =
// parent*100 + childIndex + 1 — the same encoding the DOM.getDocument
// bridge walker emits, asserted here as a cross-face equality.
// @trace REQ-BRW-048 [req:REQ-BRW-048] [level:e2e]

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;

use bao_browser::{handle_bridge_command, BaoConfig, BrowserRuntime, PageConfig};
use bao_cdp::{bridge_channel, handle_command, BridgeSender, CdpMessage, CdpResponse};
use serde_json::{json, Value};

/// The styled test document. No `#` characters (a data-URL fragment would
/// truncate the document) and a single line (URL parsing drops newlines).
const STYLED_HTML: &str = r#"<html><head><style>div.t { color: rgb(12, 34, 56); font-size: 20px; } p.x { font-weight: bold; }</style></head><body><div class="t" id="target" style="margin-top: 4px">styled</div><p class="x">para</p></body></html>"#;

/// Dispatch one command against the live bridge; any error response is a
/// test failure (the error-path tests use [`dispatch_raw`]).
fn dispatch(
    bridge: &BridgeSender,
    target: &str,
    id: i64,
    method: &str,
    params: Value,
) -> Value {
    let resp = dispatch_raw(bridge, target, id, method, params);
    if let Some(e) = resp.error {
        panic!("{method} failed: {} {}", e.code, e.message);
    }
    resp.result.expect("success response carries a result")
}

/// Dispatch one command and hand back the raw CdpResponse (error-path face).
fn dispatch_raw(
    bridge: &BridgeSender,
    target: &str,
    id: i64,
    method: &str,
    params: Value,
) -> CdpResponse {
    let msg = CdpMessage {
        id: Some(id),
        method: method.to_string(),
        params: Some(params),
        session_id: None,
    };
    let params = msg.params.clone();
    handle_command(msg, target, &params, Some(bridge))
}

/// Runtime.evaluate helper that tolerates transient mid-navigation errors:
/// returns the unwrapped `result.result` value, or None while the page is
/// still navigating.
fn eval_tolerant(
    bridge: &BridgeSender,
    target: &str,
    id: i64,
    expression: &str,
) -> Option<Value> {
    let resp = dispatch_raw(
        bridge,
        target,
        id,
        "Runtime.evaluate",
        json!({ "expression": expression, "returnByValue": true }),
    );
    if resp.error.is_some() {
        return None;
    }
    resp.result.map(|r| r["result"]["value"].clone())
}

/// Wait until the navigated document exposes `div.t` — the gate is content
/// only the real document has (the about:blank placeholder also has a body,
/// so weak gates pass mid-navigation).
fn wait_div_ready(bridge: &BridgeSender, target: &str, id: i64) {
    for _ in 0..200 {
        if eval_tolerant(bridge, target, id, "!!document.querySelector('div.t')")
            == Some(json!(true))
        {
            return;
        }
        std::thread::sleep(Duration::from_millis(50));
    }
    panic!("navigated document never became ready");
}

/// Navigate the fresh page to the styled document and wait for it.
fn navigate_to_styled_page(bridge: &BridgeSender, target: &str) {
    let url = format!("data:text/html;charset=utf-8,{STYLED_HTML}");
    let resp = dispatch(
        bridge,
        target,
        1,
        "Page.navigate",
        json!({ "url": url }),
    );
    assert!(resp["frameId"].is_string(), "navigate returns a frameId");
    wait_div_ready(bridge, target, 2);
}

/// Depth-first search for the first node with `nodeName` in a
/// DOM.getDocument-style tree.
fn find_node<'a>(node: &'a Value, name: &str) -> Option<&'a Value> {
    if node["nodeName"].as_str() == Some(name) {
        return Some(node);
    }
    node["children"]
        .as_array()
        .and_then(|kids| kids.iter().find_map(|k| find_node(k, name)))
}

/// Depth-first search for the first node with `nodeName` in a flat
/// DOM.getFlattenedDocument node list.
fn find_flat<'a>(nodes: &'a [Value], name: &str) -> &'a Value {
    nodes
        .iter()
        .find(|n| n["nodeName"].as_str() == Some(name))
        .unwrap_or_else(|| panic!("flat nodes must contain {name}"))
}

/// Run `client` against a live page navigated to [`STYLED_HTML`]. The main
/// thread spins the servo event loop and drains the bridge with the
/// production handler while the client thread drives CDP dispatches.
fn with_styled_page(client: impl FnOnce(&BridgeSender, &str) + Send + 'static) {
    let runtime = BrowserRuntime::new(BaoConfig::default()).expect("BrowserRuntime::new");
    let page = runtime
        .create_page(&PageConfig {
            url: None,
            ..Default::default()
        })
        .expect("initial page");
    let target = page.id().to_string();

    let (bridge_tx, bridge_rx) = bridge_channel(Duration::from_secs(30));
    let done = Arc::new(AtomicBool::new(false));
    let done_client = done.clone();
    let sender = bridge_tx.clone();
    let client = std::thread::spawn(move || {
        client(&sender, &target);
        done_client.store(true, Ordering::Relaxed);
    });

    let deadline = std::time::Instant::now() + Duration::from_secs(120);
    while !done.load(Ordering::Relaxed) && std::time::Instant::now() < deadline {
        runtime.spin_event_loop();
        bridge_rx.drain(|cmd| handle_bridge_command(cmd, runtime.page_pool()));
        std::thread::yield_now();
    }

    client.join().expect("client phase must not panic");
    assert!(
        done.load(Ordering::Relaxed),
        "client phase completed all assertions"
    );
}

// ---------------------------------------------------------------------------
// 1. CSS.getComputedStyleForNode — real engine-computed values
// ---------------------------------------------------------------------------

#[test]
fn computed_style_matches_page_declarations() {
    with_styled_page(|bridge, target| {
        navigate_to_styled_page(bridge, target);

        // Identity unification: the DOM.getDocument walker's nodeIds and the
        // flattened list's nodeIds must address the same node.
        let doc = dispatch(bridge, target, 3, "DOM.getDocument", json!({}));
        let doc_div = find_node(&doc["root"], "DIV").expect("document tree has the div");
        let div_id = doc_div["nodeId"].as_i64().expect("nodeId is an integer");

        let flat = dispatch(
            bridge,
            target,
            4,
            "DOM.getFlattenedDocument",
            json!({ "depth": -1 }),
        );
        let nodes = flat["nodes"].as_array().expect("nodes array");
        let flat_div = find_flat(nodes, "DIV");
        assert_eq!(
            flat_div["nodeId"].as_i64(),
            Some(div_id),
            "flatten and getDocument must share one node identity"
        );

        let cs = dispatch(
            bridge,
            target,
            5,
            "CSS.getComputedStyleForNode",
            json!({ "nodeId": div_id }),
        );
        let styles = cs["computedStyle"].as_array().expect("computedStyle array");
        assert!(
            !styles.is_empty(),
            "a styled element has a real computed style list"
        );

        // font-size: declared 20px — the engine's computed value.
        let font_size = styles
            .iter()
            .find(|s| s["name"] == "font-size")
            .unwrap_or_else(|| panic!("font-size present in {styles:?}"));
        assert_eq!(font_size["value"], "20px");

        // color: declared rgb(12, 34, 56) — match the channel-insensitive
        // serialization (rgb vs rgba form).
        let color = styles
            .iter()
            .find(|s| s["name"] == "color")
            .unwrap_or_else(|| panic!("color present in {styles:?}"));
        let color_value = color["value"].as_str().expect("color value is a string");
        assert!(
            color_value.contains("12, 34, 56"),
            "computed color must reflect the declaration, got: {color_value}"
        );
    });
}

// ---------------------------------------------------------------------------
// 2. CSS.getInlineStylesForNode — the style attribute's real declarations
// ---------------------------------------------------------------------------

#[test]
fn inline_styles_report_style_attribute() {
    with_styled_page(|bridge, target| {
        navigate_to_styled_page(bridge, target);

        let flat = dispatch(
            bridge,
            target,
            3,
            "DOM.getFlattenedDocument",
            json!({ "depth": -1 }),
        );
        let nodes = flat["nodes"].as_array().expect("nodes array");
        let div_id = find_flat(nodes, "DIV")["nodeId"].as_i64().unwrap();
        let p_id = find_flat(nodes, "P")["nodeId"].as_i64().unwrap();

        let inline = dispatch(
            bridge,
            target,
            4,
            "CSS.getInlineStylesForNode",
            json!({ "nodeId": div_id }),
        );
        let props = inline["inlineStyle"]["cssProperties"]
            .as_array()
            .expect("inlineStyle.cssProperties array");
        let margin_top = props
            .iter()
            .find(|p| p["name"] == "margin-top")
            .unwrap_or_else(|| panic!("margin-top present in {props:?}"));
        assert_eq!(margin_top["value"], "4px");
        assert_eq!(margin_top["important"], false);

        // A node without a style attribute reports a real null — there is
        // nothing to report, not a suppressed answer.
        let bare = dispatch(
            bridge,
            target,
            5,
            "CSS.getInlineStylesForNode",
            json!({ "nodeId": p_id }),
        );
        assert!(
            bare["inlineStyle"].is_null(),
            "node without a style attribute has no inline style"
        );
    });
}

// ---------------------------------------------------------------------------
// 3. CSS.getMatchedStylesForNode — matching author rules + declarations
// ---------------------------------------------------------------------------

#[test]
fn matched_styles_report_author_rules() {
    with_styled_page(|bridge, target| {
        navigate_to_styled_page(bridge, target);

        let flat = dispatch(
            bridge,
            target,
            3,
            "DOM.getFlattenedDocument",
            json!({ "depth": -1 }),
        );
        let nodes = flat["nodes"].as_array().expect("nodes array");
        let div_id = find_flat(nodes, "DIV")["nodeId"].as_i64().unwrap();
        let p_id = find_flat(nodes, "P")["nodeId"].as_i64().unwrap();

        let ms = dispatch(
            bridge,
            target,
            4,
            "CSS.getMatchedStylesForNode",
            json!({ "nodeId": div_id }),
        );
        let rules = ms["matchedCSSRules"].as_array().expect("matchedCSSRules");
        assert!(
            !rules.is_empty(),
            "the div matches the stylesheet's div.t rule"
        );
        let rule = &rules[0];
        assert_eq!(
            rule["rule"]["selectorList"]["selectors"][0]["text"],
            json!("div.t"),
            "the reported selector is the stylesheet's selector"
        );
        let decls = rule["rule"]["style"]["cssProperties"]
            .as_array()
            .expect("rule style declarations");
        let color = decls
            .iter()
            .find(|d| d["name"] == "color")
            .unwrap_or_else(|| panic!("color declaration present in {decls:?}"));
        let color_value = color["value"].as_str().expect("declared color value");
        assert!(
            color_value.contains("12, 34, 56"),
            "declared color must match the stylesheet, got: {color_value}"
        );
        assert_eq!(color["important"], false);

        // Selector fidelity: the p.x element matches p.x, not div.t.
        let ms_p = dispatch(
            bridge,
            target,
            5,
            "CSS.getMatchedStylesForNode",
            json!({ "nodeId": p_id }),
        );
        let rules_p = ms_p["matchedCSSRules"].as_array().expect("matchedCSSRules");
        let selectors: Vec<&str> = rules_p
            .iter()
            .filter_map(|r| r["rule"]["selectorList"]["selectors"][0]["text"].as_str())
            .collect();
        assert!(
            selectors.contains(&"p.x"),
            "p.x must match its own rule, got: {selectors:?}"
        );
        assert!(
            !selectors.contains(&"div.t"),
            "div.t must not match the p element, got: {selectors:?}"
        );
    });
}

// ---------------------------------------------------------------------------
// 4. DOM.getFlattenedDocument — flat list, parentId links, depth semantics
// ---------------------------------------------------------------------------

#[test]
fn flattened_document_depth_semantics_and_parent_links() {
    with_styled_page(|bridge, target| {
        navigate_to_styled_page(bridge, target);

        // depth: 1 → root plus one level of children (document + doctype +
        // html). BODY is two levels down and must NOT appear.
        let shallow = dispatch(
            bridge,
            target,
            3,
            "DOM.getFlattenedDocument",
            json!({ "depth": 1 }),
        );
        let shallow_nodes = shallow["nodes"].as_array().expect("nodes array");
        assert_eq!(
            shallow_nodes[0]["nodeId"],
            1,
            "the root document is nodeId 1"
        );
        assert!(
            shallow_nodes
                .iter()
                .any(|n| n["nodeName"].as_str() == Some("HTML")),
            "depth 1 includes the html element"
        );
        assert!(
            shallow_nodes.iter().all(|n| n["nodeName"].as_str() != Some("BODY")),
            "depth 1 must not descend to BODY, got: {shallow_nodes:?}"
        );

        // Every node except the root carries parentId.
        for node in &shallow_nodes[1..] {
            assert!(
                node.get("parentId").is_some(),
                "non-root node lacks parentId: {node}"
            );
        }

        // depth: -1 → the entire subtree.
        let full = dispatch(
            bridge,
            target,
            4,
            "DOM.getFlattenedDocument",
            json!({ "depth": -1 }),
        );
        let nodes = full["nodes"].as_array().expect("nodes array");
        let body = find_flat(nodes, "BODY");
        let div = find_flat(nodes, "DIV");
        assert!(
            div.get("parentId").is_some() && div["parentId"] == body["nodeId"],
            "the div's parentId links to the body"
        );

        // Element nodes carry their real attributes as a flat name/value list.
        let attrs = div["attributes"].as_array().expect("attributes array");
        let class_idx = attrs
            .iter()
            .position(|a| a.as_str() == Some("class"))
            .expect("class attribute present");
        assert_eq!(attrs[class_idx + 1], "t", "class attribute value");

        // The div's text child is in the flat list and linked back.
        let div_id = div["nodeId"].as_i64().unwrap();
        let text_nodes: Vec<&Value> = nodes
            .iter()
            .filter(|n| n["parentId"].as_i64() == Some(div_id))
            .collect();
        assert!(
            text_nodes
                .iter()
                .any(|n| n["nodeType"].as_i64() == Some(3)),
            "the div's text child is flattened with a parentId link"
        );
    });
}

// ---------------------------------------------------------------------------
// 5. DOM.getNodeForOwner — RemoteObject objectId → canonical nodeId
// ---------------------------------------------------------------------------

#[test]
fn node_for_owner_maps_object_id_to_canonical_node_id() {
    with_styled_page(|bridge, target| {
        navigate_to_styled_page(bridge, target);

        let flat = dispatch(
            bridge,
            target,
            3,
            "DOM.getFlattenedDocument",
            json!({ "depth": -1 }),
        );
        let nodes = flat["nodes"].as_array().expect("nodes array");
        let div_id = find_flat(nodes, "DIV")["nodeId"].as_i64().unwrap();
        let html_id = find_flat(nodes, "HTML")["nodeId"].as_i64().unwrap();

        // RemoteObject handle → nodeId roundtrip. dispatch() returns the
        // CDP result value, which for returnByValue:false is
        // {result: RemoteObject, exceptionDetails} — the objectId lives at
        // ev["result"]["objectId"].
        let ev = dispatch(
            bridge,
            target,
            4,
            "Runtime.evaluate",
            json!({
                "expression": "document.querySelector('div.t')",
                "returnByValue": false
            }),
        );
        let object_id = ev["result"]["objectId"]
            .as_str()
            .unwrap_or_else(|| {
                panic!(
                    "evaluateHandle mints an objectId, got: {ev}"
                )
            })
            .to_string();

        let owner = dispatch(
            bridge,
            target,
            5,
            "DOM.getNodeForOwner",
            json!({ "objectId": object_id }),
        );
        assert_eq!(
            owner["nodeId"].as_i64(),
            Some(div_id),
            "the object's nodeId is the canonical DOM-face id"
        );

        // Legacy "node-N" DOM handles normalize to the same identity space.
        let legacy = dispatch(
            bridge,
            target,
            6,
            "DOM.getNodeForOwner",
            json!({ "objectId": "node-2" }),
        );
        assert_eq!(
            legacy["nodeId"].as_i64(),
            Some(html_id),
            "legacy node-2 (documentElement) resolves to the html nodeId"
        );

        // Dead handle → explicit error, never a fabricated id.
        let bogus = dispatch_raw(
            bridge,
            target,
            7,
            "DOM.getNodeForOwner",
            json!({ "objectId": "obj-does-not-exist" }),
        );
        let err = bogus.error.expect("unknown objectId must fail");
        assert_eq!(err.code, -32000, "Chrome's DevTools server-error class");

        // A non-Node RemoteObject is not a node — explicit error.
        let ev_obj = dispatch(
            bridge,
            target,
            8,
            "Runtime.evaluate",
            json!({ "expression": "({a: 1})", "returnByValue": false }),
        );
        let plain_object_id = ev_obj["result"]["objectId"]
            .as_str()
            .expect("plain object handle")
            .to_string();
        let not_node = dispatch_raw(
            bridge,
            target,
            9,
            "DOM.getNodeForOwner",
            json!({ "objectId": plain_object_id }),
        );
        let err = not_node.error.expect("non-Node object must fail");
        assert_eq!(err.code, -32000);
        assert!(err.message.contains("not a DOM Node"));
    });
}

// ---------------------------------------------------------------------------
// 6. Fail-closed data paths — unknown node ids and dead object registries
// ---------------------------------------------------------------------------

#[test]
fn css_dom_query_fail_closed_on_unknown_node() {
    with_styled_page(|bridge, target| {
        navigate_to_styled_page(bridge, target);

        // An addressable-shape id that matches no real node → -32000
        // (Chrome's "No node with given id found" class), never an empty
        // style list.
        for method in [
            "CSS.getComputedStyleForNode",
            "CSS.getMatchedStylesForNode",
            "CSS.getInlineStylesForNode",
        ] {
            let resp = dispatch_raw(
                bridge,
                target,
                3,
                method,
                json!({ "nodeId": 999_999 }),
            );
            let err = resp
                .error
                .unwrap_or_else(|| panic!("{method} must fail on an unknown node"));
            assert_eq!(err.code, -32000, "{method}: {}", err.message);
            assert!(err.message.contains("No node with given id found"));
        }

        // Non-element node (the document itself, nodeId 1) — styles require
        // an Element, and the face says so instead of returning a list.
        let resp = dispatch_raw(
            bridge,
            target,
            4,
            "CSS.getComputedStyleForNode",
            json!({ "nodeId": 1 }),
        );
        let err = resp.error.expect("document node must fail the style query");
        assert_eq!(err.code, -32000);
        assert!(err.message.contains("not an element"));
    });
}

// ===========================================================================
// REQ-BRW-048 follow-up wave: the remaining DevTools panel faces —
// querySelector/querySelectorAll canonical ids, describeNode, getBoxModel,
// resolveNode, pushNodesByBackendIdsToFrontend, setStyleTexts. Same harness,
// same canonical identity SSOT.
// ===========================================================================

/// Dispatch without a bridge (pure dispatch face — no runtime needed).
fn dispatch_no_bridge(method: &str, params: Value) -> CdpResponse {
    let msg = CdpMessage {
        id: Some(1),
        method: method.to_string(),
        params: Some(params),
        session_id: None,
    };
    let params = msg.params.clone();
    handle_command(msg, "t", &params, None)
}

// ---------------------------------------------------------------------------
// 7. querySelector / querySelectorAll — canonical nodeIds, no fake sequences
// ---------------------------------------------------------------------------

#[test]
fn query_selector_returns_canonical_node_ids() {
    with_styled_page(|bridge, target| {
        navigate_to_styled_page(bridge, target);

        let flat = dispatch(
            bridge,
            target,
            3,
            "DOM.getFlattenedDocument",
            json!({ "depth": -1 }),
        );
        let nodes = flat["nodes"].as_array().expect("nodes array");
        let div_id = find_flat(nodes, "DIV")["nodeId"].as_i64().unwrap();
        let p_id = find_flat(nodes, "P")["nodeId"].as_i64().unwrap();

        // querySelector → the same canonical id the flatten face reported.
        let qs = dispatch(
            bridge,
            target,
            4,
            "DOM.querySelector",
            json!({ "selector": "div.t" }),
        );
        assert_eq!(
            qs["nodeId"].as_i64(),
            Some(div_id),
            "querySelector must return the canonical identity"
        );

        // No match → nodeId 0 (the CDP convention, on a live page).
        let miss = dispatch(
            bridge,
            target,
            5,
            "DOM.querySelector",
            json!({ "selector": "#does-not-exist" }),
        );
        assert_eq!(miss["nodeId"], 0);

        // querySelectorAll → real ids in document order (the former
        // 1..=count sequence addressed wrong nodes).
        let qsa = dispatch(
            bridge,
            target,
            6,
            "DOM.querySelectorAll",
            json!({ "selector": "body > *" }),
        );
        let ids: Vec<i64> = qsa["nodeIds"]
            .as_array()
            .expect("nodeIds array")
            .iter()
            .map(|v| v.as_i64().expect("integer id"))
            .collect();
        assert_eq!(ids, vec![div_id, p_id]);
    });
}

// ---------------------------------------------------------------------------
// 8. describeNode — real node data, children within depth
// ---------------------------------------------------------------------------

#[test]
fn describe_node_reports_live_node_data() {
    with_styled_page(|bridge, target| {
        navigate_to_styled_page(bridge, target);

        let flat = dispatch(
            bridge,
            target,
            3,
            "DOM.getFlattenedDocument",
            json!({ "depth": -1 }),
        );
        let nodes = flat["nodes"].as_array().expect("nodes array");
        let flat_div = find_flat(nodes, "DIV").clone();
        let div_id = flat_div["nodeId"].as_i64().unwrap();

        let desc = dispatch(
            bridge,
            target,
            4,
            "DOM.describeNode",
            json!({ "nodeId": div_id, "depth": 1 }),
        );
        let node = &desc["node"];

        // Fields agree with the flatten face for the same node.
        assert_eq!(node["nodeId"], flat_div["nodeId"]);
        assert_eq!(node["nodeName"], "DIV");
        assert_eq!(node["nodeType"], flat_div["nodeType"]);
        assert_eq!(node["childNodeCount"], flat_div["childNodeCount"]);
        assert_eq!(node["attributes"], flat_div["attributes"]);

        // depth 1 → children present (parentId-linked), grandchildren not.
        let children = node["children"].as_array().expect("children array");
        assert!(
            !children.is_empty(),
            "the styled div has a text child"
        );
        for child in children {
            assert_eq!(child["parentId"], div_id);
            assert!(
                child.get("children").is_none(),
                "depth 1 must not descend past one level: {child}"
            );
        }

        // depth discrimination must be observable: describe the body, whose
        // grandchildren exist. depth 1 → children carry no "children" key;
        // depth -1 → the whole subtree (the div child has its text child).
        let body_id = find_flat(nodes, "BODY")["nodeId"].as_i64().unwrap();
        let body_shallow = dispatch(
            bridge,
            target,
            5,
            "DOM.describeNode",
            json!({ "nodeId": body_id, "depth": 1 }),
        );
        let shallow_kids = body_shallow["node"]["children"].as_array().unwrap();
        assert!(!shallow_kids.is_empty());
        for kid in shallow_kids {
            assert!(
                kid.get("children").is_none(),
                "depth 1 must not descend below one level: {kid}"
            );
        }
        let body_full = dispatch(
            bridge,
            target,
            6,
            "DOM.describeNode",
            json!({ "nodeId": body_id, "depth": -1 }),
        );
        let full_kids = body_full["node"]["children"].as_array().unwrap();
        let div_child = full_kids
            .iter()
            .find(|k| k["nodeName"] == "DIV")
            .expect("body subtree contains the div");
        assert!(
            div_child.get("children").is_some(),
            "depth -1 descends below one level"
        );

        // objectId form resolves the same node.
        let ev = dispatch(
            bridge,
            target,
            7,
            "Runtime.evaluate",
            json!({ "expression": "document.querySelector('div.t')", "returnByValue": false }),
        );
        let object_id = ev["result"]["objectId"].as_str().unwrap().to_string();
        let by_oid = dispatch(
            bridge,
            target,
            8,
            "DOM.describeNode",
            json!({ "objectId": object_id }),
        );
        assert_eq!(by_oid["node"]["nodeId"], flat_div["nodeId"]);
    });
}

// ---------------------------------------------------------------------------
// 9. getBoxModel — real geometry, cross-checked against the page's own rect
// ---------------------------------------------------------------------------

#[test]
fn get_box_model_reports_page_rect() {
    with_styled_page(|bridge, target| {
        navigate_to_styled_page(bridge, target);

        let flat = dispatch(
            bridge,
            target,
            3,
            "DOM.getFlattenedDocument",
            json!({ "depth": -1 }),
        );
        let div_id = find_flat(flat["nodes"].as_array().unwrap(), "DIV")["nodeId"]
            .as_i64()
            .unwrap();

        // Independent read: the page reports its own border box.
        let baseline = dispatch(
            bridge,
            target,
            4,
            "Runtime.evaluate",
            json!({
                "expression": "(function(){ var r = document.querySelector('div.t').getBoundingClientRect(); return JSON.stringify({x: r.left, y: r.top, w: r.width, h: r.height}); })()",
                "returnByValue": true
            }),
        );
        // 3b74a655 envelope: rbv=true `value` is the raw completion value —
        // the baseline expression returns JSON.stringify(...), i.e. a
        // JSON-encoded string that must be peeled before field access.
        let rect_text = baseline["result"]["value"].as_str().expect("baseline value is a JSON string");
        let rect: Value = serde_json::from_str(rect_text).expect("baseline rect parses");
        let bx = rect["x"].as_f64().unwrap();
        let by = rect["y"].as_f64().unwrap();
        let bw = rect["w"].as_f64().unwrap();
        let bh = rect["h"].as_f64().unwrap();

        let bm = dispatch(
            bridge,
            target,
            5,
            "DOM.getBoxModel",
            json!({ "nodeId": div_id }),
        );
        let model = &bm["model"];
        let width = model["width"].as_f64().unwrap();
        let height = model["height"].as_f64().unwrap();
        assert!(
            (width - bw).abs() < 0.5 && (height - bh).abs() < 0.5,
            "model border box {width}x{height} must match the page rect {bw}x{bh}"
        );

        // The border quad starts at the rect's top-left corner; the content
        // quad is inset inside it (the div has no border/padding here).
        let border = model["border"].as_array().unwrap();
        let border_x = border[0].as_f64().unwrap();
        let border_y = border[1].as_f64().unwrap();
        assert!((border_x - bx).abs() < 0.5 && (border_y - by).abs() < 0.5);
        let content = model["content"].as_array().unwrap();
        assert_eq!(content.len(), 8, "content is a 4-corner quad");
        assert!(content[0].as_f64().unwrap() >= border_x - 0.5);
        assert!(content[1].as_f64().unwrap() >= border_y - 0.5);
    });
}

// ---------------------------------------------------------------------------
// 10. resolveNode — nodeId → RemoteObject objectId → getNodeForOwner roundtrip
// ---------------------------------------------------------------------------

#[test]
fn resolve_node_roundtrips_to_canonical_node_id() {
    with_styled_page(|bridge, target| {
        navigate_to_styled_page(bridge, target);

        let flat = dispatch(
            bridge,
            target,
            3,
            "DOM.getFlattenedDocument",
            json!({ "depth": -1 }),
        );
        let div_id = find_flat(flat["nodes"].as_array().unwrap(), "DIV")["nodeId"]
            .as_i64()
            .unwrap();

        // The registry is Runtime-minted — establish it the way a client
        // would (returnByValue:false evaluate).
        dispatch(
            bridge,
            target,
            4,
            "Runtime.evaluate",
            json!({ "expression": "({})", "returnByValue": false }),
        );

        let resolved = dispatch(
            bridge,
            target,
            5,
            "DOM.resolveNode",
            json!({ "nodeId": div_id }),
        );
        let object = &resolved["object"];
        assert_eq!(object["subtype"], "node");
        assert_eq!(object["description"], "div#target");
        let object_id = object["objectId"].as_str().expect("minted objectId");

        // Roundtrip: the handle maps back to the same canonical nodeId.
        let owner = dispatch(
            bridge,
            target,
            6,
            "DOM.getNodeForOwner",
            json!({ "objectId": object_id }),
        );
        assert_eq!(owner["nodeId"].as_i64(), Some(div_id));

        // Before any registry-minting evaluate, resolveNode fails explicitly
        // rather than conjuring a parallel registry.
        let fresh_runtime_check = dispatch_raw(
            bridge,
            target,
            7,
            "DOM.resolveNode",
            json!({ "objectId": "obj-never-minted" }),
        );
        let err = fresh_runtime_check.error.expect("dead handle must fail");
        assert_eq!(err.code, -32000);
    });
}

// ---------------------------------------------------------------------------
// 11. pushNodesByBackendIdsToFrontend — backend ≡ canonical, liveness only
// ---------------------------------------------------------------------------

#[test]
fn push_nodes_by_backend_ids_frontends_canonical_ids() {
    with_styled_page(|bridge, target| {
        navigate_to_styled_page(bridge, target);

        let flat = dispatch(
            bridge,
            target,
            3,
            "DOM.getFlattenedDocument",
            json!({ "depth": -1 }),
        );
        let nodes = flat["nodes"].as_array().unwrap();
        let div_id = find_flat(nodes, "DIV")["nodeId"].as_i64().unwrap();
        let body_id = find_flat(nodes, "BODY")["nodeId"].as_i64().unwrap();

        let resp = dispatch(
            bridge,
            target,
            4,
            "DOM.pushNodesByBackendIdsToFrontend",
            json!({ "backendNodeIds": [div_id, 999_999, body_id] }),
        );
        let ids: Vec<i64> = resp["nodeIds"]
            .as_array()
            .expect("nodeIds array")
            .iter()
            .map(|v| v.as_i64().unwrap())
            .collect();
        assert_eq!(
            ids,
            vec![div_id, body_id],
            "live ids pass through, unknown ids are omitted (never fabricated)"
        );
    });
}

// ---------------------------------------------------------------------------
// 12. setStyleTexts — real CSSOM write, read back through the style faces
// ---------------------------------------------------------------------------

#[test]
fn set_style_texts_writes_and_reads_back() {
    with_styled_page(|bridge, target| {
        navigate_to_styled_page(bridge, target);

        let flat = dispatch(
            bridge,
            target,
            3,
            "DOM.getFlattenedDocument",
            json!({ "depth": -1 }),
        );
        let div_id = find_flat(flat["nodes"].as_array().unwrap(), "DIV")["nodeId"]
            .as_i64()
            .unwrap();

        // Discover the stylesheet + rule through the read face: the sheet
        // ordinal is the styleSheetId; the div.t rule is index 0 in the
        // test sheet (declared before p.x).
        let matched = dispatch(
            bridge,
            target,
            4,
            "CSS.getMatchedStylesForNode",
            json!({ "nodeId": div_id }),
        );
        let rule0 = &matched["matchedCSSRules"][0];
        assert_eq!(rule0["rule"]["selectorList"]["selectors"][0]["text"], "div.t");
        let style_sheet_id = rule0["rule"]["styleSheetId"]
            .as_str()
            .expect("matched rules expose the stylesheet ordinal")
            .to_string();

        let edit = |text: &str| {
            dispatch(
                bridge,
                target,
                8,
                "CSS.setStyleTexts",
                json!({
                    "edits": [{
                        "styleSheetId": style_sheet_id,
                        "range": { "startLine": 0 },
                        "text": text
                    }]
                })
            )
        };

        // Write: new declarations replace the rule's declaration block.
        let written = edit("color: rgb(9, 9, 9); font-size: 40px;");
        let style = &written["styles"][0];
        assert_eq!(style["styleSheetId"], json!(style_sheet_id));
        let props = style["cssProperties"].as_array().expect("cssProperties");
        let color = props
            .iter()
            .find(|p| p["name"] == "color")
            .expect("color declaration");
        assert!(color["value"].as_str().unwrap().contains("9, 9, 9"));

        // Read-back 1: the computed style really restyled (not a stored echo).
        let cs = dispatch(
            bridge,
            target,
            9,
            "CSS.getComputedStyleForNode",
            json!({ "nodeId": div_id }),
        );
        let styles = cs["computedStyle"].as_array().unwrap();
        let fs = styles.iter().find(|s| s["name"] == "font-size").unwrap();
        assert_eq!(fs["value"], "40px", "the write must restyle the page");

        // Read-back 2: the matched-rule declarations reflect the edit.
        let matched2 = dispatch(
            bridge,
            target,
            10,
            "CSS.getMatchedStylesForNode",
            json!({ "nodeId": div_id }),
        );
        let decls = matched2["matchedCSSRules"][0]["rule"]["style"]["cssProperties"]
            .as_array()
            .unwrap();
        let fs_decl = decls.iter().find(|d| d["name"] == "font-size").unwrap();
        assert_eq!(fs_decl["value"], "40px");

        // Restore the original declaration (clean state for later waves).
        edit("color: rgb(12, 34, 56); font-size: 20px;");
        let cs2 = dispatch(
            bridge,
            target,
            11,
            "CSS.getComputedStyleForNode",
            json!({ "nodeId": div_id }),
        );
        let styles2 = cs2["computedStyle"].as_array().unwrap();
        let fs2 = styles2.iter().find(|s| s["name"] == "font-size").unwrap();
        assert_eq!(fs2["value"], "20px", "restore returns the page to baseline");

        // Dead ends are explicit: unknown sheet ordinal, missing range,
        // non-style rule target.
        for (edits, needle) in [
            (
                json!([{ "styleSheetId": "9", "range": { "startLine": 0 }, "text": "color: red" }]),
                "unknown styleSheetId",
            ),
            (
                json!([{ "styleSheetId": style_sheet_id, "text": "color: red" }]),
                "range must carry the rule index",
            ),
        ] {
            let resp = dispatch_raw(bridge, target, 12, "CSS.setStyleTexts", json!({ "edits": edits }));
            let err = resp.error.expect("bad edit must fail explicitly");
            assert_eq!(err.code, -32000, "{needle}: {}", err.message);
            assert!(err.message.contains(needle));
        }
    });
}

// ---------------------------------------------------------------------------
// 13. No-bridge face: every live-document query fails closed (-32603 / -32602)
// ---------------------------------------------------------------------------

#[test]
fn dom_live_query_methods_fail_closed_without_bridge() {
    // Pure dispatch face — no runtime, no bridge.
    let with_node = json!({ "nodeId": 1 });
    for (method, params, code) in [
        ("DOM.getDocument", json!({}), -32603),
        ("DOM.querySelector", json!({ "selector": "div" }), -32603),
        ("DOM.querySelectorAll", json!({ "selector": "div" }), -32603),
        ("DOM.describeNode", with_node.clone(), -32603),
        ("DOM.getBoxModel", with_node.clone(), -32603),
        ("DOM.resolveNode", with_node.clone(), -32603),
        ("DOM.pushNodesByBackendIdsToFrontend", json!({ "backendNodeIds": [1] }), -32603),
        // Missing required params are rejected before the bridge check.
        ("DOM.querySelector", json!({}), -32602),
        ("DOM.describeNode", json!({}), -32602),
        ("DOM.getBoxModel", json!({}), -32602),
        ("DOM.resolveNode", json!({}), -32602),
        ("DOM.pushNodesByBackendIdsToFrontend", json!({}), -32602),
        ("CSS.setStyleTexts", json!({}), -32602),
    ] {
        let resp = dispatch_no_bridge(method, params);
        let err = resp
            .error
            .unwrap_or_else(|| panic!("{method}: explicit error required"));
        assert_eq!(err.code, code, "{method}: {}", err.message);
        assert!(resp.result.is_none(), "{method}: error carries no result");
    }
}