rightkit-qa 0.7.0

Rust QA harness for Right Suite apps: engine black-box scenarios, hidden native UI driving (the in-app control server is the dev-only `control` feature), Rust-test scenario wrapper, dynamic paid-provider mocks, loudness/WAV/PNG/frame validators, run lock, orphan sweep, hashed evidence.
Documentation
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
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
//! Windows in-process input via WebView2 `CallDevToolsProtocolMethod`
//! (`Input.dispatchMouseEvent` / `Input.dispatchKeyEvent` / `Runtime.evaluate`
//! / `Page.captureScreenshot`). No `SendInput`, no window activation.
//!
//! Verified on Windows by Pulse's hub journey, which drives a hidden WebView2 through this
//! module on a `windows-2025` GitHub runner (rightkit-qa-driver 0.1.11, now rightkit-qa, with `wv2-0_39`; the
//! passing run is Pulse CI 38084463429). That run covers move, down/up, click, eval and
//! screenshot; it does not cover every method here (for example wheel and key paths are
//! exercised only where the journey uses them).

use super::{Button, KeyEv, Mouse, MouseEv};
use rightkit_automation::json::str_field;
use std::sync::atomic::{AtomicU64, AtomicU8, Ordering};
use std::sync::mpsc::channel;
use std::sync::{Arc, OnceLock};
use std::time::{Duration, Instant};
use tauri::{Runtime, WebviewWindow};
#[cfg(not(any(feature = "wv2-0_38", feature = "wv2-0_39")))]
compile_error!("rightkit-qa's Windows control server needs one of `wv2-0_38` (Tauri 2.11) or `wv2-0_39` (Tauri 2.12+); neither is a default feature");
use webview2_com::CallDevToolsProtocolMethodCompletedHandler;
use windows::core::HSTRING;
#[cfg(all(feature = "wv2-0_38", not(feature = "wv2-0_39")))]
use ::{webview2_com_038 as webview2_com, windows_061 as windows};
#[cfg(feature = "wv2-0_39")]
use ::{webview2_com_039 as webview2_com, windows_062 as windows};

const CDP_TIMEOUT: Duration = Duration::from_secs(15);
const PROBE_TIMEOUT: Duration = Duration::from_secs(3);

/// Opt-in tracing: `RIGHTKIT_CONTROL_TRACE=1` writes to stderr, any other non-empty value is
/// a file path to append to. Off by default; nothing is formatted when off.
fn trace_sink() -> &'static Option<String> {
    static SINK: OnceLock<Option<String>> = OnceLock::new();
    SINK.get_or_init(|| {
        std::env::var("RIGHTKIT_CONTROL_TRACE")
            .ok()
            .map(|v| v.trim().to_string())
            .filter(|v| !v.is_empty() && v != "0")
    })
}

fn trace_enabled() -> bool {
    trace_sink().is_some()
}

#[link(name = "kernel32")]
extern "system" {
    fn GetCurrentThreadId() -> u32;
}

fn trace(line: impl FnOnce() -> String) {
    let Some(sink) = trace_sink() else { return };
    let ms = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_millis())
        .unwrap_or(0);
    let line = format!(
        "[rk-control-trace {ms} tid={}] {}\n",
        unsafe { GetCurrentThreadId() },
        line()
    );
    if sink == "1" {
        eprint!("{line}");
    } else if let Ok(mut f) = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(sink)
    {
        use std::io::Write;
        let _ = f.write_all(line.as_bytes());
    }
}

/// How far a devtools call got: 0 queued for the UI thread, 1 issued to WebView2, 2 completed.
const STAGE_QUEUED: u8 = 0;
const STAGE_ISSUED: u8 = 1;
const STAGE_DONE: u8 = 2;

fn cdp_once<R: Runtime>(
    win: &WebviewWindow<R>,
    id: u64,
    method: &str,
    params: String,
    timeout: Duration,
    stage: Arc<AtomicU8>,
) -> Result<String, String> {
    let (tx, rx) = channel::<Result<String, String>>();
    let method_owned = method.to_string();
    let queued = Instant::now();
    trace(|| format!("devtools#{id} enqueue {method}"));
    let stage_ui = stage.clone();
    win.with_webview(move |pv| unsafe {
        stage_ui.store(STAGE_ISSUED, Ordering::SeqCst);
        trace(|| {
            format!(
                "devtools#{id} dispatch {method_owned} after {}ms on the UI thread",
                queued.elapsed().as_millis()
            )
        });
        let core = match pv.controller().CoreWebView2() {
            Ok(c) => c,
            Err(e) => {
                let _ = tx.send(Err(e.to_string()));
                return;
            }
        };
        let tx2 = tx.clone();
        let stage_done = stage_ui.clone();
        let name = method_owned.clone();
        let handler =
            CallDevToolsProtocolMethodCompletedHandler::create(Box::new(move |res, json| {
                stage_done.store(STAGE_DONE, Ordering::SeqCst);
                trace(|| {
                    format!(
                        "devtools#{id} complete {name} after {}ms ok={}",
                        queued.elapsed().as_millis(),
                        res.is_ok()
                    )
                });
                let _ = tx2.send(res.map(|_| json).map_err(|e| e.to_string()));
                Ok(())
            }));
        if let Err(e) = core.CallDevToolsProtocolMethod(
            &HSTRING::from(method_owned),
            &HSTRING::from(params),
            &handler,
        ) {
            let _ = tx.send(Err(e.to_string()));
        }
    })
    .map_err(|e| e.to_string())?;
    match rx.recv_timeout(timeout) {
        Ok(r) => r,
        Err(_) => Err("devtools call timed out".to_string()),
    }
}

