drasi-lib 0.6.0

Embedded Drasi for in-process data change processing using continuous queries
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
// Copyright 2025 The Drasi Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#[cfg(test)]
pub(crate) mod manager_tests {
    use super::super::*;
    use crate::channels::*;
    use crate::test_helpers::wait_for_component_status;
    use anyhow::Result;
    use async_trait::async_trait;
    use std::collections::HashMap;
    use std::sync::Arc;
    use std::time::Duration;
    use tokio::sync::RwLock;

    struct MockQueryProvider;

    #[async_trait]
    impl QueryProvider for MockQueryProvider {
        async fn get_query_instance(
            &self,
            id: &str,
        ) -> anyhow::Result<Arc<dyn crate::queries::Query>> {
            Err(anyhow::anyhow!("No query '{id}' in test"))
        }
    }

    /// A simple test mock reaction for unit testing the ReactionManager.
    pub struct TestMockReaction {
        id: String,
        queries: Vec<String>,
        auto_start: bool,
        /// Status handle — always available, wired to graph during initialize().
        status_handle: crate::component_graph::ComponentStatusHandle,
    }

    impl TestMockReaction {
        pub fn new(id: String, queries: Vec<String>) -> Self {
            let status_handle = crate::component_graph::ComponentStatusHandle::new(&id);
            Self {
                id,
                queries,
                auto_start: true,
                status_handle,
            }
        }

        pub fn with_auto_start(id: String, queries: Vec<String>, auto_start: bool) -> Self {
            let status_handle = crate::component_graph::ComponentStatusHandle::new(&id);
            Self {
                id,
                queries,
                auto_start,
                status_handle,
            }
        }
    }

    #[async_trait]
    impl crate::reactions::Reaction for TestMockReaction {
        fn id(&self) -> &str {
            &self.id
        }

        fn type_name(&self) -> &str {
            "log"
        }

        fn properties(&self) -> HashMap<String, serde_json::Value> {
            HashMap::new()
        }

        fn query_ids(&self) -> Vec<String> {
            self.queries.clone()
        }

        fn auto_start(&self) -> bool {
            self.auto_start
        }

        async fn initialize(&self, context: crate::context::ReactionRuntimeContext) {
            self.status_handle.wire(context.update_tx.clone()).await;
        }

        async fn start(&self) -> anyhow::Result<()> {
            self.status_handle
                .set_status(
                    ComponentStatus::Starting,
                    Some("Starting reaction".to_string()),
                )
                .await;
            self.status_handle
                .set_status(
                    ComponentStatus::Running,
                    Some("Reaction started".to_string()),
                )
                .await;
            Ok(())
        }

        async fn stop(&self) -> anyhow::Result<()> {
            self.status_handle
                .set_status(
                    ComponentStatus::Stopping,
                    Some("Stopping reaction".to_string()),
                )
                .await;
            self.status_handle
                .set_status(
                    ComponentStatus::Stopped,
                    Some("Reaction stopped".to_string()),
                )
                .await;
            Ok(())
        }

        async fn status(&self) -> ComponentStatus {
            self.status_handle.get_status().await
        }

        async fn enqueue_query_result(&self, _result: QueryResult) -> Result<()> {
            Ok(())
        }
    }

    /// Helper to create a TestMockReaction instance
    pub fn create_test_mock_reaction(id: String, queries: Vec<String>) -> TestMockReaction {
        TestMockReaction::new(id, queries)
    }

    async fn create_test_manager() -> (
        Arc<ReactionManager>,
        Arc<tokio::sync::RwLock<crate::component_graph::ComponentGraph>>,
    ) {
        // Use the global shared log registry for test isolation with tracing
        let log_registry = crate::managers::get_or_init_global_registry();
        let (graph, update_rx) = crate::component_graph::ComponentGraph::new("test-instance");
        let update_tx = graph.update_sender();
        let graph = Arc::new(tokio::sync::RwLock::new(graph));

        // Spawn a mini graph update loop for tests
        {
            let graph_clone = graph.clone();
            tokio::spawn(async move {
                let mut rx = update_rx;
                while let Some(update) = rx.recv().await {
                    let mut g = graph_clone.write().await;
                    g.apply_update(update);
                }
            });
        }

        let manager = Arc::new(ReactionManager::new(
            "test-instance",
            log_registry,
            graph.clone(),
            update_tx,
        ));
        // Inject mock QueryProvider so add_reaction() can construct ReactionRuntimeContext
        manager
            .inject_query_provider(Arc::new(MockQueryProvider))
            .await;
        (manager, graph)
    }

