this-rs 0.0.9

Framework for building complex multi-entity REST and GraphQL APIs with many relationships
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
//! Configuration loading and management

pub mod events;
pub mod sinks;

use crate::core::LinkDefinition;
use anyhow::Result;
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

pub use events::*;
pub use sinks::*;

/// Authorization configuration for an entity
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EntityAuthConfig {
    /// Policy for listing entities (GET /{entities})
    #[serde(default = "default_auth_policy")]
    pub list: String,

    /// Policy for getting a single entity (GET /{entities}/{id})
    #[serde(default = "default_auth_policy")]
    pub get: String,

    /// Policy for creating an entity (POST /{entities})
    #[serde(default = "default_auth_policy")]
    pub create: String,

    /// Policy for updating an entity (PUT /{entities}/{id})
    #[serde(default = "default_auth_policy")]
    pub update: String,

    /// Policy for deleting an entity (DELETE /{entities}/{id})
    #[serde(default = "default_auth_policy")]
    pub delete: String,

    /// Policy for listing links (GET /{entities}/{id}/{link_route})
    #[serde(default = "default_auth_policy")]
    pub list_links: String,

    /// Policy for creating links (POST /{entities}/{id}/{link_type}/{target_type}/{target_id})
    #[serde(default = "default_auth_policy")]
    pub create_link: String,

    /// Policy for deleting links (DELETE /{entities}/{id}/{link_type}/{target_type}/{target_id})
    #[serde(default = "default_auth_policy")]
    pub delete_link: String,
}

fn default_auth_policy() -> String {
    "authenticated".to_string()
}

impl Default for EntityAuthConfig {
    fn default() -> Self {
        Self {
            list: default_auth_policy(),
            get: default_auth_policy(),
            create: default_auth_policy(),
            update: default_auth_policy(),
            delete: default_auth_policy(),
            list_links: default_auth_policy(),
            create_link: default_auth_policy(),
            delete_link: default_auth_policy(),
        }
    }
}

/// Configuration for an entity type
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EntityConfig {
    /// Singular form (e.g., "user", "company")
    pub singular: String,

    /// Plural form (e.g., "users", "companies")
    pub plural: String,

    /// Authorization configuration
    #[serde(default)]
    pub auth: EntityAuthConfig,
}

/// Validation rule for a link type
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationRule {
    /// Source entity type
    pub source: String,

    /// Allowed target entity types
    pub targets: Vec<String>,
}

/// Complete configuration for the links system
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LinksConfig {
    /// List of entity configurations
    pub entities: Vec<EntityConfig>,

    /// List of link definitions
    pub links: Vec<LinkDefinition>,

    /// Optional validation rules (link_type -> rules)
    #[serde(default)]
    pub validation_rules: Option<HashMap<String, Vec<ValidationRule>>>,

    /// Optional event flow configuration (backend, flows, consumers)
    #[serde(default)]
    pub events: Option<EventsConfig>,

    /// Optional sink configurations (notification destinations)
    #[serde(default)]
    pub sinks: Option<Vec<SinkConfig>>,
}

impl LinksConfig {
    /// Load configuration from a YAML file
    pub fn from_yaml_file(path: &str) -> Result<Self> {
        let content = std::fs::read_to_string(path)?;
        let config: Self = serde_yaml::from_str(&content)?;
        Ok(config)
    }

    /// Load configuration from a YAML string
    pub fn from_yaml_str(yaml: &str) -> Result<Self> {
        let config: Self = serde_yaml::from_str(yaml)?;
        Ok(config)
    }

