rskit-bootstrap 0.2.0-alpha.2

Application lifecycle orchestration: typestate App, Component registry, hooks
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
//! Behavioral tests for application lifecycle, registry ordering, and hooks.

use std::sync::{
    Arc,
    atomic::{AtomicBool, AtomicUsize, Ordering},
};
use std::time::Duration;

use parking_lot::Mutex;

use async_trait::async_trait;
use rskit_bootstrap::component::Component;
use rskit_bootstrap::{
    AppBuilder, CancellationToken, Health, HealthStatus, LazyComponent, Registry, RegistryConfig,
};
use rskit_config::{AppConfig, ServiceConfig};
use rskit_errors::{AppError, AppResult, ErrorCode};

// ── Test config ──────────────────────────────────────────────────────────────

#[derive(Debug, Default, serde::Deserialize)]
struct TestCfg {
    #[serde(default)]
    service: ServiceConfig,
}

impl rskit_validation::Validate for TestCfg {
    fn validate(&self) -> Result<(), validator::ValidationErrors> {
        rskit_validation::Validate::validate(&self.service)
    }
}

impl rskit_config::AppConfig for TestCfg {
    fn apply_defaults(&mut self) {}
    fn service_config(&self) -> &ServiceConfig {
        &self.service
    }
}

// ── Mock components ──────────────────────────────────────────────────────────

struct MockComponent {
    name: String,
    start_count: Arc<AtomicUsize>,
    stop_count: Arc<AtomicUsize>,
    healthy: bool,
    fail_on_start: Option<String>,
    fail_on_stop: Option<String>,
    start_order: Option<Arc<Mutex<Vec<String>>>>,
    stop_order: Option<Arc<Mutex<Vec<String>>>>,
    start_delay: Option<Duration>,
}

impl MockComponent {
    fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            start_count: Arc::new(AtomicUsize::new(0)),
            stop_count: Arc::new(AtomicUsize::new(0)),
            healthy: true,
            fail_on_start: None,
            fail_on_stop: None,
            start_order: None,
            stop_order: None,
            start_delay: None,
        }
    }

    fn with_fail_on_start(mut self, msg: impl Into<String>) -> Self {
        self.fail_on_start = Some(msg.into());
        self
    }

    fn with_fail_on_stop(mut self, msg: impl Into<String>) -> Self {
        self.fail_on_stop = Some(msg.into());
        self
    }

    fn with_start_order(mut self, order: Arc<Mutex<Vec<String>>>) -> Self {
        self.start_order = Some(order);
        self
    }

    fn with_stop_order(mut self, order: Arc<Mutex<Vec<String>>>) -> Self {
        self.stop_order = Some(order);
        self
    }

    fn with_unhealthy(mut self) -> Self {
        self.healthy = false;
        self
    }

    fn with_start_delay(mut self, delay: Duration) -> Self {
        self.start_delay = Some(delay);
        self
    }
}

#[async_trait]
impl Component for MockComponent {
    fn name(&self) -> &str {
        &self.name
    }

    async fn start(&self) -> AppResult<()> {
        if let Some(delay) = self.start_delay {
            tokio::time::sleep(delay).await;
        }
        if let Some(msg) = &self.fail_on_start {
            return Err(AppError::service_unavailable(msg.clone()));
        }
        self.start_count.fetch_add(1, Ordering::SeqCst);
        if let Some(order) = &self.start_order {
            order.lock().push(self.name.clone());
        }
        Ok(())
    }

    async fn stop(&self) -> AppResult<()> {
        if let Some(msg) = &self.fail_on_stop {
            return Err(AppError::service_unavailable(msg.clone()));
        }
        self.stop_count.fetch_add(1, Ordering::SeqCst);
        if let Some(order) = &self.stop_order {
            order.lock().push(self.name.clone());
        }
        Ok(())
    }

    fn health(&self) -> Health {
        if self.healthy {
            Health::healthy(&self.name)
        } else {
            Health::unhealthy(&self.name, "mock unhealthy")
        }
    }
}

// ── 1. AppBuilder construction and validation ────────────────────────────────

#[tokio::test]
async fn app_builder_default_config() {
    let cfg = TestCfg::default();
    let app = AppBuilder::new(cfg).build();
    assert!(app.is_ok(), "default config should build successfully");
}

