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
#[cfg(test)]
mod advanced_binding_tests {
    use super::super::{AdvancedBindingBuilder, IocContainer, ServiceBinder, ServiceScope};
    use serial_test::serial;

    // Test concrete implementations - avoiding trait objects for now
    #[derive(Default, Clone)]
    struct RedisCache {
        name: String,
    }

    impl RedisCache {
        fn new_with_name(name: impl Into<String>) -> Self {
            Self { name: name.into() }
        }

        fn get_name(&self) -> &str {
            &self.name
        }
    }

    #[derive(Default, Clone)]
    struct MemoryCache {
        name: String,
    }

    impl MemoryCache {
        fn new_with_name(name: impl Into<String>) -> Self {
            Self { name: name.into() }
        }

        fn get_name(&self) -> &str {
            &self.name
        }
    }

    #[derive(Default)]
    struct LocalStorage;

    #[derive(Default)]
    struct S3Storage;

    #[derive(Default)]
    struct SmtpEmailService;

    #[derive(Default)]
    struct SendGridEmailService;

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

        // Test with concrete types instead of trait objects
        let redis_config = AdvancedBindingBuilder::<RedisCache>::new()
            .named("redis")
            .with_lifetime(ServiceScope::Singleton)
            .config();

        container.with_implementation::<RedisCache, RedisCache>(redis_config);

        let memory_config = AdvancedBindingBuilder::<MemoryCache>::new()
            .named("memory")
            .with_lifetime(ServiceScope::Transient)
            .config();

        container.with_implementation::<MemoryCache, MemoryCache>(memory_config);

        container.build().expect("Failed to build container");

