reasonkit-core 0.1.8

The Reasoning Engine — Auditable Reasoning for Production AI | Rust-Native | Turn Prompts into Protocols
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
//! Web Browser Interface Trait for ReasonKit Core
//!
//! This module defines the abstraction layer for reasonkit-core to interact with
//! reasonkit-web's browser automation and content extraction capabilities.
//!
//! # Architecture
//!
//! ```text
//! ReasonKit Core --> WebBrowserAdapter --> ReasonKit Web
//!                   (trait interface)
//!        |- navigate()
//!        |- extract_content()
//!        `- capture_screenshot()
//! ```
//!
//! # Design Principles
//!
//! - **Async-First**: All operations use `async-trait` for future compatibility
//! - **Type-Safe**: Strong types for URLs, content, and capture formats
//! - **Error Handling**: Comprehensive error types via `thiserror`
//! - **Flexible Implementations**: Support local MCP server, FFI, or HTTP bindings
//! - **Performance**: Connection pooling and caching by default
//!
//! # Example
//!
//! ```rust,no_run
//! use reasonkit::web_interface::{WebBrowserAdapter, NavigateOptions, CaptureFormat};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // Implementation will use concrete adapter (e.g., McpWebAdapter, HttpWebAdapter)
//!     // let adapter = McpWebAdapter::new(config).await?;
//!
//!     // Navigate to URL
//!     // let page = adapter.navigate(
//!     //     "https://example.com",
//!     //     NavigateOptions::default(),
//!     // ).await?;
//!
//!     // Extract main content
//!     // let content = adapter.extract_content(
//!     //     &page,
//!     //     ExtractOptions::default(),
//!     // ).await?;
//!
//!     // Capture screenshot
//!     // let screenshot = adapter.capture_screenshot(
//!     //     &page,
//!     //     CaptureOptions::default().format(CaptureFormat::Png),
//!     // ).await?;
//!
//!     Ok(())
//! }
//! ```

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
use thiserror::Error;

// =============================================================================
// ERROR TYPES
// =============================================================================

/// Web browser adapter error types
#[derive(Error, Debug)]
pub enum WebAdapterError {
    /// Navigation to URL failed
    #[error("Navigation failed: {message}")]
    NavigationFailed { message: String },

    /// Content extraction failed
    #[error("Content extraction failed: {message}")]
    ExtractionFailed { message: String },

    /// Screenshot/capture failed
    #[error("Capture failed: {format:?} - {message}")]
    CaptureFailed {
        format: CaptureFormat,
        message: String,
    },

    /// Page navigation timed out
    #[error("Navigation timeout after {0}ms")]
    NavigationTimeout(u64),

    /// Invalid URL provided
    #[error("Invalid URL: {0}")]
    InvalidUrl(String),

    /// Adapter not connected
    #[error("Adapter not connected to web service")]
    NotConnected,

    /// Connection lost
    #[error("Connection to web service lost")]
    ConnectionLost,

    /// Selector parsing error
    #[error("Invalid CSS selector: {0}")]
    InvalidSelector(String),

    /// JavaScript execution failed
    #[error("JavaScript execution failed: {message}")]
    JavaScriptError { message: String },

    /// Unsupported capture format for current implementation
    #[error("Capture format not supported: {0:?}")]
    UnsupportedFormat(CaptureFormat),

    /// Resource not found (HTTP 404, etc.)
    #[error("Resource not found: {0}")]
    NotFound(String),

    /// Network error
    #[error("Network error: {0}")]
    Network(String),

    /// Serialization/deserialization error
    #[error("Serialization error: {0}")]
    Serialization(String),

    /// Generic adapter error
    #[error("{0}")]
    Generic(String),
}

/// Result type alias for web adapter operations
pub type WebAdapterResult<T> = std::result::Result<T, WebAdapterError>;

// =============================================================================
// DATA TYPES
// =============================================================================

/// Represents a page/tab in the browser
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PageHandle {
    /// Unique page identifier
    pub id: String,
    /// Current page URL
    pub url: String,
    /// Page title
    pub title: String,
    /// Whether page is still valid/accessible
    pub is_active: bool,
}

impl fmt::Display for PageHandle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Page({}: {})", self.id, self.url)
    }
}