fn cdp<R: Runtime>(win: &WebviewWindow<R>, method: &str, params: String) -> Result<String, String> {
    static SEQ: AtomicU64 = AtomicU64::new(0);
    let id = SEQ.fetch_add(1, Ordering::SeqCst) + 1;
    let stage = Arc::new(AtomicU8::new(STAGE_QUEUED));
    match cdp_once(win, id, method, params, CDP_TIMEOUT, stage.clone()) {
        Err(e) if e == "devtools call timed out" => {
            let why = match stage.load(Ordering::SeqCst) {
                STAGE_QUEUED => format!(
                    "{method}: the UI thread never ran the call within {}s (the main thread is blocked or not dispatching)",
                    CDP_TIMEOUT.as_secs()
                ),
                _ => {
                    // The call reached WebView2. A browser-level call that is answered by the
                    // browser process separates "renderer unresponsive" from "channel dead".
                    let probe_stage = Arc::new(AtomicU8::new(STAGE_QUEUED));
                    let probe = cdp_once(
                        win,
                        id,
                        "Browser.getVersion",
                        "{}".into(),
                        PROBE_TIMEOUT,
                        probe_stage,
                    );
                    let mut why = if probe.as_ref().err().map(|e| e.as_str())
                        == Some("devtools call timed out")
                    {
                        format!(
                            "{method} was issued but neither it nor a browser-level probe completed (devtools channel or browser process unresponsive)"
                        )
                    } else {
                        format!(
                            "{method} was issued and the browser process still answers, but the page did not complete it within {}s (renderer main thread busy or blocked, a modal script dialog, or a paused debugger)",
                            CDP_TIMEOUT.as_secs()
                        )
                    };
                    if capture_enabled() {
                        why.push_str("; page stacks: ");
                        why.push_str(&capture_stacks(win, id));
                    }
                    why
                }
            };
            trace(|| format!("devtools#{id} TIMEOUT {why}"));
            Err(format!("devtools call timed out: {why}"))
        }
        other => other,
    }
}

/// Issue several devtools calls from ONE UI-thread hop and wait for all of them. WebView2
/// starts them in order and input events are delivered in order; the first failure (in call order)
/// is returned. Use for input sequences only: a call that must see the effect of an earlier one
/// (hide, capture, restore) is made with separate [`cdp`] calls.
fn cdp_many<R: Runtime>(
    win: &WebviewWindow<R>,
    calls: Vec<(String, String)>,
) -> Result<(), String> {
    if calls.is_empty() {
        return Ok(());
    }
    let n = calls.len();
    let (tx, rx) = channel::<(usize, Result<(), String>)>();
    win.with_webview(move |pv| unsafe {
        let core = match pv.controller().CoreWebView2() {
            Ok(c) => c,
            Err(e) => {
                let _ = tx.send((0, Err(e.to_string())));
                return;
            }
        };
        for (i, (method, params)) in calls.into_iter().enumerate() {
            let tx2 = tx.clone();
            let handler =
                CallDevToolsProtocolMethodCompletedHandler::create(Box::new(move |res, _| {
                    let _ = tx2.send((i, res.map_err(|e| e.to_string())));
                    Ok(())
                }));
            if let Err(e) = core.CallDevToolsProtocolMethod(
                &HSTRING::from(method),
                &HSTRING::from(params),
                &handler,
            ) {
                let _ = tx.send((i, Err(e.to_string())));
            }
        }
    })
    .map_err(|e| e.to_string())?;
    let deadline = Instant::now() + CDP_TIMEOUT + Duration::from_millis(50 * n as u64);
    let mut first_err: Option<(usize, String)> = None;
    let mut seen = 0;
    while seen < n {
        let left = deadline.saturating_duration_since(Instant::now());
        match rx.recv_timeout(left) {
            Ok((i, r)) => {
                seen += 1;
                if let Err(e) = r {
                    if first_err.as_ref().is_none_or(|(j, _)| i < *j) {
                        first_err = Some((i, e));
                    }
                }
            }
            Err(_) => {
                return Err(format!(
                    "devtools call timed out: {seen} of {n} batched calls completed"
                ))
            }
        }
    }
    first_err.map_or(Ok(()), |(_, e)| Err(e))
}

/// Stack capture on a devtools timeout is on with `RIGHTKIT_CONTROL_TRACE` or
/// `RIGHTKIT_CONTROL_STACK_ON_TIMEOUT=1`. It enables the V8 debugger at webview creation
/// (`Debugger.enable` is not an interrupting command, so it would queue behind a busy script if
/// sent only after the hang) and later interrupts the running script with `Debugger.pause`.
fn capture_enabled() -> bool {
    static ON: OnceLock<bool> = OnceLock::new();
    *ON.get_or_init(|| {
        trace_enabled()
            || std::env::var("RIGHTKIT_CONTROL_STACK_ON_TIMEOUT")
                .map(|v| v.trim() == "1")
                .unwrap_or(false)
    })
}

/// Receives the next `Debugger.paused` payload while a capture is waiting for it.
static PAUSE_TX: std::sync::Mutex<Option<std::sync::mpsc::Sender<String>>> =
    std::sync::Mutex::new(None);

