Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 185 additions & 0 deletions crates/challenge-sdk/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ impl Default for DataQuery {
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;

#[test]
fn test_data_key_spec() {
Expand All @@ -410,6 +411,37 @@ mod tests {
assert_eq!(spec.ttl_blocks, 100);
}

#[test]
fn test_challenge_scoped() {
let spec = DataKeySpec::new("leaderboard").challenge_scoped();
assert_eq!(spec.scope, DataScope::Challenge);
}

#[test]
fn test_global_scoped() {
let spec = DataKeySpec::new("global_config").global_scoped();
assert_eq!(spec.scope, DataScope::Global);
}

#[test]
fn test_with_schema() {
let schema = json!({"type": "number", "minimum": 0});
let spec = DataKeySpec::new("score").with_schema(schema.clone());
assert_eq!(spec.schema, Some(schema));
}

#[test]
fn test_no_consensus() {
let spec = DataKeySpec::new("local_data").no_consensus();
assert!(!spec.requires_consensus);
}

#[test]
fn test_min_consensus() {
let spec = DataKeySpec::new("important_data").min_consensus(5);
assert_eq!(spec.min_consensus, 5);
}

#[test]
fn test_data_verification() {
let accept = DataVerification::accept();
Expand All @@ -420,6 +452,35 @@ mod tests {
assert_eq!(reject.reason, Some("Bad data".to_string()));
}

#[test]
fn test_accept_with_transform() {
let transformed = vec![4, 5, 6];
let verification = DataVerification::accept_with_transform(transformed.clone());
assert!(verification.accepted);
assert_eq!(verification.transformed_value, Some(transformed));
}

#[test]
fn test_with_ttl() {
let verification = DataVerification::accept().with_ttl(500);
assert_eq!(verification.ttl_override, Some(500));
}

#[test]
fn test_with_event() {
let event = DataEvent::new("update", json!({"key": "value"}));
let verification = DataVerification::accept().with_event(event.clone());
assert_eq!(verification.events.len(), 1);
assert_eq!(verification.events[0].event_type, "update");
}

#[test]
fn test_data_event_new() {
let event = DataEvent::new("test_event", json!({"data": 123}));
assert_eq!(event.event_type, "test_event");
assert_eq!(event.data, json!({"data": 123}));
}

#[test]
fn test_data_submission() {
let sub = DataSubmission::new("score", vec![1, 2, 3], "validator1")
Expand All @@ -430,4 +491,128 @@ mod tests {
assert_eq!(sub.block_height, 100);
assert_eq!(sub.epoch, 5);
}

#[test]
fn test_data_submission_with_metadata() {
let sub = DataSubmission::new("score", vec![1, 2, 3], "validator1")
.with_metadata("source", json!("test"));

assert_eq!(sub.metadata.get("source"), Some(&json!("test")));
}

#[test]
fn test_value_json() {
let data = json!({"score": 85});
let json_str = serde_json::to_vec(&data).unwrap();
let sub = DataSubmission::new("score", json_str, "validator1");

let parsed: serde_json::Value = sub.value_json().unwrap();
assert_eq!(parsed, data);
}

#[test]
fn test_value_string() {
let text = "Hello, World!";
let sub = DataSubmission::new("message", text.as_bytes().to_vec(), "validator1");

let parsed = sub.value_string().unwrap();
assert_eq!(parsed, text);
}

#[test]
fn test_stored_data_is_expired() {
let stored = StoredData {
key: "test".to_string(),
value: vec![1, 2, 3],
scope: DataScope::Validator,
validator: Some("validator1".to_string()),
stored_at_block: 100,
expires_at_block: Some(200),
version: 1,
};

assert!(!stored.is_expired(150));
assert!(stored.is_expired(200));
assert!(stored.is_expired(250));

// Test permanent storage (no expiry)
let permanent = StoredData {
expires_at_block: None,
..stored
};
assert!(!permanent.is_expired(1000000));
}

#[test]
fn test_stored_data_value_json() {
let data = json!({"result": "success"});
let json_bytes = serde_json::to_vec(&data).unwrap();

let stored = StoredData {
key: "result".to_string(),
value: json_bytes,
scope: DataScope::Challenge,
validator: None,
stored_at_block: 100,
expires_at_block: None,
version: 1,
};

let parsed: serde_json::Value = stored.value_json().unwrap();
assert_eq!(parsed, data);
}

#[test]
fn test_data_query_new() {
let query = DataQuery::new();
assert!(query.key_pattern.is_none());
assert!(query.scope.is_none());
assert!(query.validator.is_none());
assert!(!query.include_expired);
assert!(query.limit.is_none());
assert!(query.offset.is_none());
}

#[test]
fn test_data_query_key() {
let query = DataQuery::new().key("score*");
assert_eq!(query.key_pattern, Some("score*".to_string()));
}

#[test]
fn test_data_query_scope() {
let query = DataQuery::new().scope(DataScope::Challenge);
assert_eq!(query.scope, Some(DataScope::Challenge));
}

#[test]
fn test_data_query_validator() {
let query = DataQuery::new().validator("validator1");
assert_eq!(query.validator, Some("validator1".to_string()));
}

#[test]
fn test_data_query_include_expired() {
let query = DataQuery::new().include_expired();
assert!(query.include_expired);
}

#[test]
fn test_data_query_limit() {
let query = DataQuery::new().limit(50);
assert_eq!(query.limit, Some(50));
}

#[test]
fn test_data_query_offset() {
let query = DataQuery::new().offset(100);
assert_eq!(query.offset, Some(100));
}

#[test]
fn test_data_query_default() {
let query = DataQuery::default();
assert!(query.key_pattern.is_none());
assert!(!query.include_expired);
}
}
161 changes: 161 additions & 0 deletions crates/challenge-sdk/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,15 @@ mod tests {
assert!(db.is_ok());
}

#[test]
fn test_challenge_id() {
let dir = tempdir().unwrap();
let challenge_id = ChallengeId::new();
let db = ChallengeDatabase::open(dir.path(), challenge_id).unwrap();

assert_eq!(db.challenge_id(), challenge_id);
}

#[test]
fn test_agent_storage() {
let dir = tempdir().unwrap();
Expand All @@ -307,6 +316,21 @@ mod tests {
assert_eq!(loaded.unwrap().hash, "test_hash_123");
}

#[test]
fn test_list_agents() {
let dir = tempdir().unwrap();
let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()).unwrap();

let agent1 = AgentInfo::new("hash1".to_string());
let agent2 = AgentInfo::new("hash2".to_string());

db.save_agent(&agent1).unwrap();
db.save_agent(&agent2).unwrap();

let agents = db.list_agents().unwrap();
assert_eq!(agents.len(), 2);
}

#[test]
fn test_result_storage() {
let dir = tempdir().unwrap();
Expand All @@ -321,6 +345,40 @@ mod tests {
assert_eq!(results[0].score, 0.85);
}

#[test]
fn test_get_all_results() {
let dir = tempdir().unwrap();
let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()).unwrap();

let result1 = EvaluationResult::new(uuid::Uuid::new_v4(), "agent1".to_string(), 0.85);
let result2 = EvaluationResult::new(uuid::Uuid::new_v4(), "agent2".to_string(), 0.90);

db.save_result(&result1).unwrap();
db.save_result(&result2).unwrap();

let results = db.get_all_results().unwrap();
assert_eq!(results.len(), 2);
}

#[test]
fn test_get_latest_results() {
let dir = tempdir().unwrap();
let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()).unwrap();

// Save multiple results for same agent
let mut result1 = EvaluationResult::new(uuid::Uuid::new_v4(), "agent1".to_string(), 0.70);
result1.timestamp = chrono::Utc::now() - chrono::Duration::hours(1);

let result2 = EvaluationResult::new(uuid::Uuid::new_v4(), "agent1".to_string(), 0.90);

db.save_result(&result1).unwrap();
db.save_result(&result2).unwrap();

let latest = db.get_latest_results().unwrap();
assert_eq!(latest.len(), 1);
assert_eq!(latest[0].score, 0.90);
}

#[test]
fn test_kv_store() {
let dir = tempdir().unwrap();
Expand All @@ -331,4 +389,107 @@ mod tests {
let value: Option<i32> = db.kv_get("my_key").unwrap();
assert_eq!(value, Some(42));
}

#[test]
fn test_kv_delete() {
let dir = tempdir().unwrap();
let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()).unwrap();

db.kv_set("key_to_delete", &"value").unwrap();

let deleted = db.kv_delete("key_to_delete").unwrap();
assert!(deleted);

let value: Option<String> = db.kv_get("key_to_delete").unwrap();
assert!(value.is_none());

// Delete non-existent key
let deleted = db.kv_delete("non_existent").unwrap();
assert!(!deleted);
}

#[test]
fn test_kv_keys() {
let dir = tempdir().unwrap();
let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()).unwrap();

db.kv_set("key1", &1).unwrap();
db.kv_set("key2", &2).unwrap();
db.kv_set("key3", &3).unwrap();

let keys = db.kv_keys().unwrap();
assert_eq!(keys.len(), 3);
assert!(keys.contains(&"key1".to_string()));
assert!(keys.contains(&"key2".to_string()));
assert!(keys.contains(&"key3".to_string()));
}

#[test]
fn test_set_meta() {
let dir = tempdir().unwrap();
let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()).unwrap();

db.set_meta("author", "test_author").unwrap();

let value = db.get_meta("author").unwrap();
assert_eq!(value, Some("test_author".to_string()));
}