    /// Merge multiple configurations into one
    ///
    /// Rules:
    /// - Entities: Combined from all configs, duplicates (by singular name) use last definition
    /// - Links: Combined from all configs, duplicates (by link_type+source+target) use last definition
    /// - Validation rules: Merged by link_type, rules combined for each link type
    pub fn merge(configs: Vec<LinksConfig>) -> Self {
        if configs.is_empty() {
            return Self {
                entities: vec![],
                links: vec![],
                validation_rules: None,
                events: None,
                sinks: None,
            };
        }

        if configs.len() == 1 {
            return configs.into_iter().next().unwrap();
        }

        let mut entities_map: IndexMap<String, EntityConfig> = IndexMap::new();
        let mut links_map: IndexMap<(String, String, String), LinkDefinition> = IndexMap::new();
        let mut validation_rules_map: HashMap<String, Vec<ValidationRule>> = HashMap::new();

        // Merge entities (last one wins for duplicates)
        for config in &configs {
            for entity in &config.entities {
                entities_map.insert(entity.singular.clone(), entity.clone());
            }
        }

        // Merge links (last one wins for duplicates)
        for config in &configs {
            for link in &config.links {
                let key = (
                    link.link_type.clone(),
                    link.source_type.clone(),
                    link.target_type.clone(),
                );
                links_map.insert(key, link.clone());
            }
        }

        // Merge validation rules (combine rules for same link_type)
        for config in &configs {
            if let Some(rules) = &config.validation_rules {
                for (link_type, link_rules) in rules {
                    validation_rules_map
                        .entry(link_type.clone())
                        .or_default()
                        .extend(link_rules.clone());
                }
            }
        }

        // Merge events: backend last-wins, flows are concatenated (with duplicate warning)
        let mut merged_events: Option<EventsConfig> = None;
        for config in &configs {
            if let Some(events) = &config.events {
                if let Some(ref mut existing) = merged_events {
                    // Backend: last-wins (consistent with entities/links merge behavior)
                    existing.backend = events.backend.clone();
                    existing.flows.extend(events.flows.clone());
                    existing.consumers.extend(events.consumers.clone());
                } else {
                    merged_events = Some(events.clone());
                }
            }
        }

        // Detect duplicate flow names and warn
        if let Some(ref events) = merged_events {
            let mut seen_names = std::collections::HashSet::new();
            for flow in &events.flows {
                if !seen_names.insert(&flow.name) {
                    tracing::warn!(
                        flow_name = %flow.name,
                        "config merge: duplicate flow name detected — \
                         later definition will shadow earlier one at runtime"
                    );
                }
            }
        }

        // Merge sinks: deduplicate by name (last wins), preserving insertion order
        let mut sinks_map: IndexMap<String, SinkConfig> = IndexMap::new();
        for config in &configs {
            if let Some(sinks) = &config.sinks {
                for sink in sinks {
                    sinks_map.insert(sink.name.clone(), sink.clone());
                }
            }
        }
        let merged_sinks = if sinks_map.is_empty() {
            None
        } else {
            Some(sinks_map.into_values().collect())
        };

        // Convert back to vectors
        let entities: Vec<EntityConfig> = entities_map.into_values().collect();
        let links: Vec<LinkDefinition> = links_map.into_values().collect();
        let validation_rules = if validation_rules_map.is_empty() {
            None
        } else {
            Some(validation_rules_map)
        };

        Self {
            entities,
            links,
            validation_rules,
            events: merged_events,
            sinks: merged_sinks,
        }
    }

    /// Validate if a link combination is allowed
    ///
    /// If no validation rules are defined, all combinations are allowed (permissive mode)
    pub fn is_valid_link(&self, link_type: &str, source_type: &str, target_type: &str) -> bool {
        // If no validation rules, accept everything
        let Some(rules) = &self.validation_rules else {
            return true;
        };

        // Check if there are rules for this link type
        let Some(link_rules) = rules.get(link_type) else {
            return true; // No rules for this link type, accept
        };

        // Check if the combination is in the rules
        link_rules.iter().any(|rule| {
            rule.source == source_type && rule.targets.contains(&target_type.to_string())
        })
    }