#[tokio::test]
async fn app_builder_with_graceful_timeout() {
    let cfg = TestCfg::default();
    let app = AppBuilder::new(cfg)
        .with_graceful_timeout(Duration::from_secs(5))
        .build();
    assert!(app.is_ok());
}

#[tokio::test]
async fn app_builder_with_component() {
    let cfg = TestCfg::default();
    let comp = Arc::new(MockComponent::new("db"));
    let app = AppBuilder::new(cfg).with_component(comp).build();
    assert!(app.is_ok());
}

#[tokio::test]
async fn app_builder_with_multiple_components() {
    let cfg = TestCfg::default();
    let app = AppBuilder::new(cfg)
        .with_component(Arc::new(MockComponent::new("db")))
        .with_component(Arc::new(MockComponent::new("cache")))
        .with_component(Arc::new(MockComponent::new("kafka")))
        .build();
    assert!(app.is_ok());
}

// ── 2. Component registration and ordering ───────────────────────────────────

#[tokio::test]
async fn registry_start_order_matches_registration() {
    let order: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));

    let mut reg = Registry::new();
    reg.register(Arc::new(
        MockComponent::new("alpha").with_start_order(order.clone()),
    ));
    reg.register(Arc::new(
        MockComponent::new("beta").with_start_order(order.clone()),
    ));
    reg.register(Arc::new(
        MockComponent::new("gamma").with_start_order(order.clone()),
    ));

    reg.start_all().await.unwrap();
    let started = order.lock();
    assert_eq!(*started, vec!["alpha", "beta", "gamma"]);
}

#[tokio::test]
async fn registry_stop_order_is_lifo() {
    let stop_order: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));

    let mut reg = Registry::new();
    reg.register(Arc::new(
        MockComponent::new("first").with_stop_order(stop_order.clone()),
    ));
    reg.register(Arc::new(
        MockComponent::new("second").with_stop_order(stop_order.clone()),
    ));
    reg.register(Arc::new(
        MockComponent::new("third").with_stop_order(stop_order.clone()),
    ));

    reg.start_all().await.unwrap();
    reg.stop_all().await.unwrap();

    let order = stop_order.lock();
    assert_eq!(*order, vec!["third", "second", "first"]);
}

// ── 3. Lifecycle hook ordering ───────────────────────────────────────────────

#[tokio::test]
async fn lifecycle_hooks_execute_in_order() {
    let order: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
    let cfg = TestCfg::default();

    let o1 = order.clone();
    let o2 = order.clone();
    let o3 = order.clone();
    let o4 = order.clone();

    let app = AppBuilder::new(cfg)
        .build()
        .unwrap()
        .before_start(move |_tok| {
            let o = o1.clone();
            async move {
                o.lock().push("configure".into());
                Ok(())
            }
        })
        .before_start(move |_tok| {
            let o = o2.clone();
            async move {
                o.lock().push("start".into());
                Ok(())
            }
        })
        .after_start(move |_tok| {
            let o = o3.clone();
            async move {
                o.lock().push("ready".into());
                Ok(())
            }
        })
        .before_stop(move |_tok| {
            let o = o4.clone();
            async move {
                o.lock().push("stop".into());
                Ok(())
            }
        });

    let result = app.run_task(|_cfg, _cancel| async move { Ok(()) }).await;
    assert!(result.is_ok());

    let executed = order.lock();
    assert_eq!(
        *executed,
        vec!["configure", "start", "ready", "stop"],
        "run_task: configure → start → ready → task → stop"
    );
}

// ── 4. Hook error propagation ────────────────────────────────────────────────

#[tokio::test]
async fn configure_hook_error_propagates() {
    let cfg = TestCfg::default();
    let app = AppBuilder::new(cfg)
        .build()
        .unwrap()
        .before_start(|_tok| async { Err(AppError::service_unavailable("configure boom")) });

    let result = app.run_task(|_cfg, _cancel| async move { Ok(()) }).await;
    assert!(result.is_err());
}

#[tokio::test]
async fn start_hook_error_propagates() {
    let cfg = TestCfg::default();
    let app = AppBuilder::new(cfg)
        .build()
        .unwrap()
        .before_start(|_tok| async { Err(AppError::service_unavailable("start boom")) });

    let result = app.run_task(|_cfg, _cancel| async move { Ok(()) }).await;
    assert!(result.is_err());
}