/// `fn url:line:col` for the top `limit` frames of a `Debugger.paused` payload.
fn frames_summary(paused: &str, limit: usize) -> String {
    let Ok(v) = serde_json::from_str::<serde_json::Value>(paused) else {
        return format!(
            "unparsable Debugger.paused payload: {}",
            &paused[..paused.len().min(200)]
        );
    };
    let reason = v["reason"].as_str().unwrap_or("?");
    let frames: Vec<String> = v["callFrames"]
        .as_array()
        .map(|a| {
            a.iter()
                .take(limit)
                .map(|f| {
                    let name = f["functionName"]
                        .as_str()
                        .filter(|n| !n.is_empty())
                        .unwrap_or("(anonymous)");
                    let url = f["url"].as_str().filter(|u| !u.is_empty()).unwrap_or("?");
                    format!(
                        "{name} {url}:{}:{}",
                        f["location"]["lineNumber"].as_i64().unwrap_or(-1) + 1,
                        f["location"]["columnNumber"].as_i64().unwrap_or(-1) + 1
                    )
                })
                .collect()
        })
        .unwrap_or_default();
    format!("[reason={reason}] {}", frames.join(" <- "))
}

/// Interrupt the page's main thread up to three times and report where it was. Out of band: the
/// stuck `Runtime.evaluate` is not involved. If no pause arrives within 2 s the renderer is in
/// native code or gone.
fn capture_stacks<R: Runtime>(win: &WebviewWindow<R>, id: u64) -> String {
    let quiet = || Arc::new(AtomicU8::new(STAGE_QUEUED));
    // Normally a no-op (armed at webview creation); re-sent in case a navigation reset the agent.
    let _ = cdp_once(
        win,
        id,
        "Debugger.enable",
        "{}".into(),
        Duration::from_millis(500),
        quiet(),
    );
    let mut samples = Vec::new();
    for n in 1..=3 {
        let (tx, rx) = channel::<String>();
        *PAUSE_TX.lock().unwrap_or_else(|e| e.into_inner()) = Some(tx);
        let sent = cdp_once(
            win,
            id,
            "Debugger.pause",
            "{}".into(),
            Duration::from_secs(2),
            quiet(),
        );
        let paused = rx.recv_timeout(Duration::from_secs(2));
        *PAUSE_TX.lock().unwrap_or_else(|e| e.into_inner()) = None;
        match paused {
            Ok(p) => {
                let line = frames_summary(&p, 15);
                trace(|| format!("devtools#{id} STACK sample {n}: {line}"));
                samples.push(format!("sample {n}: {line}"));
                let _ = cdp_once(
                    win,
                    id,
                    "Debugger.resume",
                    "{}".into(),
                    Duration::from_secs(2),
                    quiet(),
                );
            }
            Err(_) => {
                let line = format!(
                    "sample {n}: no Debugger.paused within 2s (Debugger.pause {}); the renderer is in native code, not running script, or gone",
                    match &sent { Ok(_) => "was accepted".to_string(), Err(e) => format!("failed: {e}") }
                );
                trace(|| format!("devtools#{id} STACK {line}"));
                samples.push(line);
                break;
            }
        }
        std::thread::sleep(Duration::from_millis(300));
    }
    samples.join(" | ")
}

/// Enable the V8 debugger at webview creation and route `Debugger.paused` events: to a waiting
/// capture, or (unrequested pauses, for example a `debugger;` statement) logged and resumed at
/// once so the page behaves as it would without the harness.
fn arm_debugger<R: Runtime>(webview: &tauri::Webview<R>) {
    use webview2_com::{
        DevToolsProtocolEventReceivedEventHandler, NavigationCompletedEventHandler,
    };
    let label = webview.label().to_string();
    let _ = webview.with_webview(move |pv| unsafe {
        let Ok(core) = pv.controller().CoreWebView2() else {
            return;
        };
        let mut token: i64 = 0;
        if let Ok(receiver) =
            core.GetDevToolsProtocolEventReceiver(&HSTRING::from("Debugger.paused"))
        {
            let l = label.clone();
            let h =
                DevToolsProtocolEventReceivedEventHandler::create(Box::new(move |sender, args| {
                    let Some(a) = args else { return Ok(()) };
                    let mut p = windows::core::PWSTR::null();
                    let _ = a.ParameterObjectAsJson(&mut p);
                    let payload = pwstr_string(p);
                    let waiting = PAUSE_TX
                        .lock()
                        .unwrap_or_else(|e| e.into_inner())
                        .as_ref()
                        .map(|tx| tx.send(payload.clone()).is_ok())
                        .unwrap_or(false);
                    if !waiting {
                        trace(|| {
                            format!(
                                "[{l}] unrequested Debugger.paused {} (resuming)",
                                frames_summary(&payload, 5)
                            )
                        });
                        if let Some(core) = sender {
                            let noop = CallDevToolsProtocolMethodCompletedHandler::create(
                                Box::new(|_, _| Ok(())),
                            );
                            let _ = core.CallDevToolsProtocolMethod(
                                &HSTRING::from("Debugger.resume"),
                                &HSTRING::from("{}"),
                                &noop,
                            );
                        }
                    }
                    Ok(())
                }));
            let _ = receiver.add_DevToolsProtocolEventReceived(&h, &mut token);
        }
        let send_enable = |core: &webview2_com::Microsoft::Web::WebView2::Win32::ICoreWebView2| {
            let h = CallDevToolsProtocolMethodCompletedHandler::create(Box::new(|_, _| Ok(())));
            let _ = core.CallDevToolsProtocolMethod(
                &HSTRING::from("Debugger.enable"),
                &HSTRING::from("{}"),
                &h,
            );
        };
        send_enable(&core);
        // A navigation can reset the agent; re-enable after each one.
        let nav = NavigationCompletedEventHandler::create(Box::new(move |sender, _| {
            if let Some(core) = sender {
                send_enable(&core);
            }
            Ok(())
        }));
        let _ = core.add_NavigationCompleted(&nav, &mut token);
        trace(|| format!("[{label}] debugger armed for stack capture"));
    });
}

fn pwstr_string(p: windows::core::PWSTR) -> String {
    if p.is_null() {
        String::new()
    } else {
        unsafe { p.to_string() }.unwrap_or_default()
    }
}

