use browsing::browser::{Browser, BrowserProfile};
use browsing::dom::serializer::SimplifiedNode;
use browsing::dom::service::DomService;
use browsing::dom::views::{
DOMElement, DOMRect, EnhancedAXNode, EnhancedDOMTreeNode, NodeType, SerializedDOMState,
};
use serde_json::json;
use std::collections::HashMap;
#[tokio::test]
async fn test_dom_service_creation() {
let dom_service = DomService::new();
}
#[tokio::test]
async fn test_dom_service_configuration() {
let dom_service = DomService::new()
.with_browser(std::sync::Arc::new(Browser::new(BrowserProfile::default())))
.with_cdp_client(
std::sync::Arc::new(browsing::browser::cdp::CdpClient::new(
"ws://localhost:9222".to_string(),
)),
"test-session".to_string(),
)
.with_target_id("test-target".to_string());
}
#[test]
fn test_dom_node_creation() {
let node = EnhancedDOMTreeNode::new(
1,
1,
NodeType::ElementNode,
"DIV".to_string(),
"".to_string(),
"test-target".to_string(),
);
assert_eq!(node.node_id, 1);
assert_eq!(node.backend_node_id, 1);
assert_eq!(node.node_type, NodeType::ElementNode);
assert_eq!(node.node_name, "DIV");
assert_eq!(node.target_id, "test-target");
}
#[test]
fn test_dom_node_attributes() {
let mut attrs = HashMap::new();
attrs.insert("id".to_string(), "test-id".to_string());
attrs.insert("class".to_string(), "test-class".to_string());
let mut node = EnhancedDOMTreeNode::new(
1,
1,
NodeType::ElementNode,
"DIV".to_string(),
"test content".to_string(),
"test-target".to_string(),
);
node.attributes = attrs;
assert_eq!(node.attributes.get("id"), Some(&"test-id".to_string()));
assert_eq!(
node.attributes.get("class"),
Some(&"test-class".to_string())
);
}
#[test]
fn test_simplified_node_creation() {
let mut attrs = HashMap::new();
attrs.insert("role".to_string(), "button".to_string());
let mut enhanced_node = EnhancedDOMTreeNode::new(
1,
1,
NodeType::ElementNode,
"BUTTON".to_string(),
"Click me".to_string(),
"test-target".to_string(),
);
enhanced_node.attributes = attrs;
let simplified = SimplifiedNode::new(enhanced_node);
assert!(simplified.should_display);
assert!(!simplified.is_interactive);
assert_eq!(simplified.interactive_index, None);
}
#[test]
fn test_simplified_dom_creation() {
let root_node = EnhancedDOMTreeNode::new(
0,
0,
NodeType::DocumentNode,
"#document".to_string(),
"".to_string(),
"test-target".to_string(),
);
let simplified_dom = SimplifiedNode::new(root_node);
assert!(simplified_dom.should_display);
assert!(!simplified_dom.is_interactive);
}
#[test]
fn test_dom_element_serialization() {
let mut attrs = HashMap::new();
attrs.insert("id".to_string(), "test-element".to_string());
let element = DOMElement {
index: 1,
tag: "div".to_string(),
text: Some("Test content".to_string()),
attributes: attrs,
children: vec![],
};
let json_str = serde_json::to_string(&element).unwrap();
let dict: serde_json::Value = serde_json::from_str(&json_str).unwrap();
assert_eq!(dict.get("index"), Some(&json!(1)));
assert_eq!(dict.get("tag"), Some(&json!("div")));
assert_eq!(dict.get("text"), Some(&json!("Test content")));
}
#[test]
fn test_dom_rect_creation() {
let rect = DOMRect::new(10.0, 20.0, 100.0, 200.0);
assert_eq!(rect.x, 10.0);
assert_eq!(rect.y, 20.0);
assert_eq!(rect.width, 100.0);
assert_eq!(rect.height, 200.0);
}
#[test]
fn test_ax_node_creation() {
let mut properties = Vec::new();
properties.push(browsing::dom::views::EnhancedAXProperty {
name: "role".to_string(),
value: Some(json!("button")),
});
let ax_node = EnhancedAXNode {
ax_node_id: "ax-1".to_string(),
ignored: false,
role: Some("button".to_string()),
name: Some("Submit".to_string()),
description: Some("Submit form".to_string()),
properties: Some(properties),
child_ids: None,
};
assert_eq!(ax_node.role, Some("button".to_string()));
assert_eq!(ax_node.name, Some("Submit".to_string()));
assert!(!ax_node.ignored);
}
#[test]
fn test_dom_state_serialization() {
let state = SerializedDOMState {
html: Some("<html><body>Test</body></html>".to_string()),
text: Some("Test content".to_string()),
markdown: Some("# Test".to_string()),
elements: vec![],
selector_map: HashMap::new(),
};
let json_str = serde_json::to_string(&state);
assert!(json_str.is_ok());
let deserialized: Result<SerializedDOMState, _> = serde_json::from_str(&json_str.unwrap());
assert!(deserialized.is_ok());
}
#[test]
fn test_node_type_variants() {
let types = [
NodeType::ElementNode,
NodeType::AttributeNode,
NodeType::TextNode,
NodeType::CdataSectionNode,
NodeType::EntityReferenceNode,
NodeType::EntityNode,
NodeType::ProcessingInstructionNode,
NodeType::CommentNode,
NodeType::DocumentNode,
NodeType::DocumentTypeNode,
NodeType::DocumentFragmentNode,
NodeType::NotationNode,
];
for node_type in types {
let _node = EnhancedDOMTreeNode::new(
1,
1,
node_type,
"TEST".to_string(),
"".to_string(),
"test-target".to_string(),
);
}
}
#[tokio::test]
async fn test_dom_extraction_flow() {
let dom_service = DomService::new();
let _configured_service =
dom_service.with_browser(std::sync::Arc::new(Browser::new(BrowserProfile::default())));
}
#[test]
fn test_dom_node_with_children() {
let mut parent_attrs = HashMap::new();
parent_attrs.insert("class".to_string(), "parent".to_string());
let mut child_attrs = HashMap::new();
child_attrs.insert("class".to_string(), "child".to_string());
let child_node = EnhancedDOMTreeNode::new(
2,
2,
NodeType::ElementNode,
"SPAN".to_string(),
"Child text".to_string(),
"test-target".to_string(),
);
let mut parent_node = EnhancedDOMTreeNode::new(
1,
1,
NodeType::ElementNode,
"DIV".to_string(),
"".to_string(),
"test-target".to_string(),
);
parent_node.attributes = parent_attrs;
parent_node.children_nodes = Some(vec![child_node]);
assert!(parent_node.children_nodes.is_some());
let children = parent_node.children_nodes.as_ref().unwrap();
assert_eq!(children.len(), 1);
assert_eq!(children[0].node_name, "SPAN");
}