#[tokio::test]
async fn stop_hook_error_propagates() {
    let cfg = TestCfg::default();
    let app = AppBuilder::new(cfg)
        .build()
        .unwrap()
        .before_stop(|_tok| async { Err(AppError::service_unavailable("stop boom")) });

    let result = app.run_task(|_cfg, _cancel| async move { Ok(()) }).await;
    assert!(result.is_err());
}

#[tokio::test]
async fn run_task_preserves_task_error_when_shutdown_also_fails() {
    let cfg = TestCfg::default();
    let app = AppBuilder::new(cfg)
        .build()
        .unwrap()
        .before_stop(|_tok| async { Err(AppError::service_unavailable("stop boom")) });

    let error = app
        .run_task(|_cfg, _cancel| async move { Err(AppError::invalid_input("task", "task boom")) })
        .await
        .expect_err("task failure should remain primary");

    assert_eq!(error.code(), ErrorCode::InvalidInput);
    assert!(error.message().contains("application shutdown also failed"));
    assert!(
        error
            .details()
            .get("shutdown_error")
            .is_some_and(|detail| detail.as_str().is_some_and(|msg| msg.contains("stop boom")))
    );
}

#[tokio::test]
async fn hook_error_stops_subsequent_hooks() {
    let second_called = Arc::new(AtomicBool::new(false));
    let second_called_clone = second_called.clone();

    let cfg = TestCfg::default();
    let app = AppBuilder::new(cfg)
        .build()
        .unwrap()
        .before_start(|_tok| async { Err(AppError::service_unavailable("first fails")) })
        .before_start(move |_tok| {
            let called = second_called_clone.clone();
            async move {
                called.store(true, Ordering::SeqCst);
                Ok(())
            }
        });

    let _ = app.run_task(|_cfg, _cancel| async move { Ok(()) }).await;
    assert!(
        !second_called.load(Ordering::SeqCst),
        "second hook should not run after first fails"
    );
}

// ── 5. start_all_concurrent with cancellation ────────────────────────────────

#[tokio::test]
async fn start_all_concurrent_succeeds() {
    let config = RegistryConfig {
        concurrency: 0, // unlimited
        start_timeout: Duration::from_secs(5),
        stop_timeout: Duration::from_secs(5),
    };
    let mut reg = Registry::with_config(config);

    let start_order: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
    for name in ["a", "b", "c"] {
        reg.register(Arc::new(
            MockComponent::new(name).with_start_order(start_order.clone()),
        ));
    }

    let cancel = CancellationToken::new();
    let result = reg.start_all_concurrent(cancel).await;
    assert!(result.is_ok());

    let started = start_order.lock();
    assert_eq!(started.len(), 3, "all 3 should have started");
}

#[tokio::test]
async fn start_all_concurrent_with_cancellation() {
    let config = RegistryConfig {
        concurrency: 1, // sequential for predictability
        start_timeout: Duration::from_secs(5),
        stop_timeout: Duration::from_secs(5),
    };
    let mut reg = Registry::with_config(config);

    // First component is slow enough to be cancelled
    reg.register(Arc::new(
        MockComponent::new("slow").with_start_delay(Duration::from_secs(10)),
    ));
    reg.register(Arc::new(MockComponent::new("fast")));

    let cancel = CancellationToken::new();
    let cancel_clone = cancel.clone();

    // Cancel after a short delay
    tokio::spawn(async move {
        tokio::time::sleep(Duration::from_millis(50)).await;
        cancel_clone.cancel();
    });

    let result = reg.start_all_concurrent(cancel).await;
    // Should complete (cancelled) without hanging
    assert!(result.is_ok(), "cancellation should return Ok");
}

#[tokio::test]
async fn start_all_concurrent_error_propagates() {
    let config = RegistryConfig {
        concurrency: 2,
        start_timeout: Duration::from_secs(5),
        stop_timeout: Duration::from_secs(5),
    };
    let mut reg = Registry::with_config(config);

    reg.register(Arc::new(MockComponent::new("ok")));
    reg.register(Arc::new(
        MockComponent::new("fail").with_fail_on_start("concurrent boom"),
    ));

    let cancel = CancellationToken::new();
    let result = reg.start_all_concurrent(cancel).await;
    assert!(result.is_err());
}

// ── 6. stop_all error collection ─────────────────────────────────────────────