/// Options for page navigation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NavigateOptions {
    /// Maximum time to wait for page load (milliseconds)
    /// Default: 30000 (30 seconds)
    pub timeout_ms: u64,

    /// Wait until event
    /// Possible values: "load", "domcontentloaded", "networkidle"
    /// Default: "load"
    pub wait_until: NavigateWaitEvent,

    /// JavaScript to execute after page load
    pub inject_js: Option<String>,

    /// Headers to send with navigation request
    pub headers: HashMap<String, String>,

    /// User agent override
    pub user_agent: Option<String>,

    /// Viewport dimensions (width, height)
    pub viewport: Option<(u32, u32)>,

    /// Follow redirects (default: true)
    pub follow_redirects: bool,
}

impl Default for NavigateOptions {
    fn default() -> Self {
        Self {
            timeout_ms: 30000,
            wait_until: NavigateWaitEvent::Load,
            inject_js: None,
            headers: HashMap::new(),
            user_agent: None,
            viewport: None,
            follow_redirects: true,
        }
    }
}

/// Page load wait event
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum NavigateWaitEvent {
    /// Wait for page load event
    #[default]
    Load,
    /// Wait for DOM content loaded event
    DomContentLoaded,
    /// Wait for network to idle (no pending requests)
    NetworkIdle,
}

impl fmt::Display for NavigateWaitEvent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Load => write!(f, "load"),
            Self::DomContentLoaded => write!(f, "domcontentloaded"),
            Self::NetworkIdle => write!(f, "networkidle"),
        }
    }
}

/// Extracted content from a page
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ExtractedContent {
    /// Main body text
    pub text: String,

    /// Extracted HTML (optional, if structured extraction requested)
    pub html: Option<String>,

    /// Extracted links
    pub links: Vec<ExtractedLink>,

    /// Extracted images
    pub images: Vec<ExtractedImage>,

    /// Extracted metadata
    pub metadata: ContentMetadata,

    /// Structured data (JSON-LD, microdata, etc.)
    pub structured_data: Option<serde_json::Value>,

    /// Language detection
    pub language: Option<String>,

    /// Extraction confidence (0.0-1.0)
    pub confidence: f32,
}

/// Extracted link from content
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ExtractedLink {
    /// Link text
    pub text: String,
    /// Link URL
    pub href: String,
    /// Link title attribute
    pub title: Option<String>,
}

/// Extracted image from content
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ExtractedImage {
    /// Image URL
    pub src: String,
    /// Alt text
    pub alt: Option<String>,
    /// Image title
    pub title: Option<String>,
}

/// Content metadata
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ContentMetadata {
    /// Page title
    pub title: Option<String>,
    /// Page description
    pub description: Option<String>,
    /// Open Graph image
    pub og_image: Option<String>,
    /// Open Graph title
    pub og_title: Option<String>,
    /// Content type (text/html, application/json, etc.)
    pub content_type: Option<String>,
    /// Character encoding
    pub charset: Option<String>,
    /// Author
    pub author: Option<String>,
    /// Publication date
    pub publish_date: Option<String>,
    /// Custom meta tags
    pub custom_meta: HashMap<String, String>,
}

/// Options for content extraction
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExtractOptions {
    /// CSS selector for main content area (optional, auto-detect if not specified)
    pub content_selector: Option<String>,

    /// Extract links (default: true)
    pub extract_links: bool,

    /// Extract images (default: false)
    pub extract_images: bool,

    /// Extract structured data (default: false)
    pub extract_structured_data: bool,

    /// Remove script and style tags (default: true)
    pub remove_scripts: bool,

    /// Minimum text length to include (default: 20 chars)
    pub min_text_length: usize,

    /// Detect language (default: false)
    pub detect_language: bool,

    /// Custom JavaScript to execute for extraction
    pub custom_js: Option<String>,
}

impl Default for ExtractOptions {
    fn default() -> Self {
        Self {
            content_selector: None,
            extract_links: true,
            extract_images: false,
            extract_structured_data: false,
            remove_scripts: true,
            min_text_length: 20,
            detect_language: false,
            custom_js: None,
        }
    }
}

/// Screenshot/capture format
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, Hash)]
#[serde(rename_all = "lowercase")]
pub enum CaptureFormat {
    /// PNG image (lossless, recommended)
    #[default]
    Png,
    /// JPEG image (compressed, smaller file size)
    Jpeg,
    /// PDF document
    Pdf,
    /// MHTML archive (page + resources)
    Mhtml,
    /// Full HTML source
    Html,
    /// WebP image (modern, good compression)
    Webp,
}

impl fmt::Display for CaptureFormat {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Png => write!(f, "png"),
            Self::Jpeg => write!(f, "jpeg"),
            Self::Pdf => write!(f, "pdf"),
            Self::Mhtml => write!(f, "mhtml"),
            Self::Html => write!(f, "html"),
            Self::Webp => write!(f, "webp"),
        }
    }
}