/// Opt-in (`RIGHTKIT_CONTROL_TRACE`): log WebView2 navigation, dialog, new-window and process
/// failure events for this webview so a stuck devtools call can be matched to what the page did.
pub fn install_trace<R: Runtime>(webview: &tauri::Webview<R>) {
    use webview2_com::{
        NavigationCompletedEventHandler, NavigationStartingEventHandler,
        NewWindowRequestedEventHandler, ProcessFailedEventHandler, ScriptDialogOpeningEventHandler,
    };
    if capture_enabled() {
        arm_debugger(webview);
    }
    if !trace_enabled() {
        return;
    }
    let label = webview.label().to_string();
    let _ = webview.with_webview(move |pv| unsafe {
        let core = match pv.controller().CoreWebView2() {
            Ok(c) => c,
            Err(e) => {
                trace(|| format!("[{label}] install_trace: no CoreWebView2: {e}"));
                return;
            }
        };
        let mut token: i64 = 0;
        let l = label.clone();
        let h = NavigationStartingEventHandler::create(Box::new(move |_s, args| {
            if let Some(a) = args {
                let (mut uri, mut id, mut user) = (
                    windows::core::PWSTR::null(),
                    0u64,
                    windows::core::BOOL::default(),
                );
                let _ = a.Uri(&mut uri);
                let _ = a.NavigationId(&mut id);
                let _ = a.IsUserInitiated(&mut user);
                trace(|| {
                    format!(
                        "[{l}] NavigationStarting id={id} user={} uri={}",
                        user.as_bool(),
                        pwstr_string(uri)
                    )
                });
            }
            Ok(())
        }));
        let _ = core.add_NavigationStarting(&h, &mut token);
        let l = label.clone();
        let h = NavigationCompletedEventHandler::create(Box::new(move |_s, args| {
            if let Some(a) = args {
                let (mut id, mut ok) = (0u64, windows::core::BOOL::default());
                let mut status = Default::default();
                let _ = a.NavigationId(&mut id);
                let _ = a.IsSuccess(&mut ok);
                let _ = a.WebErrorStatus(&mut status);
                trace(|| {
                    format!(
                        "[{l}] NavigationCompleted id={id} success={} webErrorStatus={}",
                        ok.as_bool(),
                        status.0
                    )
                });
            }
            Ok(())
        }));
        let _ = core.add_NavigationCompleted(&h, &mut token);
        let l = label.clone();
        let h = ScriptDialogOpeningEventHandler::create(Box::new(move |_s, args| {
            if let Some(a) = args {
                let (mut uri, mut msg) =
                    (windows::core::PWSTR::null(), windows::core::PWSTR::null());
                let mut kind = Default::default();
                let _ = a.Uri(&mut uri);
                let _ = a.Message(&mut msg);
                let _ = a.Kind(&mut kind);
                trace(|| {
                    format!(
                        "[{l}] ScriptDialogOpening kind={} uri={} message={}",
                        kind.0,
                        pwstr_string(uri),
                        pwstr_string(msg)
                    )
                });
            }
            Ok(())
        }));
        let _ = core.add_ScriptDialogOpening(&h, &mut token);
        let l = label.clone();
        let h = NewWindowRequestedEventHandler::create(Box::new(move |_s, args| {
            if let Some(a) = args {
                let mut uri = windows::core::PWSTR::null();
                let _ = a.Uri(&mut uri);
                trace(|| format!("[{l}] NewWindowRequested uri={}", pwstr_string(uri)));
            }
            Ok(())
        }));
        let _ = core.add_NewWindowRequested(&h, &mut token);
        let l = label.clone();
        let h = ProcessFailedEventHandler::create(Box::new(move |_s, args| {
            if let Some(a) = args {
                let mut kind = Default::default();
                let _ = a.ProcessFailedKind(&mut kind);
                trace(|| format!("[{l}] ProcessFailed kind={}", kind.0));
            }
            Ok(())
        }));
        let _ = core.add_ProcessFailed(&h, &mut token);
        trace(|| format!("[{label}] install_trace: handlers registered"));
    });
    // Every 2 s: controller visibility and bounds plus a browser-level round trip, so a
    // hidden or zero-sized controller and a stalled browser process are visible in the log.
    let probe_webview = webview.clone();
    let label = webview.label().to_string();
    std::thread::spawn(move || loop {
        std::thread::sleep(Duration::from_secs(2));
        let label = label.clone();
        let queued = Instant::now();
        let sent = probe_webview.with_webview(move |pv| unsafe {
            let controller = pv.controller();
            let mut visible = windows::core::BOOL::default();
            let mut bounds = windows::Win32::Foundation::RECT::default();
            let _ = controller.IsVisible(&mut visible);
            let _ = controller.Bounds(&mut bounds);
            let dispatched = queued.elapsed().as_millis();
            let Ok(core) = controller.CoreWebView2() else {
                return;
            };
            let sent = Instant::now();
            let handler = CallDevToolsProtocolMethodCompletedHandler::create(Box::new(
                move |res, _json| {
                    trace(|| {
                        format!(
                            "[{label}] probe isVisible={} bounds={}x{} at ({},{}) ui_dispatch={}ms Browser.getVersion={}ms ok={}",
                            visible.as_bool(),
                            bounds.right - bounds.left,
                            bounds.bottom - bounds.top,
                            bounds.left,
                            bounds.top,
                            dispatched,
                            sent.elapsed().as_millis(),
                            res.is_ok()
                        )
                    });
                    Ok(())
                },
            ));
            let _ = core.CallDevToolsProtocolMethod(
                &HSTRING::from("Browser.getVersion"),
                &HSTRING::from("{}"),
                &handler,
            );
        });
        if sent.is_err() {
            break;
        }
    });
}