#[tokio::test]
async fn stop_all_continues_on_error() {
    let stop_order: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));

    let mut reg = Registry::new();
    reg.register(Arc::new(
        MockComponent::new("first").with_stop_order(stop_order.clone()),
    ));
    // Middle component fails on stop
    reg.register(Arc::new(
        MockComponent::new("fail-middle").with_fail_on_stop("stop error"),
    ));
    reg.register(Arc::new(
        MockComponent::new("last").with_stop_order(stop_order.clone()),
    ));

    reg.start_all().await.unwrap();
    let result = reg.stop_all().await;
    assert!(result.is_err());

    let order = stop_order.lock();
    // Both non-failing components should have stopped (reverse order)
    assert_eq!(*order, vec!["last", "first"]);
}

// ── 7. health_all aggregation ────────────────────────────────────────────────

#[test]
fn health_all_empty_registry() {
    let reg = Registry::new();
    let healths = reg.health_all();
    assert!(healths.is_empty());
}

#[test]
fn health_all_mixed_statuses() {
    let mut reg = Registry::new();
    reg.register(Arc::new(MockComponent::new("healthy-comp")));
    reg.register(Arc::new(
        MockComponent::new("unhealthy-comp").with_unhealthy(),
    ));

    let healths = reg.health_all();
    assert_eq!(healths.len(), 2);
    assert_eq!(healths[0].status, HealthStatus::Healthy);
    assert_eq!(healths[1].status, HealthStatus::Unhealthy);
}

#[test]
fn health_all_preserves_component_names() {
    let mut reg = Registry::new();
    reg.register(Arc::new(MockComponent::new("db")));
    reg.register(Arc::new(MockComponent::new("cache")));
    reg.register(Arc::new(MockComponent::new("queue")));

    let healths = reg.health_all();
    let names: Vec<&str> = healths.iter().map(|h| h.name.as_str()).collect();
    assert_eq!(names, vec!["db", "cache", "queue"]);
}

// ── 8. LazyComponent initialization + health ─────────────────────────────────

#[tokio::test]
async fn lazy_component_defers_construction() {
    let constructed = Arc::new(AtomicBool::new(false));
    let constructed_clone = constructed.clone();

    let lazy = LazyComponent::new("lazy-db", move || {
        constructed_clone.store(true, Ordering::SeqCst);
        Arc::new(MockComponent::new("inner-db")) as Arc<dyn Component>
    });

    // Before start, factory should not have been called
    assert!(
        !constructed.load(Ordering::SeqCst),
        "factory should not be called before start"
    );

    // Health before start should be healthy (lazy default)
    assert!(lazy.health().is_healthy());

    // Start triggers construction
    lazy.start().await.unwrap();
    assert!(
        constructed.load(Ordering::SeqCst),
        "factory should be called after start"
    );
}