/// Captured page content/screenshot
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CapturedPage {
    /// Format of captured content
    pub format: CaptureFormat,

    /// Raw captured data (PNG bytes, PDF bytes, HTML string, etc.)
    pub data: Vec<u8>,

    /// MIME type
    pub mime_type: String,

    /// File size in bytes
    pub size_bytes: usize,

    /// Capture metadata
    pub metadata: CaptureMetadata,
}

impl CapturedPage {
    /// Get captured data as string (for text formats)
    pub fn as_string(&self) -> WebAdapterResult<String> {
        String::from_utf8(self.data.clone())
            .map_err(|e| WebAdapterError::Serialization(e.to_string()))
    }
}

/// Capture metadata
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CaptureMetadata {
    /// Page URL that was captured
    pub url: String,

    /// Page title at time of capture
    pub title: Option<String>,

    /// Viewport width used for capture
    pub viewport_width: u32,

    /// Viewport height used for capture
    pub viewport_height: u32,

    /// Whether full page was captured (vs viewport)
    pub full_page: bool,

    /// Device scale factor (1.0 for normal, 2.0 for retina, etc.)
    pub device_scale_factor: f32,
}

/// Options for page capture/screenshot
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CaptureOptions {
    /// Capture format (default: PNG)
    pub format: CaptureFormat,

    /// Capture full page or just viewport (default: true for PNG/JPEG, false for PDF)
    pub full_page: bool,

    /// Timeout for capture (milliseconds, default: 10000)
    pub timeout_ms: u64,

    /// Quality for JPEG/WebP (0-100, default: 80)
    pub quality: Option<u8>,

    /// Omit background (PNG only, default: false)
    pub omit_background: bool,

    /// Device scale factor (default: 1.0)
    pub device_scale_factor: Option<f32>,

    /// Wait delay before capture (milliseconds, default: 0)
    pub delay_ms: u64,

    /// JavaScript to execute before capture
    pub execute_js: Option<String>,
}

impl Default for CaptureOptions {
    fn default() -> Self {
        Self {
            format: CaptureFormat::Png,
            full_page: true,
            timeout_ms: 10000,
            quality: Some(80),
            omit_background: false,
            device_scale_factor: None,
            delay_ms: 0,
            execute_js: None,
        }
    }
}

impl CaptureOptions {
    /// Set capture format
    pub fn format(mut self, format: CaptureFormat) -> Self {
        self.format = format;
        self
    }

    /// Set full page capture
    pub fn full_page(mut self, full: bool) -> Self {
        self.full_page = full;
        self
    }

    /// Set quality (for JPEG/WebP)
    pub fn quality(mut self, quality: u8) -> Self {
        self.quality = Some(quality.min(100));
        self
    }

    /// Set timeout
    pub fn timeout_ms(mut self, timeout: u64) -> Self {
        self.timeout_ms = timeout;
        self
    }
}

// =============================================================================
// TRAIT DEFINITION
// =============================================================================

/// Web browser adapter trait for reasonkit-core
///
/// Provides abstraction for browser automation and content extraction.
/// Implementations can use MCP servers, HTTP binding, FFI, or other mechanisms
/// to communicate with reasonkit-web.
///
/// # Implementing the Trait
///
/// - **McpWebAdapter**: Uses MCP stdio server (local or remote)
/// - **HttpWebAdapter**: Uses HTTP JSON-RPC binding
/// - **LocalWebAdapter**: Direct FFI binding for same-process usage
///
/// # Connection Lifecycle
///
/// 1. Create adapter with configuration
/// 2. Call `connect()` to establish connection
/// 3. Use navigation/extraction/capture methods
/// 4. Call `disconnect()` when done
///
/// Implementations MUST handle reconnection automatically on transient failures.
#[async_trait]
pub trait WebBrowserAdapter: Send + Sync {
    // -------------------------------------------------------------------------
    // LIFECYCLE
    // -------------------------------------------------------------------------

    /// Initialize and connect to the web browser service
    ///
    /// # Errors
    ///
    /// Returns `WebAdapterError::NotConnected` if service is unavailable.
    ///
    /// # Implementation Notes
    ///
    /// - May start a browser process (headless Chrome, etc.)
    /// - May connect to an existing MCP server
    /// - May verify API compatibility
    /// - Should implement connection pooling if needed
    async fn connect(&mut self) -> WebAdapterResult<()>;

