axterminator 0.7.1

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

use std::io::{Read, Write};
use std::net::TcpStream;
use std::sync::atomic::{AtomicU32, Ordering};

use serde::Deserialize;
use serde_json::{json, Value};
use tracing::{debug, info};
use tungstenite::{connect as ws_connect, stream::MaybeTlsStream, Message, WebSocket};

use crate::error::{AXError, AXResult};

// ── Public types ──────────────────────────────────────────────────────────────

/// Bounding rectangle in screen coordinates.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Rect {
    pub x: f64,
    pub y: f64,
    pub width: f64,
    pub height: f64,
}

impl Rect {
    /// Create a new rect.
    #[must_use]
    pub fn new(x: f64, y: f64, width: f64, height: f64) -> Self {
        Self {
            x,
            y,
            width,
            height,
        }
    }

    /// Center point of the rect.
    #[must_use]
    pub fn center(&self) -> (f64, f64) {
        (self.x + self.width * 0.5, self.y + self.height * 0.5)
    }

    /// Area of the rect.
    #[must_use]
    pub fn area(&self) -> f64 {
        self.width * self.height
    }
}

/// A DOM element retrieved from an Electron app via CDP.
#[derive(Debug, Clone)]
pub struct ElectronElement {
    /// CDP backend node ID — stable within a CDP session.
    pub node_id: i64,
    /// HTML tag name, lower-cased (e.g. `"button"`, `"input"`).
    pub tag: String,
    /// CSS classes applied to the element.
    pub classes: Vec<String>,
    /// Visible text content (inner text).
    pub text: String,
    /// Screen bounds of the element, when available.
    pub bounds: Option<Rect>,
}

/// Live connection to an Electron app's Chrome DevTools Protocol endpoint.
///
/// Obtained via [`ElectronConnection::connect`].  Not `Clone` — each
/// connection owns a WebSocket.
pub struct ElectronConnection {
    socket: WebSocket<MaybeTlsStream<TcpStream>>,
    debug_port: u16,
    next_id: AtomicU32,
}

impl std::fmt::Debug for ElectronConnection {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ElectronConnection")
            .field("debug_port", &self.debug_port)
            .finish()
    }
}

// ── Connection establishment ──────────────────────────────────────────────────

impl ElectronConnection {
    /// Connect to an Electron app's CDP endpoint on the given port.
    ///
    /// Performs an HTTP probe to `/json/version` first so callers get an
    /// explicit `Err` rather than a socket hang when the port is closed.
    ///
    /// # Errors
    ///
    /// * [`AXError::AppNotFound`] — no CDP listener on `port`.
    /// * [`AXError::SystemError`] — WebSocket handshake failed.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use axterminator::electron_cdp::ElectronConnection;
    /// let conn = ElectronConnection::connect(9222);
    /// ```
    pub fn connect(port: u16) -> AXResult<Self> {
        if !probe_cdp_port(port) {
            return Err(AXError::AppNotFound(format!(
                "No CDP endpoint on port {port}"
            )));
        }

        let ws_url = format!("ws://127.0.0.1:{port}/json");
        debug!(port, "Connecting to Electron CDP");

        let (socket, _) = ws_connect(&ws_url)
            .map_err(|e| AXError::SystemError(format!("CDP WebSocket failed: {e}")))?;

        info!(port, "Electron CDP connection established");

        Ok(Self {
            socket,
            debug_port: port,
            next_id: AtomicU32::new(1),
        })
    }

    /// The debug port this connection is bound to.
    #[must_use]
    pub fn port(&self) -> u16 {
        self.debug_port
    }

    /// Retrieve the list of CDP targets (pages/workers) from the HTTP endpoint.
    ///
    /// Uses an ephemeral TCP connection rather than the WebSocket so it does
    /// not consume a protocol message slot.
    ///
    /// # Errors
    ///
    /// Returns [`AXError::SystemError`] on TCP or JSON parse failure.
    pub fn list_targets(&self) -> AXResult<Vec<CdpTarget>> {
        let raw = http_get(self.debug_port, "/json")
            .map_err(|e| AXError::SystemError(format!("CDP /json failed: {e}")))?;
        serde_json::from_str::<Vec<CdpTarget>>(&raw)
            .map_err(|e| AXError::SystemError(format!("CDP target parse failed: {e}")))
    }
}

// ── CDP method execution ──────────────────────────────────────────────────────

