arc-core 0.2.3

Event sourcing primitives for arc framework (headless, no web dependencies)
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
//! # Projection Module
//!
//! Three-trait architecture for building read models from event streams:
//!
//! - **[`Projector`]** — stateless event handler (the "machine"). Contains the pure
//!   logic for transforming events into read model writes.
//! - **[`Projection`]** — composed read model unit (the "output"). Ties a projector
//!   to its storage backend.
//! - **[`ReadModelStore`](crate::read_model_store::ReadModelStore)** — persistence
//!   layer for projections. Backend-agnostic storage (defined in `read_model_store` module).
//!
//! ## Design Principles
//!
//! - **Separation of concerns**: Handler logic (projector) is separate from storage
//!   (read model store) and orchestration (projection engine)
//! - **Stateless projectors**: Projectors take `&self`, not `&mut self`. All mutable
//!   state lives in the `ReadModelStore` via interior mutability.
//! - **Rebuildable**: Projections can be rebuilt from scratch by replaying events
//! - **Idempotent**: Handling the same event multiple times should be safe
//! - **Composable**: One projector per read model concern; swap backends freely
//!
//! ## Example
//!
//! ```rust,ignore
//! use arc_core::projection::{Projector, Projection, ProjectionUnit, ProjectionEngine};
//! use arc_core::read_model_store::{ReadModelStore, InMemoryReadModelStore};
//! use arc_core::event::Event;
//! use std::sync::Arc;
//!
//! struct UserListProjector;
//!
//! #[async_trait]
//! impl Projector for UserListProjector {
//!     fn name(&self) -> &str { "UserList" }
//!
//!     fn handles(&self) -> Vec<String> {
//!         vec!["UserCreated".to_string(), "ProfileUpdated".to_string()]
//!     }
//!
//!     async fn apply(&self, event: &Event, store: &dyn ReadModelStore) -> ProjectionResult<()> {
//!         match event.event_type.as_str() {
//!             "UserCreated" => {
//!                 store.upsert(Upsert::new("users_view", &event.aggregate_id, event.payload.clone())).await
//!                     .map_err(|e| ProjectionError::handle_failed("UserList", &event.event_type, &event.event_id.to_string(), e.to_string()))?;
//!             }
//!             _ => {}
//!         }
//!         Ok(())
//!     }
//! }
//!
//! // Compose: projector + store = projection
//! let store = Arc::new(InMemoryReadModelStore::new());
//! let projection = ProjectionUnit::new(Box::new(UserListProjector), store, "users_view");
//!
//! // Register with engine
//! let mut engine = ProjectionEngine::new(event_store);
//! engine.register(Box::new(projection));
//! engine.process(&event).await?;
//! ```

use crate::event::Event;
use crate::event_bus::EventHandler;
use crate::event_store::EventStore;
use crate::read_model_store::ReadModelStore;
use async_trait::async_trait;
use std::sync::Arc;
use thiserror::Error;

// ---------------------------------------------------------------------------
// Errors
// ---------------------------------------------------------------------------

/// Errors that can occur during projection operations.
#[derive(Debug, Error)]
pub enum ProjectionError {
    /// Error handling an event
    #[error(
        "Projection '{name}' failed to handle event {event_type} (event_id: {event_id}): {message}"
    )]
    HandleFailed {
        name: String,
        event_type: String,
        event_id: String,
        message: String,
    },

    /// Error clearing projection state
    #[error("Projection '{name}' failed to clear: {message}")]
    ClearFailed { name: String, message: String },

    /// Error rebuilding projection
    #[error("Projection '{name}' failed to rebuild: {message}")]
    RebuildFailed { name: String, message: String },

    /// Event store error during rebuild
    #[error("Failed to load events for rebuild: {0}")]
    EventStoreError(String),

    /// Read model store error
    #[error("Read model store error in projection '{name}': {message}")]
    ReadModelError { name: String, message: String },

    /// Other errors
    #[error("Projection error: {message}")]
    Other { message: String },
}

impl ProjectionError {
    /// Create a handle failed error.
    pub fn handle_failed(
        name: impl Into<String>,
        event_type: impl Into<String>,
        event_id: impl Into<String>,
        message: impl Into<String>,
    ) -> Self {
        ProjectionError::HandleFailed {
            name: name.into(),
            event_type: event_type.into(),
            event_id: event_id.into(),
            message: message.into(),
        }
    }