#[tokio::test]
async fn lazy_component_stop_before_start_is_noop() {
    let lazy = LazyComponent::new("lazy-noop", || {
        Arc::new(MockComponent::new("inner")) as Arc<dyn Component>
    });

    // Stop before start should succeed
    let result = lazy.stop().await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn lazy_component_health_delegates_after_start() {
    let lazy = LazyComponent::new("lazy-health", || {
        Arc::new(MockComponent::new("inner").with_unhealthy()) as Arc<dyn Component>
    });

    // Before start: healthy (lazy default)
    assert!(lazy.health().is_healthy());

    // After start: delegates to inner component
    lazy.start().await.unwrap();
    assert!(!lazy.health().is_healthy());
}

#[tokio::test]
async fn lazy_component_name_is_correct() {
    let lazy = LazyComponent::new("my-lazy", || {
        Arc::new(MockComponent::new("inner")) as Arc<dyn Component>
    });
    assert_eq!(lazy.name(), "my-lazy");
}

// ── 9. Registry config (concurrency, timeouts) ──────────────────────────────

#[test]
fn registry_config_defaults() {
    let config = RegistryConfig::default();
    assert_eq!(config.concurrency, 1);
    assert_eq!(config.start_timeout, Duration::from_secs(30));
    assert_eq!(config.stop_timeout, Duration::from_secs(30));
}

#[test]
fn registry_with_custom_config() {
    let config = RegistryConfig {
        concurrency: 4,
        start_timeout: Duration::from_secs(10),
        stop_timeout: Duration::from_secs(5),
    };
    let reg = Registry::with_config(config);
    assert!(reg.is_empty());
    assert_eq!(reg.len(), 0);
}

#[tokio::test]
async fn registry_concurrent_with_limited_concurrency() {
    let config = RegistryConfig {
        concurrency: 2,
        start_timeout: Duration::from_secs(5),
        stop_timeout: Duration::from_secs(5),
    };
    let mut reg = Registry::with_config(config);

    for i in 0..5 {
        reg.register(Arc::new(MockComponent::new(format!("comp-{}", i))));
    }

    let cancel = CancellationToken::new();
    let result = reg.start_all_concurrent(cancel).await;
    assert!(result.is_ok());
    assert_eq!(reg.len(), 5);
}

// ── 10. Graceful timeout handling ────────────────────────────────────────────

#[tokio::test]
async fn graceful_timeout_on_slow_stop_hook() {
    let cfg = TestCfg::default();
    let app = AppBuilder::new(cfg)
        .with_graceful_timeout(Duration::from_millis(100))
        .build()
        .unwrap()
        .before_stop(|_tok| async {
            tokio::time::sleep(Duration::from_secs(10)).await;
            Ok(())
        });

    // Use the shutdown_token to trigger shutdown via run() (which uses graceful_shutdown)
    let token = app.shutdown_token();
    tokio::spawn(async move {
        tokio::time::sleep(Duration::from_millis(50)).await;
        token.cancel();
    });

    let result = tokio::time::timeout(Duration::from_secs(5), app.run()).await;

    assert!(result.is_ok(), "should complete within 5 seconds");
    assert!(result.unwrap().is_err());
}

// ── 11. CancellationToken propagation ────────────────────────────────────────

#[tokio::test]
async fn shutdown_token_accessible() {
    let cfg = TestCfg::default();
    let app = AppBuilder::new(cfg).build().unwrap();
    let token = app.shutdown_token();
    assert!(!token.is_cancelled());
}

#[tokio::test]
async fn cancellation_token_propagated_to_task() {
    let cfg = TestCfg::default();
    let received_token = Arc::new(AtomicBool::new(false));
    let received_clone = received_token.clone();

    let app = AppBuilder::new(cfg).build().unwrap();

    let result = app
        .run_task(move |_cfg, cancel: CancellationToken| {
            let received = received_clone.clone();
            async move {
                received.store(!cancel.is_cancelled(), Ordering::SeqCst);
                Ok(())
            }
        })
        .await;

    assert!(result.is_ok());
    assert!(
        received_token.load(Ordering::SeqCst),
        "task should receive a valid (not cancelled) token"
    );
}

// ── 12. run_task completion triggers shutdown ─────────────────────────────────

#[tokio::test]
async fn run_task_completes_and_stops() {
    let stop_order: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
    let cfg = TestCfg::default();
    let comp = Arc::new(MockComponent::new("db").with_stop_order(stop_order.clone()));

    let app = AppBuilder::new(cfg).with_component(comp).build().unwrap();

    let result = app.run_task(|_cfg, _cancel| async { Ok(()) }).await;

    assert!(result.is_ok());
    let order = stop_order.lock();
    assert_eq!(*order, vec!["db"], "components should stop after task");
}

#[tokio::test]
async fn run_task_error_still_shuts_down() {
    let comp = Arc::new(MockComponent::new("db"));
    let comp_stop = comp.stop_count.clone();

    let cfg = TestCfg::default();
    let app = AppBuilder::new(cfg).with_component(comp).build().unwrap();

    let result = app
        .run_task(|_cfg, _cancel| async {
            Err::<(), AppError>(AppError::service_unavailable("task failed"))
        })
        .await;

    assert!(result.is_err());
    assert_eq!(
        comp_stop.load(Ordering::SeqCst),
        1,
        "component should be stopped even when task fails"
    );
}

#[tokio::test]
async fn run_task_receives_config() {
    let cfg = TestCfg::default();
    let app = AppBuilder::new(cfg).build().unwrap();

    let result = app
        .run_task(|cfg: Arc<TestCfg>, _cancel| async move {
            // Verify we can access config
            let _ = cfg.service_config();
            Ok(())
        })
        .await;

    assert!(result.is_ok());
}

// ── 13. Empty registry operations ────────────────────────────────────────────

#[tokio::test]
async fn empty_registry_start_all() {
    let reg = Registry::new();
    let result = reg.start_all().await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn empty_registry_stop_all() {
    let reg = Registry::new();
    reg.stop_all().await.unwrap();
    // Should complete without panic
}

#[tokio::test]
async fn empty_registry_start_all_concurrent() {
    let reg = Registry::new();
    let cancel = CancellationToken::new();
    let result = reg.start_all_concurrent(cancel).await;
    assert!(result.is_ok());
}

#[test]
fn empty_registry_health_all() {
    let reg = Registry::new();
    assert!(reg.health_all().is_empty());
}

#[test]
fn empty_registry_len_and_is_empty() {
    let reg = Registry::new();
    assert_eq!(reg.len(), 0);
    assert!(reg.is_empty());

    let mut reg2 = Registry::new();
    reg2.register(Arc::new(MockComponent::new("x")));
    assert_eq!(reg2.len(), 1);
    assert!(!reg2.is_empty());
}

// ── Health type tests ────────────────────────────────────────────────────────

#[test]
fn health_healthy_constructor() {
    let h = Health::healthy("comp");
    assert_eq!(h.name, "comp");
    assert_eq!(h.status, HealthStatus::Healthy);
    assert!(h.message.is_none());
    assert!(h.is_healthy());
}

#[test]
fn health_degraded_constructor() {
    let h = Health::degraded("comp", "high latency");
    assert_eq!(h.status, HealthStatus::Degraded);
    assert_eq!(h.message, Some("high latency".to_string()));
    assert!(!h.is_healthy());
}

#[test]
fn health_unhealthy_constructor() {
    let h = Health::unhealthy("comp", "connection refused");
    assert_eq!(h.status, HealthStatus::Unhealthy);
    assert_eq!(h.message, Some("connection refused".to_string()));
    assert!(!h.is_healthy());
}

#[test]
fn health_status_display() {
    assert_eq!(format!("{}", HealthStatus::Healthy), "healthy");
    assert_eq!(format!("{}", HealthStatus::Degraded), "degraded");
    assert_eq!(format!("{}", HealthStatus::Unhealthy), "unhealthy");
}

#[test]
fn health_equality() {
    let h1 = Health::healthy("a");
    let h2 = Health::healthy("a");
    assert_eq!(h1, h2);

    let h3 = Health::healthy("b");
    assert_ne!(h1, h3);
}

// ── App with components full lifecycle ───────────────────────────────────────

#[tokio::test]
async fn full_lifecycle_with_components_and_hooks() {
    let order: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
    let cfg = TestCfg::default();

    let o_start = order.clone();
    let o_stop = order.clone();
    let comp = Arc::new(
        MockComponent::new("db")
            .with_start_order(order.clone())
            .with_stop_order(order.clone()),
    );

    let app = AppBuilder::new(cfg)
        .with_component(comp)
        .build()
        .unwrap()
        .before_start(move |_tok| {
            let o = o_start.clone();
            async move {
                o.lock().push("hook:start".into());
                Ok(())
            }
        })
        .before_stop(move |_tok| {
            let o = o_stop.clone();
            async move {
                o.lock().push("hook:stop".into());
                Ok(())
            }
        });

    let result = app.run_task(|_cfg, _cancel| async { Ok(()) }).await;
    assert!(result.is_ok());

    let executed = order.lock();
    assert_eq!(*executed, vec!["hook:start", "db", "hook:stop", "db"],);
}

#[tokio::test]
async fn component_start_failure_prevents_task() {
    let cfg = TestCfg::default();
    let comp = Arc::new(MockComponent::new("bad").with_fail_on_start("connection refused"));

    let task_ran = Arc::new(AtomicBool::new(false));
    let task_ran_clone = task_ran.clone();

    let app = AppBuilder::new(cfg).with_component(comp).build().unwrap();

    let result = app
        .run_task(move |_cfg, _cancel| {
            let ran = task_ran_clone.clone();
            async move {
                ran.store(true, Ordering::SeqCst);
                Ok(())
            }
        })
        .await;

    assert!(result.is_err());
    assert!(
        !task_ran.load(Ordering::SeqCst),
        "task should not run when component fails to start"
    );
}

#[tokio::test]
async fn app_config_accessible_from_task() {
    let cfg = TestCfg::default();
    let app = AppBuilder::new(cfg).build().unwrap();

    let result = app
        .run_task(|cfg: Arc<TestCfg>, _cancel| async move {
            let sc = cfg.service_config();
            assert_eq!(sc.name, "service"); // default name
            Ok(())
        })
        .await;

    assert!(result.is_ok());
}