fn cdp_mods(flags: u64) -> u32 {
    use rightkit_automation::keys::appkit::*;
    (if flags & FLAG_ALT != 0 { 1 } else { 0 })
        | (if flags & FLAG_CTRL != 0 { 2 } else { 0 })
        | (if flags & FLAG_CMD != 0 { 4 } else { 0 })
        | (if flags & FLAG_SHIFT != 0 { 8 } else { 0 })
}

fn mouse_params(e: &MouseEv) -> String {
    let (kind, button, x, y, clicks, flags) = (e.kind, e.button, e.x, e.y, e.clicks, e.flags);
    // CDP modifiers: Alt=1, Ctrl=2, Meta=4, Shift=8.
    let modifiers = [
        (rightkit_automation::keys::appkit::FLAG_ALT, 1),
        (rightkit_automation::keys::appkit::FLAG_CTRL, 2),
        (rightkit_automation::keys::appkit::FLAG_CMD, 4),
        (rightkit_automation::keys::appkit::FLAG_SHIFT, 8),
    ]
    .iter()
    .filter(|(f, _)| flags & f != 0)
    .map(|(_, m)| m)
    .sum::<u32>();
    let (b, mask) = match button {
        Button::Left => ("left", 1),
        Button::Right => ("right", 2),
    };
    let (ty, button, buttons) = match kind {
        Mouse::Down => ("mousePressed", b, mask),
        Mouse::Up => ("mouseReleased", b, 0),
        Mouse::Move => ("mouseMoved", "none", 0),
        Mouse::Drag => ("mouseMoved", b, mask),
    };
    format!(
        r#"{{"type":"{ty}","x":{x},"y":{y},"button":"{button}","buttons":{buttons},"clickCount":{clicks},"modifiers":{modifiers}}}"#
    )
}

pub fn mouse<R: Runtime>(
    win: &WebviewWindow<R>,
    kind: Mouse,
    button: Button,
    x: f64,
    y: f64,
    clicks: i64,
    flags: u64,
) -> Result<(), String> {
    cdp(
        win,
        "Input.dispatchMouseEvent",
        mouse_params(&MouseEv {
            kind,
            button,
            x,
            y,
            clicks,
            flags,
        }),
    )
    .map(|_| ())
}

/// Pointer events in order from one UI-thread hop, no pause between them.
pub fn mouse_batch<R: Runtime>(win: &WebviewWindow<R>, evs: &[MouseEv]) -> Result<(), String> {
    cdp_many(
        win,
        evs.iter()
            .map(|e| ("Input.dispatchMouseEvent".to_string(), mouse_params(e)))
            .collect(),
    )
}