#[test]
fn test_get_meta() {
let dir = tempdir().unwrap();
let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()).unwrap();

let value = db.get_meta("non_existent").unwrap();
assert!(value.is_none());

db.set_meta("key", "value").unwrap();
let value = db.get_meta("key").unwrap();
assert_eq!(value, Some("value".to_string()));
}

#[test]
fn test_get_version() {
let dir = tempdir().unwrap();
let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()).unwrap();

// Should return 0 for new database
let version = db.get_version().unwrap();
assert_eq!(version, 0);
}

#[test]
fn test_set_version() {
let dir = tempdir().unwrap();
let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()).unwrap();

db.set_version(5).unwrap();

let version = db.get_version().unwrap();
assert_eq!(version, 5);
}

#[test]
fn test_open_tree() {
let dir = tempdir().unwrap();
let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()).unwrap();

let custom_tree = db.open_tree("custom_data").unwrap();

custom_tree.insert(b"key", b"value").unwrap();
let value = custom_tree.get(b"key").unwrap();
assert_eq!(value.as_ref().map(|v| v.as_ref()), Some(b"value".as_ref()));
}

#[test]
fn test_flush() {
let dir = tempdir().unwrap();
let db = ChallengeDatabase::open(dir.path(), ChallengeId::new()).unwrap();

db.kv_set("test_key", &"test_value").unwrap();

// Flush should succeed
let result = db.flush();
assert!(result.is_ok());
}
}
Loading