dist_agent_lang 1.0.21

Agentic programming with library and CLI support for Off/On-chain network integration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// Comprehensive FFI (Foreign Function Interface) Tests
// Tests HTTP/REST and FFI interfaces, auto-detection, and integration
//
// Note: These tests intentionally avoid HTTP interface calls to keep tests focused
// on FFI-specific functionality (interface selection, auto-detection, configuration).
// HTTP interface testing is covered in http_server_tests.rs and http_security_mutation_tests.rs.

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() {
    // Test network patterns
    let (has_network, has_compute) = ServiceMetadata::analyze_function("chain::get_balance");
    assert!(has_network);
    assert!(!has_compute);

    // Test compute patterns
    let (has_network, has_compute) = ServiceMetadata::analyze_function("hash_data");
    assert!(!has_network);
    assert!(has_compute);

    // Test mixed
    let (has_network, has_compute) = ServiceMetadata::analyze_function("process_data");
    assert!(!has_network);
    assert!(has_compute);

    // Test database pattern
    let (has_network, has_compute) = ServiceMetadata::analyze_function("database::query");
    assert!(has_network);
    assert!(!has_compute);
}

#[test]
fn test_service_metadata_detect_interface_type() {
    // High frequency -> FFI
    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);

    // Network only -> HTTP
    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);

    // Compute only -> FFI
    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);

    // Mixed -> Both
    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();

    // Hash function -> FFI
    let interface = selector.select_interface("Service", "hash_data", &[]);
    assert_eq!(interface, InterfaceType::FFI);

    // Chain function -> HTTP
    let interface = selector.select_interface("Service", "chain::get_balance", &[]);
    assert_eq!(interface, InterfaceType::HTTP);

    // Database function -> HTTP
    let interface = selector.select_interface("Service", "database::query", &[]);
    assert_eq!(interface, InterfaceType::HTTP);

    // Unknown function -> Both (default)
    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);

    // Should use 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);
    // Should create successfully (no panic)
}

#[test]
fn test_auto_detect_interface_hash_function() {
    // Test interface selection logic without HTTP interface initialization
    // Hash functions should prefer FFI based on pattern matching
    use dist_agent_lang::ffi::InterfaceSelector;

    let selector = InterfaceSelector::new();

    // Hash function should prefer FFI
    let interface = selector.select_interface("CryptoService", "hash_data", &[]);
    assert_eq!(interface, InterfaceType::FFI);
}

#[test]
fn test_auto_detect_interface_chain_function() {
    // Test interface selection logic without actually calling HTTP interface
    // to avoid reqwest runtime initialization issues in tests
    use dist_agent_lang::ffi::InterfaceSelector;

    let selector = InterfaceSelector::new();

    // Chain function should prefer HTTP based on pattern matching
    let interface = selector.select_interface("ChainService", "chain::get_balance", &[]);
    assert_eq!(interface, InterfaceType::HTTP);
}

#[test]
fn test_estimate_value_size() {
    // Test value size estimation logic without HTTP interface initialization
    use dist_agent_lang::ffi::InterfaceSelector;

    let selector = InterfaceSelector::new();

    // Test small value - should prefer FFI
    let small_args = vec![Value::String("test".to_string())];
    let interface_small = selector.select_interface("Service", "hash_data", &small_args);
    // Hash function should prefer FFI regardless of size
    assert_eq!(interface_small, InterfaceType::FFI);

    // Test large value - pattern matching takes precedence over size
    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);
    // Still prefers FFI due to function pattern
    assert_eq!(interface_large, InterfaceType::FFI);
}

#[test]
fn test_ffi_interface_http_only() {
    // Test HTTP-only configuration without initializing HTTP client
    // to avoid reqwest runtime issues in tests
    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));

    // Should attempt FFI call
    assert!(result.is_err() || result.is_ok()); // Either is fine for this test
}

#[test]
fn test_ffi_interface_both_with_preference() {
    // Test preference logic without HTTP interface initialization
    let config = FFIConfig::both();
    assert_eq!(config.interface_type, InterfaceType::Both);

    // Test FFI-only config with preference
    let config_ffi = FFIConfig::ffi_only();
    let interface_ffi = FFIInterface::new(config_ffi);

    let args = vec![Value::String("test".to_string())];

    // Prefer FFI - should work with FFI-only config
    let _result1 = interface_ffi.call("Service", "function", &args, Some(true));
}

// Integration tests with runtime
#[test]
fn test_ffi_integration_with_runtime() {
    let mut runtime = Runtime::new();

    // Create a simple service
    // @trust requires @chain (security validation enforced in parser)
    let source = r#"
        @trust("hybrid")
        @chain("ethereum")
        service TestService {
            fn add(a: int, b: int) -> int {
                return a + b;
            }
        }
    "#;

    // Parse and execute
    let program = dist_agent_lang::parse_source(source).unwrap();
    let _result = runtime.execute_program(program, None);

    // Test FFI interface with runtime
    let config = FFIConfig::ffi_only();
    let _interface = FFIInterface::new(config);

    let _args = [Value::Int(5), Value::Int(3)];
    // Note: This requires the runtime to be accessible from FFI
    // For now, just verify the interface can be created
}

#[test]
fn test_value_size_estimation() {
    // Test value size estimation logic
    // Note: Size estimation is used internally by auto_detect_interface
    // but pattern matching takes precedence in InterfaceSelector

    use dist_agent_lang::ffi::InterfaceSelector;

    let selector = InterfaceSelector::new();

    // Test small value - hash function should prefer FFI
    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);

    // Test large value - pattern matching takes precedence
    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);
    // Still prefers FFI due to function pattern (pattern > size heuristic)
    assert_eq!(interface_large, InterfaceType::FFI);

    // Test that values can be created and measured
    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() {
    // Test fallback mechanism logic without HTTP interface initialization
    // The fallback logic is: try FFI first, if it fails, fallback to HTTP
    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);
}

// Performance-related tests
#[test]
fn test_auto_detection_performance_patterns() {
    let selector = InterfaceSelector::new();

    // High-frequency patterns should prefer FFI
    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);
    }

    // Network patterns should prefer HTTP
    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();

    // Mixed operations - these match compute patterns so will use FFI
    let compute_heavy_functions = vec![
        "process_and_store",  // matches "process"
        "compute_and_send",   // matches "compute"
        "transform_and_save", // matches "transform"
    ];

    for func in compute_heavy_functions {
        let interface = selector.select_interface("Service", func, &[]);
        // These match compute patterns, so they prefer FFI
        assert_eq!(
            interface,
            InterfaceType::FFI,
            "{} should use FFI (compute-heavy)",
            func
        );
    }

    // Truly mixed operations with no clear pattern should use Both
    let truly_mixed_functions = vec!["unknown_operation", "generic_handler", "custom_logic"];

    for func in truly_mixed_functions {
        let interface = selector.select_interface("Service", func, &[]);
        // No pattern match, defaults to Both
        assert_eq!(interface, InterfaceType::Both, "{} should use Both", func);
    }
}