#![cfg(all(test, feature = "wasm"))]
use wasm_bindgen::JsValue;
use wasm_bindgen_test::*;
use crate::wasm::{AnonymousFactory as __, Graph, P};
wasm_bindgen_test_configure!(run_in_browser);
fn id_to_js(id: u64) -> JsValue {
JsValue::from(id)
}
#[wasm_bindgen_test]
fn test_graph_create() {
let graph = Graph::new();
assert_eq!(graph.vertex_count(), 0);
assert_eq!(graph.edge_count(), 0);
}
#[wasm_bindgen_test]
fn test_add_vertex() {
let graph = Graph::new();
let props = js_sys::Object::new();
js_sys::Reflect::set(&props, &"name".into(), &"Alice".into()).unwrap();
js_sys::Reflect::set(&props, &"age".into(), &JsValue::from(30i32)).unwrap();
let id = graph.add_vertex("person", props.into()).unwrap();
assert!(id > 0 || id == 0); assert_eq!(graph.vertex_count(), 1);
}
#[wasm_bindgen_test]
fn test_add_edge() {
let graph = Graph::new();
let alice_id = graph.add_vertex("person", JsValue::NULL).unwrap();
let bob_id = graph.add_vertex("person", JsValue::NULL).unwrap();
let edge_id = graph
.add_edge(id_to_js(alice_id), id_to_js(bob_id), "knows", JsValue::NULL)
.unwrap();
assert!(edge_id > 0 || edge_id == 0);
assert_eq!(graph.edge_count(), 1);
}
#[wasm_bindgen_test]
fn test_get_vertex() {
let graph = Graph::new();
let props = js_sys::Object::new();
js_sys::Reflect::set(&props, &"name".into(), &"Alice".into()).unwrap();
let id = graph.add_vertex("person", props.into()).unwrap();
let vertex = graph.get_vertex(id_to_js(id));
assert!(vertex.is_ok());
}
#[wasm_bindgen_test]
fn test_remove_vertex() {
let graph = Graph::new();
let id = graph.add_vertex("person", JsValue::NULL).unwrap();
assert_eq!(graph.vertex_count(), 1);
let removed = graph.remove_vertex(id_to_js(id)).unwrap();
assert!(removed);
assert_eq!(graph.vertex_count(), 0);
}
#[wasm_bindgen_test]
fn test_remove_edge() {
let graph = Graph::new();
let alice = graph.add_vertex("person", JsValue::NULL).unwrap();
let bob = graph.add_vertex("person", JsValue::NULL).unwrap();
let edge_id = graph
.add_edge(id_to_js(alice), id_to_js(bob), "knows", JsValue::NULL)
.unwrap();
assert_eq!(graph.edge_count(), 1);
let removed = graph.remove_edge(id_to_js(edge_id)).unwrap();
assert!(removed);
assert_eq!(graph.edge_count(), 0);
}
#[wasm_bindgen_test]
fn test_v_traversal() {
let graph = Graph::new();
graph.add_vertex("person", JsValue::NULL).unwrap();
graph.add_vertex("person", JsValue::NULL).unwrap();
graph.add_vertex("software", JsValue::NULL).unwrap();
let count = graph.v(None).unwrap().to_count();
assert_eq!(count, 3);
}
#[wasm_bindgen_test]
fn test_has_label_filter() {
let graph = Graph::new();
graph.add_vertex("person", JsValue::NULL).unwrap();
graph.add_vertex("person", JsValue::NULL).unwrap();
graph.add_vertex("software", JsValue::NULL).unwrap();
let count = graph.v(None).unwrap().has_label("person").to_count();
assert_eq!(count, 2);
}
#[wasm_bindgen_test]
fn test_out_navigation() {
let graph = Graph::new();
let alice = graph.add_vertex("person", JsValue::NULL).unwrap();
let bob = graph.add_vertex("person", JsValue::NULL).unwrap();
let charlie = graph.add_vertex("person", JsValue::NULL).unwrap();
graph
.add_edge(id_to_js(alice), id_to_js(bob), "knows", JsValue::NULL)
.unwrap();
graph
.add_edge(id_to_js(alice), id_to_js(charlie), "knows", JsValue::NULL)
.unwrap();
let ids_arr = js_sys::Array::new();
ids_arr.push(&id_to_js(alice));
let count = graph.v(Some(ids_arr)).unwrap().out().to_count();
assert_eq!(count, 2);
}
#[wasm_bindgen_test]
fn test_in_navigation() {
let graph = Graph::new();
let alice = graph.add_vertex("person", JsValue::NULL).unwrap();
let bob = graph.add_vertex("person", JsValue::NULL).unwrap();
graph
.add_edge(id_to_js(alice), id_to_js(bob), "knows", JsValue::NULL)
.unwrap();
let ids_arr = js_sys::Array::new();
ids_arr.push(&id_to_js(bob));
let count = graph.v(Some(ids_arr)).unwrap().in_().to_count();
assert_eq!(count, 1);
}
#[wasm_bindgen_test]
fn test_values_transform() {
let graph = Graph::new();
let props = js_sys::Object::new();
js_sys::Reflect::set(&props, &"name".into(), &"Alice".into()).unwrap();
graph.add_vertex("person", props.into()).unwrap();
let props2 = js_sys::Object::new();
js_sys::Reflect::set(&props2, &"name".into(), &"Bob".into()).unwrap();
graph.add_vertex("person", props2.into()).unwrap();
let result = graph.v(None).unwrap().values("name").to_list().unwrap();
let arr = js_sys::Array::from(&result);
assert_eq!(arr.length(), 2);
}
#[wasm_bindgen_test]
fn test_limit_step() {
let graph = Graph::new();
for _ in 0..10 {
graph.add_vertex("node", JsValue::NULL).unwrap();
}
let count = graph
.v(None)
.unwrap()
.limit(5u64.into())
.unwrap()
.to_count();
assert_eq!(count, 5);
}
#[wasm_bindgen_test]
fn test_dedup_step() {
let graph = Graph::new();
let alice = graph.add_vertex("person", JsValue::NULL).unwrap();
let bob = graph.add_vertex("person", JsValue::NULL).unwrap();
graph
.add_edge(id_to_js(alice), id_to_js(bob), "knows", JsValue::NULL)
.unwrap();
graph
.add_edge(id_to_js(alice), id_to_js(bob), "likes", JsValue::NULL)
.unwrap();
let ids_arr = js_sys::Array::new();
ids_arr.push(&id_to_js(alice));
let without_dedup = graph.v(Some(ids_arr)).unwrap().out().to_count();
assert_eq!(without_dedup, 2);
let ids_arr2 = js_sys::Array::new();
ids_arr2.push(&id_to_js(alice));
let with_dedup = graph.v(Some(ids_arr2)).unwrap().out().dedup().to_count();
assert_eq!(with_dedup, 1);
}
#[wasm_bindgen_test]
fn test_predicate_eq() {
let graph = Graph::new();
let props1 = js_sys::Object::new();
js_sys::Reflect::set(&props1, &"age".into(), &JsValue::from(30i32)).unwrap();
graph.add_vertex("person", props1.into()).unwrap();
let props2 = js_sys::Object::new();
js_sys::Reflect::set(&props2, &"age".into(), &JsValue::from(25i32)).unwrap();
graph.add_vertex("person", props2.into()).unwrap();
let pred = P::eq(JsValue::from(30i32)).unwrap();
let count = graph.v(None).unwrap().has_where("age", pred).to_count();
assert_eq!(count, 1);
}
#[wasm_bindgen_test]
fn test_predicate_gt() {
let graph = Graph::new();
let props1 = js_sys::Object::new();
js_sys::Reflect::set(&props1, &"age".into(), &JsValue::from(30i32)).unwrap();
graph.add_vertex("person", props1.into()).unwrap();
let props2 = js_sys::Object::new();
js_sys::Reflect::set(&props2, &"age".into(), &JsValue::from(25i32)).unwrap();
graph.add_vertex("person", props2.into()).unwrap();
let props3 = js_sys::Object::new();
js_sys::Reflect::set(&props3, &"age".into(), &JsValue::from(35i32)).unwrap();
graph.add_vertex("person", props3.into()).unwrap();
let pred = P::gt(JsValue::from(28i32)).unwrap();
let count = graph.v(None).unwrap().has_where("age", pred).to_count();
assert_eq!(count, 2); }
#[wasm_bindgen_test]
fn test_order_builder() {
let graph = Graph::new();
let props1 = js_sys::Object::new();
js_sys::Reflect::set(&props1, &"name".into(), &"Charlie".into()).unwrap();
graph.add_vertex("person", props1.into()).unwrap();
let props2 = js_sys::Object::new();
js_sys::Reflect::set(&props2, &"name".into(), &"Alice".into()).unwrap();
graph.add_vertex("person", props2.into()).unwrap();
let props3 = js_sys::Object::new();
js_sys::Reflect::set(&props3, &"name".into(), &"Bob".into()).unwrap();
graph.add_vertex("person", props3.into()).unwrap();
let result = graph
.v(None)
.unwrap()
.values("name")
.order()
.by_asc()
.build()
.to_list()
.unwrap();
let arr = js_sys::Array::from(&result);
assert_eq!(arr.length(), 3);
assert_eq!(arr.get(0).as_string().unwrap(), "Alice");
}
#[wasm_bindgen_test]
fn test_group_count_builder() {
let graph = Graph::new();
graph.add_vertex("person", JsValue::NULL).unwrap();
graph.add_vertex("person", JsValue::NULL).unwrap();
graph.add_vertex("software", JsValue::NULL).unwrap();
let result = graph
.v(None)
.unwrap()
.group_count()
.by_label()
.build()
.to_list()
.unwrap();
let arr = js_sys::Array::from(&result);
assert_eq!(arr.length(), 1); }
#[wasm_bindgen_test]
fn test_union_step() {
let graph = Graph::new();
let alice = graph.add_vertex("person", JsValue::NULL).unwrap();
let bob = graph.add_vertex("person", JsValue::NULL).unwrap();
let project = graph.add_vertex("software", JsValue::NULL).unwrap();
graph
.add_edge(id_to_js(alice), id_to_js(bob), "knows", JsValue::NULL)
.unwrap();
graph
.add_edge(id_to_js(alice), id_to_js(project), "created", JsValue::NULL)
.unwrap();
let out_step = __::out();
let ids_arr = js_sys::Array::new();
ids_arr.push(&id_to_js(alice));
let count = graph
.v(Some(ids_arr))
.unwrap()
.union(vec![out_step])
.to_count();
assert_eq!(count, 2);
}
#[wasm_bindgen_test]
fn test_optional_step() {
let graph = Graph::new();
let alice = graph.add_vertex("person", JsValue::NULL).unwrap();
let bob = graph.add_vertex("person", JsValue::NULL).unwrap();
graph
.add_edge(id_to_js(alice), id_to_js(bob), "knows", JsValue::NULL)
.unwrap();
let ids_arr1 = js_sys::Array::new();
ids_arr1.push(&id_to_js(alice));
let with_friend = graph
.v(Some(ids_arr1))
.unwrap()
.optional(__::out())
.to_count();
assert_eq!(with_friend, 1);
let ids_arr2 = js_sys::Array::new();
ids_arr2.push(&id_to_js(bob));
let without_friend = graph
.v(Some(ids_arr2))
.unwrap()
.optional(__::out())
.to_count();
assert_eq!(without_friend, 1);
}
#[wasm_bindgen_test]
fn test_min_step() {
let graph = Graph::new();
let props1 = js_sys::Object::new();
js_sys::Reflect::set(&props1, &"age".into(), &JsValue::from(30i32)).unwrap();
graph.add_vertex("person", props1.into()).unwrap();
let props2 = js_sys::Object::new();
js_sys::Reflect::set(&props2, &"age".into(), &JsValue::from(25i32)).unwrap();
graph.add_vertex("person", props2.into()).unwrap();
let props3 = js_sys::Object::new();
js_sys::Reflect::set(&props3, &"age".into(), &JsValue::from(35i32)).unwrap();
graph.add_vertex("person", props3.into()).unwrap();
let result = graph.v(None).unwrap().values("age").min().first().unwrap();
assert_eq!(result.as_f64().unwrap() as i32, 25);
}
#[wasm_bindgen_test]
fn test_max_step() {
let graph = Graph::new();
let props1 = js_sys::Object::new();
js_sys::Reflect::set(&props1, &"age".into(), &JsValue::from(30i32)).unwrap();
graph.add_vertex("person", props1.into()).unwrap();
let props2 = js_sys::Object::new();
js_sys::Reflect::set(&props2, &"age".into(), &JsValue::from(25i32)).unwrap();
graph.add_vertex("person", props2.into()).unwrap();
let props3 = js_sys::Object::new();
js_sys::Reflect::set(&props3, &"age".into(), &JsValue::from(35i32)).unwrap();
graph.add_vertex("person", props3.into()).unwrap();
let result = graph.v(None).unwrap().values("age").max().first().unwrap();
assert_eq!(result.as_f64().unwrap() as i32, 35);
}
fn build_algo_graph() -> (Graph, u64, u64, u64) {
let graph = Graph::new();
let alice = graph.add_vertex("person", JsValue::NULL).unwrap();
let bob = graph.add_vertex("person", JsValue::NULL).unwrap();
let charlie = graph.add_vertex("person", JsValue::NULL).unwrap();
let props1 = js_sys::Object::new();
js_sys::Reflect::set(&props1, &"weight".into(), &JsValue::from(1.0f64)).unwrap();
graph
.add_edge(id_to_js(alice), id_to_js(bob), "knows", props1.into())
.unwrap();
let props2 = js_sys::Object::new();
js_sys::Reflect::set(&props2, &"weight".into(), &JsValue::from(2.0f64)).unwrap();
graph
.add_edge(id_to_js(bob), id_to_js(charlie), "knows", props2.into())
.unwrap();
let props3 = js_sys::Object::new();
js_sys::Reflect::set(&props3, &"weight".into(), &JsValue::from(10.0f64)).unwrap();
graph
.add_edge(id_to_js(alice), id_to_js(charlie), "knows", props3.into())
.unwrap();
(graph, alice, bob, charlie)
}
#[wasm_bindgen_test]
fn test_shortest_path() {
let (graph, alice, _bob, charlie) = build_algo_graph();
let ids_arr = js_sys::Array::new();
ids_arr.push(&id_to_js(alice));
let result = graph
.v(Some(ids_arr))
.unwrap()
.shortest_path(id_to_js(charlie))
.unwrap()
.to_list()
.unwrap();
let arr = js_sys::Array::from(&result);
assert!(arr.length() >= 1);
}
#[wasm_bindgen_test]
fn test_shortest_path_weighted() {
let (graph, alice, _bob, charlie) = build_algo_graph();
let ids_arr = js_sys::Array::new();
ids_arr.push(&id_to_js(alice));
let result = graph
.v(Some(ids_arr))
.unwrap()
.shortest_path_weighted(id_to_js(charlie), "weight")
.unwrap()
.to_list()
.unwrap();
let arr = js_sys::Array::from(&result);
assert!(arr.length() >= 1);
}
#[wasm_bindgen_test]
fn test_bfs() {
let (graph, alice, _bob, _charlie) = build_algo_graph();
let ids_arr = js_sys::Array::new();
ids_arr.push(&id_to_js(alice));
let count = graph.v(Some(ids_arr)).unwrap().bfs(None).to_count();
assert!(count >= 2);
}
#[wasm_bindgen_test]
fn test_dfs() {
let (graph, alice, _bob, _charlie) = build_algo_graph();
let ids_arr = js_sys::Array::new();
ids_arr.push(&id_to_js(alice));
let count = graph.v(Some(ids_arr)).unwrap().dfs(None).to_count();
assert!(count >= 2);
}
#[wasm_bindgen_test]
fn test_bidirectional_bfs() {
let (graph, alice, _bob, charlie) = build_algo_graph();
let ids_arr = js_sys::Array::new();
ids_arr.push(&id_to_js(alice));
let result = graph
.v(Some(ids_arr))
.unwrap()
.bidirectional_bfs(id_to_js(charlie))
.unwrap()
.to_list()
.unwrap();
let arr = js_sys::Array::from(&result);
assert!(arr.length() >= 1);
}
#[wasm_bindgen_test]
fn test_iddfs() {
let (graph, alice, _bob, charlie) = build_algo_graph();
let ids_arr = js_sys::Array::new();
ids_arr.push(&id_to_js(alice));
let result = graph
.v(Some(ids_arr))
.unwrap()
.iddfs(id_to_js(charlie), 5)
.unwrap()
.to_list()
.unwrap();
let arr = js_sys::Array::from(&result);
assert!(arr.length() >= 1);
}
#[wasm_bindgen_test]
fn test_astar() {
let (graph, alice, _bob, charlie) = build_algo_graph();
graph
.set_vertex_property(id_to_js(alice), "h", JsValue::from(2.0f64))
.unwrap();
graph
.set_vertex_property(id_to_js(_bob), "h", JsValue::from(1.0f64))
.unwrap();
graph
.set_vertex_property(id_to_js(charlie), "h", JsValue::from(0.0f64))
.unwrap();
let ids_arr = js_sys::Array::new();
ids_arr.push(&id_to_js(alice));
let result = graph
.v(Some(ids_arr))
.unwrap()
.astar(id_to_js(charlie), "weight", "h")
.unwrap()
.to_list()
.unwrap();
let arr = js_sys::Array::from(&result);
assert!(arr.length() >= 1);
}
#[wasm_bindgen_test]
fn test_k_shortest_paths() {
let (graph, alice, _bob, charlie) = build_algo_graph();
let ids_arr = js_sys::Array::new();
ids_arr.push(&id_to_js(alice));
let result = graph
.v(Some(ids_arr))
.unwrap()
.k_shortest_paths(id_to_js(charlie), 2, "weight")
.unwrap()
.to_list()
.unwrap();
let arr = js_sys::Array::from(&result);
assert!(arr.length() >= 1);
}
#[wasm_bindgen_test]
fn test_within_distance_predicate() {
let graph = Graph::new();
let props1 = js_sys::Object::new();
let loc1 = js_sys::Array::new();
loc1.push(&JsValue::from(-122.4194f64)); loc1.push(&JsValue::from(37.7749f64));
js_sys::Reflect::set(&props1, &"location".into(), &loc1.into()).unwrap();
js_sys::Reflect::set(&props1, &"name".into(), &"SF".into()).unwrap();
graph.add_vertex("city", props1.into()).unwrap();
let props2 = js_sys::Object::new();
let loc2 = js_sys::Array::new();
loc2.push(&JsValue::from(-73.9857f64)); loc2.push(&JsValue::from(40.7484f64));
js_sys::Reflect::set(&props2, &"location".into(), &loc2.into()).unwrap();
js_sys::Reflect::set(&props2, &"name".into(), &"NYC".into()).unwrap();
graph.add_vertex("city", props2.into()).unwrap();
let pred = P::within_distance(-122.4194, 37.7749, 50.0);
let count = graph.v(None).unwrap().has_where("location", pred).to_count();
assert_eq!(count, 1);
}
#[wasm_bindgen_test]
fn test_anonymous_bfs() {
let (graph, alice, _bob, _charlie) = build_algo_graph();
let ids_arr = js_sys::Array::new();
ids_arr.push(&id_to_js(alice));
let count = graph
.v(Some(ids_arr))
.unwrap()
.union(vec![__::bfs(None)])
.to_count();
assert!(count >= 2);
}
#[wasm_bindgen_test]
fn test_anonymous_dfs() {
let (graph, alice, _bob, _charlie) = build_algo_graph();
let ids_arr = js_sys::Array::new();
ids_arr.push(&id_to_js(alice));
let count = graph
.v(Some(ids_arr))
.unwrap()
.union(vec![__::dfs(None)])
.to_count();
assert!(count >= 2);
}