impl ElectronConnection {
    /// Execute a CDP method and await its response.
    ///
    /// Skips asynchronous event messages until the matching response ID
    /// arrives.
    ///
    /// # Errors
    ///
    /// * [`AXError::SystemError`] — transport error.
    /// * [`AXError::ActionFailed`] — CDP returned a protocol error.
    pub fn execute(&mut self, method: &str, params: Value) -> AXResult<Value> {
        let id = self.next_id.fetch_add(1, Ordering::SeqCst);
        let msg = json!({ "id": id, "method": method, "params": params });

        debug!(method, %id, "CDP request");
        self.socket
            .send(Message::Text(msg.to_string().into()))
            .map_err(|e| AXError::SystemError(format!("CDP send: {e}")))?;

        loop {
            let frame = self
                .socket
                .read()
                .map_err(|e| AXError::SystemError(format!("CDP read: {e}")))?;

            if let Message::Text(text) = frame {
                let resp: CdpResponse = serde_json::from_str(&text)
                    .map_err(|e| AXError::SystemError(format!("CDP parse: {e}")))?;

                if resp.id == Some(id) {
                    return match resp.error {
                        Some(err) => Err(AXError::ActionFailed(format!("CDP: {}", err.message))),
                        None => Ok(resp.result.unwrap_or(Value::Null)),
                    };
                }
                // Event or unrelated response — skip.
            }
        }
    }
}

// ── DOM operations ────────────────────────────────────────────────────────────

impl ElectronConnection {
    /// Query DOM elements matching a CSS selector.
    ///
    /// Returns all matched elements enriched with tag, classes, text content,
    /// and bounding rect.
    ///
    /// # Arguments
    ///
    /// * `selector` — Standard CSS selector (e.g. `"button.submit"`, `"#nav a"`).
    ///
    /// # Errors
    ///
    /// Returns [`AXError::SystemError`] on CDP transport failure or
    /// [`AXError::ActionFailed`] on a CDP protocol error.
    pub fn query_selector(&mut self, selector: &str) -> AXResult<Vec<ElectronElement>> {
        // Get document root
        let doc = self.execute("DOM.getDocument", json!({ "depth": 0 }))?;
        let root_id = doc["root"]["nodeId"]
            .as_i64()
            .ok_or_else(|| AXError::SystemError("No root nodeId".into()))?;

        // Query all matching nodes
        let result = self.execute(
            "DOM.querySelectorAll",
            json!({ "nodeId": root_id, "selector": selector }),
        )?;

        let node_ids = match result["nodeIds"].as_array() {
            Some(ids) => ids.clone(),
            None => return Ok(vec![]),
        };

        node_ids
            .into_iter()
            .filter_map(|v| v.as_i64())
            .map(|nid| self.enrich_element(nid))
            .collect()
    }

    /// Retrieve the full accessibility tree via CDP.
    ///
    /// Uses `Accessibility.getFullAXTree` (Chromium-only, available in
    /// Electron ≥ 6).  Each returned node is mapped to an [`ElectronElement`]
    /// with tag set to the AX role.
    ///
    /// # Errors
    ///
    /// Returns [`AXError::ActionFailed`] when the protocol extension is
    /// unavailable in the target app.
    pub fn get_accessibility_tree(&mut self) -> AXResult<Vec<ElectronElement>> {
        let result = self.execute("Accessibility.getFullAXTree", json!({}))?;

        let nodes = result["nodes"]
            .as_array()
            .ok_or_else(|| AXError::SystemError("AX tree missing 'nodes'".into()))?;

        let elements = nodes.iter().map(ax_node_to_element).collect();

        Ok(elements)
    }