    /// Disconnect from web service and cleanup resources
    ///
    /// # Implementation Notes
    ///
    /// - Should close browser processes gracefully
    /// - Should save cache/session state if applicable
    /// - Idempotent (safe to call multiple times)
    async fn disconnect(&mut self) -> WebAdapterResult<()>;

    /// Check if adapter is currently connected
    fn is_connected(&self) -> bool;

    // -------------------------------------------------------------------------
    // NAVIGATION
    // -------------------------------------------------------------------------

    /// Navigate to a URL and return a page handle
    ///
    /// # Arguments
    ///
    /// * `url` - Target URL to navigate to
    /// * `options` - Navigation options (timeout, wait event, etc.)
    ///
    /// # Errors
    ///
    /// Returns:
    /// - `WebAdapterError::InvalidUrl` if URL is malformed
    /// - `WebAdapterError::NavigationTimeout` if timeout exceeded
    /// - `WebAdapterError::NavigationFailed` for other failures (network, SSL, 404, etc.)
    ///
    /// # Implementation Notes
    ///
    /// - MUST validate URL before navigation
    /// - MUST respect timeout_ms in options
    /// - MUST wait for specified event (load, domcontentloaded, etc.)
    /// - SHOULD respect custom headers and user agent if provided
    /// - SHOULD inject JavaScript after load if provided
    /// - SHOULD handle redirects according to options
    /// - MUST return page handle with unique ID and current URL
    async fn navigate(
        &mut self,
        url: &str,
        options: NavigateOptions,
    ) -> WebAdapterResult<PageHandle>;

    /// Go back in browser history
    ///
    /// # Errors
    ///
    /// Returns `WebAdapterError::NavigationFailed` if unable to go back.
    async fn go_back(&mut self) -> WebAdapterResult<PageHandle>;

    /// Go forward in browser history
    ///
    /// # Errors
    ///
    /// Returns `WebAdapterError::NavigationFailed` if unable to go forward.
    async fn go_forward(&mut self) -> WebAdapterResult<PageHandle>;

    /// Reload current page
    ///
    /// # Errors
    ///
    /// Returns `WebAdapterError::NavigationFailed` if unable to reload.
    async fn reload(&mut self) -> WebAdapterResult<PageHandle>;

    // -------------------------------------------------------------------------
    // CONTENT EXTRACTION
    // -------------------------------------------------------------------------

    /// Extract content from a page
    ///
    /// # Arguments
    ///
    /// * `page` - Page handle to extract from
    /// * `options` - Extraction options
    ///
    /// # Errors
    ///
    /// Returns:
    /// - `WebAdapterError::ExtractionFailed` if extraction fails
    /// - `WebAdapterError::InvalidSelector` if custom selector is invalid
    /// - `WebAdapterError::JavaScriptError` if custom JS fails
    ///
    /// # Implementation Notes
    ///
    /// - MUST extract main content text
    /// - SHOULD auto-detect main content area if no selector provided
    /// - SHOULD extract links and images according to options
    /// - SHOULD execute custom JavaScript if provided
    /// - SHOULD detect language if requested
    /// - MUST include confidence score (0.0-1.0)
    /// - SHOULD normalize whitespace
    /// - SHOULD extract metadata (title, description, og tags)
    async fn extract_content(
        &mut self,
        page: &PageHandle,
        options: ExtractOptions,
    ) -> WebAdapterResult<ExtractedContent>;

    /// Execute custom JavaScript on page
    ///
    /// # Arguments
    ///
    /// * `page` - Page to execute on
    /// * `script` - JavaScript code to execute
    ///
    /// # Returns
    ///
    /// Serialized result of JavaScript execution as JSON value
    ///
    /// # Errors
    ///
    /// Returns `WebAdapterError::JavaScriptError` if execution fails.
    ///
    /// # Implementation Notes
    ///
    /// - MUST timeout execution if it takes too long (>30s)
    /// - MUST return JSON-serializable result
    /// - SHOULD return last expression value
    async fn execute_js(
        &mut self,
        page: &PageHandle,
        script: &str,
    ) -> WebAdapterResult<serde_json::Value>;

    /// Get text content using CSS selector
    ///
    /// # Arguments
    ///
    /// * `page` - Page to query
    /// * `selector` - CSS selector
    ///
    /// # Errors
    ///
    /// Returns:
    /// - `WebAdapterError::InvalidSelector` if selector is invalid
    /// - `WebAdapterError::ExtractionFailed` if element not found
    async fn get_text(&mut self, page: &PageHandle, selector: &str) -> WebAdapterResult<String>;