pub fn wheel<R: Runtime>(
    win: &WebviewWindow<R>,
    x: f64,
    y: f64,
    dx: f64,
    dy: f64,
) -> Result<(), String> {
    cdp(
        win,
        "Input.dispatchMouseEvent",
        format!(r#"{{"type":"mouseWheel","x":{x},"y":{y},"deltaX":{dx},"deltaY":{dy}}}"#),
    )
    .map(|_| ())
}

/// Windows VK for a US-layout printable character: letters and digits are their ASCII
/// uppercase value; punctuation uses the `VK_OEM_*` codes (the ASCII value would collide
/// with `VK_SNAPSHOT`, `VK_DELETE`, `VK_LWIN`, `VK_RIGHT`, ...). Shifted symbols map to
/// their base key. Anything else has no virtual key (0).
fn char_vk(c: char) -> u32 {
    match c {
        'a'..='z' | 'A'..='Z' | '0'..='9' => c.to_ascii_uppercase() as u32,
        ';' | ':' => 186,
        '=' | '+' => 187,
        ',' | '<' => 188,
        '-' | '_' => 189,
        '.' | '>' => 190,
        '/' | '?' => 191,
        '`' | '~' => 192,
        '[' | '{' => 219,
        '\\' | '|' => 220,
        ']' | '}' => 221,
        '\'' | '"' => 222,
        '!' => 49,
        '@' => 50,
        '#' => 51,
        '$' => 52,
        '%' => 53,
        '^' => 54,
        '&' => 55,
        '*' => 56,
        '(' => 57,
        ')' => 48,
        _ => 0,
    }
}

/// `code` is the macOS virtual keycode used across the crate; map it to a Windows VK.
fn vk(code: u16, chars: &str) -> u32 {
    match code {
        0x24 => 13,
        0x30 => 9,
        0x31 => 32,
        0x33 => 8,
        0x35 => 27,
        0x75 => 46,
        0x7B => 37,
        0x7E => 38,
        0x7C => 39,
        0x7D => 40,
        0x73 => 36,
        0x77 => 35,
        0x74 => 33,
        0x79 => 34,
        // F1..F12 (mac codes -> VK_F1 = 112 ..).
        0x7A => 112,
        0x78 => 113,
        0x63 => 114,
        0x76 => 115,
        0x60 => 116,
        0x61 => 117,
        0x62 => 118,
        0x64 => 119,
        0x65 => 120,
        0x6D => 121,
        0x67 => 122,
        0x6F => 123,
        _ => rightkit_automation::keys::appkit::ansi_char(code)
            .map(char_vk)
            .filter(|v| *v != 0)
            .unwrap_or_else(|| chars.chars().next().map(char_vk).unwrap_or(0)),
    }
}

/// CDP `key` value for the named keys the crate maps by virtual code; otherwise the character itself.
fn key_value(vk: u32, chars: &str) -> String {
    match vk {
        13 => "Enter".into(),
        9 => "Tab".into(),
        8 => "Backspace".into(),
        27 => "Escape".into(),
        37 => "ArrowLeft".into(),
        38 => "ArrowUp".into(),
        39 => "ArrowRight".into(),
        40 => "ArrowDown".into(),
        36 => "Home".into(),
        35 => "End".into(),
        33 => "PageUp".into(),
        34 => "PageDown".into(),
        _ => chars.to_string(),
    }
}

/// The full `Input.dispatchKeyEvent` payload, serialized by serde_json so quotes,
/// backslashes, control characters and non-ASCII text can never break the JSON.
/// `text` is sent only on key-down of a character-producing key without Ctrl/Meta;
/// control characters other than Enter/Tab are never sent as text.
fn key_event_params(down: bool, code: u16, chars: &str, flags: u64) -> String {
    let vk = vk(code, chars);
    let mods = cdp_mods(flags);
    let producing = |c: char| !c.is_control() || c == '\r' || c == '\t';
    let text: String = if down && mods & 6 == 0 && chars.chars().all(producing) {
        chars.to_string()
    } else {
        String::new()
    };
    let ty = if !down {
        "keyUp"
    } else if text.is_empty() {
        "rawKeyDown"
    } else {
        "keyDown"
    };
    let mut v = serde_json::json!({
        "type": ty,
        "modifiers": mods,
        "windowsVirtualKeyCode": vk,
        "nativeVirtualKeyCode": vk,
        "key": key_value(vk, chars),
    });
    if !text.is_empty() {
        v["text"] = serde_json::Value::String(text.clone());
        v["unmodifiedText"] = serde_json::Value::String(text);
    }
    v.to_string()
}

#[allow(dead_code)]
pub fn key<R: Runtime>(
    win: &WebviewWindow<R>,
    down: bool,
    code: u16,
    chars: &str,
    flags: u64,
) -> Result<(), String> {
    cdp(
        win,
        "Input.dispatchKeyEvent",
        key_event_params(down, code, chars, flags),
    )
    .map(|_| ())
}

/// Insert text exactly as given (any Unicode, emoji, newlines) with `Input.insertText`;
/// per-character key events cannot express characters with no virtual key.
#[allow(dead_code)]
pub fn insert_text<R: Runtime>(win: &WebviewWindow<R>, text: &str) -> Result<(), String> {
    cdp(
        win,
        "Input.insertText",
        serde_json::json!({ "text": text }).to_string(),
    )
    .map(|_| ())
}

/// Key events (and text insertions for characters with no virtual key) in order from one
/// UI-thread hop, no pause between them.
pub fn keys<R: Runtime>(win: &WebviewWindow<R>, evs: &[KeyEv]) -> Result<(), String> {
    cdp_many(
        win,
        evs.iter()
            .map(|e| match e {
                KeyEv::Key {
                    down,
                    code,
                    chars,
                    flags,
                } => (
                    "Input.dispatchKeyEvent".to_string(),
                    key_event_params(*down, *code, chars, *flags),
                ),
                KeyEv::Text(t) => (
                    "Input.insertText".to_string(),
                    serde_json::json!({ "text": t }).to_string(),
                ),
            })
            .collect(),
    )
}

/// Evaluate a JS expression that yields a Promise and wait for its string result
/// (`awaitPromise`); the caller keeps `timeout` under [`CDP_TIMEOUT`].
pub fn wait_slice<R: Runtime>(
    win: &WebviewWindow<R>,
    expr: &str,
    _timeout: Duration,
) -> Result<String, String> {
    let r = cdp(
        win,
        "Runtime.evaluate",
        serde_json::json!({ "expression": expr, "returnByValue": true, "awaitPromise": true })
            .to_string(),
    )?;
    if r.contains("\"exceptionDetails\"") {
        return Err(format!(
            "wait script failed: {}",
            r.chars().take(300).collect::<String>()
        ));
    }
    Ok(str_field(&r, "value").unwrap_or_default())
}

/// PNG with the secret elements hidden for exactly the capture. Hide, capture and restore are
/// issued strictly one after another: each completes before the next starts, so the restore can
/// never overtake the capture. A page that cannot run `hide_js` is never captured; the restore runs
/// whatever the capture did.
pub fn screenshot_masked<R: Runtime>(
    win: &WebviewWindow<R>,
    hide_js: &str,
    restore_js: &str,
) -> Result<(Vec<u8>, u64), String> {
    let masked = eval(win, hide_js).map(|s| s.trim().trim_matches('"').parse::<u64>().unwrap_or(0));
    let png = match &masked {
        Ok(_) => Some(screenshot(win)),
        Err(_) => None,
    };
    let restored = eval(win, restore_js);
    match (masked, png, restored) {
        (Err(e), _, _) => Err(format!("screenshot masking failed ({e}); capture refused")),
        (Ok(_), _, Err(e)) => Err(format!("could not restore masked elements: {e}")),
        (Ok(_), Some(Err(e)), _) => Err(e),
        (Ok(n), Some(Ok(png)), Ok(_)) => Ok((png, n)),
        (Ok(_), None, _) => Err("capture never ran".to_string()),
    }
}

pub fn eval<R: Runtime>(win: &WebviewWindow<R>, js: &str) -> Result<String, String> {
    let r = cdp(
        win,
        "Runtime.evaluate",
        serde_json::json!({ "expression": js, "returnByValue": true }).to_string(),
    )?;
    Ok(str_field(&r, "value").unwrap_or_default())
}

/// Make the page report focus without taking the user's foreground. Chromium's focus
/// emulation (`Emulation.setFocusEmulationEnabled`) gives `document.hasFocus()`, `:focus` and
/// `:focus-visible` the answers of a focused window while the host window stays where it is, so
/// there is deliberately no `SetForegroundWindow`. WebView2 focus is then moved into the page.
pub fn focus_window<R: Runtime>(win: &WebviewWindow<R>) -> Result<String, String> {
    cdp(
        win,
        "Emulation.setFocusEmulationEnabled",
        serde_json::json!({ "enabled": true }).to_string(),
    )?;
    let (tx, rx) = channel::<Result<(), String>>();
    win.with_webview(move |pv| unsafe {
        use webview2_com::Microsoft::Web::WebView2::Win32::COREWEBVIEW2_MOVE_FOCUS_REASON_PROGRAMMATIC;
        let r = pv
            .controller()
            .MoveFocus(COREWEBVIEW2_MOVE_FOCUS_REASON_PROGRAMMATIC)
            .map_err(|e| e.to_string());
        let _ = tx.send(r);
    })
    .map_err(|e| e.to_string())?;
    rx.recv_timeout(Duration::from_secs(10))
        .map_err(|_| "webview focus call timed out".to_string())??;
    Ok("focus emulation enabled, webview focused (no SetForegroundWindow)".into())
}

#[repr(C)]
#[derive(Default)]
struct Rect {
    left: i32,
    top: i32,
    right: i32,
    bottom: i32,
}

#[link(name = "user32")]
extern "system" {
    fn GetClientRect(hwnd: *mut std::ffi::c_void, rect: *mut Rect) -> i32;
    fn GetWindowRect(hwnd: *mut std::ffi::c_void, rect: *mut Rect) -> i32;
    fn SetWindowPos(
        hwnd: *mut std::ffi::c_void,
        after: *mut std::ffi::c_void,
        x: i32,
        y: i32,
        width: i32,
        height: i32,
        flags: u32,
    ) -> i32;
}

/// The page's CSS viewport right now, for the settle loop in `viewport.rs`.
pub fn read_viewport<R: Runtime>(
    win: &WebviewWindow<R>,
    _: Duration,
) -> Result<(f64, f64), String> {
    let s = eval(win, "innerWidth + ',' + innerHeight")?;
    let (w, h) = s.split_once(',').ok_or("CSS viewport measurement failed")?;
    Ok((
        w.trim().parse().map_err(|_| "invalid CSS width")?,
        h.trim().parse().map_err(|_| "invalid CSS height")?,
    ))
}

/// SetWindowPos has no work-area clamp. Preserve visibility, z-order and focus.
/// Converges on the target when it can; a size the system will not give is not an
/// error here: the caller reads back and reports what the page actually has.
pub fn apply_viewport<R: Runtime>(
    win: &WebviewWindow<R>,
    width: u32,
    height: u32,
) -> Result<(), String> {
    let hwnd = win.hwnd().map_err(|e| e.to_string())?.0 as usize;
    let target = (width as f64, height as f64);
    let measure = || -> Result<(f64, f64), String> {
        let s = eval(win, "innerWidth + ',' + innerHeight")?;
        let (w, h) = s.split_once(',').ok_or("CSS viewport measurement failed")?;
        Ok((
            w.parse().map_err(|_| "invalid CSS width")?,
            h.parse().map_err(|_| "invalid CSS height")?,
        ))
    };
    let mut viewport = measure()?;
    if viewport == target {
        return Ok(());
    }
    for _ in 0..3 {
        if viewport.0 <= 0.0 || viewport.1 <= 0.0 {
            return Err("CSS viewport is empty".into());
        }
        let (tx, rx) = channel();
        win.run_on_main_thread(move || unsafe {
            let hwnd = hwnd as *mut std::ffi::c_void;
            let (mut client, mut frame) = (Rect::default(), Rect::default());
            let result = (|| {
                if GetClientRect(hwnd, &mut client) == 0 || GetWindowRect(hwnd, &mut frame) == 0 {
                    return Err(std::io::Error::last_os_error().to_string());
                }
                let scale = (client.right - client.left) as f64 / viewport.0;
                let w = (frame.right - frame.left) as f64 + (target.0 - viewport.0) * scale;
                let h = (frame.bottom - frame.top) as f64 + (target.1 - viewport.1) * scale;
                if SetWindowPos(
                    hwnd,
                    std::ptr::null_mut(),
                    0,
                    0,
                    w.round() as i32,
                    h.round() as i32,
                    0x0002 | 0x0004 | 0x0010 | 0x0200,
                ) == 0
                {
                    return Err(std::io::Error::last_os_error().to_string());
                }
                Ok(())
            })();
            let _ = tx.send(result);
        })
        .map_err(|e| e.to_string())?;
        rx.recv_timeout(Duration::from_secs(10))
            .map_err(|_| "window resize timed out")??;
        for _ in 0..40 {
            viewport = measure()?;
            if viewport == target {
                return Ok(());
            }
            std::thread::sleep(Duration::from_millis(5));
        }
    }
    Ok(())
}

fn screenshot<R: Runtime>(win: &WebviewWindow<R>) -> Result<Vec<u8>, String> {
    let r = cdp(
        win,
        "Page.captureScreenshot",
        r#"{"format":"png","optimizeForSpeed":true}"#.into(),
    )?;
    let b64 = str_field(&r, "data").ok_or("no screenshot data")?;
    let mut out = Vec::with_capacity(b64.len() * 3 / 4);
    let (mut acc, mut bits) = (0u32, 0);
    for c in b64.bytes() {
        let v = match c {
            b'A'..=b'Z' => c - b'A',
            b'a'..=b'z' => c - b'a' + 26,
            b'0'..=b'9' => c - b'0' + 52,
            b'+' => 62,
            b'/' => 63,
            _ => continue,
        } as u32;
        acc = (acc << 6) | v;
        bits += 6;
        if bits >= 8 {
            bits -= 8;
            out.push((acc >> bits) as u8);
            acc &= (1 << bits) - 1;
        }
    }
    Ok(out)
}

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

    /// Hostile inputs must round-trip through the CDP payload unchanged.
    #[test]
    fn key_payload_is_valid_json_for_hostile_text() {
        for t in [
            "\"",
            "\\",
            "a\"b\\c",
            "\n",
            "é",
            "日本",
            "\u{1f600}",
            "\u{7f}",
        ] {
            let v: serde_json::Value =
                serde_json::from_str(&key_event_params(true, 0xFF, t, 0)).expect("valid JSON");
            let text = v.get("text").and_then(|x| x.as_str());
            if t.chars().all(|c| !c.is_control()) {
                assert_eq!(text, Some(t), "{t:?}");
            } else {
                assert_eq!(text, None, "control text is not inserted: {t:?}");
            }
        }
        let enter: serde_json::Value =
            serde_json::from_str(&key_event_params(true, 0x24, "\r", 0)).unwrap();
        assert_eq!(enter["key"], "Enter");
        assert_eq!(enter["text"], "\r");
    }
}