    /// Create a clear failed error.
    pub fn clear_failed(name: impl Into<String>, message: impl Into<String>) -> Self {
        ProjectionError::ClearFailed {
            name: name.into(),
            message: message.into(),
        }
    }

    /// Create a rebuild failed error.
    pub fn rebuild_failed(name: impl Into<String>, message: impl Into<String>) -> Self {
        ProjectionError::RebuildFailed {
            name: name.into(),
            message: message.into(),
        }
    }

    /// Create a read model error.
    pub fn read_model_error(name: impl Into<String>, message: impl Into<String>) -> Self {
        ProjectionError::ReadModelError {
            name: name.into(),
            message: message.into(),
        }
    }

    /// Create a generic error.
    pub fn other(message: impl Into<String>) -> Self {
        ProjectionError::Other {
            message: message.into(),
        }
    }
}

/// Result type for projection operations.
pub type ProjectionResult<T> = Result<T, ProjectionError>;

// ---------------------------------------------------------------------------
// Projector trait — the stateless event handler
// ---------------------------------------------------------------------------

/// A projector contains the pure event-handling logic for building a read model.
///
/// Projectors are stateless — they receive events and translate them into write
/// operations against a [`ReadModelStore`]. They do not own the store or the
/// read model state.
///
/// # Design
///
/// - **Stateless**: all state lives in the `ReadModelStore`
/// - **Deterministic**: same events + empty store = same read model
/// - **Composable**: one projector per read model concern
/// - **`&self`**: safe to share across threads
///
/// # Idempotency
///
/// `apply()` should be idempotent — handling the same event twice must produce
/// the same result. Use UPSERT, check event_id, or make operations naturally
/// idempotent (SET vs INCREMENT).
///
/// # Example
///
/// ```rust,ignore
/// struct UserListProjector;
///
/// #[async_trait]
/// impl Projector for UserListProjector {
///     fn name(&self) -> &str { "UserList" }
///
///     fn handles(&self) -> Vec<String> {
///         vec!["UserCreated".to_string()]
///     }
///
///     async fn apply(&self, event: &Event, store: &dyn ReadModelStore) -> ProjectionResult<()> {
///         store.upsert(Upsert::new("users_view", &event.aggregate_id, event.payload.clone())).await
///             .map_err(|e| ProjectionError::handle_failed(
///                 "UserList", &event.event_type, &event.event_id.to_string(), e.to_string()
///             ))?;
///         Ok(())
///     }
/// }
/// ```
#[async_trait]
pub trait Projector: Send + Sync {
    /// Unique name identifying this projector.
    ///
    /// Used for logging, monitoring, position tracking, and rebuild targeting.
    fn name(&self) -> &str;

    /// Event types this projector handles.
    ///
    /// Only events whose `event_type` is in this list will be passed to `apply()`.
    fn handles(&self) -> Vec<String>;

    /// Apply a single event to the read model via the store.
    ///
    /// This method should be idempotent: applying the same event twice
    /// must produce the same result.
    async fn apply(&self, event: &Event, store: &dyn ReadModelStore) -> ProjectionResult<()>;