    // -------------------------------------------------------------------------
    // SCREENSHOTS & CAPTURE
    // -------------------------------------------------------------------------

    /// Capture page as screenshot or document
    ///
    /// # Arguments
    ///
    /// * `page` - Page to capture
    /// * `options` - Capture options (format, quality, etc.)
    ///
    /// # Errors
    ///
    /// Returns:
    /// - `WebAdapterError::CaptureFailed` if capture fails
    /// - `WebAdapterError::UnsupportedFormat` if format not supported
    ///
    /// # Implementation Notes
    ///
    /// - MUST support PNG, JPEG, PDF formats at minimum
    /// - MAY support MHTML, WebP if available
    /// - MUST respect quality setting for JPEG/WebP
    /// - MUST capture full page or viewport according to options
    /// - SHOULD wait for delay_ms before capturing
    /// - SHOULD execute custom JavaScript before capture if provided
    /// - MUST include capture metadata (viewport size, scale factor, etc.)
    async fn capture_screenshot(
        &mut self,
        page: &PageHandle,
        options: CaptureOptions,
    ) -> WebAdapterResult<CapturedPage>;

    // -------------------------------------------------------------------------
    // DIAGNOSTICS
    // -------------------------------------------------------------------------

    /// Get connection status and diagnostics
    ///
    /// # Returns
    ///
    /// JSON object with connection info, statistics, etc.
    fn diagnostics(&self) -> serde_json::Value;

    /// Get adapter name (for logging/debugging)
    fn name(&self) -> &str;

    /// Get adapter version
    fn version(&self) -> &str;
}

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

    #[test]
    fn test_navigate_options_default() {
        let opts = NavigateOptions::default();
        assert_eq!(opts.timeout_ms, 30000);
        assert_eq!(opts.wait_until, NavigateWaitEvent::Load);
        assert!(opts.follow_redirects);
    }

    #[test]
    fn test_capture_options_builder() {
        let opts = CaptureOptions::default()
            .format(CaptureFormat::Jpeg)
            .quality(90)
            .full_page(false);

        assert_eq!(opts.format, CaptureFormat::Jpeg);
        assert_eq!(opts.quality, Some(90));
        assert!(!opts.full_page);
    }

    #[test]
    fn test_capture_format_display() {
        assert_eq!(CaptureFormat::Png.to_string(), "png");
        assert_eq!(CaptureFormat::Jpeg.to_string(), "jpeg");
        assert_eq!(CaptureFormat::Pdf.to_string(), "pdf");
    }

    #[test]
    fn test_page_handle_display() {
        let page = PageHandle {
            id: "page-1".to_string(),
            url: "https://example.com".to_string(),
            title: "Example".to_string(),
            is_active: true,
        };

        assert_eq!(page.to_string(), "Page(page-1: https://example.com)");
    }

    #[test]
    fn test_extract_options_default() {
        let opts = ExtractOptions::default();
        assert!(opts.extract_links);
        assert!(!opts.extract_images);
        assert!(!opts.extract_structured_data);
        assert!(opts.remove_scripts);
    }

    #[test]
    fn test_content_metadata_default() {
        let meta = ContentMetadata::default();
        assert!(meta.title.is_none());
        assert!(meta.custom_meta.is_empty());
    }

    #[test]
    fn test_navigate_wait_event_display() {
        assert_eq!(NavigateWaitEvent::Load.to_string(), "load");
        assert_eq!(
            NavigateWaitEvent::DomContentLoaded.to_string(),
            "domcontentloaded"
        );
        assert_eq!(NavigateWaitEvent::NetworkIdle.to_string(), "networkidle");
    }

    #[test]
    fn test_quality_clamping() {
        let opts = CaptureOptions::default().quality(150);
        assert_eq!(opts.quality, Some(100));
    }

    #[test]
    fn test_capture_page_as_string() {
        let page = CapturedPage {
            format: CaptureFormat::Html,
            data: "<html>test</html>".as_bytes().to_vec(),
            mime_type: "text/html".to_string(),
            size_bytes: 17,
            metadata: CaptureMetadata {
                url: "https://example.com".to_string(),
                title: None,
                viewport_width: 1024,
                viewport_height: 768,
                full_page: false,
                device_scale_factor: 1.0,
            },
        };

        assert!(page.as_string().is_ok());
        assert_eq!(page.as_string().unwrap(), "<html>test</html>");
    }
}