umari 0.0.1

SDK for building event-sourced WASM components for the Umari runtime
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
use std::collections::HashMap;

use chrono::{DateTime, Utc};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use umadb_dcb::DcbQueryItem;
use uuid::Uuid;
use validator::Validate;

use crate::{
    domain_id::DomainIdBindings, emit::Emit, error::CommandExecuteError, event::EventDomainId,
    folds::FoldSet, rules::RuleSet,
};

/// Trait for command input structs that declare domain ID bindings.
///
/// Fields annotated with `#[domain_id("field_name")]` specify which
/// domain ID fields in events should match which input values.
///
/// # Example
///
/// ```rust,ignore
/// #[derive(CommandInput, Deserialize)]
/// struct TransferInput {
///     #[domain_id("account_id")]
///     source_account: String,
///     #[domain_id("account_id")]
///     dest_account: String,
///     amount: f64,
/// }
/// ```
///
/// This generates a query for events where `account_id` is either
/// `source_account` or `dest_account`.
pub trait CommandInput {
    /// Returns the domain ID bindings for this input.
    ///
    /// Maps domain ID field names to the values to query for.
    fn domain_id_bindings(&self) -> DomainIdBindings;
}

/// A trait implemented when using the `export_command!` macro, with the command name matching the crate name.
pub trait CommandName {
    const COMMAND_NAME: &'static str;
}

/// The main trait for implementing command handlers.
///
/// A command handler:
/// 1. Declares its query via `Query` (which events) and `Input` (which domain IDs)
/// 2. Rebuilds state by processing historical events via `apply`
/// 3. Makes decisions and emits new events via `handle`
///
/// The handler must implement `Default` as a fresh instance is created
/// for each command execution.
///
/// # Example
///
/// ```ignore
/// #[derive(EventSet)]
/// enum Query {
///     OpenedAccount(OpenedAccount),
///     SentFunds(SentFunds),
/// }
///
/// #[derive(CommandInput, Deserialize)]
/// struct Input {
///     #[domain_id]
///     account_id: String,
///     amount: f64,
/// }
///
/// #[derive(Default)]
/// struct Withdraw {
///     balance: f64,
/// }
///
/// impl Command for Withdraw {
///     type Query = Query;
///     type Input = Input;
///
///     fn apply(&mut self, event: Query) {
///         match event {
///             Query::OpenedAccount(ev) => self.balance = ev.initial_balance,
///             Query::SentFunds(ev) => self.balance -= ev.amount,
///         }
///     }
///
///     fn handle(self, input: Input) -> Result<Emit, CommandError> {
///         if self.balance < input.amount {
///             return Err(CommandError::rejected("Insufficient funds"));
///         }
///         
///         Ok(Emit::new().event(SentFunds {
///             account_id: input.account_id,
///             amount: input.amount,
///             recipient_id: None,
///         }))
///     }
/// }
/// ```
pub trait Command {
    /// The input type for this command.
    /// Defines the domain ID bindings for the query.
    type Input: CommandInput + Validate + JsonSchema;

    type State: FoldSet;

    fn rules(_input: &Self::Input) -> impl RuleSet {}

    fn emit(state: Self::State, input: Self::Input) -> Emit;
}

pub trait CommandExecute: Command + CommandName {
    fn execute(input: &Self::Input) -> Result<CommandReceipt, CommandExecuteError>;

    fn execute_with(
        input: &Self::Input,
        ctx: CommandContext,
    ) -> Result<CommandReceipt, CommandExecuteError>;
}

impl<T> CommandExecute for T
where
    T: Command + CommandName,
    T::Input: Serialize,
{
    #[inline(always)]
    fn execute(input: &Self::Input) -> Result<CommandReceipt, CommandExecuteError> {
        use crate::runtime::command::umari::command::executor::CommandContext;

        execute_inner::<T>(
            T::COMMAND_NAME,
            input,
            &CommandContext {
                correlation_id: None,
                triggering_event_id: None,
                idempotency_key: None,
            },
        )
    }

    #[inline(always)]
    fn execute_with(
        input: &Self::Input,
        ctx: CommandContext,
    ) -> Result<CommandReceipt, CommandExecuteError> {
        use crate::runtime::command::umari::command::executor::CommandContext;

        execute_inner::<T>(
            T::COMMAND_NAME,
            input,
            &CommandContext {
                correlation_id: ctx.correlation_id.as_ref().map(ToString::to_string),
                triggering_event_id: ctx.triggering_event_id.as_ref().map(ToString::to_string),
                idempotency_key: ctx.idempotency_key.as_ref().map(ToString::to_string),
            },
        )
    }
}

