elif-core 0.7.1

Core architecture foundation for the elif.rs LLM-friendly web framework
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Integration tests for IoC Phase 5: Integration & Migration
//!
//! Tests the complete integration of IoC container with controller factory,
//! middleware injection, and migration utilities.

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use crate::container::{IocContainer, ServiceBinder};
    use crate::foundation::traits::{FrameworkComponent, Service};

    // Test services for integration testing
    #[derive(Clone, Debug, Default)]
    pub struct UserRepository {
        #[allow(dead_code)]
        pub users: Vec<String>,
    }

    unsafe impl Send for UserRepository {}
    unsafe impl Sync for UserRepository {}

    impl FrameworkComponent for UserRepository {}

    impl Service for UserRepository {}

    #[derive(Clone, Debug, Default)]
    pub struct EmailService {
        #[allow(dead_code)]
        pub sent_emails: Vec<String>,
    }

    unsafe impl Send for EmailService {}
    unsafe impl Sync for EmailService {}

    impl FrameworkComponent for EmailService {}

    impl Service for EmailService {}

    #[derive(Clone, Debug, Default)]
    pub struct LoggerService {
        #[allow(dead_code)]
        pub logs: Vec<String>,
    }

    unsafe impl Send for LoggerService {}
    unsafe impl Sync for LoggerService {}

    impl FrameworkComponent for LoggerService {}

    impl Service for LoggerService {}

    // Test controller that uses dependency injection
    pub struct UserController {
        pub user_repo: Arc<UserRepository>,
        pub email_service: Arc<EmailService>,
        pub logger: Arc<LoggerService>,
    }

    impl UserController {
        pub fn from_ioc_container(
            container: &IocContainer,
            _scope: Option<&crate::container::ScopeId>,
        ) -> Result<Self, String> {
            let user_repo = container
                .resolve::<UserRepository>()
                .map_err(|e| format!("Failed to resolve UserRepository: {}", e))?;
            let email_service = container
                .resolve::<EmailService>()
                .map_err(|e| format!("Failed to resolve EmailService: {}", e))?;
            let logger = container
                .resolve::<LoggerService>()
                .map_err(|e| format!("Failed to resolve LoggerService: {}", e))?;

            Ok(Self {
                user_repo,
                email_service,
                logger,
            })
        }

        pub fn create_user(&self, username: &str) -> Result<(), String> {
            // Simulate user creation logic
            if username.is_empty() {
                return Err("Username cannot be empty".to_string());
            }

            // Would normally modify the repository
            println!("Creating user: {}", username);
            Ok(())
        }

        pub fn send_welcome_email(&self, email: &str) -> Result<(), String> {
            // Simulate email sending
            println!("Sending welcome email to: {}", email);
            Ok(())
        }
    }

    // Test middleware that uses dependency injection
    pub struct LoggingMiddleware {
        pub logger: Arc<LoggerService>,
    }

    impl LoggingMiddleware {
        pub fn from_ioc_container(
            container: &IocContainer,
            _scope: Option<&crate::container::ScopeId>,
        ) -> Result<Self, String> {
            let logger = container
                .resolve::<LoggerService>()
                .map_err(|e| format!("Failed to resolve LoggerService: {}", e))?;

            Ok(Self { logger })
        }

        pub fn log_request(&self, path: &str) {
            println!("Logging request to: {}", path);
        }
    }

    #[tokio::test]
    async fn test_complete_ioc_integration() {
        // Setup IoC container with all services
        let mut container = IocContainer::new();

        container.bind::<UserRepository, UserRepository>();
        container.bind::<EmailService, EmailService>();
        container.bind::<LoggerService, LoggerService>();

        container.build().expect("Container build should succeed");

        // Test controller creation with dependency injection
        let user_controller = UserController::from_ioc_container(&container, None)
            .expect("Controller creation should succeed");

        // Test that services are properly injected
        // Just verify services are available, don't check exact type names
        assert!(Arc::strong_count(&user_controller.user_repo) > 0);
        assert!(Arc::strong_count(&user_controller.email_service) > 0);
        assert!(Arc::strong_count(&user_controller.logger) > 0);

        // Test controller functionality
        user_controller
            .create_user("test_user")
            .expect("User creation should succeed");
        user_controller
            .send_welcome_email("test@example.com")
            .expect("Email sending should succeed");
    }

    #[tokio::test]
    async fn test_middleware_dependency_injection() {
        // Setup IoC container
        let mut container = IocContainer::new();
        container.bind::<LoggerService, LoggerService>();
        container.build().expect("Container build should succeed");

        // Create middleware with dependency injection
        let logging_middleware = LoggingMiddleware::from_ioc_container(&container, None)
            .expect("Middleware creation should succeed");

        // Test middleware functionality
        logging_middleware.log_request("/api/users");
        assert!(Arc::strong_count(&logging_middleware.logger) > 0);
    }

    #[tokio::test]
    async fn test_scoped_service_injection() {
        let mut container = IocContainer::new();

        // Register as scoped service to get the same instance within a scope
        let scoped_config = crate::container::AdvancedBindingBuilder::<LoggerService>::new()
            .with_lifetime(crate::container::ServiceScope::Scoped)
            .config();
        container.with_implementation::<LoggerService, LoggerService>(scoped_config);

        container.build().expect("Container build should succeed");

        // Create a scope for request-specific services
        let scope_id = container
            .create_scope()
            .expect("Scope creation should succeed");

        // Test scoped service resolution
        let logger1 = container
            .resolve_scoped::<LoggerService>(&scope_id)
            .expect("Scoped service resolution should succeed");
        let logger2 = container
            .resolve_scoped::<LoggerService>(&scope_id)
            .expect("Second scoped service resolution should succeed");

        // Should be the same instance within the scope
        assert!(Arc::ptr_eq(&logger1, &logger2));

        // Cleanup scope
        container
            .dispose_scope(&scope_id)
            .await
            .expect("Scope disposal should succeed");
    }

    #[tokio::test]
    async fn test_named_service_injection() {
        let mut container = IocContainer::new();

        // Bind multiple implementations of the same service with different names
        container.bind_named::<LoggerService, LoggerService>("primary");
        container.bind_named::<LoggerService, LoggerService>("secondary");

        container.build().expect("Container build should succeed");

        // Resolve named services
        let primary = container
            .resolve_named::<LoggerService>("primary")
            .expect("Primary logger resolution should succeed");
        let secondary = container
            .resolve_named::<LoggerService>("secondary")
            .expect("Secondary logger resolution should succeed");

        // Verify we got valid services
        assert!(Arc::strong_count(&primary) > 0);
        assert!(Arc::strong_count(&secondary) > 0);
        assert!(!Arc::ptr_eq(&primary, &secondary));
    }

    #[tokio::test]
    async fn test_factory_pattern_integration() {
        let mut container = IocContainer::new();

        // Bind a factory that creates services with dependencies
        container.bind_factory::<LoggerService, _, _>(|| Ok(LoggerService::default()));

        container.build().expect("Container build should succeed");

        // Resolve factory-created service
        let logger = container
            .resolve::<LoggerService>()
            .expect("Factory service resolution should succeed");
        // Verify service was resolved
        assert!(Arc::strong_count(&logger) > 0);
    }

    #[tokio::test]
    async fn test_collection_binding_integration() {
        let mut container = IocContainer::new();

        // Bind each service individually since collections don't work with trait objects
        container.bind::<UserRepository, UserRepository>();
        container.bind::<EmailService, EmailService>();
        container.bind::<LoggerService, LoggerService>();

        container.build().expect("Container build should succeed");

        // Verify services were registered
        let user_repo = container
            .resolve::<UserRepository>()
            .expect("UserRepository should be resolvable");
        let email_service = container
            .resolve::<EmailService>()
            .expect("EmailService should be resolvable");
        let logger = container
            .resolve::<LoggerService>()
            .expect("LoggerService should be resolvable");

        // Verify all services are available
        assert!(Arc::strong_count(&user_repo) > 0);
        assert!(Arc::strong_count(&email_service) > 0);
        assert!(Arc::strong_count(&logger) > 0);
    }

    #[tokio::test]
    async fn test_conditional_binding() {
        let mut container = IocContainer::new();

        // Bind service conditionally based on environment
        let is_development = std::env::var("ENVIRONMENT")
            .unwrap_or_else(|_| "development".to_string())
            == "development";

        if is_development {
            container.bind::<LoggerService, LoggerService>();
        } else {
            // In production, we might use a different implementation
            container.bind::<LoggerService, LoggerService>();
        }

        container.build().expect("Container build should succeed");

        let logger = container
            .resolve::<LoggerService>()
            .expect("Conditional service resolution should succeed");
        // Verify service was resolved
        assert!(Arc::strong_count(&logger) > 0);
    }

    #[tokio::test]
    async fn test_lazy_initialization() {
        let mut container = IocContainer::new();

        // Bind service with lazy initialization
        container.bind_lazy::<LoggerService, _, _>(|| {
            println!("Lazy initializing LoggerService");
            LoggerService::default()
        });

        container.build().expect("Container build should succeed");

        // Service should be created only when first resolved
        let logger = container
            .resolve::<LoggerService>()
            .expect("Lazy service resolution should succeed");
        // Verify service was resolved
        assert!(Arc::strong_count(&logger) > 0);
    }

    #[tokio::test]
    async fn test_parameterized_factory() {
        let mut container = IocContainer::new();

        // For now, use a regular factory since parameterized factories
        // don't have a resolve method that accepts parameters
        container.bind_factory::<LoggerService, _, _>(|| {
            let service = LoggerService::default();
            println!("Creating logger from factory");
            Ok(service)
        });

        container.build().expect("Container build should succeed");

        // Resolve the factory-created service
        let logger = container
            .resolve::<LoggerService>()
            .expect("Factory service resolution should succeed");
        // Verify service was resolved
        assert!(Arc::strong_count(&logger) > 0);
    }

    #[tokio::test]
    async fn test_container_validation() {
        let mut container = IocContainer::new();

        // Add services with dependencies
        container.bind::<UserRepository, UserRepository>();
        container.bind::<EmailService, EmailService>();
        container.bind::<LoggerService, LoggerService>();

        container.build().expect("Container build should succeed");

        // Validate container configuration
        container
            .validate()
            .expect("Container validation should succeed");

        // Test comprehensive validation
        let validation_errors = container.validate_all_services();
        assert!(validation_errors.is_ok(), "All services should be valid");
    }

    #[test]
    fn test_service_statistics() {
        let mut container = IocContainer::new();

        container.bind::<UserRepository, UserRepository>();
        container.bind_singleton::<EmailService, EmailService>();
        container.bind_transient::<LoggerService, LoggerService>();

        container.build().expect("Container build should succeed");

        let stats = container.get_statistics();
        assert_eq!(stats.total_services, 3);
        assert_eq!(stats.singleton_services, 1);
        assert_eq!(stats.transient_services, 2); // UserRepository and LoggerService are transient
    }

    #[tokio::test]
    async fn test_async_service_initialization() {
        // Test async initialization
        {
            let mut container = IocContainer::new();
            container.bind::<LoggerService, LoggerService>();
            container.build().expect("Container build should succeed");

            container
                .initialize_async()
                .await
                .expect("Async initialization should succeed");
        }

        // Test async initialization with timeout (separate container)
        {
            let mut container = IocContainer::new();
            container.bind::<LoggerService, LoggerService>();
            container.build().expect("Container build should succeed");

            container
                .initialize_async_with_timeout(std::time::Duration::from_secs(5))
                .await
                .expect("Async initialization with timeout should succeed");
        }
    }

    #[tokio::test]
    async fn test_lifecycle_management() {
        let mut container = IocContainer::new();

        container.bind::<LoggerService, LoggerService>();
        container.build().expect("Container build should succeed");

        // Test lifecycle operations
        // Initialize all services
        container
            .initialize_async()
            .await
            .expect("Service initialization should succeed");

        // Dispose all services
        container
            .dispose_all()
            .await
            .expect("Service disposal should succeed");
    }

    #[test]
    fn test_debug_and_introspection() {
        let mut container = IocContainer::new();

        container.bind::<UserRepository, UserRepository>();
        container.bind::<EmailService, EmailService>();
        container.build().expect("Container build should succeed");

        // Test debug information
        let registered_services = container.get_registered_services();
        assert!(!registered_services.is_empty());

        let service_info = container.get_service_info::<UserRepository>();
        assert!(service_info.is_some());
    }
}