use heromodels::db::hero::OurDB; use heromodels::db::{Collection, Db}; use heromodels::models::grid4::node::node_index::{country, nodegroupid, pubkey}; use heromodels::models::grid4::node::{ComputeSlice, DeviceInfo, Node}; use heromodels_core::Model; use std::sync::Arc; fn create_test_db() -> Arc { let ts = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .unwrap() .as_nanos(); let path = format!("/tmp/grid4_node_test_{}", ts); let _ = std::fs::remove_dir_all(&path); Arc::new(OurDB::new(path, true).expect("create OurDB")) } #[test] fn grid4_node_basic_roundtrip_and_indexes() { let db = create_test_db(); let nodes = db.collection::().expect("open node collection"); // Clean any leftover if let Ok(existing) = nodes.get_all() { for n in existing { let _ = nodes.delete_by_id(n.get_id()); } } // Build a node with some compute slices and device info let cs = ComputeSlice::new() .nodeid(1) .slice_id(1) .mem_gb(32.0) .storage_gb(512.0) .passmark(5000) .vcores(16) .gpus(1) .price_cc(0.25); let dev = DeviceInfo { vendor: "ACME".into(), ..Default::default() }; let n = Node::new() .nodegroupid(42) .uptime(99) .add_compute_slice(cs) .devices(dev) .country("BE") .pubkey("PUB_NODE_1") .build(); let (id, stored) = nodes.set(&n).expect("store node"); assert!(id > 0); assert_eq!(stored.country, "BE"); // get by id let fetched = nodes.get_by_id(id).expect("get by id").expect("exists"); assert_eq!(fetched.pubkey, "PUB_NODE_1"); // query by top-level indexes let by_country = nodes.get::("BE").expect("query country"); assert_eq!(by_country.len(), 1); assert_eq!(by_country[0].get_id(), id); let by_group = nodes.get::(&42).expect("query group"); assert_eq!(by_group.len(), 1); let by_pubkey = nodes.get::("PUB_NODE_1").expect("query pubkey"); assert_eq!(by_pubkey.len(), 1); // update let updated = fetched.clone().country("NL"); let (_, back) = nodes.set(&updated).expect("update node"); assert_eq!(back.country, "NL"); // delete nodes.delete_by_id(id).expect("delete"); assert!(nodes.get_by_id(id).expect("get after delete").is_none()); }