reratui-hooks 0.2.0

React-inspired hooks for state management and side effects in Reratui TUI applications
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//! Comprehensive tests for the useFuture hook
//!
//! This module contains extensive tests covering:
//! - Basic future execution and state management
//! - Dependency tracking and re-execution
//! - Error handling and edge cases
//! - Cancellation behavior
//! - Thread safety and concurrent access

use super::*;
use crate::test_utils::{
    with_async_component_id, with_async_hook_context, with_async_test_isolate,
};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tokio::time::sleep;

#[tokio::test]
async fn test_basic_future_success() {
    with_async_test_isolate(|| async {
        with_async_component_id("BasicFutureComponent", |_context| async {
            let handle = use_future(
                || async { Ok::<i32, String>(42) },
                (), // No dependencies - run once
            );

            // Initially should be pending
            assert!(handle.is_pending());
            assert_eq!(handle.state(), FutureState::Pending);

            // Wait for the future to complete
            sleep(Duration::from_millis(50)).await;

            // Should now be resolved
            assert!(
                handle.is_resolved(),
                "Future should be resolved after waiting"
            );
            assert_eq!(handle.value(), Some(42));
            assert_eq!(handle.state(), FutureState::Resolved(42));
        })
        .await;
    })
    .await;
}

#[tokio::test]
async fn test_basic_future_error() {
    with_async_test_isolate(|| async {
        with_async_component_id("ErrorFutureComponent", |_context| async {
            let handle = use_future(
                || async { Err::<i32, String>("Test error".to_string()) },
                (),
            );

            // Initially should be pending
            assert!(handle.is_pending());

            // Wait for the future to complete
            sleep(Duration::from_millis(50)).await;

            // Should now be in error state
            assert!(
                handle.is_error(),
                "Future should be in error state after waiting"
            );
            assert_eq!(handle.error(), Some("Test error".to_string()));
            assert_eq!(handle.state(), FutureState::Error("Test error".to_string()));
        })
        .await;
    })
    .await;
}

#[tokio::test]
async fn test_future_with_dependencies() {
    with_async_test_isolate(|| async {
        let counter = Arc::new(AtomicUsize::new(0));

        // First render
        with_async_component_id("DependencyFutureComponent", |_context| async {
            let counter_clone = counter.clone();
            let handle1 = use_future(
                move || {
                    let counter = counter_clone.clone();
                    async move {
                        counter.fetch_add(1, Ordering::SeqCst);
                        Ok::<usize, String>(42)
                    }
                },
                1, // dependency value
            );

            // Wait for the future to complete
            sleep(Duration::from_millis(10)).await;
            assert_eq!(counter.load(Ordering::SeqCst), 1);
            assert!(handle1.is_resolved());
        })
        .await;

        // Second render with same dependency - should not re-execute
        with_async_component_id("DependencyFutureComponent", |_context| async {
            let counter_clone2 = counter.clone();
            let _handle2 = use_future(
                move || {
                    let counter = counter_clone2.clone();
                    async move {
                        counter.fetch_add(1, Ordering::SeqCst);
                        Ok::<usize, String>(42)
                    }
                },
                1, // same dependency value
            );

            sleep(Duration::from_millis(10)).await;

            // Should still have executed only once since dependency didn't change
            assert_eq!(counter.load(Ordering::SeqCst), 1);
        })
        .await;
    })
    .await;
}

#[tokio::test]
async fn test_future_dependency_change() {
    with_async_test_isolate(|| async {
        let counter = Arc::new(AtomicUsize::new(0));
        let results = Arc::new(Mutex::new(Vec::new()));

        // First execution with dependency = 1
        with_async_hook_context(|_context| async {
            let counter_clone = counter.clone();
            let results_clone = results.clone();
            let handle = use_future(
                move || {
                    let counter = counter_clone.clone();
                    let results = results_clone.clone();
                    async move {
                        let count = counter.fetch_add(1, Ordering::SeqCst);
                        let result = format!("execution_{}", count);
                        results.lock().unwrap().push(result.clone());
                        Ok::<String, String>(result)
                    }
                },
                1, // dependency = 1
            );

            sleep(Duration::from_millis(10)).await;
            assert!(handle.is_resolved());
        })
        .await;

        // Second execution with dependency = 2 (changed)
        with_async_hook_context(|_context| async {
            let counter_clone = counter.clone();
            let results_clone = results.clone();
            let handle = use_future(
                move || {
                    let counter = counter_clone.clone();
                    let results = results_clone.clone();
                    async move {
                        let count = counter.fetch_add(1, Ordering::SeqCst);
                        let result = format!("execution_{}", count);
                        results.lock().unwrap().push(result.clone());
                        Ok::<String, String>(result)
                    }
                },
                2, // dependency = 2 (changed)
            );

            sleep(Duration::from_millis(10)).await;
            assert!(handle.is_resolved());
        })
        .await;

        // Should have executed twice due to dependency change
        assert_eq!(counter.load(Ordering::SeqCst), 2);
        let results_vec = results.lock().unwrap();
        assert_eq!(results_vec.len(), 2);
        assert_eq!(results_vec[0], "execution_0");
        assert_eq!(results_vec[1], "execution_1");
    })
    .await;
}