    /// Execute JavaScript in the Electron app context.
    ///
    /// Uses `Runtime.evaluate` — returns the serialised result as a `String`.
    /// For object results this is a JSON representation; primitives are their
    /// natural string form.
    ///
    /// # Arguments
    ///
    /// * `expr` — JavaScript expression to evaluate (e.g. `"document.title"`).
    ///
    /// # Errors
    ///
    /// * [`AXError::ActionFailed`] — JavaScript exception or CDP error.
    /// * [`AXError::SystemError`] — Transport failure.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use axterminator::electron_cdp::ElectronConnection;
    /// let mut conn = ElectronConnection::connect(9222).unwrap();
    /// let title = conn.evaluate_js("document.title").unwrap();
    /// ```
    pub fn evaluate_js(&mut self, expr: &str) -> AXResult<String> {
        let result = self.execute(
            "Runtime.evaluate",
            json!({
                "expression": expr,
                "returnByValue": true,
                "awaitPromise": false,
            }),
        )?;

        // Check for JS exception
        if let Some(exc) = result.get("exceptionDetails") {
            let msg = exc["exception"]["description"]
                .as_str()
                .or_else(|| exc["text"].as_str())
                .unwrap_or("JavaScript exception");
            return Err(AXError::ActionFailed(msg.into()));
        }

        let value = &result["result"]["value"];
        Ok(match value {
            Value::String(s) => s.clone(),
            Value::Null => "null".into(),
            other => other.to_string(),
        })
    }

    /// Dispatch a mouse click at the center of an element via `Input.*` events.
    ///
    /// More reliable than accessibility actions for Electron apps because it
    /// targets the exact DOM coordinates rather than relying on AX wiring.
    ///
    /// # Errors
    ///
    /// Returns [`AXError::ActionFailed`] when the element has no bounds.
    pub fn click_element(&mut self, element: &ElectronElement) -> AXResult<()> {
        let bounds = element
            .bounds
            .ok_or_else(|| AXError::ActionFailed("Element has no bounds for click".into()))?;

        let (x, y) = bounds.center();
        self.dispatch_click(x, y)
    }

    /// Type text into the currently focused element via `Input.insertText`.
    ///
    /// # Errors
    ///
    /// Returns [`AXError::SystemError`] on CDP transport failure.
    pub fn type_text(&mut self, text: &str) -> AXResult<()> {
        self.execute("Input.insertText", json!({ "text": text }))?;
        Ok(())
    }

    // ── Private helpers ─────────────────────────────────────────────────

    fn dispatch_click(&mut self, x: f64, y: f64) -> AXResult<()> {
        let down = json!({
            "type": "mousePressed",
            "x": x, "y": y,
            "button": "left",
            "clickCount": 1,
        });
        let up = json!({
            "type": "mouseReleased",
            "x": x, "y": y,
            "button": "left",
            "clickCount": 1,
        });

        self.execute("Input.dispatchMouseEvent", down)?;
        self.execute("Input.dispatchMouseEvent", up)?;
        Ok(())
    }

    /// Fetch tag, classes, text, and bounds for a single CDP node ID.
    fn enrich_element(&mut self, node_id: i64) -> AXResult<ElectronElement> {
        // Resolve tag + classes from node description
        let desc = self.execute("DOM.describeNode", json!({ "nodeId": node_id, "depth": 0 }))?;

        let tag = desc["node"]["localName"]
            .as_str()
            .unwrap_or("unknown")
            .to_lowercase();

        let classes = parse_class_list(desc["node"]["attributes"].as_array());

        // Visible text via JS
        let text = self
            .evaluate_js(&format!(
                "(function(){{var n=document.querySelectorAll('*')[{node_id}];return n?n.innerText:''}})()"
            ))
            .unwrap_or_default();

        // Bounding box
        let bounds = self.get_box_model(node_id).ok();

        Ok(ElectronElement {
            node_id,
            tag,
            classes,
            text,
            bounds,
        })
    }

    fn get_box_model(&mut self, node_id: i64) -> AXResult<Rect> {
        let result = self.execute("DOM.getBoxModel", json!({ "nodeId": node_id }))?;
        let content = result["model"]["content"]
            .as_array()
            .ok_or_else(|| AXError::SystemError("No content box".into()))?;

        // content = [x0,y0, x1,y1, x2,y2, x3,y3] — top-left and bottom-right
        let x0 = content.first().and_then(Value::as_f64).unwrap_or(0.0);
        let y0 = content.get(1).and_then(Value::as_f64).unwrap_or(0.0);
        let x1 = content.get(4).and_then(Value::as_f64).unwrap_or(0.0);
        let y1 = content.get(5).and_then(Value::as_f64).unwrap_or(0.0);

        Ok(Rect::new(x0, y0, x1 - x0, y1 - y0))
    }
}

// ── CDP data types ────────────────────────────────────────────────────────────

/// A CDP debug target (page, worker, etc.) returned by `/json`.
#[derive(Debug, Clone, Deserialize)]
pub struct CdpTarget {
    pub id: String,
    pub title: String,
    #[serde(rename = "type")]
    pub target_type: String,
    #[serde(rename = "webSocketDebuggerUrl")]
    pub ws_url: Option<String>,
}