    /// Find a link definition
    pub fn find_link_definition(
        &self,
        link_type: &str,
        source_type: &str,
        target_type: &str,
    ) -> Option<&LinkDefinition> {
        self.links.iter().find(|def| {
            def.link_type == link_type
                && def.source_type == source_type
                && def.target_type == target_type
        })
    }

    /// Create a default configuration for testing
    pub fn default_config() -> Self {
        Self {
            entities: vec![
                EntityConfig {
                    singular: "user".to_string(),
                    plural: "users".to_string(),
                    auth: EntityAuthConfig::default(),
                },
                EntityConfig {
                    singular: "company".to_string(),
                    plural: "companies".to_string(),
                    auth: EntityAuthConfig::default(),
                },
                EntityConfig {
                    singular: "car".to_string(),
                    plural: "cars".to_string(),
                    auth: EntityAuthConfig::default(),
                },
            ],
            links: vec![
                LinkDefinition {
                    link_type: "owner".to_string(),
                    source_type: "user".to_string(),
                    target_type: "car".to_string(),
                    forward_route_name: "cars-owned".to_string(),
                    reverse_route_name: "users-owners".to_string(),
                    description: Some("User owns a car".to_string()),
                    required_fields: None,
                    auth: None,
                },
                LinkDefinition {
                    link_type: "driver".to_string(),
                    source_type: "user".to_string(),
                    target_type: "car".to_string(),
                    forward_route_name: "cars-driven".to_string(),
                    reverse_route_name: "users-drivers".to_string(),
                    description: Some("User drives a car".to_string()),
                    required_fields: None,
                    auth: None,
                },
                LinkDefinition {
                    link_type: "worker".to_string(),
                    source_type: "user".to_string(),
                    target_type: "company".to_string(),
                    forward_route_name: "companies-work".to_string(),
                    reverse_route_name: "users-workers".to_string(),
                    description: Some("User works at a company".to_string()),
                    required_fields: Some(vec!["role".to_string()]),
                    auth: None,
                },
            ],
            validation_rules: None,
            events: None,
            sinks: None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_config() {
        let config = LinksConfig::default_config();

        assert_eq!(config.entities.len(), 3);
        assert_eq!(config.links.len(), 3);
    }

    #[test]
    fn test_yaml_serialization() {
        let config = LinksConfig::default_config();
        let yaml = serde_yaml::to_string(&config).unwrap();

        // Should be able to parse it back
        let parsed = LinksConfig::from_yaml_str(&yaml).unwrap();
        assert_eq!(parsed.entities.len(), config.entities.len());
        assert_eq!(parsed.links.len(), config.links.len());
    }

    #[test]
    fn test_link_auth_config_parsing() {
        let yaml = r#"
entities:
  - singular: order
    plural: orders
  - singular: invoice
    plural: invoices

links:
  - link_type: has_invoice
    source_type: order
    target_type: invoice
    forward_route_name: invoices
    reverse_route_name: order
    auth:
      list: authenticated
      create: service_only
      delete: admin_only
"#;

        let config = LinksConfig::from_yaml_str(yaml).unwrap();
        assert_eq!(config.links.len(), 1);

        let link_def = &config.links[0];
        assert!(link_def.auth.is_some());

        let auth = link_def.auth.as_ref().unwrap();
        assert_eq!(auth.list, "authenticated");
        assert_eq!(auth.create, "service_only");
        assert_eq!(auth.delete, "admin_only");
    }

    #[test]
    fn test_link_without_auth_config() {
        let yaml = r#"
entities:
  - singular: invoice
    plural: invoices
  - singular: payment
    plural: payments

links:
  - link_type: payment
    source_type: invoice
    target_type: payment
    forward_route_name: payments
    reverse_route_name: invoice
"#;

        let config = LinksConfig::from_yaml_str(yaml).unwrap();
        assert_eq!(config.links.len(), 1);

        let link_def = &config.links[0];
        assert!(link_def.auth.is_none());
    }

    #[test]
    fn test_mixed_link_auth_configs() {
        let yaml = r#"
entities:
  - singular: order
    plural: orders
  - singular: invoice
    plural: invoices
  - singular: payment
    plural: payments

links:
  - link_type: has_invoice
    source_type: order
    target_type: invoice
    forward_route_name: invoices
    reverse_route_name: order
    auth:
      list: authenticated
      create: service_only
      delete: admin_only
  
  - link_type: payment
    source_type: invoice
    target_type: payment
    forward_route_name: payments
    reverse_route_name: invoice
"#;

        let config = LinksConfig::from_yaml_str(yaml).unwrap();
        assert_eq!(config.links.len(), 2);

        // First link has auth
        assert!(config.links[0].auth.is_some());
        let auth1 = config.links[0].auth.as_ref().unwrap();
        assert_eq!(auth1.create, "service_only");

        // Second link has no auth
        assert!(config.links[1].auth.is_none());
    }

    #[test]
    fn test_merge_empty() {
        let merged = LinksConfig::merge(vec![]);
        assert_eq!(merged.entities.len(), 0);
        assert_eq!(merged.links.len(), 0);
    }

    #[test]
    fn test_merge_single() {
        let config = LinksConfig::default_config();
        let merged = LinksConfig::merge(vec![config.clone()]);
        assert_eq!(merged.entities.len(), config.entities.len());
        assert_eq!(merged.links.len(), config.links.len());
    }

    #[test]
    fn test_merge_disjoint_configs() {
        let config1 = LinksConfig {
            entities: vec![EntityConfig {
                singular: "order".to_string(),
                plural: "orders".to_string(),
                auth: EntityAuthConfig::default(),
            }],
            links: vec![],
            validation_rules: None,
            events: None,
            sinks: None,
        };

        let config2 = LinksConfig {
            entities: vec![EntityConfig {
                singular: "invoice".to_string(),
                plural: "invoices".to_string(),
                auth: EntityAuthConfig::default(),
            }],
            links: vec![],
            validation_rules: None,
            events: None,
            sinks: None,
        };

        let merged = LinksConfig::merge(vec![config1, config2]);
        assert_eq!(merged.entities.len(), 2);
    }

    #[test]
    fn test_merge_overlapping_entities() {
        let auth1 = EntityAuthConfig {
            list: "public".to_string(),
            ..Default::default()
        };

        let config1 = LinksConfig {
            entities: vec![EntityConfig {
                singular: "user".to_string(),
                plural: "users".to_string(),
                auth: auth1,
            }],
            links: vec![],
            validation_rules: None,
            events: None,
            sinks: None,
        };

        let auth2 = EntityAuthConfig {
            list: "authenticated".to_string(),
            ..Default::default()
        };

        let config2 = LinksConfig {
            entities: vec![EntityConfig {
                singular: "user".to_string(),
                plural: "users".to_string(),
                auth: auth2,
            }],
            links: vec![],
            validation_rules: None,
            events: None,
            sinks: None,
        };

        let merged = LinksConfig::merge(vec![config1, config2]);

        // Should have only 1 entity (last wins)
        assert_eq!(merged.entities.len(), 1);
        assert_eq!(merged.entities[0].auth.list, "authenticated");
    }

    #[test]
    fn test_merge_validation_rules() {
        let mut rules1 = HashMap::new();
        rules1.insert(
            "works_at".to_string(),
            vec![ValidationRule {
                source: "user".to_string(),
                targets: vec!["company".to_string()],
            }],
        );

        let config1 = LinksConfig {
            entities: vec![],
            links: vec![],
            validation_rules: Some(rules1),
            events: None,
            sinks: None,
        };

        let mut rules2 = HashMap::new();
        rules2.insert(
            "works_at".to_string(),
            vec![ValidationRule {
                source: "user".to_string(),
                targets: vec!["project".to_string()],
            }],
        );

        let config2 = LinksConfig {
            entities: vec![],
            links: vec![],
            validation_rules: Some(rules2),
            events: None,
            sinks: None,
        };

        let merged = LinksConfig::merge(vec![config1, config2]);

        // Validation rules should be combined
        assert!(merged.validation_rules.is_some());
        let rules = merged.validation_rules.unwrap();
        assert_eq!(rules["works_at"].len(), 2);
    }

    #[test]
    fn test_find_link_definition_found() {
        let config = LinksConfig::default_config();

        let def = config.find_link_definition("owner", "user", "car");
        assert!(def.is_some(), "should find owner link from user to car");
        let def = def.expect("link definition should exist");
        assert_eq!(def.link_type, "owner");
        assert_eq!(def.source_type, "user");
        assert_eq!(def.target_type, "car");
    }

    #[test]
    fn test_find_link_definition_not_found() {
        let config = LinksConfig::default_config();

        let def = config.find_link_definition("nonexistent", "user", "car");
        assert!(def.is_none(), "should not find a nonexistent link type");

        // Wrong source type
        let def = config.find_link_definition("owner", "company", "car");
        assert!(def.is_none(), "should not find link with wrong source type");
    }

    #[test]
    fn test_is_valid_link_source_type_mismatch() {
        let mut rules = HashMap::new();
        rules.insert(
            "owner".to_string(),
            vec![ValidationRule {
                source: "user".to_string(),
                targets: vec!["car".to_string()],
            }],
        );

        let config = LinksConfig {
            entities: vec![],
            links: vec![],
            validation_rules: Some(rules),
            events: None,
            sinks: None,
        };

        // Correct combination
        assert!(config.is_valid_link("owner", "user", "car"));

        // Source type mismatch
        assert!(
            !config.is_valid_link("owner", "company", "car"),
            "should reject mismatched source type"
        );

        // Target type mismatch
        assert!(
            !config.is_valid_link("owner", "user", "truck"),
            "should reject mismatched target type"
        );
    }

    #[test]
    fn test_is_valid_link_empty_targets() {
        let mut rules = HashMap::new();
        rules.insert(
            "membership".to_string(),
            vec![ValidationRule {
                source: "user".to_string(),
                targets: vec![], // empty targets list
            }],
        );

        let config = LinksConfig {
            entities: vec![],
            links: vec![],
            validation_rules: Some(rules),
            events: None,
            sinks: None,
        };

        // With empty targets, no target type can match
        assert!(
            !config.is_valid_link("membership", "user", "group"),
            "should reject when targets list is empty"
        );
    }

    #[test]
    fn test_yaml_backward_compatible_without_events() {
        // Old-style YAML without events/sinks should still parse
        let yaml = r#"
entities:
  - singular: user
    plural: users
links:
  - link_type: follows
    source_type: user
    target_type: user
    forward_route_name: following
    reverse_route_name: followers
"#;

        let config = LinksConfig::from_yaml_str(yaml).unwrap();
        assert_eq!(config.entities.len(), 1);
        assert_eq!(config.links.len(), 1);
        assert!(config.events.is_none());
        assert!(config.sinks.is_none());
    }

    #[test]
    fn test_yaml_with_events_and_sinks() {
        let yaml = r#"
entities:
  - singular: user
    plural: users
  - singular: capture
    plural: captures

links:
  - link_type: follows
    source_type: user
    target_type: user
    forward_route_name: following
    reverse_route_name: followers
  - link_type: likes
    source_type: user
    target_type: capture
    forward_route_name: liked-captures
    reverse_route_name: likers
  - link_type: owns
    source_type: user
    target_type: capture
    forward_route_name: captures
    reverse_route_name: owner

events:
  backend:
    type: memory
  flows:
    - name: notify-new-follower
      trigger:
        kind: link.created
        link_type: follows
      pipeline:
        - resolve:
            from: source_id
            as: follower
        - map:
            template:
              type: follow
              message: "{{ follower.name }} started following you"
        - deliver:
            sinks: [push-notification, in-app-notification]
    - name: notify-like
      trigger:
        kind: link.created
        link_type: likes
      pipeline:
        - resolve:
            from: target_id
            via: owns
            direction: reverse
            as: owner
        - filter:
            condition: "source_id != owner.id"
        - batch:
            key: target_id
            window: 5m
        - deliver:
            sink: push-notification
  consumers:
    - name: mobile-feed
      seek: last_acknowledged

sinks:
  - name: push-notification
    type: push
    config:
      provider: expo
  - name: in-app-notification
    type: in_app
    config:
      ttl: 30d
"#;

        let config = LinksConfig::from_yaml_str(yaml).unwrap();

        // Entities and links
        assert_eq!(config.entities.len(), 2);
        assert_eq!(config.links.len(), 3);

        // Events
        assert!(config.events.is_some());
        let events = config.events.as_ref().unwrap();
        assert_eq!(events.backend.backend_type, "memory");
        assert_eq!(events.flows.len(), 2);
        assert_eq!(events.flows[0].name, "notify-new-follower");
        assert_eq!(events.flows[1].name, "notify-like");
        assert_eq!(events.consumers.len(), 1);
        assert_eq!(events.consumers[0].name, "mobile-feed");

        // Sinks
        assert!(config.sinks.is_some());
        let sinks = config.sinks.as_ref().unwrap();
        assert_eq!(sinks.len(), 2);
        assert_eq!(sinks[0].name, "push-notification");
        assert_eq!(sinks[0].sink_type, SinkType::Push);
        assert_eq!(sinks[1].name, "in-app-notification");
        assert_eq!(sinks[1].sink_type, SinkType::InApp);
    }

    #[test]
    fn test_merge_configs_with_events() {
        let config1 = LinksConfig {
            entities: vec![EntityConfig {
                singular: "user".to_string(),
                plural: "users".to_string(),
                auth: EntityAuthConfig::default(),
            }],
            links: vec![],
            validation_rules: None,
            events: Some(EventsConfig {
                backend: BackendConfig::default(),
                flows: vec![FlowConfig {
                    name: "flow-a".to_string(),
                    description: None,
                    trigger: TriggerConfig {
                        kind: "link.created".to_string(),
                        link_type: Some("follows".to_string()),
                        entity_type: None,
                    },
                    pipeline: vec![],
                }],
                consumers: vec![],
            }),
            sinks: Some(vec![SinkConfig {
                name: "push".to_string(),
                sink_type: SinkType::Push,
                config: HashMap::new(),
            }]),
        };

        let config2 = LinksConfig {
            entities: vec![],
            links: vec![],
            validation_rules: None,
            events: Some(EventsConfig {
                backend: BackendConfig::default(),
                flows: vec![FlowConfig {
                    name: "flow-b".to_string(),
                    description: None,
                    trigger: TriggerConfig {
                        kind: "entity.created".to_string(),
                        link_type: None,
                        entity_type: Some("user".to_string()),
                    },
                    pipeline: vec![],
                }],
                consumers: vec![ConsumerConfig {
                    name: "mobile".to_string(),
                    seek: SeekMode::LastAcknowledged,
                }],
            }),
            sinks: Some(vec![SinkConfig {
                name: "in-app".to_string(),
                sink_type: SinkType::InApp,
                config: HashMap::new(),
            }]),
        };

        let merged = LinksConfig::merge(vec![config1, config2]);

        // Events should be merged
        let events = merged.events.unwrap();
        assert_eq!(events.flows.len(), 2);
        assert_eq!(events.flows[0].name, "flow-a");
        assert_eq!(events.flows[1].name, "flow-b");
        assert_eq!(events.consumers.len(), 1);

        // Sinks should be merged (deduplicated by name)
        let sinks = merged.sinks.unwrap();
        assert_eq!(sinks.len(), 2);
    }
}