#[cfg(test)]
mod vk_tests {
    use super::*;
    use rightkit_automation::keys::appkit::{chord, named};

    fn vk_of(spec: &str) -> u32 {
        let (code, _) = chord(spec).unwrap();
        let last = spec
            .rsplit('+')
            .next()
            .filter(|l| !l.is_empty())
            .unwrap_or("+");
        let ch = rightkit_automation::keys::appkit::named_char(last)
            .map(String::from)
            .or_else(|| (last.chars().count() == 1).then(|| last.to_string()))
            .unwrap_or_default();
        vk(code, &ch)
    }

    #[test]
    fn named_keys_map_to_windows_vk() {
        for (n, v) in [
            ("enter", 13),
            ("tab", 9),
            ("space", 32),
            ("backspace", 8),
            ("esc", 27),
            ("left", 37),
            ("up", 38),
            ("right", 39),
            ("down", 40),
            ("home", 36),
            ("end", 35),
            ("pageup", 33),
            ("pagedown", 34),
            ("forwarddelete", 46),
        ] {
            assert_eq!(vk(named(n).unwrap(), ""), v, "{n}");
        }
    }

    #[test]
    fn function_keys_map_to_vk_f1_through_f12() {
        for n in 1..=12u32 {
            assert_eq!(vk_of(&format!("f{n}")), 111 + n, "f{n}");
        }
    }