#[derive(Debug, Deserialize)]
struct CdpResponse {
    id: Option<u32>,
    result: Option<Value>,
    error: Option<CdpError>,
}

#[derive(Debug, Deserialize)]
struct CdpError {
    #[allow(dead_code)]
    code: i32,
    message: String,
}

// ── Free functions ────────────────────────────────────────────────────────────

/// Probe whether a CDP endpoint is listening on `port`.
///
/// Sends a minimal HTTP/1.1 request and checks for a JSON-like response.
#[must_use]
pub fn probe_cdp_port(port: u16) -> bool {
    let Ok(mut stream) = TcpStream::connect(format!("127.0.0.1:{port}")) else {
        return false;
    };
    let req = format!(
        "GET /json/version HTTP/1.1\r\nHost: 127.0.0.1:{port}\r\nConnection: close\r\n\r\n"
    );
    if stream.write_all(req.as_bytes()).is_err() {
        return false;
    }
    let mut buf = [0u8; 512];
    let Ok(n) = stream.read(&mut buf) else {
        return false;
    };
    let resp = String::from_utf8_lossy(&buf[..n]);
    resp.contains("Browser") || resp.contains("webSocketDebuggerUrl")
}

/// HTTP GET helper — returns the response body as a String.
fn http_get(port: u16, path: &str) -> std::io::Result<String> {
    let mut stream = TcpStream::connect(format!("127.0.0.1:{port}"))?;
    let req = format!("GET {path} HTTP/1.1\r\nHost: 127.0.0.1:{port}\r\nConnection: close\r\n\r\n");
    stream.write_all(req.as_bytes())?;

    let mut body = String::new();
    stream.read_to_string(&mut body)?;

    // Strip HTTP headers
    if let Some(pos) = body.find("\r\n\r\n") {
        Ok(body[pos + 4..].to_string())
    } else {
        Ok(body)
    }
}

/// Parse CSS class tokens from a CDP attribute list.
///
/// Attributes come as flat `[name, value, name, value, …]` pairs.
fn parse_class_list(attrs: Option<&Vec<Value>>) -> Vec<String> {
    let Some(attrs) = attrs else {
        return vec![];
    };

    // Walk pairs: attrs[i] = name, attrs[i+1] = value
    attrs
        .chunks(2)
        .find(|pair| pair.first().and_then(Value::as_str) == Some("class"))
        .and_then(|pair| pair.get(1)?.as_str())
        .map(|classes| classes.split_whitespace().map(str::to_string).collect())
        .unwrap_or_default()
}