    /// Create a test manager that also returns the shared graph for event subscription.
    async fn create_test_manager_with_graph() -> (
        Arc<ReactionManager>,
        Arc<tokio::sync::RwLock<crate::component_graph::ComponentGraph>>,
    ) {
        create_test_manager().await
    }

    /// Helper: register a reaction in the graph, then provision it in the manager.
    /// Registers placeholder query nodes for any referenced queries not already in the graph.
    async fn add_reaction(
        manager: &ReactionManager,
        graph: &tokio::sync::RwLock<crate::component_graph::ComponentGraph>,
        reaction: impl crate::reactions::Reaction + 'static,
    ) -> anyhow::Result<()> {
        let reaction_id = reaction.id().to_string();
        let reaction_type = reaction.type_name().to_string();
        let query_ids = reaction.query_ids();
        {
            let mut g = graph.write().await;
            // Ensure referenced queries exist as placeholder nodes
            for qid in &query_ids {
                if !g.contains(qid) {
                    g.register_query(qid, HashMap::new(), &[])?;
                }
            }
            let mut metadata = HashMap::new();
            metadata.insert("kind".to_string(), reaction_type);
            g.register_reaction(&reaction_id, metadata, &query_ids)?;
        }
        manager.provision_reaction(reaction).await
    }

    /// Helper: teardown a reaction in the manager, then deregister from the graph.
    async fn delete_reaction(
        manager: &ReactionManager,
        graph: &tokio::sync::RwLock<crate::component_graph::ComponentGraph>,
        id: &str,
        cleanup: bool,
    ) -> anyhow::Result<()> {
        manager.teardown_reaction(id.to_string(), cleanup).await?;
        let mut g = graph.write().await;
        g.deregister(id)?;
        Ok(())
    }

    #[tokio::test]
    async fn test_add_reaction() {
        let (manager, graph) = create_test_manager().await;

        let reaction =
            create_test_mock_reaction("test-reaction".to_string(), vec!["query1".to_string()]);
        let result = add_reaction(&manager, &graph, reaction).await;

        assert!(result.is_ok());

        // Verify reaction was added
        let reactions = manager.list_reactions().await;
        assert_eq!(reactions.len(), 1);
        assert_eq!(reactions[0].0, "test-reaction");
    }