    #[test]
    fn digits_and_letters_use_ascii_vk() {
        for d in '0'..='9' {
            assert_eq!(vk_of(&format!("cmd+{d}")), d as u32, "{d}");
        }
        for l in 'a'..='z' {
            assert_eq!(
                vk_of(&format!("ctrl+{l}")),
                l.to_ascii_uppercase() as u32,
                "{l}"
            );
        }
    }

    #[test]
    fn punctuation_uses_oem_vk_not_colliding_ascii() {
        for (spec, v) in [
            ("cmd+;", 186),
            ("cmd+=", 187),
            ("cmd+,", 188),
            ("cmd+-", 189),
            ("cmd+.", 190),
            ("cmd+/", 191),
            ("cmd+`", 192),
            ("cmd+[", 219),
            ("cmd+\\", 220),
            ("cmd+]", 221),
            ("cmd+'", 222),
            ("cmd+comma", 188),
            ("cmd+period", 190),
            ("cmd+slash", 191),
            ("cmd+minus", 189),
            ("cmd+equal", 187),
        ] {
            assert_eq!(vk_of(spec), v, "{spec}");
        }
    }

    #[test]
    fn cmd_plus_is_shifted_oem_plus() {
        let (code, flags) = chord("cmd++").unwrap();
        assert_eq!(
            flags,
            rightkit_automation::keys::appkit::FLAG_CMD
                | rightkit_automation::keys::appkit::FLAG_SHIFT
        );
        assert_eq!(vk(code, "+"), 187);
        assert_eq!(cdp_mods(flags), 4 | 8);
    }

    #[test]
    fn typed_symbols_never_collide_with_navigation_vk() {
        // Unmapped-code typed characters fall back to the character's own VK.
        assert_eq!(vk(0xFF, "!"), 49);
        assert_eq!(vk(0xFF, "%"), 53);
        assert_eq!(vk(0xFF, "'"), 222);
        assert_eq!(vk(0xFF, "("), 57);
        assert_eq!(vk(0xFF, "é"), 0);
    }

    #[test]
    fn modifier_flags_map_to_cdp_modifiers() {
        use rightkit_automation::keys::appkit::*;
        assert_eq!(cdp_mods(FLAG_ALT), 1);
        assert_eq!(cdp_mods(FLAG_CTRL), 2);
        assert_eq!(cdp_mods(FLAG_CMD), 4);
        assert_eq!(cdp_mods(FLAG_SHIFT), 8);
        assert_eq!(cdp_mods(FLAG_ALT | FLAG_CTRL | FLAG_CMD | FLAG_SHIFT), 15);
        assert_eq!(cdp_mods(0), 0);
    }

    #[test]
    fn key_event_payload_is_valid_json_with_oem_vk() {
        let v: serde_json::Value = serde_json::from_str(&key_event_params(
            true,
            0x2B,
            ",",
            rightkit_automation::keys::appkit::FLAG_CMD,
        ))
        .unwrap();
        assert_eq!(v["windowsVirtualKeyCode"], 188);
        assert_eq!(v["modifiers"], 4);
        assert_eq!(v["key"], ",");
        assert!(v.get("text").is_none());
    }
}