#[tokio::test]
async fn test_future_state_methods() {
    // Test FutureState utility methods
    let pending: FutureState<i32, String> = FutureState::Pending;
    assert!(pending.is_pending());
    assert!(!pending.is_resolved());
    assert!(!pending.is_error());
    assert_eq!(pending.error(), None);

    let resolved: FutureState<i32, String> = FutureState::Resolved(42);
    assert!(!resolved.is_pending());
    assert!(resolved.is_resolved());
    assert!(!resolved.is_error());
    assert_eq!(resolved.value(), Some(&42));
    assert_eq!(resolved.error(), None);

    let error: FutureState<i32, String> = FutureState::Error("test error".to_string());
    assert!(!error.is_pending());
    assert!(!error.is_resolved());
    assert!(error.is_error());
    assert_eq!(error.value(), None);
    assert_eq!(error.error(), Some(&"test error".to_string()));
}

#[tokio::test]
async fn test_future_state_map() {
    let resolved: FutureState<i32, String> = FutureState::Resolved(42);
    let mapped = resolved.map(|x| x * 2);
    assert_eq!(mapped, FutureState::Resolved(84));

    let pending: FutureState<i32, String> = FutureState::Pending;
    let mapped_pending = pending.map(|x| x * 2);
    assert_eq!(mapped_pending, FutureState::Pending);

    let error: FutureState<i32, String> = FutureState::Error("test".to_string());
    let mapped_error = error.map(|x: i32| x * 2);
    assert_eq!(mapped_error, FutureState::Error("test".to_string()));
}

#[tokio::test]
async fn test_future_state_map_err() {
    let error: FutureState<i32, String> = FutureState::Error("test".to_string());
    let mapped = error.map_err(|e| format!("Error: {}", e));
    assert_eq!(mapped, FutureState::Error("Error: test".to_string()));

    let resolved: FutureState<i32, String> = FutureState::Resolved(42);
    let mapped_resolved = resolved.map_err(|e: String| format!("Error: {}", e));
    assert_eq!(mapped_resolved, FutureState::Resolved(42));
}

#[tokio::test]
async fn test_future_handle_clone() {
    with_async_test_isolate(|| async {
        with_async_component_id("CloneFutureComponent", |_context| async {
            let handle = use_future(|| async { Ok::<i32, String>(42) }, ());
            let handle_clone = handle.clone();

            // Wait for completion
            sleep(Duration::from_millis(50)).await;

            // Both handles should see the same state
            assert_eq!(handle.state(), handle_clone.state());
            assert_eq!(handle.value(), handle_clone.value());
            assert!(handle.is_resolved(), "Original handle should be resolved");
            assert!(
                handle_clone.is_resolved(),
                "Cloned handle should be resolved"
            );
        })
        .await;
    })
    .await;
}

/// Test optimized state access methods to ensure they don't clone unnecessarily
#[tokio::test]
async fn test_optimized_state_access() {
    with_async_test_isolate(|| async {
        with_async_component_id("OptimizedStateComponent", |_context| async {
            let handle = use_future(
                || async { Ok::<String, String>("test_value".to_string()) },
                (),
            );

            // Wait for completion
            sleep(Duration::from_millis(50)).await;

            // Test optimized methods
            assert!(
                handle.is_resolved(),
                "Future should be resolved after waiting"
            );
            assert!(!handle.is_pending());
            assert!(!handle.is_error());

            // Test value extraction
            assert_eq!(handle.value(), Some("test_value".to_string()));
            assert_eq!(handle.error(), None);
        })
        .await;
    })
    .await;
}

/// Test memory cleanup when future hook is dropped
#[tokio::test]
async fn test_memory_cleanup_on_drop() {
    static DROP_COUNT: AtomicUsize = AtomicUsize::new(0);

    #[derive(Clone)]
    struct DropTracker;

    impl Drop for DropTracker {
        fn drop(&mut self) {
            DROP_COUNT.fetch_add(1, Ordering::SeqCst);
        }
    }

    with_async_test_isolate(|| async {
        let _initial_count = DROP_COUNT.load(Ordering::SeqCst);

        {
            let _handle = with_async_hook_context(|_context| async {
                use_future(
                    || async {
                        let _tracker = DropTracker;
                        Ok::<String, String>("test".to_string())
                    },
                    (),
                )
            })
            .await;

            // Wait for future to complete
            sleep(Duration::from_millis(10)).await;
        } // handle dropped here

        // Give some time for cleanup
        sleep(Duration::from_millis(10)).await;

        // The test passes if no panic occurs during cleanup
        // The actual memory cleanup is tested by the Drop implementation
        // which is automatically called when the handle goes out of scope

        // We can't reliably test the drop count in this async environment
        // because the DropTracker is inside the async closure and may not
        // be dropped in a predictable way. The important thing is that
        // the Drop implementation exists and will be called.
    })
    .await;
}

