#![cfg(feature = "http-server")]
use std::sync::Arc;
use aletheiadb::core::PropertyMapBuilder;
use aletheiadb::http::{AppState, ServerConfig, build_test_router};
use aletheiadb::{AletheiaDB, AletheiaDBConfig, NodeId};
use autumn_web::test::TestApp;
use serde_json::{Value, json};
fn unified_config(data_dir: &std::path::Path) -> AletheiaDBConfig {
ServerConfig::builder()
.data_dir(data_dir)
.build()
.to_unified_config()
.expect("data_dir set => Some(config)")
}
#[tokio::test]
async fn node_survives_database_restart_with_same_data_dir() {
let tempdir = tempfile::tempdir().expect("create tempdir");
let data_path = tempdir.path().to_path_buf();
let alice_id: u64;
{
let db =
Arc::new(AletheiaDB::with_unified_config(unified_config(&data_path)).expect("open db"));
let state = AppState::new(db.clone());
let config = ServerConfig::default();
let router = build_test_router(state, &config).expect("build router");
let client = TestApp::from_router(router);
let resp = client
.post("/query")
.json(&json!({
"operation": "create_node",
"label": "Person",
"properties": { "name": "Alice", "age": 30 }
}))
.send()
.await;
assert_eq!(resp.status.as_u16(), 200);
let body: Value = serde_json::from_slice(&resp.body).unwrap();
assert_eq!(body["success"], true);
alice_id = body["data"]["id"]
.as_u64()
.expect("response should include node id");
db.persist_indexes().expect("persist_indexes");
drop(client);
drop(db);
}
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
let db =
Arc::new(AletheiaDB::with_unified_config(unified_config(&data_path)).expect("reopen db"));
let nid = NodeId::new(alice_id).expect("valid node id");
let node = db
.get_node(nid)
.expect("node should still exist after restart");
assert_eq!(node.id.as_u64(), alice_id);
let state = AppState::new(db);
let config = ServerConfig::default();
let router = build_test_router(state, &config).expect("build router");
let client = TestApp::from_router(router);
let resp = client
.post("/query")
.json(&json!({ "operation": "get_node", "node_id": alice_id }))
.send()
.await;
assert_eq!(resp.status.as_u16(), 200);
let body: Value = serde_json::from_slice(&resp.body).unwrap();
assert_eq!(body["success"], true);
assert_eq!(body["data"]["label"], "Person");
assert_eq!(body["data"]["properties"]["name"], "Alice");
assert_eq!(body["data"]["properties"]["age"], 30);
}
#[tokio::test]
async fn data_dir_none_means_in_memory() {
let config = ServerConfig::default();
assert!(config.data_dir().is_none());
let config = ServerConfig::builder().port(8081).build();
assert!(config.data_dir().is_none());
}
#[tokio::test]
async fn data_dir_some_round_trips_through_builder() {
let path = std::path::PathBuf::from("/var/lib/aletheiadb");
let config = ServerConfig::builder().data_dir(&path).build();
assert_eq!(config.data_dir(), Some(path.as_path()));
let tempdir = tempfile::tempdir().expect("tempdir");
let db = AletheiaDB::with_unified_config(unified_config(tempdir.path())).expect("open");
let _ = db
.create_node("Probe", PropertyMapBuilder::new().insert("k", "v").build())
.expect("create_node");
drop(db);
assert!(
tempdir.path().join("wal").exists(),
"WAL directory should have been created"
);
assert!(
tempdir.path().join("indexes").exists(),
"Indexes directory should have been created"
);
}