use dist_agent_lang::ffi::{
CallFrequency, FFIConfig, FFIInterface, InterfaceSelector, InterfaceType, ServiceMetadata,
};
use dist_agent_lang::runtime::engine::Runtime;
use dist_agent_lang::runtime::values::Value;
#[test]
fn test_ffi_config_default() {
let config = FFIConfig::default();
assert_eq!(config.interface_type, InterfaceType::Both);
assert!(config.enable_http);
assert!(config.enable_ffi);
}
#[test]
fn test_ffi_config_http_only() {
let config = FFIConfig::http_only();
assert_eq!(config.interface_type, InterfaceType::HTTP);
assert!(config.enable_http);
assert!(!config.enable_ffi);
}
#[test]
fn test_ffi_config_ffi_only() {
let config = FFIConfig::ffi_only();
assert_eq!(config.interface_type, InterfaceType::FFI);
assert!(!config.enable_http);
assert!(config.enable_ffi);
}
#[test]
fn test_ffi_config_auto_detect() {
let config = FFIConfig::auto_detect();
assert_eq!(config.interface_type, InterfaceType::Both);
assert!(config.enable_http);
assert!(config.enable_ffi);
}
#[test]
fn test_interface_selector_new() {
let selector = InterfaceSelector::new();
assert_eq!(selector.default_interface(), InterfaceType::Both);
}
#[test]
fn test_interface_selector_register_service() {
let mut selector = InterfaceSelector::new();
let metadata = ServiceMetadata {
name: "TestService".to_string(),
function_names: vec!["hash".to_string(), "sign".to_string()],
has_network_operations: false,
has_compute_operations: true,
estimated_call_frequency: CallFrequency::High,
};
selector.register_service(metadata);
assert_eq!(selector.service_count(), 1);
}
#[test]
fn test_service_metadata_analyze_function() {
let (has_network, has_compute) = ServiceMetadata::analyze_function("chain::get_balance");
assert!(has_network);
assert!(!has_compute);
let (has_network, has_compute) = ServiceMetadata::analyze_function("hash_data");
assert!(!has_network);
assert!(has_compute);
let (has_network, has_compute) = ServiceMetadata::analyze_function("process_data");
assert!(!has_network);
assert!(has_compute);
let (has_network, has_compute) = ServiceMetadata::analyze_function("database::query");
assert!(has_network);
assert!(!has_compute);
}
#[test]
fn test_service_metadata_detect_interface_type() {
let metadata = ServiceMetadata {
name: "HighFreqService".to_string(),
function_names: vec![],
has_network_operations: false,
has_compute_operations: true,
estimated_call_frequency: CallFrequency::High,
};
assert_eq!(metadata.detect_interface_type(), InterfaceType::FFI);
let metadata = ServiceMetadata {
name: "NetworkService".to_string(),
function_names: vec![],
has_network_operations: true,
has_compute_operations: false,
estimated_call_frequency: CallFrequency::Low,
};
assert_eq!(metadata.detect_interface_type(), InterfaceType::HTTP);
let metadata = ServiceMetadata {
name: "ComputeService".to_string(),
function_names: vec![],
has_network_operations: false,
has_compute_operations: true,
estimated_call_frequency: CallFrequency::Medium,
};
assert_eq!(metadata.detect_interface_type(), InterfaceType::FFI);
let metadata = ServiceMetadata {
name: "MixedService".to_string(),
function_names: vec![],
has_network_operations: true,
has_compute_operations: true,
estimated_call_frequency: CallFrequency::Medium,
};
assert_eq!(metadata.detect_interface_type(), InterfaceType::Both);
}
#[test]
fn test_interface_selector_select_interface() {
let selector = InterfaceSelector::new();
let interface = selector.select_interface("Service", "hash_data", &[]);
assert_eq!(interface, InterfaceType::FFI);
let interface = selector.select_interface("Service", "chain::get_balance", &[]);
assert_eq!(interface, InterfaceType::HTTP);
let interface = selector.select_interface("Service", "database::query", &[]);
assert_eq!(interface, InterfaceType::HTTP);
let interface = selector.select_interface("Service", "unknown_function", &[]);
assert_eq!(interface, InterfaceType::Both);
}
#[test]
fn test_interface_selector_with_metadata() {
let mut selector = InterfaceSelector::new();
let metadata = ServiceMetadata {
name: "CryptoService".to_string(),
function_names: vec!["hash".to_string(), "sign".to_string()],
has_network_operations: false,
has_compute_operations: true,
estimated_call_frequency: CallFrequency::High,
};
selector.register_service(metadata);
let interface = selector.select_interface("CryptoService", "hash", &[]);
assert_eq!(interface, InterfaceType::FFI);
}
#[test]
fn test_ffi_interface_creation() {
let config = FFIConfig::both();
let _interface = FFIInterface::new(config);
}
#[test]
fn test_auto_detect_interface_hash_function() {
use dist_agent_lang::ffi::InterfaceSelector;
let selector = InterfaceSelector::new();
let interface = selector.select_interface("CryptoService", "hash_data", &[]);
assert_eq!(interface, InterfaceType::FFI);
}
#[test]
fn test_auto_detect_interface_chain_function() {
use dist_agent_lang::ffi::InterfaceSelector;
let selector = InterfaceSelector::new();
let interface = selector.select_interface("ChainService", "chain::get_balance", &[]);
assert_eq!(interface, InterfaceType::HTTP);
}
#[test]
fn test_estimate_value_size() {
use dist_agent_lang::ffi::InterfaceSelector;
let selector = InterfaceSelector::new();
let small_args = vec![Value::String("test".to_string())];
let interface_small = selector.select_interface("Service", "hash_data", &small_args);
assert_eq!(interface_small, InterfaceType::FFI);
let large_string = "x".repeat(2048);
let large_args = vec![Value::String(large_string)];
let interface_large = selector.select_interface("Service", "hash_data", &large_args);
assert_eq!(interface_large, InterfaceType::FFI);
}
#[test]
fn test_ffi_interface_http_only() {
let config = FFIConfig::http_only();
assert_eq!(config.interface_type, InterfaceType::HTTP);
assert!(config.enable_http);
assert!(!config.enable_ffi);
}
#[test]
fn test_ffi_interface_ffi_only() {
let config = FFIConfig::ffi_only();
let interface = FFIInterface::new(config);
let args = vec![Value::String("test".to_string())];
let result = interface.call("Service", "function", &args, Some(true));
assert!(result.is_err() || result.is_ok()); }
#[test]
fn test_ffi_interface_both_with_preference() {
let config = FFIConfig::both();
assert_eq!(config.interface_type, InterfaceType::Both);
let config_ffi = FFIConfig::ffi_only();
let interface_ffi = FFIInterface::new(config_ffi);
let args = vec![Value::String("test".to_string())];
let _result1 = interface_ffi.call("Service", "function", &args, Some(true));
}
#[test]
fn test_ffi_integration_with_runtime() {
let mut runtime = Runtime::new();
let source = r#"
@trust("hybrid")
@chain("ethereum")
service TestService {
fn add(a: int, b: int) -> int {
return a + b;
}
}
"#;
let program = dist_agent_lang::parse_source(source).unwrap();
let _result = runtime.execute_program(program, None);
let config = FFIConfig::ffi_only();
let _interface = FFIInterface::new(config);
let _args = [Value::Int(5), Value::Int(3)];
}
#[test]
fn test_value_size_estimation() {
use dist_agent_lang::ffi::InterfaceSelector;
let selector = InterfaceSelector::new();
let small_args = vec![Value::String("test".to_string())];
let interface_small = selector.select_interface("Service", "hash_data", &small_args);
assert_eq!(interface_small, InterfaceType::FFI);
let large_string = "x".repeat(2048);
let large_args = vec![Value::String(large_string)];
let interface_large = selector.select_interface("Service", "hash_data", &large_args);
assert_eq!(interface_large, InterfaceType::FFI);
let test_values = [
Value::Int(42),
Value::Float(3.15),
Value::String("hello".to_string()),
Value::Bool(true),
Value::Null,
];
assert_eq!(test_values.len(), 5);
}
#[test]
fn test_interface_fallback_mechanism() {
let config = FFIConfig::both();
assert_eq!(config.interface_type, InterfaceType::Both);
assert!(config.enable_http);
assert!(config.enable_ffi);
}
#[test]
fn test_call_frequency_enum() {
assert_eq!(CallFrequency::Low as u8, 0);
assert_eq!(CallFrequency::Medium as u8, 1);
assert_eq!(CallFrequency::High as u8, 2);
}
#[test]
fn test_interface_type_equality() {
assert_eq!(InterfaceType::HTTP, InterfaceType::HTTP);
assert_eq!(InterfaceType::FFI, InterfaceType::FFI);
assert_eq!(InterfaceType::Both, InterfaceType::Both);
assert_ne!(InterfaceType::HTTP, InterfaceType::FFI);
}
#[test]
fn test_auto_detection_performance_patterns() {
let selector = InterfaceSelector::new();
let high_freq_functions = vec![
"hash_data",
"sign_data",
"batch_process",
"parallel_compute",
];
for func in high_freq_functions {
let interface = selector.select_interface("Service", func, &[]);
assert_eq!(interface, InterfaceType::FFI, "{} should prefer FFI", func);
}
let network_functions = vec![
"chain::get_balance",
"database::query",
"fetch_data",
"api_request",
];
for func in network_functions {
let interface = selector.select_interface("Service", func, &[]);
assert_eq!(
interface,
InterfaceType::HTTP,
"{} should prefer HTTP",
func
);
}
}
#[test]
fn test_mixed_operation_detection() {
let selector = InterfaceSelector::new();
let compute_heavy_functions = vec![
"process_and_store", "compute_and_send", "transform_and_save", ];
for func in compute_heavy_functions {
let interface = selector.select_interface("Service", func, &[]);
assert_eq!(
interface,
InterfaceType::FFI,
"{} should use FFI (compute-heavy)",
func
);
}
let truly_mixed_functions = vec!["unknown_operation", "generic_handler", "custom_logic"];
for func in truly_mixed_functions {
let interface = selector.select_interface("Service", func, &[]);
assert_eq!(interface, InterfaceType::Both, "{} should use Both", func);
}
}