    #[tokio::test]
    async fn test_add_duplicate_reaction() {
        let (manager, graph) = create_test_manager().await;

        let reaction1 = create_test_mock_reaction("test-reaction".to_string(), vec![]);
        let reaction2 = create_test_mock_reaction("test-reaction".to_string(), vec![]);

        // Add reaction first time
        assert!(add_reaction(&manager, &graph, reaction1).await.is_ok());

        // Try to add same reaction again
        let result = add_reaction(&manager, &graph, reaction2).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("already exists"));
    }

    #[tokio::test]
    async fn test_delete_reaction() {
        let (manager, graph) = create_test_manager().await;

        let reaction = create_test_mock_reaction("test-reaction".to_string(), vec![]);
        add_reaction(&manager, &graph, reaction).await.unwrap();

        // Delete the reaction
        let result = delete_reaction(&manager, &graph, "test-reaction", false).await;
        assert!(result.is_ok());

        // Verify reaction was removed
        let reactions = manager.list_reactions().await;
        assert_eq!(reactions.len(), 0);
    }

    #[tokio::test]
    async fn test_delete_nonexistent_reaction() {
        let (manager, graph) = create_test_manager().await;

        let result = delete_reaction(&manager, &graph, "nonexistent", false).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not found"));
    }

    #[tokio::test]
    async fn test_get_reaction_info() {
        let (manager, graph) = create_test_manager().await;

        let reaction =
            create_test_mock_reaction("test-reaction".to_string(), vec!["query1".to_string()]);
        add_reaction(&manager, &graph, reaction).await.unwrap();

        let retrieved = manager.get_reaction("test-reaction".to_string()).await;
        assert!(retrieved.is_ok());

        let retrieved = retrieved.unwrap();
        assert_eq!(retrieved.id, "test-reaction");
        assert_eq!(retrieved.reaction_type, "log");
        assert_eq!(retrieved.queries, vec!["query1".to_string()]);
    }

    #[tokio::test]
    async fn test_list_reactions_with_status() {
        let (manager, graph) = create_test_manager().await;

        // Add multiple reactions
        let reaction1 = create_test_mock_reaction("reaction1".to_string(), vec![]);
        let reaction2 = create_test_mock_reaction("reaction2".to_string(), vec![]);

        add_reaction(&manager, &graph, reaction1).await.unwrap();
        add_reaction(&manager, &graph, reaction2).await.unwrap();

        let reactions = manager.list_reactions().await;
        assert_eq!(reactions.len(), 2);

        // Both should be in Added state initially
        for (_, status) in reactions {
            assert!(matches!(status, ComponentStatus::Added));
        }
    }

    #[tokio::test]
    async fn test_get_reaction_status() {
        let (manager, graph) = create_test_manager().await;

        let reaction = create_test_mock_reaction("test-reaction".to_string(), vec![]);
        add_reaction(&manager, &graph, reaction).await.unwrap();

        let status = manager
            .get_reaction_status("test-reaction".to_string())
            .await;
        assert!(status.is_ok());
        assert!(matches!(status.unwrap(), ComponentStatus::Added));
    }

    #[tokio::test]
    async fn test_get_nonexistent_reaction_status() {
        let (manager, graph) = create_test_manager().await;

        let status = manager.get_reaction_status("nonexistent".to_string()).await;
        assert!(status.is_err());
        assert!(status.unwrap_err().to_string().contains("not found"));
    }

    /// Test that concurrent add_reaction calls with the same ID are handled atomically.
    /// Only one should succeed, the others should fail with "already exists".
    /// This tests the TOCTOU fix where we use a single write lock for check-and-insert.
    #[tokio::test]
    async fn test_concurrent_add_reaction_same_id() {
        let (manager, graph) = create_test_manager().await;

        // Spawn multiple tasks trying to add a reaction with the same ID concurrently
        let mut handles = Vec::new();
        for i in 0..10 {
            let manager_clone = manager.clone();
            let graph_clone = graph.clone();
            handles.push(tokio::spawn(async move {
                let reaction = create_test_mock_reaction("same-reaction".to_string(), vec![]);
                let result = add_reaction(&manager_clone, &graph_clone, reaction).await;
                (i, result.is_ok())
            }));
        }

        // Wait for all tasks to complete
        let mut success_count = 0;
        let mut failure_count = 0;
        for handle in handles {
            let (_i, succeeded) = handle.await.unwrap();
            if succeeded {
                success_count += 1;
            } else {
                failure_count += 1;
            }
        }

        // Exactly one should succeed, all others should fail
        assert_eq!(success_count, 1, "Exactly one add_reaction should succeed");
        assert_eq!(failure_count, 9, "All other add_reaction calls should fail");

        // Verify only one reaction exists
        let reactions = manager.list_reactions().await;
        assert_eq!(reactions.len(), 1);
        assert_eq!(reactions[0].0, "same-reaction");
    }

    /// Test that concurrent add_reaction calls with different IDs all succeed.
    #[tokio::test]
    async fn test_concurrent_add_reaction_different_ids() {
        let (manager, graph) = create_test_manager().await;

        // Spawn multiple tasks adding reactions with unique IDs
        let mut handles = Vec::new();
        for i in 0..10 {
            let manager_clone = manager.clone();
            let graph_clone = graph.clone();
            handles.push(tokio::spawn(async move {
                let reaction = create_test_mock_reaction(format!("reaction-{i}"), vec![]);
                add_reaction(&manager_clone, &graph_clone, reaction).await
            }));
        }

        // Wait for all tasks to complete
        for handle in handles {
            let result = handle.await.unwrap();
            assert!(
                result.is_ok(),
                "All add_reaction calls with unique IDs should succeed"
            );
        }

        // Verify all 10 reactions exist
        let reactions = manager.list_reactions().await;
        assert_eq!(reactions.len(), 10);
    }

    // ============================================================================
    // Auto-start tests
    // ============================================================================

    /// Test that start_all only starts reactions with auto_start=true
    #[tokio::test]
    async fn test_start_all_respects_auto_start() {
        let (manager, graph) = create_test_manager().await;

        // Add reaction with auto_start=true
        let reaction1 =
            TestMockReaction::with_auto_start("auto-start-reaction".to_string(), vec![], true);
        add_reaction(&manager, &graph, reaction1).await.unwrap();

        // Add reaction with auto_start=false
        let reaction2 =
            TestMockReaction::with_auto_start("no-auto-start-reaction".to_string(), vec![], false);
        add_reaction(&manager, &graph, reaction2).await.unwrap();

        // Subscribe BEFORE the operation that triggers status changes
        let mut event_rx = graph.read().await.subscribe();

        // Start all reactions
        manager.start_all().await.unwrap();

        // Wait for the auto-start reaction to reach Running
        wait_for_component_status(
            &mut event_rx,
            "auto-start-reaction",
            ComponentStatus::Running,
            Duration::from_secs(5),
        )
        .await;

        // Check statuses
        let status1 = manager
            .get_reaction_status("auto-start-reaction".to_string())
            .await
            .unwrap();
        let status2 = manager
            .get_reaction_status("no-auto-start-reaction".to_string())
            .await
            .unwrap();

        assert!(
            matches!(status1, ComponentStatus::Running),
            "Reaction with auto_start=true should be running"
        );
        assert!(
            matches!(status2, ComponentStatus::Added),
            "Reaction with auto_start=false should still be in Added state"
        );
    }

    /// Test that reaction auto_start defaults to true
    #[tokio::test]
    async fn test_reaction_auto_start_defaults_to_true() {
        let (manager, graph) = create_test_manager().await;

        // Create reaction using default constructor (should have auto_start=true)
        let reaction = create_test_mock_reaction("default-reaction".to_string(), vec![]);

        // Verify auto_start is true
        use crate::reactions::Reaction;
        assert!(reaction.auto_start(), "Default auto_start should be true");

        add_reaction(&manager, &graph, reaction).await.unwrap();

        // Subscribe BEFORE the operation that triggers status changes
        let mut event_rx = graph.read().await.subscribe();

        // Start all should start this reaction
        manager.start_all().await.unwrap();

        // Wait for the reaction to reach Running
        wait_for_component_status(
            &mut event_rx,
            "default-reaction",
            ComponentStatus::Running,
            Duration::from_secs(5),
        )
        .await;

        let status = manager
            .get_reaction_status("default-reaction".to_string())
            .await
            .unwrap();
        assert!(
            matches!(status, ComponentStatus::Running),
            "Default reaction should be started by start_all"
        );
    }

    /// Test that reaction with auto_start=false can be manually started
    #[tokio::test]
    async fn test_reaction_auto_start_false_can_be_manually_started() {
        let (manager, graph) = create_test_manager().await;

        // Add reaction with auto_start=false
        let reaction =
            TestMockReaction::with_auto_start("manual-reaction".to_string(), vec![], false);
        add_reaction(&manager, &graph, reaction).await.unwrap();

        // start_all should not start it
        manager.start_all().await.unwrap();

        // No status change expected for auto_start=false; yield to let any queued work complete
        tokio::task::yield_now().await;

        let status = manager
            .get_reaction_status("manual-reaction".to_string())
            .await
            .unwrap();
        assert!(
            matches!(status, ComponentStatus::Added),
            "Reaction with auto_start=false should not be started by start_all"
        );

        // Subscribe BEFORE the operation that triggers status change
        let mut event_rx = graph.read().await.subscribe();

        // Manually start the reaction
        manager
            .start_reaction("manual-reaction".to_string())
            .await
            .unwrap();

        // Wait for the reaction to reach Running
        wait_for_component_status(
            &mut event_rx,
            "manual-reaction",
            ComponentStatus::Running,
            Duration::from_secs(5),
        )
        .await;

        let status = manager
            .get_reaction_status("manual-reaction".to_string())
            .await
            .unwrap();
        assert!(
            matches!(status, ComponentStatus::Running),
            "Reaction with auto_start=false should be manually startable"
        );
    }

    // ============================================================================
    // Cleanup Tests
    // ============================================================================

    /// Test that deleting a reaction cleans up its event history
    #[tokio::test]
    async fn test_delete_reaction_cleans_up_event_history() {
        let (manager, graph) = create_test_manager().await;

        // Add a reaction
        let reaction = create_test_mock_reaction("cleanup-events-reaction".to_string(), vec![]);
        add_reaction(&manager, &graph, reaction).await.unwrap();

        // Record an event manually to simulate lifecycle
        manager
            .record_event(ComponentEvent {
                component_id: "cleanup-events-reaction".to_string(),
                component_type: ComponentType::Reaction,
                status: ComponentStatus::Running,
                timestamp: chrono::Utc::now(),
                message: Some("Test event".to_string()),
            })
            .await;

        // Verify events exist
        let events = manager.get_reaction_events("cleanup-events-reaction").await;
        assert!(!events.is_empty(), "Expected events after recording");

        // Delete the reaction
        delete_reaction(&manager, &graph, "cleanup-events-reaction", false)
            .await
            .unwrap();

        // Verify events are cleaned up
        let events_after = manager.get_reaction_events("cleanup-events-reaction").await;
        assert!(events_after.is_empty(), "Expected no events after deletion");
    }

    /// Test that deleting a reaction cleans up its log history
    #[tokio::test]
    async fn test_delete_reaction_cleans_up_log_history() {
        let (manager, graph) = create_test_manager().await;

        // Add a reaction
        let reaction = create_test_mock_reaction("cleanup-logs-reaction".to_string(), vec![]);
        add_reaction(&manager, &graph, reaction).await.unwrap();

        // Subscribe to create the channel
        let result = manager.subscribe_logs("cleanup-logs-reaction").await;
        assert!(result.is_some(), "Expected to subscribe to reaction logs");

        // Delete the reaction
        delete_reaction(&manager, &graph, "cleanup-logs-reaction", false)
            .await
            .unwrap();

        // Verify logs are cleaned up (subscribe should fail for non-existent reaction)
        let result = manager.subscribe_logs("cleanup-logs-reaction").await;
        assert!(result.is_none(), "Expected None for deleted reaction logs");
    }

    // ========================================================================
    // Deprovision tests
    // ========================================================================

    /// A test reaction that tracks deprovision calls.
    struct DeprovisionTestReaction {
        id: String,
        queries: Vec<String>,
        status_handle: crate::component_graph::ComponentStatusHandle,
        deprovision_called: Arc<std::sync::atomic::AtomicBool>,
    }

    impl DeprovisionTestReaction {
        fn new(id: &str) -> (Self, Arc<std::sync::atomic::AtomicBool>) {
            let deprovision_called = Arc::new(std::sync::atomic::AtomicBool::new(false));
            (
                Self {
                    id: id.to_string(),
                    queries: vec![],
                    status_handle: crate::component_graph::ComponentStatusHandle::new(id),
                    deprovision_called: deprovision_called.clone(),
                },
                deprovision_called,
            )
        }

        fn new_simple(id: &str) -> Self {
            Self {
                id: id.to_string(),
                queries: vec![],
                status_handle: crate::component_graph::ComponentStatusHandle::new(id),
                deprovision_called: Arc::new(std::sync::atomic::AtomicBool::new(false)),
            }
        }
    }

    #[async_trait]
    impl crate::reactions::Reaction for DeprovisionTestReaction {
        fn id(&self) -> &str {
            &self.id
        }

        fn type_name(&self) -> &str {
            "deprovision-test"
        }

        fn properties(&self) -> HashMap<String, serde_json::Value> {
            HashMap::new()
        }

        fn query_ids(&self) -> Vec<String> {
            self.queries.clone()
        }

        fn auto_start(&self) -> bool {
            false
        }

        async fn initialize(&self, context: crate::context::ReactionRuntimeContext) {
            self.status_handle.wire(context.update_tx.clone()).await;
        }

        async fn start(&self) -> anyhow::Result<()> {
            self.status_handle
                .set_status(ComponentStatus::Running, None)
                .await;
            Ok(())
        }

        async fn stop(&self) -> anyhow::Result<()> {
            self.status_handle
                .set_status(ComponentStatus::Stopped, None)
                .await;
            Ok(())
        }

        async fn status(&self) -> ComponentStatus {
            self.status_handle.get_status().await
        }

        async fn deprovision(&self) -> anyhow::Result<()> {
            self.deprovision_called
                .store(true, std::sync::atomic::Ordering::SeqCst);
            Ok(())
        }

        async fn enqueue_query_result(&self, _result: QueryResult) -> Result<()> {
            Ok(())
        }
    }

    #[tokio::test]
    async fn test_delete_reaction_with_cleanup_calls_deprovision() {
        let (manager, graph) = create_test_manager().await;

        let (reaction, deprovision_flag) = DeprovisionTestReaction::new("deprovision-reaction");
        add_reaction(&manager, &graph, reaction).await.unwrap();

        delete_reaction(&manager, &graph, "deprovision-reaction", true)
            .await
            .unwrap();

        assert!(
            deprovision_flag.load(std::sync::atomic::Ordering::SeqCst),
            "deprovision() should have been called"
        );
    }

    #[tokio::test]
    async fn test_delete_reaction_without_cleanup_skips_deprovision() {
        let (manager, graph) = create_test_manager().await;

        let (reaction, deprovision_flag) = DeprovisionTestReaction::new("no-deprovision-reaction");
        add_reaction(&manager, &graph, reaction).await.unwrap();

        delete_reaction(&manager, &graph, "no-deprovision-reaction", false)
            .await
            .unwrap();

        assert!(
            !deprovision_flag.load(std::sync::atomic::Ordering::SeqCst),
            "deprovision() should NOT have been called"
        );
    }

    // ========================================================================
    // Update (replace instance) tests
    // ========================================================================

    #[tokio::test]
    async fn test_update_reaction_replaces_stopped_reaction() {
        let (manager, graph) = create_test_manager().await;

        let reaction = DeprovisionTestReaction::new_simple("reconfig-stopped-reaction");
        add_reaction(&manager, &graph, reaction).await.unwrap();

        let new_reaction = DeprovisionTestReaction::new_simple("reconfig-stopped-reaction");
        manager
            .update_reaction("reconfig-stopped-reaction".to_string(), new_reaction)
            .await
            .unwrap();

        let status = manager
            .get_reaction_status("reconfig-stopped-reaction".to_string())
            .await
            .unwrap();
        assert_eq!(status, ComponentStatus::Stopped);
    }

    #[tokio::test]
    async fn test_update_reaction_stops_and_restarts_running_reaction() {
        let (manager, graph) = create_test_manager().await;

        let reaction = DeprovisionTestReaction::new_simple("reconfig-running-reaction");
        add_reaction(&manager, &graph, reaction).await.unwrap();

        // Subscribe BEFORE the operation that triggers status change
        let mut event_rx = graph.read().await.subscribe();

        manager
            .start_reaction("reconfig-running-reaction".to_string())
            .await
            .unwrap();
        // Wait for async graph update to propagate
        wait_for_component_status(
            &mut event_rx,
            "reconfig-running-reaction",
            ComponentStatus::Running,
            Duration::from_secs(5),
        )
        .await;
        let status = manager
            .get_reaction_status("reconfig-running-reaction".to_string())
            .await
            .unwrap();
        assert_eq!(status, ComponentStatus::Running);

        let new_reaction = DeprovisionTestReaction::new_simple("reconfig-running-reaction");
        manager
            .update_reaction("reconfig-running-reaction".to_string(), new_reaction)
            .await
            .unwrap();

        // Wait for async graph update to propagate
        wait_for_component_status(
            &mut event_rx,
            "reconfig-running-reaction",
            ComponentStatus::Running,
            Duration::from_secs(5),
        )
        .await;
        let status = manager
            .get_reaction_status("reconfig-running-reaction".to_string())
            .await
            .unwrap();
        assert_eq!(status, ComponentStatus::Running);
    }

    #[tokio::test]
    async fn test_update_reaction_emits_reconfiguring_event() {
        let (manager, graph) = create_test_manager_with_graph().await;

        // Subscribe to graph events BEFORE adding reaction
        let mut event_rx = graph.read().await.subscribe();

        let reaction = DeprovisionTestReaction::new_simple("reconfig-event-reaction");
        add_reaction(&manager, &graph, reaction).await.unwrap();

        let new_reaction = DeprovisionTestReaction::new_simple("reconfig-event-reaction");
        manager
            .update_reaction("reconfig-event-reaction".to_string(), new_reaction)
            .await
            .unwrap();

        // Wait for the Reconfiguring event via the broadcast channel
        wait_for_component_status(
            &mut event_rx,
            "reconfig-event-reaction",
            ComponentStatus::Reconfiguring,
            Duration::from_secs(5),
        )
        .await;
    }

    #[tokio::test]
    async fn test_update_reaction_rejects_mismatched_id() {
        let (manager, graph) = create_test_manager().await;

        let reaction = DeprovisionTestReaction::new_simple("original-reaction");
        add_reaction(&manager, &graph, reaction).await.unwrap();

        let new_reaction = DeprovisionTestReaction::new_simple("different-id");
        let result = manager
            .update_reaction("original-reaction".to_string(), new_reaction)
            .await;
        assert!(result.is_err(), "Expected error for mismatched IDs");
        assert!(result.unwrap_err().to_string().contains("does not match"));
    }

    #[tokio::test]
    async fn test_update_nonexistent_reaction() {
        let (manager, graph) = create_test_manager().await;

        let new_reaction = DeprovisionTestReaction::new_simple("nonexistent");
        let result = manager
            .update_reaction("nonexistent".to_string(), new_reaction)
            .await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not found"));
    }
}