fn execute_inner<T>(
    name: &str,
    input: &T::Input,
    ctx: &crate::runtime::command::umari::command::executor::CommandContext,
) -> Result<CommandReceipt, CommandExecuteError>
where
    T: Command,
    T::Input: Serialize,
{
    use crate::runtime::command::umari::command::executor::execute;

    let result = execute(
        name,
        &serde_json::to_string(input)
            .unwrap_or_else(|err| panic!("failed to serialize input: {err}")),
        ctx,
    )
    .map_err(CommandExecuteError)?;

    Ok(result.into())
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CommandContext {
    /// Original request ID (flows through everything)
    pub correlation_id: Option<Uuid>,
    /// Event ID that triggered this command (for sagas)
    pub triggering_event_id: Option<Uuid>,
    /// Client-supplied key for deduplicating retried command executions.
    pub idempotency_key: Option<Uuid>,
}

impl CommandContext {
    pub fn new() -> Self {
        CommandContext {
            correlation_id: None,
            triggering_event_id: None,
            idempotency_key: None,
        }
    }

    pub fn with_correlation_id(mut self, correlation_id: Uuid) -> Self {
        self.correlation_id = Some(correlation_id);
        self
    }

    pub fn with_triggering_event_id(mut self, triggering_event_id: Uuid) -> Self {
        self.triggering_event_id = Some(triggering_event_id);
        self
    }

    pub fn with_idempotency_key(mut self, idempotency_key: Uuid) -> Self {
        self.idempotency_key = Some(idempotency_key);
        self
    }
}

impl Default for CommandContext {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct EventMeta {
    pub timestamp: DateTime<Utc>,
}

#[derive(Clone, Debug)]
pub struct CommandReceipt {
    pub position: Option<u64>,
    pub events: Vec<EmittedEventRef>,
}

#[derive(Clone, Debug)]
pub struct EmittedEventRef {
    pub id: Uuid,
    pub event_type: String,
    pub tags: Vec<String>,
}

pub(crate) fn build_query_items_from_domain_ids(
    event_domain_ids: &[EventDomainId],
    bindings: &DomainIdBindings,
) -> Vec<DcbQueryItem> {
    // Group event types by their (dynamic fields, static tags) signature
    let mut groups: HashMap<(Vec<&str>, Vec<(&str, &str)>), Vec<&str>> = HashMap::new();

    for entry in event_domain_ids {
        // Only include dynamic fields that are in our input bindings
        let mut relevant_dynamic: Vec<&str> = entry
            .dynamic_fields
            .iter()
            .filter(|f| bindings.contains_key(*f))
            .copied()
            .collect();
        relevant_dynamic.sort();

        let mut static_tags: Vec<(&str, &str)> = entry.static_fields.to_vec();
        static_tags.sort();

        groups
            .entry((relevant_dynamic, static_tags))
            .or_default()
            .push(entry.event_type);
    }

    // Build one QueryItem per group
    let mut items = Vec::new();

    for ((dynamic_fields, static_fields), event_types) in groups {
        // Build effective bindings: runtime bindings for dynamic + statics as singletons
        let mut effective: DomainIdBindings = dynamic_fields
            .iter()
            .filter_map(|f| bindings.get(f).map(|v| (*f, v.clone())))
            .collect();
        for (field, value) in &static_fields {
            effective.entry(field).or_default().push(value.to_string());
        }

        if effective.is_empty() {
            // No matching domain IDs - query by type only
            items.push(DcbQueryItem::new().types(event_types.iter().copied()));
            continue;
        }

        let tag_combinations = cartesian_product(&effective);

        for tags in tag_combinations {
            items.push(
                DcbQueryItem::new()
                    .tags(tags)
                    .types(event_types.iter().copied()),
            );
        }
    }

    items
}

fn cartesian_product(bindings: &DomainIdBindings) -> Vec<Vec<String>> {
    let binding_groups: Vec<_> = bindings.iter().collect();

    if binding_groups.is_empty() {
        return vec![vec![]];
    }

    let mut combinations: Vec<Vec<String>> = vec![vec![]];

    for (field_name, values) in binding_groups {
        combinations = combinations
            .into_iter()
            .flat_map(|existing| {
                values.iter().map(move |value| {
                    let mut combo = existing.clone();
                    combo.push(format!("{field_name}:{value}"));
                    combo
                })
            })
            .collect();
    }

    combinations
}

#[cfg(test)]
mod tests {
    use serde_json::Value;
    use umadb_dcb::DcbQueryItem;

    use crate::{error::SerializationError, event::EventSet};

    use super::*;

    fn build_query_items<E: EventSet>(bindings: &DomainIdBindings) -> Vec<DcbQueryItem> {
        build_query_items_from_domain_ids(&E::event_domain_ids(), bindings)
    }

    fn bindings(pairs: &[(&'static str, &[&str])]) -> DomainIdBindings {
        pairs
            .iter()
            .map(|(k, v)| (*k, v.iter().map(|s| s.to_string()).collect()))
            .collect()
    }

    /// Extract tags and types from query items for easier assertion
    fn extract(items: &[DcbQueryItem]) -> Vec<(Vec<String>, Vec<String>)> {
        items
            .iter()
            .map(|item| {
                let mut tags = item.tags.clone();
                let mut types = item.types.clone();
                tags.sort();
                types.sort();
                (tags, types)
            })
            .collect()
    }

    fn sorted<T: Ord>(mut v: Vec<T>) -> Vec<T> {
        v.sort();
        v
    }

    // =========================================================================
    // Mock EventSet implementations for testing
    // =========================================================================

    struct SingleFieldEvents;
    impl EventSet for SingleFieldEvents {
        type Item = Self;

        fn event_types() -> Vec<&'static str> {
            vec!["EventA", "EventB"]
        }

        fn event_domain_ids() -> Vec<EventDomainId> {
            vec![
                EventDomainId {
                    event_type: "EventA",
                    dynamic_fields: &["user_id"],
                    static_fields: &[],
                },
                EventDomainId {
                    event_type: "EventB",
                    dynamic_fields: &["user_id"],
                    static_fields: &[],
                },
            ]
        }

        fn from_event(_: &str, _: Value) -> Option<Result<Self::Item, SerializationError>> {
            None
        }
    }

    struct MixedFieldEvents;
    impl EventSet for MixedFieldEvents {
        type Item = Self;

        fn event_types() -> Vec<&'static str> {
            vec!["UserRegistered", "UserCompletedOnboarding", "BetTracked"]
        }

        fn event_domain_ids() -> Vec<EventDomainId> {
            vec![
                EventDomainId {
                    event_type: "UserRegistered",
                    dynamic_fields: &["user_id"],
                    static_fields: &[],
                },
                EventDomainId {
                    event_type: "UserCompletedOnboarding",
                    dynamic_fields: &["user_id"],
                    static_fields: &[],
                },
                EventDomainId {
                    event_type: "BetTracked",
                    dynamic_fields: &["bet_id", "user_id"],
                    static_fields: &[],
                },
            ]
        }

        fn from_event(_: &str, _: Value) -> Option<Result<Self::Item, SerializationError>> {
            None
        }
    }

    struct MultipleFieldsAllShared;
    impl EventSet for MultipleFieldsAllShared {
        type Item = Self;

        fn event_types() -> Vec<&'static str> {
            vec!["TransferSent", "TransferReceived"]
        }

        fn event_domain_ids() -> Vec<EventDomainId> {
            vec![
                EventDomainId {
                    event_type: "TransferSent",
                    dynamic_fields: &["account_id", "region_id"],
                    static_fields: &[],
                },
                EventDomainId {
                    event_type: "TransferReceived",
                    dynamic_fields: &["account_id", "region_id"],
                    static_fields: &[],
                },
            ]
        }

        fn from_event(_: &str, _: Value) -> Option<Result<Self::Item, SerializationError>> {
            None
        }
    }

    struct DisjointFieldEvents;
    impl EventSet for DisjointFieldEvents {
        type Item = Self;

        fn event_types() -> Vec<&'static str> {
            vec!["UserEvent", "OrderEvent"]
        }

        fn event_domain_ids() -> Vec<EventDomainId> {
            vec![
                EventDomainId {
                    event_type: "UserEvent",
                    dynamic_fields: &["user_id"],
                    static_fields: &[],
                },
                EventDomainId {
                    event_type: "OrderEvent",
                    dynamic_fields: &["order_id"],
                    static_fields: &[],
                },
            ]
        }

        fn from_event(_: &str, _: Value) -> Option<Result<Self::Item, SerializationError>> {
            None
        }
    }

    struct NoDomainsEvent;
    impl EventSet for NoDomainsEvent {
        type Item = Self;

        fn event_types() -> Vec<&'static str> {
            vec!["GlobalEvent"]
        }

        fn event_domain_ids() -> Vec<EventDomainId> {
            vec![EventDomainId {
                event_type: "GlobalEvent",
                dynamic_fields: &[],
                static_fields: &[],
            }]
        }

        fn from_event(_: &str, _: Value) -> Option<Result<Self::Item, SerializationError>> {
            None
        }
    }

    struct StaticFieldEvents;
    impl EventSet for StaticFieldEvents {
        type Item = Self;

        fn event_types() -> Vec<&'static str> {
            vec!["ShopEvent", "GlobalShopEvent"]
        }

        fn event_domain_ids() -> Vec<EventDomainId> {
            vec![
                EventDomainId {
                    event_type: "ShopEvent",
                    dynamic_fields: &["user_id"],
                    static_fields: &[("shop_id", "warranti")],
                },
                EventDomainId {
                    event_type: "GlobalShopEvent",
                    dynamic_fields: &[],
                    static_fields: &[("shop_id", "warranti")],
                },
            ]
        }

        fn from_event(_: &str, _: Value) -> Option<Result<Self::Item, SerializationError>> {
            None
        }
    }

    // =========================================================================
    // Tests: Basic cases
    // =========================================================================

    #[test]
    fn single_field_single_value() {
        let b = bindings(&[("user_id", &["alice"])]);
        let items = build_query_items::<SingleFieldEvents>(&b);

        assert_eq!(items.len(), 1);
        let extracted = extract(&items);
        assert_eq!(extracted[0].0, vec!["user_id:alice"]);
        assert_eq!(sorted(extracted[0].1.clone()), vec!["EventA", "EventB"]);
    }

    #[test]
    fn single_field_multiple_values() {
        let b = bindings(&[("user_id", &["alice", "bob"])]);
        let items = build_query_items::<SingleFieldEvents>(&b);

        assert_eq!(items.len(), 2);
        let tags: Vec<_> = items.iter().flat_map(|i| &i.tags).collect();
        assert!(tags.contains(&&"user_id:alice".to_string()));
        assert!(tags.contains(&&"user_id:bob".to_string()));
    }

    #[test]
    fn empty_bindings() {
        let b = bindings(&[]);
        let items = build_query_items::<SingleFieldEvents>(&b);

        assert_eq!(items.len(), 1);
        assert!(items[0].tags.is_empty());
        assert_eq!(sorted(items[0].types.to_vec()), vec!["EventA", "EventB"]);
    }

    // =========================================================================
    // Tests: Mixed domain ID fields (the TrackBet case)
    // =========================================================================

    #[test]
    fn mixed_fields_groups_by_domain_signature() {
        let b = bindings(&[("user_id", &["abc"]), ("bet_id", &["xyz"])]);
        let items = build_query_items::<MixedFieldEvents>(&b);

        // Should produce 2 query items:
        // 1. UserRegistered + UserCompletedOnboarding with just user_id
        // 2. BetTracked with both bet_id and user_id
        assert_eq!(items.len(), 2);

        let extracted = extract(&items);

        // Find the user-only group
        let user_only = extracted
            .iter()
            .find(|(tags, _)| tags == &vec!["user_id:abc"])
            .expect("should have user_id only group");
        assert_eq!(
            sorted(user_only.1.clone()),
            vec!["UserCompletedOnboarding", "UserRegistered"]
        );

        // Find the bet+user group
        let bet_user = extracted
            .iter()
            .find(|(tags, _)| tags.len() == 2)
            .expect("should have bet_id + user_id group");
        assert!(bet_user.0.contains(&"bet_id:xyz".to_string()));
        assert!(bet_user.0.contains(&"user_id:abc".to_string()));
        assert_eq!(bet_user.1, vec!["BetTracked"]);
    }

    #[test]
    fn mixed_fields_partial_binding() {
        // Only provide user_id, not bet_id
        let b = bindings(&[("user_id", &["abc"])]);
        let items = build_query_items::<MixedFieldEvents>(&b);

        // All events share user_id, so should be one group
        assert_eq!(items.len(), 1);
        assert_eq!(items[0].tags, vec!["user_id:abc".to_string()]);
        assert_eq!(
            sorted(items[0].types.to_vec()),
            vec!["BetTracked", "UserCompletedOnboarding", "UserRegistered"]
        );
    }

    // =========================================================================
    // Tests: Multiple fields all shared
    // =========================================================================

    #[test]
    fn multiple_fields_all_shared_single_values() {
        let b = bindings(&[("account_id", &["alice"]), ("region_id", &["us-west"])]);
        let items = build_query_items::<MultipleFieldsAllShared>(&b);

        assert_eq!(items.len(), 1);
        let extracted = extract(&items);
        assert!(extracted[0].0.contains(&"account_id:alice".to_string()));
        assert!(extracted[0].0.contains(&"region_id:us-west".to_string()));
        assert_eq!(
            sorted(extracted[0].1.clone()),
            vec!["TransferReceived", "TransferSent"]
        );
    }

    #[test]
    fn multiple_fields_all_shared_cartesian_product() {
        let b = bindings(&[
            ("account_id", &["alice", "bob"]),
            ("region_id", &["us-west"]),
        ]);
        let items = build_query_items::<MultipleFieldsAllShared>(&b);

        // 2 accounts × 1 region = 2 query items
        assert_eq!(items.len(), 2);

        let all_tags: Vec<_> = items.iter().map(|i| sorted(i.tags.to_vec())).collect();
        assert!(all_tags.contains(&vec![
            "account_id:alice".to_string(),
            "region_id:us-west".to_string()
        ]));
        assert!(all_tags.contains(&vec![
            "account_id:bob".to_string(),
            "region_id:us-west".to_string()
        ]));
    }

    #[test]
    fn multiple_fields_full_cartesian_product() {
        let b = bindings(&[
            ("account_id", &["alice", "bob"]),
            ("region_id", &["us-west", "us-east"]),
        ]);
        let items = build_query_items::<MultipleFieldsAllShared>(&b);

        // 2 accounts × 2 regions = 4 query items
        assert_eq!(items.len(), 4);
    }

    // =========================================================================
    // Tests: Disjoint domain ID fields
    // =========================================================================

    #[test]
    fn disjoint_fields_separate_groups() {
        let b = bindings(&[("user_id", &["alice"]), ("order_id", &["order-123"])]);
        let items = build_query_items::<DisjointFieldEvents>(&b);

        // Should produce 2 groups since events don't share fields
        assert_eq!(items.len(), 2);

        let extracted = extract(&items);

        let user_group = extracted
            .iter()
            .find(|(tags, _)| tags.contains(&"user_id:alice".to_string()))
            .expect("should have user group");
        assert_eq!(user_group.1, vec!["UserEvent"]);

        let order_group = extracted
            .iter()
            .find(|(tags, _)| tags.contains(&"order_id:order-123".to_string()))
            .expect("should have order group");
        assert_eq!(order_group.1, vec!["OrderEvent"]);
    }

    // =========================================================================
    // Tests: Events with no domain IDs
    // =========================================================================

    #[test]
    fn event_with_no_domain_ids() {
        let b = bindings(&[("some_id", &["value"])]);
        let items = build_query_items::<NoDomainsEvent>(&b);

        // Event has no domain IDs, so it goes in a group with empty fields
        assert_eq!(items.len(), 1);
        assert!(items[0].tags.is_empty());
        assert_eq!(items[0].types, vec!["GlobalEvent".to_string()]);
    }

    #[test]
    fn event_with_no_domain_ids_empty_bindings() {
        let b = bindings(&[]);
        let items = build_query_items::<NoDomainsEvent>(&b);

        assert_eq!(items.len(), 1);
        assert!(items[0].tags.is_empty());
        assert_eq!(items[0].types, vec!["GlobalEvent".to_string()]);
    }

    // =========================================================================
    // Tests: Edge cases
    // =========================================================================

    #[test]
    fn binding_not_used_by_any_event() {
        let b = bindings(&[("user_id", &["alice"]), ("unused_field", &["value"])]);
        let items = build_query_items::<SingleFieldEvents>(&b);

        // unused_field should be ignored
        assert_eq!(items.len(), 1);
        assert_eq!(items[0].tags, vec!["user_id:alice".to_string()]);
    }

    #[test]
    fn multiple_values_same_field_mixed_events() {
        let b = bindings(&[("user_id", &["alice", "bob"]), ("bet_id", &["xyz"])]);
        let items = build_query_items::<MixedFieldEvents>(&b);

        // Should have:
        // - 2 items for user_id only (alice, bob) → UserRegistered, UserCompletedOnboarding
        // - 2 items for bet_id + user_id (xyz+alice, xyz+bob) → BetTracked
        assert_eq!(items.len(), 4);

        let user_only_items: Vec<_> = items
            .iter()
            .filter(|i| i.types.contains(&"UserRegistered".to_string()))
            .collect();
        assert_eq!(user_only_items.len(), 2);

        let bet_items: Vec<_> = items
            .iter()
            .filter(|i| i.types.contains(&"BetTracked".to_string()))
            .collect();
        assert_eq!(bet_items.len(), 2);
    }

    // =========================================================================
    // Tests: Static fields
    // =========================================================================

    #[test]
    fn static_field_with_dynamic_binding() {
        let b = bindings(&[("user_id", &["alice"])]);
        let items = build_query_items::<StaticFieldEvents>(&b);

        // ShopEvent: user_id=alice + shop_id=warranti → 1 item with both tags
        // GlobalShopEvent: shop_id=warranti only → 1 item
        assert_eq!(items.len(), 2);

        let extracted = extract(&items);

        let shop_user = extracted
            .iter()
            .find(|(tags, _)| tags.contains(&"user_id:alice".to_string()))
            .expect("should have shop+user item");
        assert!(shop_user.0.contains(&"shop_id:warranti".to_string()));
        assert_eq!(shop_user.1, vec!["ShopEvent"]);

        let global_shop = extracted
            .iter()
            .find(|(tags, _)| !tags.contains(&"user_id:alice".to_string()))
            .expect("should have global shop item");
        assert_eq!(global_shop.0, vec!["shop_id:warranti".to_string()]);
        assert_eq!(global_shop.1, vec!["GlobalShopEvent"]);
    }

    #[test]
    fn static_field_no_dynamic_bindings() {
        let b = bindings(&[]);
        let items = build_query_items::<StaticFieldEvents>(&b);

        // Both events have no dynamic fields, only static shop_id=warranti
        // They share the same effective binding signature, so they group together
        assert_eq!(items.len(), 1);
        assert_eq!(items[0].tags, vec!["shop_id:warranti".to_string()]);
        assert_eq!(
            sorted(items[0].types.to_vec()),
            vec!["GlobalShopEvent", "ShopEvent"]
        );
    }
}