use super::helpers::*;
use eidetica::crdt::CRDT;
use eidetica::crdt::doc::Value;
#[test]
fn test_end_to_end_data_workflow() -> eidetica::Result<()> {
let (db, tree, op, dict) = setup_complete_test_env("integration_test")?;
dict.set_at_path(
&["user", "profile", "name"],
Value::Text("Alice".to_string()),
)?;
dict.set_at_path(
&["user", "profile", "email"],
Value::Text("alice@example.com".to_string()),
)?;
dict.set_at_path(
&["user", "settings", "theme"],
Value::Text("dark".to_string()),
)?;
let user_editor = dict.get_value_mut("user");
let profile_editor = user_editor.get_value_mut("profile");
assert_text_value(&profile_editor.get_value("name")?, "Alice");
profile_editor
.get_value_mut("age")
.set(Value::Text("25".to_string()))?;
assert_text_value(&dict.get_at_path(&["user", "profile", "age"])?, "25");
let user_data = dict.get("user")?;
match user_data {
Value::Map(user_map) => {
test_serialization_roundtrip(&user_map)?;
}
_ => panic!("Expected user to be a map"),
}
user_editor.delete_child("settings")?;
assert_not_found_error(dict.get_at_path(&["user", "settings", "theme"]));
assert_text_value(&dict.get_at_path(&["user", "profile", "name"])?, "Alice");
assert_text_value(&dict.get_at_path(&["user", "profile", "age"])?, "25");
op.commit()?;
let viewer_op = tree.new_transaction()?;
let viewer_dict = setup_dict_subtree(&viewer_op, "integration_test")?;
assert_text_value(
&viewer_dict.get_at_path(&["user", "profile", "name"])?,
"Alice",
);
assert_not_found_error(viewer_dict.get_at_path(&["user", "settings", "theme"]));
Ok(())
}
#[test]
fn test_mixed_operations_consistency() -> eidetica::Result<()> {
let (_, _, op, dict) = setup_complete_test_env("consistency_test")?;
dict.set_at_path(
&["app", "config", "version"],
Value::Text("1.0.0".to_string()),
)?;
let app_editor = dict.get_value_mut("app");
let config_editor = app_editor.get_value_mut("config");
config_editor
.get_value_mut("debug")
.set(Value::Text("true".to_string()))?;
assert_text_value(&dict.get_at_path(&["app", "config", "version"])?, "1.0.0");
assert_text_value(&dict.get_at_path(&["app", "config", "debug"])?, "true");
assert_text_value(&config_editor.get_value("version")?, "1.0.0");
assert_text_value(&config_editor.get_value("debug")?, "true");
dict.set_at_path(&["app", "config"], Value::Text("simple_config".to_string()))?;
assert_text_value(&dict.get_at_path(&["app", "config"])?, "simple_config");
assert_not_found_error(dict.get_at_path(&["app", "config", "version"]));
Ok(())
}
#[test]
fn test_merge_operations_with_editors() -> eidetica::Result<()> {
let (_, _, op, dict) = setup_complete_test_env("merge_test")?;
let (map1, map2) = build_complex_merge_data();
dict.get_root_mut().set(Value::Map(map1))?;
assert_text_value(&dict.get_at_path(&["level1", "key1"])?, "value1");
assert_text_value(&dict.get_at_path(&["top_level_key"])?, "top_value");
let current_map = match dict.get_root_mut().get()? {
Value::Map(m) => m,
_ => panic!("Expected root to be a map"),
};
let merged = current_map.merge(&map2)?;
dict.get_root_mut().set(Value::Map(merged))?;
assert_text_value(&dict.get_at_path(&["level1", "key1"])?, "value1"); assert_text_value(&dict.get_at_path(&["level1", "key2"])?, "value2"); assert_text_value(
&dict.get_at_path(&["level1", "to_update"])?,
"updated_value",
); assert_not_found_error(dict.get_at_path(&["level1", "to_delete"])); assert_not_found_error(dict.get_at_path(&["top_level_key"])); assert_text_value(&dict.get_at_path(&["new_top_key"])?, "new_top_value");
Ok(())
}
#[test]
fn test_error_handling_across_apis() -> eidetica::Result<()> {
let (_, _, op, dict) = setup_complete_test_env("error_test")?;
assert_not_found_error(dict.get_at_path(&["nonexistent", "path"]));
let editor = dict.get_value_mut("nonexistent");
assert_not_found_error(editor.get());
dict.set_at_path(&["string_value"], Value::Text("test".to_string()))?;
assert_type_error(dict.get_at_path(&["string_value", "field"]));
let string_editor = dict.get_value_mut("string_value");
assert_type_error(string_editor.get_value("field"));
let root_editor = dict.get_root_mut();
assert_type_error(root_editor.set(Value::Text("not_a_map".to_string())));
assert_type_error(dict.set_at_path(&[], Value::Text("not_a_map".to_string())));
Ok(())
}
#[test]
fn test_performance_with_deep_nesting() -> eidetica::Result<()> {
let (_, _, op, dict) = setup_complete_test_env("performance_test")?;
let mut path = Vec::new();
for i in 0..10 {
path.push(format!("level_{i}"));
}
path.push("final_value".to_string());
let path_refs: Vec<&str> = path.iter().map(|s| s.as_str()).collect();
dict.set_at_path(&path_refs, Value::Text("deep_data".to_string()))?;
assert_text_value(&dict.get_at_path(&path_refs)?, "deep_data");
let mut editor = dict.get_value_mut(&path_refs[0]);
for path_segment in &path_refs[1..path_refs.len() - 1] {
editor = editor.get_value_mut(path_segment);
}
editor
.get_value_mut(&path_refs[path_refs.len() - 1])
.set(Value::Text("modified_deep_data".to_string()))?;
assert_text_value(&dict.get_at_path(&path_refs)?, "modified_deep_data");
Ok(())
}