/// Test that cancellation properly cleans up resources
#[tokio::test]
async fn test_cancellation_cleanup() {
    with_async_test_isolate(|| async {
        let handle = with_async_hook_context(|_context| async {
            use_future(
                || async {
                    // Long-running future
                    sleep(Duration::from_secs(10)).await;
                    Ok::<String, String>("should_not_complete".to_string())
                },
                (),
            )
        })
        .await;

        // Verify it's pending
        assert!(handle.is_pending());

        // Cancel the future
        handle.cancel();

        // Give some time for cancellation to take effect
        sleep(Duration::from_millis(10)).await;

        // Future should still be in pending state since it was cancelled
        // (cancellation doesn't change the state, it just stops the task)
        assert!(handle.is_pending());
    })
    .await;
}

/// Test performance of state access methods under concurrent load
#[tokio::test]
async fn test_concurrent_state_access_performance() {
    with_async_test_isolate(|| async {
        with_async_component_id("ConcurrentStateComponent", |_context| async {
            let handle = use_future(|| async { Ok::<i32, String>(42) }, ());

            // Wait for completion
            sleep(Duration::from_millis(50)).await;

            let handle = Arc::new(handle);
            let mut tasks = Vec::new();

            // Spawn multiple concurrent tasks accessing state
            for _ in 0..100 {
                let handle_clone = handle.clone();
                tasks.push(tokio::spawn(async move {
                    // Perform multiple state accesses
                    for _ in 0..10 {
                        let _ = handle_clone.is_resolved();
                        let _ = handle_clone.is_pending();
                        let _ = handle_clone.is_error();
                        let _ = handle_clone.value();
                        let _ = handle_clone.error();
                    }
                }));
            }

            // Wait for all tasks to complete
            for task in tasks {
                task.await.expect("Task should complete successfully");
            }

            // Verify state is still correct
            assert!(handle.is_resolved());
            assert_eq!(handle.value(), Some(42));
        })
        .await;
    })
    .await;
}

/// Benchmark test to demonstrate performance improvements
/// This test shows the difference between optimized and non-optimized state access
#[test]
fn test_state_access_performance_comparison() {
    use std::time::Instant;

    // Create a mock future handle with resolved state
    let handle = FutureHandle::<String, String>::new();
    handle.set_state(FutureState::Resolved("test_value".to_string()));

    let iterations = 10000;

    // Test optimized methods
    let start = Instant::now();
    for _ in 0..iterations {
        let _ = handle.is_resolved();
        let _ = handle.is_pending();
        let _ = handle.is_error();
        let _ = handle.value();
        let _ = handle.error();
    }
    let optimized_duration = start.elapsed();

    println!(
        "Optimized state access for {} iterations: {:?}",
        iterations, optimized_duration
    );

    // The optimized version should be significantly faster than the old approach
    // because it avoids cloning the entire state for each access
    assert!(
        optimized_duration.as_millis() < 100,
        "Optimized access should be fast"
    );
}

/// Security test: Verify resource exhaustion protection
#[tokio::test]
async fn test_resource_exhaustion_protection() {
    with_async_test_isolate(|| async {
        with_async_component_id("ResourceTestComponent", |_context| async {
            // This test verifies that the security limits are in place
            // We can't easily test the actual limits without spawning many futures
            // but we can verify the error handling works

            let handle = use_future(|| async { Ok::<String, String>("test".to_string()) }, ());

            // Wait for completion
            sleep(Duration::from_millis(50)).await;

            // Should complete successfully under normal conditions
            assert!(
                handle.is_resolved(),
                "Normal future should complete successfully"
            );
        })
        .await;
    })
    .await;
}

/// Security test: Verify error handling provides detailed error information
#[tokio::test]
async fn test_error_handling_security() {
    with_async_test_isolate(|| async {
        with_async_component_id("ErrorTestComponent", |_context| async {
            let handle = use_future(
                || async {
                    // Simulate a security-related error
                    Err::<String, String>("Security violation: unauthorized access".to_string())
                },
                (),
            );

            // Wait for the error to be handled
            sleep(Duration::from_millis(50)).await;

            // Should be in error state with detailed error information
            assert!(handle.is_error(), "Failed future should be in error state");

            if let Some(error) = handle.error() {
                assert!(
                    error.contains("Security violation"),
                    "Error should contain detailed security information, got: {}",
                    error
                );
            } else {
                panic!("Expected error information from failed future");
            }
        })
        .await;
    })
    .await;
}