Skip to main content

oxi_agent/tools/browse/
engine.rs

1//! Browser engine abstraction layer.
2
3#![allow(missing_docs)]
4//!
5//! Defines the core traits (`BrowserEngine`, `BrowserTab`) and shared
6//! types that all browser tools depend on. These traits are always compiled
7//! (no feature gates) so tools can use them regardless of the backend.
8//!
9//! Actual backend implementations (e.g. oxibrowser-core) are behind
10//! `#[cfg(feature = "native-browser")]` in `oxibrowser_backend.rs`.
11
12use async_trait::async_trait;
13use parking_lot::Mutex;
14use serde::{Deserialize, Serialize};
15use serde_json::Value;
16use std::collections::HashMap;
17use std::sync::Arc;
18
19/// Errors that can occur during browser operations.
20#[derive(Debug, thiserror::Error)]
21pub enum BrowserError {
22    #[error("navigation failed: {0}")]
23    Navigation(String),
24    #[error("element not found: {0}")]
25    ElementNotFound(String),
26    #[error("timeout: {0}")]
27    Timeout(String),
28    #[error("evaluation error: {0}")]
29    Evaluation(String),
30    #[error("screenshot failed: {0}")]
31    Screenshot(String),
32    #[error("tab closed: {0}")]
33    TabClosed(String),
34    #[error("browser error: {0}")]
35    Backend(String),
36    #[error("no active session — call 'open' first")]
37    NoActiveSession,
38}
39
40impl From<BrowserError> for crate::tools::ToolError {
41    fn from(e: BrowserError) -> Self {
42        e.to_string()
43    }
44}
45
46/// Shared page content returned by `goto` and `content` methods.
47#[derive(Debug, Clone, Serialize, Deserialize)]
48pub struct PageContent {
49    /// Final URL after redirects.
50    pub url: String,
51    /// Page title.
52    pub title: String,
53    /// HTTP status code.
54    pub status: u16,
55    /// Rendered page content as markdown.
56    pub markdown: String,
57    /// Raw HTML body.
58    #[serde(default)]
59    pub html: String,
60}
61
62impl PageContent {
63    /// Create an empty page (for mock / fallback).
64    pub fn empty() -> Self {
65        Self {
66            url: String::new(),
67            title: String::new(),
68            status: 0,
69            markdown: String::new(),
70            html: String::new(),
71        }
72    }
73}
74
75/// A single link on a page.
76#[derive(Debug, Clone, Serialize, Deserialize)]
77pub struct LinkInfo {
78    #[allow(missing_docs)]
79    pub text: String,
80    #[allow(missing_docs)]
81    pub href: String,
82}
83
84/// A single element matched by a CSS selector.
85#[derive(Debug, Clone, Serialize, Deserialize)]
86pub struct ElementInfo {
87    #[allow(missing_docs)]
88    pub tag: String,
89    #[allow(missing_docs)]
90    pub text: String,
91    #[serde(default)]
92    #[allow(missing_docs)]
93    pub attributes: HashMap<String, String>,
94}
95
96// ── BrowserTab trait ──────────────────────────────────────────────────────────
97
98/// Operations available on a single browser tab.
99///
100/// Implementors handle their own async runtime; this trait only
101/// defines the interface contract.
102#[async_trait]
103pub trait BrowserTab: Send + Sync {
104    /// Navigate to `url` and return page content.
105    async fn goto(&self, url: &str) -> Result<PageContent, BrowserError>;
106
107    /// Click an element matching `selector`.
108    async fn click(&self, selector: &str) -> Result<(), BrowserError>;
109
110    /// Type text into an element matching `selector`.
111    async fn type_(&self, selector: &str, text: &str) -> Result<(), BrowserError>;
112
113    /// Fill (set value of) an element matching `selector`.
114    async fn fill(&self, selector: &str, value: &str) -> Result<(), BrowserError>;
115
116    /// Press a keyboard combo (e.g. `"Enter"`, `"Control+c"`).
117    async fn press(&self, combo: &str) -> Result<(), BrowserError>;
118
119    /// Wait for an element matching `selector` to appear.
120    async fn wait_for(&self, selector: &str, timeout_ms: u64) -> Result<(), BrowserError>;
121
122    /// Get the current page content (markdown + html).
123    async fn content(&self) -> Result<PageContent, BrowserError>;
124
125    /// Get text content of all elements matching `selector`.
126    async fn query_all(&self, selector: &str) -> Result<Vec<String>, BrowserError>;
127
128    /// Evaluate a JavaScript expression and return the JSON result.
129    async fn evaluate(&self, js: &str) -> Result<Value, BrowserError>;
130
131    /// Capture a screenshot and return PNG bytes.
132    async fn screenshot(&self, width: u32) -> Result<Vec<u8>, BrowserError>;
133
134    /// Close this tab.
135    async fn close(&self) -> Result<(), BrowserError>;
136
137    /// Navigate back in history. Returns the rendered page content.
138    async fn back(&self) -> Result<PageContent, BrowserError>;
139
140    /// Navigate forward in history. Returns the rendered page content.
141    async fn forward(&self) -> Result<PageContent, BrowserError>;
142
143    /// Reload the current page. Returns the rendered page content.
144    async fn reload(&self) -> Result<PageContent, BrowserError>;
145
146    /// Select an option in a `<select>` element.
147    async fn select_option(&self, selector: &str, value: &str) -> Result<(), BrowserError>;
148
149    /// Check a checkbox or radio input.
150    async fn check(&self, selector: &str) -> Result<(), BrowserError>;
151
152    /// Uncheck a checkbox or radio input.
153    async fn uncheck(&self, selector: &str) -> Result<(), BrowserError>;
154
155    // ── Advanced interaction ───────────────────────────────────
156
157    /// Clear the value of an input element.
158    async fn clear(&self, selector: &str) -> Result<(), BrowserError> {
159        self.fill(selector, "").await
160    }
161
162    /// Hover over an element.
163    async fn hover(&self, selector: &str) -> Result<(), BrowserError> {
164        let sel = serde_json::to_string(selector).unwrap_or_default();
165        let js = format!(
166            r#"(function() {{ var el = document.querySelector({sel}); if (!el) return null; el.dispatchEvent(new MouseEvent('mouseover', {{bubbles:true}})); return el.tagName; }})()"#
167        );
168        self.evaluate(&js).await.map(|_| ())
169    }
170
171    /// Double-click an element.
172    async fn double_click(&self, selector: &str) -> Result<(), BrowserError> {
173        let sel = serde_json::to_string(selector).unwrap_or_default();
174        let js = format!(
175            r#"(function() {{ var el = document.querySelector({sel}); if (!el) return null; el.dispatchEvent(new MouseEvent('dblclick', {{bubbles:true}})); return el.tagName; }})()"#
176        );
177        self.evaluate(&js).await.map(|_| ())
178    }
179
180    /// Right-click (context menu) an element.
181    async fn right_click(&self, selector: &str) -> Result<(), BrowserError> {
182        let sel = serde_json::to_string(selector).unwrap_or_default();
183        let js = format!(
184            r#"(function() {{ var el = document.querySelector({sel}); if (!el) return null; el.dispatchEvent(new MouseEvent('contextmenu', {{bubbles:true, button:2}})); return el.tagName; }})()"#
185        );
186        self.evaluate(&js).await.map(|_| ())
187    }
188
189    /// Scroll the page by delta pixels.
190    async fn scroll(&self, delta_x: f64, delta_y: f64) -> Result<(), BrowserError> {
191        let js = format!("window.scrollBy({}, {})", delta_x, delta_y);
192        self.evaluate(&js).await.map(|_| ())
193    }
194
195    /// Scroll an element into view.
196    async fn scroll_into_view(&self, selector: &str) -> Result<(), BrowserError> {
197        let sel = serde_json::to_string(selector).unwrap_or_default();
198        let js = format!(
199            r#"(function() {{ var el = document.querySelector({sel}); if (!el) return null; el.scrollIntoView(); return el.tagName; }})()"#
200        );
201        self.evaluate(&js).await.map(|_| ())
202    }
203
204    /// Drag from one element to another.
205    async fn drag(&self, from_selector: &str, to_selector: &str) -> Result<(), BrowserError> {
206        let from_sel = serde_json::to_string(from_selector).unwrap_or_default();
207        let to_sel = serde_json::to_string(to_selector).unwrap_or_default();
208        let js = format!(
209            r#"(function() {{ var src = document.querySelector({from_sel}); var dst = document.querySelector({to_sel}); if (!src || !dst) return null; src.dispatchEvent(new DragEvent('dragstart', {{bubbles:true}})); dst.dispatchEvent(new DragEvent('drop', {{bubbles:true}})); src.dispatchEvent(new DragEvent('dragend', {{bubbles:true}})); return 'ok'; }})()"#
210        );
211        self.evaluate(&js).await.map(|_| ())
212    }
213
214    /// Upload a file to a file input element.
215    async fn upload_file(&self, selector: &str, path: &str) -> Result<(), BrowserError> {
216        let sel = serde_json::to_string(selector).unwrap_or_default();
217        let p = serde_json::to_string(path).unwrap_or_default();
218        let js = format!(
219            r#"(function() {{ var el = document.querySelector({sel}); if (!el || el.type !== 'file') return null; if (typeof DataTransfer === 'undefined') return null; var dt = new DataTransfer(); var f = new File([], {p}.split('/').pop()); dt.items.add(f); el.files = dt.files; el.dispatchEvent(new Event('change', {{bubbles:true}})); return el.tagName; }})()"#
220        );
221        self.evaluate(&js).await.map(|_| ())
222    }
223
224    /// Get the value or text content of an element.
225    async fn get_value(&self, selector: &str) -> Result<String, BrowserError> {
226        let sel = serde_json::to_string(selector).unwrap_or_default();
227        let js = format!(
228            r#"(function() {{ var el = document.querySelector({sel}); if (!el) return null; return (el.value !== undefined ? el.value : el.textContent) || ''; }})()"#
229        );
230        let val = self.evaluate(&js).await?;
231        Ok(val.as_str().unwrap_or("").to_string())
232    }
233
234    /// Evaluate JS that may return a promise; awaits by default.
235    async fn evaluate_await(&self, js: &str) -> Result<Value, BrowserError> {
236        self.evaluate(js).await
237    }
238
239    /// Returns `true` if this tab has been closed.
240    fn is_closed(&self) -> bool {
241        false
242    }
243
244    /// Return this tab's unique ID, if the backend supports it.
245    /// Defaults to `Uuid::nil()` for backends that don't track tab identity.
246    fn tab_id(&self) -> uuid::Uuid {
247        uuid::Uuid::nil()
248    }
249
250    /// Support downcasting for backend-specific access.
251    fn as_any(&self) -> &dyn std::any::Any {
252        // Default: no concrete type info.
253        &std::marker::PhantomData::<()>
254    }
255
256    /// Clear any registered progress callback for this tab.
257    /// Defaults to no-op — only backends with callback registries override.
258    fn clear_progress_callback(&self) {}
259
260    /// Register a structured browse progress callback for this tab.
261    /// Defaults to no-op — only backends with browse callback support override.
262    fn set_browse_progress_callback(&self, _cb: BrowseProgressCallback) {}
263}
264
265// ── BrowserEngine trait ───────────────────────────────────────────────────────
266
267/// Factory for opening and managing browser tabs.
268///
269/// This trait is implemented by backends (e.g. oxibrowser-core) and
270/// consumed by the tool layer via `Arc<dyn BrowserEngine>`.
271#[async_trait]
272pub trait BrowserEngine: Send + Sync {
273    /// Fetch a URL and return page content (no tab management).
274    async fn fetch(&self, url: &str) -> Result<PageContent, BrowserError> {
275        let tab = self.new_tab().await?;
276        let content = tab.goto(url).await;
277        let _ = tab.close().await;
278        content
279    }
280
281    /// Open a new browser tab and return it.
282    async fn new_tab(&self) -> Result<Box<dyn BrowserTab>, BrowserError>;
283
284    /// Close all open tabs and shut down the browser instance.
285    async fn close(&self) -> Result<(), BrowserError>;
286
287    /// Returns `true` if the browser is still alive.
288    async fn is_alive(&self) -> bool;
289
290    /// Access the engine's per-tab callback registry.
291    ///
292    /// Tools (e.g. `BrowseTool`) register per-tab callbacks keyed by
293    /// `tab_id`. The backend's background event-drain task extracts
294    /// `tab_id` from each `BrowserEvent` and routes it to the correct
295    /// callback. Backends without event streaming return an empty
296    /// registry — `set`/`invoke` become no-ops.
297    ///
298    /// Default implementation returns a fresh empty registry.
299    fn callback_registry(&self) -> Arc<TabCallbackRegistry> {
300        Arc::new(TabCallbackRegistry::new())
301    }
302}
303
304// ── BrowseProgress ──────────────────────────────────────────────────────
305
306/// Structured progress event for browser tool execution.
307///
308/// Converted from `oxibrowser_core::BrowserEvent` in the backend's drain
309/// task. Carries structured data that would be lost if flattened to a string
310/// via `short_label()`. The agent loop's browse callback receives these and
311/// enriches `ToolCallContext` with the result fields.
312///
313/// Defined here (not in `oxibrowser_backend.rs`) so the type is always
314/// available — no feature gate needed.
315#[derive(Debug, Clone, Serialize, Deserialize)]
316#[serde(tag = "kind", rename_all = "snake_case")]
317#[non_exhaustive]
318pub enum BrowseProgress {
319    /// A navigation has begun.
320    NavigationStarted {
321        /// URL being navigated to (pre-redirect).
322        url: String,
323    },
324
325    /// Waiting for a CSS selector to appear.
326    WaitingForSelector {
327        /// CSS selector being awaited.
328        selector: String,
329        /// Maximum wait time in milliseconds.
330        timeout_ms: u64,
331    },
332
333    /// Page has finished loading and JS has executed.
334    /// This is the key event — carries rich structured data.
335    DocumentReady {
336        /// Final URL after redirects.
337        url: String,
338        /// Page `<title>`.
339        title: String,
340        /// HTTP status code.
341        status: u16,
342        /// Size of the HTML body in bytes.
343        bytes: u64,
344        /// Wall-clock duration of the page load, in milliseconds.
345        duration_ms: u64,
346    },
347
348    /// A screenshot has been captured.
349    ScreenshotCaptured {
350        /// Size of the PNG payload in bytes.
351        bytes: usize,
352        /// Viewport width the screenshot was rendered at.
353        width: u32,
354        /// Render duration in milliseconds.
355        duration_ms: u64,
356    },
357
358    /// Navigation failed.
359    NavigationFailed {
360        /// URL that failed.
361        url: String,
362        /// Error description.
363        error: String,
364    },
365}
366
367// ── BrowseProgressCallback ──────────────────────────────────────────────
368
369/// Callback type for structured browse progress events.
370pub type BrowseProgressCallback = Arc<dyn Fn(BrowseProgress) + Send + Sync>;
371
372// ── TabCallbackRegistry ──────────────────────────────────────────────────
373
374/// Per-`tab_id` callback entry. Groups the string progress callback
375/// and the structured browse callback for a single tab. Both share
376/// the same lifecycle — `clear` removes both at once.
377#[derive(Default)]
378struct TabCallbacks {
379    /// String progress callback (`partial_result` text).
380    progress: Option<crate::tools::ProgressCallback>,
381    /// Structured browse progress callback (context enrichment).
382    browse: Option<BrowseProgressCallback>,
383}
384
385/// Per-`tab_id` callback registry for browser event routing.
386///
387/// Each `BrowseTool` invocation opens its own tab and registers a callback
388/// keyed by the tab's `tab_id`. The engine's background event-drain task
389/// extracts `tab_id` from each `BrowserEvent` and routes it to the correct
390/// callback. Multiple tabs can be active concurrently — each receives only
391/// its own events.
392///
393/// Tabs that have no registered callback (e.g. opened outside of a tool
394/// call) are silently ignored — `invoke` is a no-op for unknown tab IDs.
395pub struct TabCallbackRegistry {
396    entries: Mutex<HashMap<uuid::Uuid, TabCallbacks>>,
397}
398
399impl Default for TabCallbackRegistry {
400    fn default() -> Self {
401        Self::new()
402    }
403}
404
405impl TabCallbackRegistry {
406    /// Create an empty registry.
407    pub fn new() -> Self {
408        Self {
409            entries: Mutex::new(HashMap::new()),
410        }
411    }
412
413    /// Register a string progress callback for the given `tab_id`.
414    pub fn set(&self, tab_id: uuid::Uuid, cb: crate::tools::ProgressCallback) {
415        self.entries.lock().entry(tab_id).or_default().progress = Some(cb);
416    }
417
418    /// Register a structured browse progress callback for the given tab.
419    pub fn set_browse(&self, tab_id: uuid::Uuid, cb: BrowseProgressCallback) {
420        self.entries.lock().entry(tab_id).or_default().browse = Some(cb);
421    }
422
423    /// Remove **all** callbacks for `tab_id`. Called when the tab is closed.
424    pub fn clear(&self, tab_id: &uuid::Uuid) {
425        self.entries.lock().remove(tab_id);
426    }
427
428    /// Invoke the string progress callback for `tab_id`, if registered.
429    pub fn invoke(&self, tab_id: &uuid::Uuid, msg: String) {
430        if let Some(entry) = self.entries.lock().get(tab_id) {
431            if let Some(ref cb) = entry.progress {
432                cb(msg);
433            }
434        }
435    }
436
437    /// Invoke the browse progress callback for `tab_id`, if registered.
438    pub fn invoke_browse(&self, tab_id: &uuid::Uuid, progress: BrowseProgress) {
439        if let Some(entry) = self.entries.lock().get(tab_id) {
440            if let Some(ref cb) = entry.browse {
441                cb(progress);
442            }
443        }
444    }
445
446    /// Whether a string callback is registered for the given `tab_id`.
447    pub fn is_set(&self, tab_id: &uuid::Uuid) -> bool {
448        self.entries.lock().contains_key(tab_id)
449    }
450
451    /// Number of currently registered tabs.
452    pub fn len(&self) -> usize {
453        self.entries.lock().len()
454    }
455
456    /// Returns `true` if no tabs have registered callbacks.
457    pub fn is_empty(&self) -> bool {
458        self.entries.lock().is_empty()
459    }
460}
461
462#[cfg(test)]
463mod tests {
464    use super::*;
465    use std::sync::atomic::{AtomicUsize, Ordering};
466
467    #[test]
468    fn tab_callback_registry_default_is_empty() {
469        let reg = TabCallbackRegistry::new();
470        assert!(reg.is_empty());
471        assert_eq!(reg.len(), 0);
472        // invoke on empty registry is a silent no-op
473        let nil = uuid::Uuid::nil();
474        reg.invoke(&nil, "should be dropped".into());
475    }
476
477    #[test]
478    fn tab_callback_registry_set_and_invoke() {
479        let reg = TabCallbackRegistry::new();
480        let tab_a = uuid::Uuid::new_v4();
481        let tab_b = uuid::Uuid::new_v4();
482        let count = Arc::new(AtomicUsize::new(0));
483        let count_clone = Arc::clone(&count);
484        reg.set(
485            tab_a,
486            oxi_ai::progress_callback(move |msg: String| {
487                assert_eq!(msg, "hello");
488                count_clone.fetch_add(1, Ordering::SeqCst);
489            }),
490        );
491        assert!(reg.is_set(&tab_a));
492        assert!(!reg.is_set(&tab_b));
493
494        reg.invoke(&tab_a, "hello".into());
495        reg.invoke(&tab_a, "hello".into());
496        // invoke for unregistered tab_b is a no-op
497        reg.invoke(&tab_b, "hello".into());
498        assert_eq!(count.load(Ordering::SeqCst), 2);
499    }
500
501    #[test]
502    fn tab_callback_registry_set_per_tab_isolation() {
503        let reg = TabCallbackRegistry::new();
504        let tab_a = uuid::Uuid::new_v4();
505        let tab_b = uuid::Uuid::new_v4();
506        let count_a = Arc::new(AtomicUsize::new(0));
507        let count_b = Arc::new(AtomicUsize::new(0));
508
509        let ca = Arc::clone(&count_a);
510        reg.set(
511            tab_a,
512            oxi_ai::progress_callback(move |_| {
513                ca.fetch_add(1, Ordering::SeqCst);
514            }),
515        );
516        let cb_clone = Arc::clone(&count_b);
517        reg.set(
518            tab_b,
519            oxi_ai::progress_callback(move |_| {
520                cb_clone.fetch_add(1, Ordering::SeqCst);
521            }),
522        );
523
524        reg.invoke(&tab_a, "event".into());
525        assert_eq!(count_a.load(Ordering::SeqCst), 1);
526        assert_eq!(count_b.load(Ordering::SeqCst), 0);
527
528        reg.invoke(&tab_b, "event".into());
529        assert_eq!(count_a.load(Ordering::SeqCst), 1);
530        assert_eq!(count_b.load(Ordering::SeqCst), 1);
531    }
532
533    #[test]
534    fn tab_callback_registry_clear() {
535        let reg = TabCallbackRegistry::new();
536        let tab_a = uuid::Uuid::new_v4();
537        let count = Arc::new(AtomicUsize::new(0));
538        let c = Arc::clone(&count);
539        reg.set(
540            tab_a,
541            oxi_ai::progress_callback(move |_| {
542                c.fetch_add(1, Ordering::SeqCst);
543            }),
544        );
545        reg.invoke(&tab_a, "x".into());
546        assert_eq!(count.load(Ordering::SeqCst), 1);
547
548        reg.clear(&tab_a);
549        assert!(!reg.is_set(&tab_a));
550        reg.invoke(&tab_a, "y".into());
551        assert_eq!(
552            count.load(Ordering::SeqCst),
553            1,
554            "invoke after clear is no-op"
555        );
556    }
557
558    #[test]
559    fn page_content_empty() {
560        let p = PageContent::empty();
561        assert!(p.url.is_empty());
562        assert_eq!(p.status, 0);
563    }
564
565    #[test]
566    fn browser_error_display() {
567        let e = BrowserError::Navigation("connection refused".into());
568        assert!(e.to_string().contains("navigation failed"));
569    }
570
571    #[test]
572    fn link_info_serde() {
573        let link = LinkInfo {
574            text: "Example".into(),
575            href: "https://example.com".into(),
576        };
577        let json = serde_json::to_string(&link).unwrap();
578        let restored: LinkInfo = serde_json::from_str(&json).unwrap();
579        assert_eq!(restored.text, "Example");
580        assert_eq!(restored.href, "https://example.com");
581    }
582
583    #[test]
584    fn element_info_serde() {
585        let elem = ElementInfo {
586            tag: "DIV".into(),
587            text: "Hello".into(),
588            attributes: [("class".into(), "item".into())].into(),
589        };
590        let json = serde_json::to_string(&elem).unwrap();
591        assert!(json.contains("DIV"));
592        assert!(json.contains("Hello"));
593    }
594
595    #[test]
596    fn browser_error_no_active_session() {
597        let e = BrowserError::NoActiveSession;
598        assert!(e.to_string().contains("no active session"));
599    }
600
601    // ── Browse progress callback tests ──────────────────────────
602
603    #[test]
604    fn tab_callback_registry_browse_set_and_invoke() {
605        let reg = TabCallbackRegistry::new();
606        let tab = uuid::Uuid::new_v4();
607        let received: Arc<std::sync::Mutex<Vec<BrowseProgress>>> =
608            Arc::new(std::sync::Mutex::new(Vec::new()));
609        let r = Arc::clone(&received);
610        reg.set_browse(
611            tab,
612            Arc::new(move |bp: BrowseProgress| {
613                r.lock().unwrap().push(bp);
614            }),
615        );
616
617        let progress = BrowseProgress::DocumentReady {
618            url: "https://example.com".into(),
619            title: "Example".into(),
620            status: 200,
621            bytes: 1024,
622            duration_ms: 500,
623        };
624        reg.invoke_browse(&tab, progress.clone());
625
626        let events = received.lock().unwrap();
627        assert_eq!(events.len(), 1);
628        assert!(matches!(
629            &events[0],
630            BrowseProgress::DocumentReady { status: 200, .. }
631        ));
632    }
633
634    #[test]
635    fn tab_callback_registry_browse_clear_removes_both() {
636        let reg = TabCallbackRegistry::new();
637        let tab = uuid::Uuid::new_v4();
638
639        // Register both types
640        reg.set(tab, oxi_ai::progress_callback(move |_| {}));
641        reg.set_browse(tab, Arc::new(move |_: BrowseProgress| {}));
642        assert!(reg.is_set(&tab));
643
644        // clear removes both
645        reg.clear(&tab);
646        assert!(!reg.is_set(&tab));
647        assert!(reg.is_empty());
648    }
649
650    #[test]
651    fn tab_callback_registry_browse_isolation_per_tab() {
652        let reg = TabCallbackRegistry::new();
653        let tab_a = uuid::Uuid::new_v4();
654        let tab_b = uuid::Uuid::new_v4();
655
656        let count_a = Arc::new(AtomicUsize::new(0));
657        let count_b = Arc::new(AtomicUsize::new(0));
658
659        let ca = Arc::clone(&count_a);
660        reg.set_browse(
661            tab_a,
662            Arc::new(move |_: BrowseProgress| {
663                ca.fetch_add(1, Ordering::SeqCst);
664            }),
665        );
666        let cb2 = Arc::clone(&count_b);
667        reg.set_browse(
668            tab_b,
669            Arc::new(move |_: BrowseProgress| {
670                cb2.fetch_add(1, Ordering::SeqCst);
671            }),
672        );
673
674        let doc_ready = BrowseProgress::DocumentReady {
675            url: "https://example.com".into(),
676            title: "Example".into(),
677            status: 200,
678            bytes: 1024,
679            duration_ms: 100,
680        };
681        reg.invoke_browse(&tab_a, doc_ready.clone());
682        assert_eq!(count_a.load(Ordering::SeqCst), 1);
683        assert_eq!(count_b.load(Ordering::SeqCst), 0);
684
685        reg.invoke_browse(&tab_b, doc_ready);
686        assert_eq!(count_a.load(Ordering::SeqCst), 1);
687        assert_eq!(count_b.load(Ordering::SeqCst), 1);
688    }
689
690    #[test]
691    fn browse_progress_serde_roundtrip() {
692        let variants = vec![
693            BrowseProgress::NavigationStarted {
694                url: "https://example.com".into(),
695            },
696            BrowseProgress::WaitingForSelector {
697                selector: ".content".into(),
698                timeout_ms: 5000,
699            },
700            BrowseProgress::DocumentReady {
701                url: "https://example.com/page".into(),
702                title: "Test Page".into(),
703                status: 200,
704                bytes: 4096,
705                duration_ms: 1234,
706            },
707            BrowseProgress::ScreenshotCaptured {
708                bytes: 8192,
709                width: 1280,
710                duration_ms: 200,
711            },
712            BrowseProgress::NavigationFailed {
713                url: "https://fail.example.com".into(),
714                error: "connection refused".into(),
715            },
716        ];
717
718        for bp in &variants {
719            let json = serde_json::to_string(bp).unwrap();
720            let restored: BrowseProgress = serde_json::from_str(&json).unwrap();
721            let json2 = serde_json::to_string(&restored).unwrap();
722            assert_eq!(json, json2, "roundtrip failed for {:?}", bp);
723        }
724    }
725}