chrome-cli 1.2.0

A CLI tool for browser automation via the Chrome DevTools Protocol
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
use std::fmt;

use serde::Serialize;

#[repr(u8)]
#[derive(Debug, Clone, Copy)]
pub enum ExitCode {
    Success = 0,
    GeneralError = 1,
    ConnectionError = 2,
    TargetError = 3,
    TimeoutError = 4,
    ProtocolError = 5,
}

impl fmt::Display for ExitCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Success => write!(f, "success"),
            Self::GeneralError => write!(f, "general error"),
            Self::ConnectionError => write!(f, "connection error"),
            Self::TargetError => write!(f, "target error"),
            Self::TimeoutError => write!(f, "timeout error"),
            Self::ProtocolError => write!(f, "protocol error"),
        }
    }
}

#[derive(Debug)]
pub struct AppError {
    pub message: String,
    pub code: ExitCode,
    /// Pre-serialized JSON to emit instead of the default `ErrorOutput`.
    /// Used by the JS execution path to preserve its richer error schema.
    pub custom_json: Option<String>,
}

impl fmt::Display for AppError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}: {}", self.code, self.message)
    }
}

impl std::error::Error for AppError {}

impl AppError {
    #[must_use]
    pub fn not_implemented(command: &str) -> Self {
        Self {
            message: format!("{command}: not yet implemented"),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn stale_session() -> Self {
        Self {
            message: "Session is stale: Chrome is not reachable at the stored address. \
                      Run 'chrome-cli connect' to establish a new connection."
                .into(),
            code: ExitCode::ConnectionError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn no_session() -> Self {
        Self {
            message: "No active session. Run 'chrome-cli connect' or \
                      'chrome-cli connect --launch' to establish a connection."
                .into(),
            code: ExitCode::ConnectionError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn target_not_found(tab: &str) -> Self {
        Self {
            message: format!(
                "Tab '{tab}' not found. Run 'chrome-cli tabs list' to see available tabs."
            ),
            code: ExitCode::TargetError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn no_page_targets() -> Self {
        Self {
            message: "No page targets found in Chrome. Open a tab first.".into(),
            code: ExitCode::TargetError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn last_tab() -> Self {
        Self {
            message: "Cannot close the last tab. Chrome requires at least one open tab.".into(),
            code: ExitCode::TargetError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn navigation_failed(error_text: &str) -> Self {
        Self {
            message: format!("Navigation failed: {error_text}"),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn navigation_timeout(timeout_ms: u64, strategy: &str) -> Self {
        Self {
            message: format!("Navigation timed out after {timeout_ms}ms waiting for {strategy}"),
            code: ExitCode::TimeoutError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn element_not_found(selector: &str) -> Self {
        Self {
            message: format!("Element not found for selector: {selector}"),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn evaluation_failed(description: &str) -> Self {
        Self {
            message: format!("Text extraction failed: {description}"),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn snapshot_failed(description: &str) -> Self {
        Self {
            message: format!("Accessibility tree capture failed: {description}"),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn file_write_failed(path: &str, error: &str) -> Self {
        Self {
            message: format!("Failed to write snapshot to file: {path}: {error}"),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn screenshot_failed(description: &str) -> Self {
        Self {
            message: format!("Screenshot capture failed: {description}"),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn uid_not_found(uid: &str) -> Self {
        Self {
            message: format!("UID '{uid}' not found. Run 'chrome-cli page snapshot' first."),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn invalid_clip(input: &str) -> Self {
        Self {
            message: format!(
                "Invalid clip format: expected X,Y,WIDTH,HEIGHT (e.g. 10,20,200,100): {input}"
            ),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn no_active_trace() -> Self {
        Self {
            message: "No active trace. Use 'chrome-cli perf record' to record a trace.".into(),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn unknown_insight(name: &str) -> Self {
        Self {
            message: format!(
                "Unknown insight: '{name}'. Available: DocumentLatency, LCPBreakdown, \
                 RenderBlocking, LongTasks"
            ),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn trace_file_not_found(path: &str) -> Self {
        Self {
            message: format!("Trace file not found: {path}"),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn trace_parse_failed(error: &str) -> Self {
        Self {
            message: format!("Failed to parse trace file: {error}"),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn trace_timeout(timeout_ms: u64) -> Self {
        Self {
            message: format!("Trace timed out after {timeout_ms}ms"),
            code: ExitCode::TimeoutError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn js_execution_failed(description: &str) -> Self {
        Self {
            message: format!("JavaScript execution failed: {description}"),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn js_execution_failed_with_json(description: &str, json: String) -> Self {
        Self {
            message: format!("JavaScript execution failed: {description}"),
            code: ExitCode::GeneralError,
            custom_json: Some(json),
        }
    }

    #[must_use]
    pub fn script_file_not_found(path: &str) -> Self {
        Self {
            message: format!("Script file not found: {path}"),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn script_file_read_failed(path: &str, error: &str) -> Self {
        Self {
            message: format!("Failed to read script file: {path}: {error}"),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn no_js_code() -> Self {
        Self {
            message:
                "No JavaScript code provided. Specify code as argument, --file, or pipe via stdin."
                    .into(),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn no_dialog_open() -> Self {
        Self {
            message: "No dialog is currently open. A dialog must be open before it can be handled."
                .into(),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn dialog_handle_failed(reason: &str) -> Self {
        Self {
            message: format!("Dialog handling failed: {reason}"),
            code: ExitCode::ProtocolError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn no_chrome_found() -> Self {
        Self {
            message: "No Chrome instance found. Run 'chrome-cli connect' or \
                      'chrome-cli connect --launch' to establish a connection."
                .into(),
            code: ExitCode::ConnectionError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn no_snapshot_state() -> Self {
        Self {
            message: "No snapshot state found. Run 'chrome-cli page snapshot' first to assign \
                      UIDs to interactive elements."
                .into(),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn element_zero_size(target: &str) -> Self {
        Self {
            message: format!(
                "Element '{target}' has zero-size bounding box and cannot be clicked."
            ),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn invalid_key(key: &str) -> Self {
        Self {
            message: format!("Invalid key: '{key}'"),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn duplicate_modifier(modifier: &str) -> Self {
        Self {
            message: format!("Duplicate modifier: '{modifier}'"),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn interaction_failed(action: &str, reason: &str) -> Self {
        Self {
            message: format!("Interaction failed ({action}): {reason}"),
            code: ExitCode::ProtocolError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn emulation_failed(description: &str) -> Self {
        Self {
            message: format!("Emulation failed: {description}"),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn invalid_viewport(input: &str) -> Self {
        Self {
            message: format!(
                "Invalid viewport format: expected WIDTHxHEIGHT (e.g. 1280x720): {input}"
            ),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn invalid_geolocation(input: &str) -> Self {
        Self {
            message: format!(
                "Invalid geolocation format: expected LAT,LONG (e.g. 37.7749,-122.4194): {input}"
            ),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn file_not_found(path: &str) -> Self {
        Self {
            message: format!("File not found: {path}"),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn file_not_readable(path: &str) -> Self {
        Self {
            message: format!("File not readable: {path}"),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn not_file_input(target: &str) -> Self {
        Self {
            message: format!("Element is not a file input: {target}"),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn node_not_found(id: &str) -> Self {
        Self {
            message: format!("Node not found: {id}"),
            code: ExitCode::TargetError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn attribute_not_found(name: &str, node_id: &str) -> Self {
        Self {
            message: format!("Attribute '{name}' not found on node {node_id}"),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn no_parent() -> Self {
        Self {
            message: "Element has no parent (document root)".into(),
            code: ExitCode::TargetError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn stale_uid(uid: &str) -> Self {
        Self {
            message: format!(
                "UID '{uid}' refers to an element that no longer exists. \
                 Run 'chrome-cli page snapshot' to refresh."
            ),
            code: ExitCode::GeneralError,
            custom_json: None,
        }
    }

    #[must_use]
    pub fn to_json(&self) -> String {
        let output = ErrorOutput {
            error: &self.message,
            code: self.code as u8,
        };
        serde_json::to_string(&output).unwrap_or_else(|_| {
            format!(
                r#"{{"error":"{}","code":{}}}"#,
                self.message, self.code as u8
            )
        })
    }

    pub fn print_json_stderr(&self) {
        if let Some(ref json) = self.custom_json {
            eprintln!("{json}");
        } else {
            eprintln!("{}", self.to_json());
        }
    }
}

#[derive(Serialize)]
struct ErrorOutput<'a> {
    error: &'a str,
    code: u8,
}

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

    #[test]
    fn not_implemented_produces_json_with_error_and_code() {
        let err = AppError::not_implemented("tabs");
        let json = err.to_json();
        let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed["error"], "tabs: not yet implemented");
        assert_eq!(parsed["code"], 1);
    }

    #[test]
    fn exit_code_display() {
        assert_eq!(ExitCode::Success.to_string(), "success");
        assert_eq!(ExitCode::GeneralError.to_string(), "general error");
        assert_eq!(ExitCode::ConnectionError.to_string(), "connection error");
    }

    #[test]
    fn app_error_display() {
        let err = AppError::not_implemented("connect");
        assert_eq!(
            err.to_string(),
            "general error: connect: not yet implemented"
        );
    }

    #[test]
    fn stale_session_error() {
        let err = AppError::stale_session();
        assert!(err.message.contains("stale"));
        assert!(err.message.contains("chrome-cli connect"));
        assert!(matches!(err.code, ExitCode::ConnectionError));
    }

    #[test]
    fn no_session_error() {
        let err = AppError::no_session();
        assert!(err.message.contains("No active session"));
        assert!(matches!(err.code, ExitCode::ConnectionError));
    }

    #[test]
    fn target_not_found_error() {
        let err = AppError::target_not_found("ABCDEF");
        assert!(err.message.contains("ABCDEF"));
        assert!(err.message.contains("tabs list"));
        assert!(matches!(err.code, ExitCode::TargetError));
    }

    #[test]
    fn no_page_targets_error() {
        let err = AppError::no_page_targets();
        assert!(err.message.contains("No page targets"));
        assert!(matches!(err.code, ExitCode::TargetError));
    }

    #[test]
    fn last_tab_error() {
        let err = AppError::last_tab();
        assert!(err.message.contains("Cannot close the last tab"));
        assert!(err.message.contains("at least one open tab"));
        assert!(matches!(err.code, ExitCode::TargetError));
    }

    #[test]
    fn navigation_failed_error() {
        let err = AppError::navigation_failed("net::ERR_NAME_NOT_RESOLVED");
        assert!(err.message.contains("Navigation failed"));
        assert!(err.message.contains("ERR_NAME_NOT_RESOLVED"));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn navigation_timeout_error() {
        let err = AppError::navigation_timeout(30000, "load");
        assert!(err.message.contains("timed out"));
        assert!(err.message.contains("30000ms"));
        assert!(err.message.contains("load"));
        assert!(matches!(err.code, ExitCode::TimeoutError));
    }

    #[test]
    fn element_not_found_error() {
        let err = AppError::element_not_found("#missing");
        assert!(err.message.contains("Element not found"));
        assert!(err.message.contains("#missing"));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn evaluation_failed_error() {
        let err = AppError::evaluation_failed("script threw an exception");
        assert!(err.message.contains("Text extraction failed"));
        assert!(err.message.contains("script threw an exception"));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn snapshot_failed_error() {
        let err = AppError::snapshot_failed("domain not enabled");
        assert!(err.message.contains("Accessibility tree capture failed"));
        assert!(err.message.contains("domain not enabled"));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn file_write_failed_error() {
        let err = AppError::file_write_failed("/tmp/out.txt", "permission denied");
        assert!(err.message.contains("Failed to write snapshot to file"));
        assert!(err.message.contains("/tmp/out.txt"));
        assert!(err.message.contains("permission denied"));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn no_chrome_found_error() {
        let err = AppError::no_chrome_found();
        assert!(err.message.contains("No Chrome instance found"));
        assert!(matches!(err.code, ExitCode::ConnectionError));
    }

    #[test]
    fn screenshot_failed_error() {
        let err = AppError::screenshot_failed("timeout waiting for capture");
        assert!(err.message.contains("Screenshot capture failed"));
        assert!(err.message.contains("timeout waiting for capture"));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn uid_not_found_error() {
        let err = AppError::uid_not_found("s99");
        assert!(err.message.contains("s99"));
        assert!(err.message.contains("page snapshot"));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn invalid_clip_error() {
        let err = AppError::invalid_clip("abc");
        assert!(err.message.contains("Invalid clip format"));
        assert!(err.message.contains("X,Y,WIDTH,HEIGHT"));
        assert!(err.message.contains("abc"));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn no_active_trace_error() {
        let err = AppError::no_active_trace();
        assert!(err.message.contains("No active trace"));
        assert!(err.message.contains("perf record"));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn unknown_insight_error() {
        let err = AppError::unknown_insight("BadInsight");
        assert!(err.message.contains("Unknown insight"));
        assert!(err.message.contains("BadInsight"));
        assert!(err.message.contains("DocumentLatency"));
        assert!(err.message.contains("LCPBreakdown"));
        assert!(err.message.contains("RenderBlocking"));
        assert!(err.message.contains("LongTasks"));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn trace_file_not_found_error() {
        let err = AppError::trace_file_not_found("/tmp/missing.json");
        assert!(err.message.contains("Trace file not found"));
        assert!(err.message.contains("/tmp/missing.json"));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn trace_parse_failed_error() {
        let err = AppError::trace_parse_failed("unexpected EOF");
        assert!(err.message.contains("Failed to parse trace file"));
        assert!(err.message.contains("unexpected EOF"));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn trace_timeout_error() {
        let err = AppError::trace_timeout(30000);
        assert!(err.message.contains("Trace timed out"));
        assert!(err.message.contains("30000ms"));
        assert!(matches!(err.code, ExitCode::TimeoutError));
    }

    #[test]
    fn js_execution_failed_error() {
        let err = AppError::js_execution_failed("ReferenceError: foo is not defined");
        assert!(err.message.contains("JavaScript execution failed"));
        assert!(err.message.contains("ReferenceError: foo is not defined"));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn script_file_not_found_error() {
        let err = AppError::script_file_not_found("/tmp/missing.js");
        assert!(err.message.contains("Script file not found"));
        assert!(err.message.contains("/tmp/missing.js"));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn script_file_read_failed_error() {
        let err = AppError::script_file_read_failed("/tmp/bad.js", "permission denied");
        assert!(err.message.contains("Failed to read script file"));
        assert!(err.message.contains("/tmp/bad.js"));
        assert!(err.message.contains("permission denied"));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn no_dialog_open_error() {
        let err = AppError::no_dialog_open();
        assert!(err.message.contains("No dialog is currently open"));
        assert!(err.message.contains("must be open"));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn invalid_key_error() {
        let err = AppError::invalid_key("FooBar");
        assert!(err.message.contains("Invalid key"));
        assert!(err.message.contains("FooBar"));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn duplicate_modifier_error() {
        let err = AppError::duplicate_modifier("Control");
        assert!(err.message.contains("Duplicate modifier"));
        assert!(err.message.contains("Control"));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn dialog_handle_failed_error() {
        let err = AppError::dialog_handle_failed("could not dismiss");
        assert!(err.message.contains("Dialog handling failed"));
        assert!(err.message.contains("could not dismiss"));
        assert!(matches!(err.code, ExitCode::ProtocolError));
    }

    #[test]
    fn emulation_failed_error() {
        let err = AppError::emulation_failed("CDP returned error");
        assert!(err.message.contains("Emulation failed"));
        assert!(err.message.contains("CDP returned error"));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn invalid_viewport_error() {
        let err = AppError::invalid_viewport("badformat");
        assert!(err.message.contains("Invalid viewport format"));
        assert!(err.message.contains("WIDTHxHEIGHT"));
        assert!(err.message.contains("badformat"));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn invalid_geolocation_error() {
        let err = AppError::invalid_geolocation("not-a-coord");
        assert!(err.message.contains("Invalid geolocation format"));
        assert!(err.message.contains("LAT,LONG"));
        assert!(err.message.contains("not-a-coord"));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn no_js_code_error() {
        let err = AppError::no_js_code();
        assert!(err.message.contains("No JavaScript code provided"));
        assert!(err.message.contains("--file"));
        assert!(err.message.contains("stdin"));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn file_not_found_error() {
        let err = AppError::file_not_found("/nonexistent/file.txt");
        assert!(err.message.contains("File not found"));
        assert!(err.message.contains("/nonexistent/file.txt"));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn file_not_readable_error() {
        let err = AppError::file_not_readable("/tmp/secret.txt");
        assert!(err.message.contains("File not readable"));
        assert!(err.message.contains("/tmp/secret.txt"));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn not_file_input_error() {
        let err = AppError::not_file_input("s2");
        assert!(err.message.contains("Element is not a file input"));
        assert!(err.message.contains("s2"));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn js_execution_failed_with_json_carries_custom_json() {
        let custom =
            r#"{"error":"Error: test","stack":"Error: test\n    at <anonymous>:1:7","code":1}"#;
        let err = AppError::js_execution_failed_with_json("Error: test", custom.to_string());
        assert!(err.message.contains("JavaScript execution failed"));
        assert_eq!(err.custom_json.as_deref(), Some(custom));
        assert!(matches!(err.code, ExitCode::GeneralError));
    }

    #[test]
    fn js_execution_failed_without_json_has_no_custom_json() {
        let err = AppError::js_execution_failed("Error: test");
        assert!(err.custom_json.is_none());
    }
}