        // Test named resolution with concrete types
        assert!(container.resolve_named::<RedisCache>("redis").is_ok());
        assert!(container.resolve_named::<MemoryCache>("memory").is_ok());
        assert!(container
            .resolve_named::<RedisCache>("nonexistent")
            .is_err());
    }

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

        // Set up environment for test
        std::env::set_var("CACHE_PROVIDER", "redis");

        let redis_config = AdvancedBindingBuilder::<RedisCache>::new()
            .named("cache")
            .when_env("CACHE_PROVIDER", "redis")
            .config();

        container.with_implementation::<RedisCache, RedisCache>(redis_config);

        let memory_config = AdvancedBindingBuilder::<MemoryCache>::new()
            .named("cache")
            .when_env("CACHE_PROVIDER", "memory")
            .config();

        container.with_implementation::<MemoryCache, MemoryCache>(memory_config);

        container.build().expect("Failed to build container");

        // Should resolve to Redis since environment is set to "redis"
        let cache = container.resolve_named::<RedisCache>("cache");
        assert!(cache.is_ok());

        // Clean up
        std::env::remove_var("CACHE_PROVIDER");
    }

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

        // Enable feature flag
        std::env::set_var("FEATURE_CLOUD_STORAGE", "1");

        let s3_config = AdvancedBindingBuilder::<S3Storage>::new()
            .when_feature("cloud_storage")
            .config();

        container.with_implementation::<S3Storage, S3Storage>(s3_config);

        let local_config = AdvancedBindingBuilder::<LocalStorage>::new()
            .when_not_feature("cloud_storage")
            .config();

        container.with_implementation::<LocalStorage, LocalStorage>(local_config);

        container.build().expect("Failed to build container");

        // Should resolve to S3 since feature is enabled
        let storage = container.resolve::<S3Storage>();
        assert!(storage.is_ok());

        // Clean up
        std::env::remove_var("FEATURE_CLOUD_STORAGE");
    }

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

        // Set profile to production
        std::env::set_var("PROFILE", "production");

        let prod_config = AdvancedBindingBuilder::<SmtpEmailService>::new()
            .named("main_email")
            .in_profile("production")
            .config();

        container.with_implementation::<SmtpEmailService, SmtpEmailService>(prod_config);

        let dev_config = AdvancedBindingBuilder::<SendGridEmailService>::new()
            .named("main_email")
            .in_profile("development")
            .config();

        container.with_implementation::<SendGridEmailService, SendGridEmailService>(dev_config);

        container.build().expect("Failed to build container");

        // Should resolve to SMTP for production
        let email = container.resolve_named::<SmtpEmailService>("main_email");
        assert!(email.is_ok());

        // Clean up
        std::env::remove_var("PROFILE");
    }

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

        let always_true_config = AdvancedBindingBuilder::<MemoryCache>::new()
            .named("always_available")
            .when(|| true)
            .config();

        container.with_implementation::<MemoryCache, MemoryCache>(always_true_config);

        let never_config = AdvancedBindingBuilder::<RedisCache>::new()
            .named("never_available")
            .when(|| false)
            .config();

        container.with_implementation::<RedisCache, RedisCache>(never_config);

        container.build().expect("Failed to build container");

        // Should only resolve the "always_available" service
        assert!(container
            .resolve_named::<MemoryCache>("always_available")
            .is_ok());
        assert!(container
            .resolve_named::<RedisCache>("never_available")
            .is_err());
    }

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

        container.bind_lazy::<MemoryCache, _, _>(|| {
            println!("Creating lazy MemoryCache instance");
            MemoryCache::default()
        });

        container.build().expect("Failed to build container");

        // Should successfully create instance via lazy factory
        let cache = container.resolve::<MemoryCache>();
        assert!(cache.is_ok());
    }

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

        // Set up multiple environment conditions
        std::env::set_var("ENVIRONMENT", "test");
        std::env::set_var("FEATURE_ADVANCED", "1");
        std::env::set_var("PROFILE", "integration");

        let complex_config = AdvancedBindingBuilder::<RedisCache>::new()
            .named("complex")
            .when_env("ENVIRONMENT", "test")
            .when_feature("advanced")
            .in_profile("integration")
            .when(|| std::env::var("USER").is_ok()) // Most systems have USER env var
            .with_lifetime(ServiceScope::Singleton)
            .config();

        container.with_implementation::<RedisCache, RedisCache>(complex_config);

        container.build().expect("Failed to build container");

        // Should resolve since all conditions are met
        let cache = container.resolve_named::<RedisCache>("complex");
        assert!(cache.is_ok());

        // Clean up
        std::env::remove_var("ENVIRONMENT");
        std::env::remove_var("FEATURE_ADVANCED");
        std::env::remove_var("PROFILE");
    }

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

        // Bind multiple concrete types
        container.bind::<RedisCache, RedisCache>();
        container.bind_named::<MemoryCache, MemoryCache>("memory");
        container.bind_singleton::<S3Storage, S3Storage>();

        container.build().expect("Failed to build container");

        // Resolve concrete implementations
        assert!(container.resolve::<RedisCache>().is_ok());
        assert!(container.resolve_named::<MemoryCache>("memory").is_ok());
        assert!(container.resolve::<S3Storage>().is_ok());
    }

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

        container.bind::<MemoryCache, MemoryCache>();
        container.bind_singleton::<RedisCache, RedisCache>();
        container.bind_named::<S3Storage, S3Storage>("s3");

        container.build().expect("Failed to build container");

        let stats = container.get_statistics();
        assert_eq!(stats.total_services, 3);
        assert_eq!(stats.singleton_services, 1);
        assert_eq!(stats.transient_services, 2);
    }

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

        container.bind::<MemoryCache, MemoryCache>();
        container.bind_singleton::<RedisCache, RedisCache>();

        container.build().expect("Failed to build container");

        // Validate all services can be resolved
        let validation_result = container.validate_all_services();
        assert!(validation_result.is_ok());
    }

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

        container.bind::<MemoryCache, MemoryCache>();
        container.bind_named::<RedisCache, RedisCache>("redis");

        // Test service existence queries
        assert!(container.contains::<MemoryCache>());
        assert!(container.contains_named::<RedisCache>("redis"));
        assert!(!container.contains_named::<RedisCache>("nonexistent"));

        container.build().expect("Failed to build container");

        let registered = container.get_registered_services();
        assert!(registered.len() >= 2);
    }

    #[test]
    #[serial]
    fn test_condition_evaluation_edge_cases() {
        // Test when environment variable doesn't exist
        let config = AdvancedBindingBuilder::<RedisCache>::new()
            .when_env("NON_EXISTENT_VAR", "any_value")
            .config();

        assert!(!config.evaluate_conditions());

        // Test when environment variable exists but value doesn't match
        std::env::set_var("TEST_VAR", "wrong_value");
        let config2 = AdvancedBindingBuilder::<MemoryCache>::new()
            .when_env("TEST_VAR", "expected_value")
            .config();

        assert!(!config2.evaluate_conditions());

        // Clean up
        std::env::remove_var("TEST_VAR");
    }

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

        // Use the fixed closure-based API that actually registers services
        container.bind_collection::<RedisCache, _>(|collection| {
            collection
                .add::<RedisCache>()
                .add_named::<RedisCache>("special_redis");
        });

        container.build().expect("Failed to build container");

        // Verify that the services were actually registered and can be resolved
        assert!(container.resolve::<RedisCache>().is_ok());
        assert!(container
            .resolve_named::<RedisCache>("special_redis")
            .is_ok());

        // Check statistics show the right number of services
        let stats = container.get_statistics();
        assert_eq!(stats.total_services, 2); // RedisCache and named RedisCache
        assert_eq!(stats.transient_services, 2); // All are transient by default
    }

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

        container.bind_factory::<MemoryCache, _, _>(|| {
            Ok(MemoryCache::new_with_name("factory-created"))
        });

        container.build().expect("Failed to build container");

        let cache = container.resolve::<MemoryCache>();
        assert!(cache.is_ok());
        assert_eq!(cache.unwrap().get_name(), "factory-created");
    }

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

        let instance = RedisCache::new_with_name("pre-created");
        container.bind_instance::<RedisCache, RedisCache>(instance);

        container.build().expect("Failed to build container");

        let resolved = container.resolve::<RedisCache>();
        assert!(resolved.is_ok());
        assert_eq!(resolved.unwrap().get_name(), "pre-created");
    }

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

        // Mix different lifetimes
        container.bind::<MemoryCache, MemoryCache>(); // Transient
        container.bind_singleton::<RedisCache, RedisCache>(); // Singleton
        container.bind_named::<S3Storage, S3Storage>("storage"); // Named transient

        container.build().expect("Failed to build container");

        // Resolve multiple times to verify singleton behavior
        let redis1 = container.resolve::<RedisCache>().unwrap();
        let redis2 = container.resolve::<RedisCache>().unwrap();

        // For singletons, we get the same Arc
        assert!(std::ptr::eq(redis1.as_ref(), redis2.as_ref()));

        // Transients should be different instances
        let memory1 = container.resolve::<MemoryCache>().unwrap();
        let memory2 = container.resolve::<MemoryCache>().unwrap();

        // Different Arc instances for transients
        assert!(!std::ptr::eq(memory1.as_ref(), memory2.as_ref()));
    }
}