    /// Initialize the read model schema (CREATE TABLE IF NOT EXISTS, etc.).
    ///
    /// Called once when the projector is first registered and before rebuilds.
    /// Default implementation does nothing (for stores that don't need schema setup).
    async fn init(&self, _store: &dyn ReadModelStore) -> ProjectionResult<()> {
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Projection trait — the composed read model unit
// ---------------------------------------------------------------------------

/// A projection is the composed unit of a projector + its read model store.
///
/// It represents a complete, self-contained read model: the logic that transforms
/// events into state, paired with the storage where that state lives.
///
/// Most users don't implement this trait directly. Instead, implement [`Projector`]
/// and compose it with a [`ReadModelStore`] via [`ProjectionUnit`].
///
/// # `&self` not `&mut self`
///
/// All methods take `&self`. Mutable state lives in the `ReadModelStore`, which
/// handles interior mutability via connection pools, `Mutex`, etc.
#[async_trait]
pub trait Projection: Send + Sync {
    /// Projection name (delegates to the projector).
    fn name(&self) -> &str;

    /// Event types this projection handles (delegates to the projector).
    fn handles(&self) -> Vec<String>;

    /// Handle a single event by applying it through the projector to the store.
    async fn handle(&self, event: &Event) -> ProjectionResult<()>;

    /// Clear all read model state for this projection.
    async fn clear(&self) -> ProjectionResult<()>;

    /// Rebuild from a set of events: clear, then replay matching events.
    async fn rebuild(&self, events: Vec<Event>) -> ProjectionResult<()> {
        self.clear().await?;
        for event in events {
            if self.handles().contains(&event.event_type) {
                self.handle(&event).await?;
            }
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// ProjectionUnit — standard composition glue
// ---------------------------------------------------------------------------

/// Standard composition of a [`Projector`] and a [`ReadModelStore`].
///
/// This is the typical way to create a [`Projection`]: provide the event-handling
/// logic (projector) and the storage backend (store), and `ProjectionUnit` wires
/// them together.
///
/// # Example
///
/// ```rust,ignore
/// let projector = Box::new(UserListProjector);
/// let store: Arc<dyn ReadModelStore> = Arc::new(SqliteReadModelStore::new(pool));
/// let projection = ProjectionUnit::new(projector, store, "users_view");
/// engine.register(Box::new(projection));
/// ```
pub struct ProjectionUnit {
    projector: Box<dyn Projector>,
    store: Arc<dyn ReadModelStore>,
    /// Table/collection name used for `clear()` (truncate target).
    table: String,
}

impl ProjectionUnit {
    /// Create a new projection unit.
    ///
    /// # Arguments
    ///
    /// - `projector`: The stateless event handler
    /// - `store`: The read model storage backend
    /// - `table`: Table/collection name to truncate on `clear()`
    pub fn new(
        projector: Box<dyn Projector>,
        store: Arc<dyn ReadModelStore>,
        table: impl Into<String>,
    ) -> Self {
        Self {
            projector,
            store,
            table: table.into(),
        }
    }
}

#[async_trait]
impl Projection for ProjectionUnit {
    fn name(&self) -> &str {
        self.projector.name()
    }

    fn handles(&self) -> Vec<String> {
        self.projector.handles()
    }

    async fn handle(&self, event: &Event) -> ProjectionResult<()> {
        self.projector.apply(event, self.store.as_ref()).await
    }

    async fn clear(&self) -> ProjectionResult<()> {
        self.store
            .truncate(&self.table)
            .await
            .map_err(|e| ProjectionError::clear_failed(self.projector.name(), e.to_string()))
    }
}

// ---------------------------------------------------------------------------
// ProjectionEngine — orchestrates multiple projections
// ---------------------------------------------------------------------------

/// Engine for managing multiple projections.
///
/// The `ProjectionEngine`:
/// - Registers fully composed [`Projection`] instances
/// - Routes events to interested projections
/// - Rebuilds projections from the event store
/// - Provides convenience registration via [`register_projector`](Self::register_projector)
///
/// # Example
///
/// ```rust,ignore
/// let event_store = Box::new(sqlite_event_store);
/// let mut engine = ProjectionEngine::new(event_store);
///
/// // Option 1: register a pre-composed projection
/// engine.register(Box::new(projection_unit));
///
/// // Option 2: convenience — register projector + store directly
/// engine.register_projector(Box::new(UserListProjector), store.clone(), "users_view");
///
/// // Process events
/// engine.process(&event).await?;
///
/// // Rebuild all projections from event store
/// engine.rebuild_all().await?;
/// ```
pub struct ProjectionEngine {
    projections: Vec<Box<dyn Projection>>,
    event_store: Box<dyn EventStore>,
}

impl ProjectionEngine {
    /// Create a new projection engine.
    pub fn new(event_store: Box<dyn EventStore>) -> Self {
        Self {
            projections: Vec::new(),
            event_store,
        }
    }

    /// Register a fully composed projection.
    pub fn register(&mut self, projection: Box<dyn Projection>) {
        tracing::info!("Registering projection: {}", projection.name());
        self.projections.push(projection);
    }

    /// Convenience: register a projector + store as a [`ProjectionUnit`].
    pub fn register_projector(
        &mut self,
        projector: Box<dyn Projector>,
        store: Arc<dyn ReadModelStore>,
        table: impl Into<String>,
    ) {
        let unit = ProjectionUnit::new(projector, store, table);
        self.register(Box::new(unit));
    }

    /// Process a single event through all interested projections.
    ///
    /// Routes the event to projections whose `handles()` includes the event type.
    pub async fn process(&self, event: &Event) -> ProjectionResult<()> {
        for projection in &self.projections {
            if projection.handles().contains(&event.event_type) {
                tracing::debug!(
                    "Processing event {} ({}) in projection {}",
                    event.event_type,
                    event.event_id,
                    projection.name()
                );

                projection.handle(event).await.map_err(|e| {
                    ProjectionError::handle_failed(
                        projection.name(),
                        &event.event_type,
                        event.event_id.to_string(),
                        e.to_string(),
                    )
                })?;
            }
        }
        Ok(())
    }

    /// Process multiple events in sequence.
    pub async fn process_batch(&self, events: Vec<Event>) -> ProjectionResult<()> {
        for event in events {
            self.process(&event).await?;
        }
        Ok(())
    }

    /// Rebuild all registered projections from the event store.
    pub async fn rebuild_all(&self) -> ProjectionResult<()> {
        tracing::info!("Rebuilding all projections");

        let events = self
            .event_store
            .stream_all(0)
            .await
            .map_err(|e| ProjectionError::EventStoreError(e.to_string()))?;

        tracing::info!("Loaded {} events for rebuild", events.len());

        for projection in &self.projections {
            tracing::info!("Rebuilding projection: {}", projection.name());

            projection
                .rebuild(events.clone())
                .await
                .map_err(|e| ProjectionError::rebuild_failed(projection.name(), e.to_string()))?;

            tracing::info!("Rebuilt projection: {}", projection.name());
        }

        Ok(())
    }

    /// Rebuild a specific projection by name.
    pub async fn rebuild_projection(&self, name: &str) -> ProjectionResult<()> {
        tracing::info!("Rebuilding projection: {}", name);

        let projection = self
            .projections
            .iter()
            .find(|p| p.name() == name)
            .ok_or_else(|| ProjectionError::other(format!("Projection not found: {}", name)))?;

        let events = self
            .event_store
            .stream_all(0)
            .await
            .map_err(|e| ProjectionError::EventStoreError(e.to_string()))?;

        projection
            .rebuild(events)
            .await
            .map_err(|e| ProjectionError::rebuild_failed(name, e.to_string()))?;

        tracing::info!("Rebuilt projection: {}", name);
        Ok(())
    }

    /// Get number of registered projections.
    pub fn projection_count(&self) -> usize {
        self.projections.len()
    }

    /// Get names of all registered projections.
    pub fn projection_names(&self) -> Vec<String> {
        self.projections
            .iter()
            .map(|p| p.name().to_string())
            .collect()
    }

    /// Union of every event type any registered projection handles. Used by
    /// [`ProjectionEngineHandler`] to declare its `handles()` set when
    /// subscribing to an [`EventBus`](crate::event_bus::EventBus).
    pub fn all_handled_event_types(&self) -> Vec<String> {
        let mut all: Vec<String> = self.projections.iter().flat_map(|p| p.handles()).collect();
        all.sort();
        all.dedup();
        all
    }
}

// ---------------------------------------------------------------------------
// EventBus adapter — drive the engine from an in-process bus
// ---------------------------------------------------------------------------

/// Adapter that lets a [`ProjectionEngine`] subscribe to an
/// [`EventBus`](crate::event_bus::EventBus). Wraps the engine in an
/// [`EventHandler`] that routes every relevant event through
/// [`ProjectionEngine::process`]. The engine stays accessible from outside
/// (e.g. for `rebuild_all`) via the same [`Arc`].
///
/// Lives in `arc-core` because the adapter needs nothing app-specific —
/// any aggregate's projector can be driven through it.
pub struct ProjectionEngineHandler {
    engine: Arc<ProjectionEngine>,
    handles: Vec<String>,
}

impl ProjectionEngineHandler {
    pub fn new(engine: Arc<ProjectionEngine>) -> Self {
        let handles = engine.all_handled_event_types();
        Self { engine, handles }
    }
}

#[async_trait]
impl EventHandler for ProjectionEngineHandler {
    fn handles(&self) -> Vec<String> {
        self.handles.clone()
    }

    async fn handle(&self, event: &Event) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        self.engine
            .process(event)
            .await
            .map_err(|e| -> Box<dyn std::error::Error + Send + Sync> { Box::new(e) })
    }
}

// ===========================================================================
// Tests
// ===========================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::event_store::{EventStore, EventStoreResult, VersionCheck};
    use crate::read_model_store::InMemoryReadModelStore;
    use std::sync::{Arc, Mutex};

    // -----------------------------------------------------------------------
    // Mock event store (unchanged — needed for ProjectionEngine)
    // -----------------------------------------------------------------------

    struct MockEventStore {
        events: Arc<Mutex<Vec<Event>>>,
    }

    impl MockEventStore {
        fn new() -> Self {
            Self {
                events: Arc::new(Mutex::new(Vec::new())),
            }
        }

        fn add_event(&self, event: Event) {
            self.events.lock().unwrap().push(event);
        }
    }

    #[async_trait]
    impl EventStore for MockEventStore {
        async fn append(
            &self,
            _aggregate_id: &str,
            _version_check: VersionCheck,
            events: Vec<Event>,
        ) -> EventStoreResult<()> {
            self.events.lock().unwrap().extend(events);
            Ok(())
        }

        async fn load(&self, aggregate_id: &str) -> EventStoreResult<Vec<Event>> {
            Ok(self
                .events
                .lock()
                .unwrap()
                .iter()
                .filter(|e| e.aggregate_id == aggregate_id)
                .cloned()
                .collect())
        }

        async fn load_from(
            &self,
            aggregate_id: &str,
            from_sequence: i64,
        ) -> EventStoreResult<Vec<Event>> {
            Ok(self
                .events
                .lock()
                .unwrap()
                .iter()
                .filter(|e| e.aggregate_id == aggregate_id && e.sequence >= from_sequence)
                .cloned()
                .collect())
        }

        async fn stream_all(&self, _from_position: i64) -> EventStoreResult<Vec<Event>> {
            Ok(self.events.lock().unwrap().clone())
        }

        async fn get_version(&self, aggregate_id: &str) -> EventStoreResult<i64> {
            Ok(self
                .events
                .lock()
                .unwrap()
                .iter()
                .filter(|e| e.aggregate_id == aggregate_id)
                .map(|e| e.sequence)
                .max()
                .unwrap_or(0))
        }
    }

    // -----------------------------------------------------------------------
    // Mock projector — stateless, writes to ReadModelStore
    // -----------------------------------------------------------------------

    struct MockProjector {
        name: String,
        handles_types: Vec<String>,
    }

    impl MockProjector {
        fn new(name: &str, handles: Vec<String>) -> Self {
            Self {
                name: name.to_string(),
                handles_types: handles,
            }
        }
    }

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

        fn handles(&self) -> Vec<String> {
            self.handles_types.clone()
        }

        async fn apply(&self, event: &Event, store: &dyn ReadModelStore) -> ProjectionResult<()> {
            use crate::read_model_store::Upsert;
            store
                .upsert(Upsert::new(
                    "test_table",
                    event.event_id.to_string(),
                    serde_json::json!({
                        "id": event.event_id.to_string(),
                        "event_type": event.event_type,
                        "version": event.sequence,
                    }),
                ))
                .await
                .map_err(|e| {
                    ProjectionError::handle_failed(
                        &self.name,
                        &event.event_type,
                        event.event_id.to_string(),
                        e.to_string(),
                    )
                })?;
            Ok(())
        }
    }

    // -----------------------------------------------------------------------
    // Helper to build a projection from mock projector + in-memory store
    // -----------------------------------------------------------------------

    fn make_projection(
        name: &str,
        handles: Vec<String>,
        store: Arc<InMemoryReadModelStore>,
    ) -> Box<ProjectionUnit> {
        Box::new(ProjectionUnit::new(
            Box::new(MockProjector::new(name, handles)),
            store,
            "test_table",
        ))
    }

    // -----------------------------------------------------------------------
    // Tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_projection_engine_new() {
        let store = Box::new(MockEventStore::new());
        let engine = ProjectionEngine::new(store);
        assert_eq!(engine.projection_count(), 0);
    }

    #[tokio::test]
    async fn test_register_projection() {
        let store = Box::new(MockEventStore::new());
        let mut engine = ProjectionEngine::new(store);

        let rm_store = Arc::new(InMemoryReadModelStore::new());
        let projection = make_projection("Test", vec!["TestEvent".to_string()], rm_store);
        engine.register(projection);

        assert_eq!(engine.projection_count(), 1);
        assert_eq!(engine.projection_names(), vec!["Test"]);
    }

    #[tokio::test]
    async fn test_process_event() {
        let store = Box::new(MockEventStore::new());
        let mut engine = ProjectionEngine::new(store);

        let rm_store = Arc::new(InMemoryReadModelStore::new());
        let projection = make_projection("Test", vec!["UserCreated".to_string()], rm_store.clone());
        engine.register(projection);

        let event = Event::new(
            "User",
            "user-1",
            1,
            "UserCreated",
            serde_json::json!({"name": "Alice"}),
        );

        engine.process(&event).await.unwrap();

        assert_eq!(rm_store.get_rows("test_table").len(), 1);
    }

    #[tokio::test]
    async fn test_projection_filtering() {
        let store = Box::new(MockEventStore::new());
        let mut engine = ProjectionEngine::new(store);

        let rm_store = Arc::new(InMemoryReadModelStore::new());
        let projection = make_projection("Test", vec!["UserCreated".to_string()], rm_store.clone());
        engine.register(projection);

        // Event that should be handled
        let event1 = Event::new("User", "user-1", 1, "UserCreated", serde_json::json!({}));
        engine.process(&event1).await.unwrap();

        // Event that should be filtered out
        let event2 = Event::new("User", "user-1", 2, "UserDeleted", serde_json::json!({}));
        engine.process(&event2).await.unwrap();

        assert_eq!(rm_store.get_rows("test_table").len(), 1);
    }

    #[tokio::test]
    async fn test_rebuild_all() {
        let event_store = MockEventStore::new();

        event_store.add_event(Event::new(
            "User",
            "user-1",
            1,
            "UserCreated",
            serde_json::json!({}),
        ));
        event_store.add_event(Event::new(
            "User",
            "user-2",
            1,
            "UserCreated",
            serde_json::json!({}),
        ));

        let mut engine = ProjectionEngine::new(Box::new(event_store));

        let rm_store = Arc::new(InMemoryReadModelStore::new());
        let projection = make_projection("Test", vec!["UserCreated".to_string()], rm_store.clone());
        engine.register(projection);

        engine.rebuild_all().await.unwrap();

        assert_eq!(rm_store.get_rows("test_table").len(), 2);
    }

    #[tokio::test]
    async fn test_multiple_projections() {
        let store = Box::new(MockEventStore::new());
        let mut engine = ProjectionEngine::new(store);

        let rm_store1 = Arc::new(InMemoryReadModelStore::new());
        let rm_store2 = Arc::new(InMemoryReadModelStore::new());

        let proj1 = make_projection(
            "Projection1",
            vec!["UserCreated".to_string()],
            rm_store1.clone(),
        );
        let proj2 = make_projection(
            "Projection2",
            vec!["UserCreated".to_string(), "UserDeleted".to_string()],
            rm_store2.clone(),
        );

        engine.register(proj1);
        engine.register(proj2);

        let event = Event::new("User", "user-1", 1, "UserCreated", serde_json::json!({}));
        engine.process(&event).await.unwrap();

        assert_eq!(rm_store1.get_rows("test_table").len(), 1);
        assert_eq!(rm_store2.get_rows("test_table").len(), 1);
    }

    #[tokio::test]
    async fn test_process_batch() {
        let store = Box::new(MockEventStore::new());
        let mut engine = ProjectionEngine::new(store);

        let rm_store = Arc::new(InMemoryReadModelStore::new());
        let projection = make_projection("Test", vec!["UserCreated".to_string()], rm_store.clone());
        engine.register(projection);

        let events = vec![
            Event::new("User", "user-1", 1, "UserCreated", serde_json::json!({})),
            Event::new("User", "user-2", 1, "UserCreated", serde_json::json!({})),
            Event::new("User", "user-3", 1, "UserCreated", serde_json::json!({})),
        ];

        engine.process_batch(events).await.unwrap();

        assert_eq!(rm_store.get_rows("test_table").len(), 3);
    }

    #[tokio::test]
    async fn test_projector_init_default() {
        let projector = MockProjector::new("Test", vec![]);
        let store = InMemoryReadModelStore::new();
        // Default init() should succeed (no-op)
        projector.init(&store).await.unwrap();
    }

    #[tokio::test]
    async fn test_register_projector_convenience() {
        let event_store = Box::new(MockEventStore::new());
        let mut engine = ProjectionEngine::new(event_store);

        let rm_store: Arc<dyn ReadModelStore> = Arc::new(InMemoryReadModelStore::new());
        engine.register_projector(
            Box::new(MockProjector::new("Convenient", vec!["X".to_string()])),
            rm_store,
            "my_table",
        );

        assert_eq!(engine.projection_count(), 1);
        assert_eq!(engine.projection_names(), vec!["Convenient"]);
    }
}