/// Convert a CDP `Accessibility.AXNode` JSON object to an [`ElectronElement`].
fn ax_node_to_element(node: &Value) -> ElectronElement {
    let node_id = node["nodeId"].as_i64().unwrap_or(0);
    let tag = node["role"]["value"]
        .as_str()
        .unwrap_or("unknown")
        .to_lowercase();
    let text = node["name"]["value"].as_str().unwrap_or("").to_string();

    ElectronElement {
        node_id,
        tag,
        classes: vec![],
        text,
        bounds: None,
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

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

    // ── Rect ───────────────────────────────────────────────────────────────

    #[test]
    fn rect_center_midpoint_of_bounds() {
        // GIVEN: Rect at (10, 20) with size 80×40
        let r = Rect::new(10.0, 20.0, 80.0, 40.0);
        // WHEN
        let (cx, cy) = r.center();
        // THEN
        assert_eq!(cx, 50.0);
        assert_eq!(cy, 40.0);
    }

    #[test]
    fn rect_area_width_times_height() {
        // GIVEN
        let r = Rect::new(0.0, 0.0, 100.0, 50.0);
        // THEN
        assert_eq!(r.area(), 5_000.0);
    }

    #[test]
    fn rect_zero_size_area_is_zero() {
        // GIVEN
        let r = Rect::new(5.0, 5.0, 0.0, 0.0);
        // THEN
        assert_eq!(r.area(), 0.0);
    }

    // ── probe_cdp_port ─────────────────────────────────────────────────────

    #[test]
    fn probe_cdp_port_closed_port_returns_false() {
        // GIVEN: Port 65000 is almost certainly not in use
        // THEN: probe returns false without panicking
        assert!(!probe_cdp_port(65_000));
    }

    #[test]
    fn probe_cdp_port_invalid_high_port_returns_false() {
        assert!(!probe_cdp_port(65_535));
    }

    // ── parse_class_list ───────────────────────────────────────────────────

    #[test]
    fn parse_class_list_extracts_classes() {
        // GIVEN: CDP attribute array [id, myId, class, "btn primary"]
        let attrs = vec![
            json!("id"),
            json!("myId"),
            json!("class"),
            json!("btn primary"),
        ];
        // WHEN
        let classes = parse_class_list(Some(&attrs));
        // THEN
        assert_eq!(classes, vec!["btn", "primary"]);
    }

    #[test]
    fn parse_class_list_no_class_attr_returns_empty() {
        // GIVEN: No class attribute
        let attrs = vec![json!("id"), json!("myId")];
        // THEN
        assert!(parse_class_list(Some(&attrs)).is_empty());
    }

    #[test]
    fn parse_class_list_none_returns_empty() {
        // GIVEN / THEN
        assert!(parse_class_list(None).is_empty());
    }

    // ── ax_node_to_element ─────────────────────────────────────────────────

    #[test]
    fn ax_node_to_element_maps_role_and_name() {
        // GIVEN: CDP AX node JSON
        let node = json!({
            "nodeId": 42,
            "role": { "value": "Button" },
            "name": { "value": "Submit" }
        });
        // WHEN
        let elem = ax_node_to_element(&node);
        // THEN
        assert_eq!(elem.node_id, 42);
        assert_eq!(elem.tag, "button");
        assert_eq!(elem.text, "Submit");
        assert!(elem.classes.is_empty());
        assert!(elem.bounds.is_none());
    }

    #[test]
    fn ax_node_to_element_missing_fields_use_defaults() {
        // GIVEN: Minimal node with no role or name
        let node = json!({});
        // WHEN
        let elem = ax_node_to_element(&node);
        // THEN: no panic, sensible defaults
        assert_eq!(elem.node_id, 0);
        assert_eq!(elem.tag, "unknown");
        assert_eq!(elem.text, "");
    }

    // ── http_get ───────────────────────────────────────────────────────────

    #[test]
    fn http_get_strips_http_headers() {
        // GIVEN: A raw HTTP response with headers + body
        // We test the stripping logic by calling the internal helper indirectly.
        // Since we cannot call a real server in unit tests, we validate the
        // stripping logic via a direct string simulation.
        let raw = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n[{\"id\":\"1\"}]";
        let split_pos = raw.find("\r\n\r\n").expect("must have separator");
        let body = &raw[split_pos + 4..];
        assert_eq!(body, "[{\"id\":\"1\"}]");
    }

    // ── ElectronConnection::connect error path ─────────────────────────────

    #[test]
    fn connect_to_closed_port_returns_error() {
        // GIVEN: Port 65001 is not a CDP endpoint
        // WHEN
        let result = ElectronConnection::connect(65_001);
        // THEN: Specific error, no panic
        assert!(result.is_err());
        let err = result.unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("No CDP endpoint") || msg.contains("CDP"),
            "unexpected error message: {msg}"
        );
    }

    // ── CdpTarget deserialisation ──────────────────────────────────────────

    #[test]
    fn cdp_target_deserialises_from_json() {
        // GIVEN: A typical /json response entry
        let json_str = r#"{
            "id": "abc-123",
            "title": "VS Code",
            "type": "page",
            "webSocketDebuggerUrl": "ws://127.0.0.1:9222/devtools/page/abc-123"
        }"#;
        // WHEN
        let target: CdpTarget = serde_json::from_str(json_str).unwrap();
        // THEN
        assert_eq!(target.id, "abc-123");
        assert_eq!(target.title, "VS Code");
        assert_eq!(target.target_type, "page");
        assert!(target.ws_url.is_some());
    }

    #[test]
    fn cdp_target_deserialises_without_ws_url() {
        // GIVEN: Worker target with no debugger URL
        let json_str = r#"{
            "id": "worker-1",
            "title": "service worker",
            "type": "service_worker"
        }"#;
        // WHEN
        let target: CdpTarget = serde_json::from_str(json_str).unwrap();
        // THEN
        assert_eq!(target.target_type, "service_worker");
        assert!(target.ws_url.is_none());
    }
}