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
//! Axum extractors for entities and links
//!
//! This module provides HTTP extractors that automatically:
//! - Deserialize and validate entities from request bodies
//! - Parse link routes and resolve definitions

use axum::Json;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use uuid::Uuid;

use crate::config::LinksConfig;
use crate::core::LinkDefinition;
use crate::links::registry::{LinkDirection, LinkRouteRegistry};

/// Errors that can occur during extraction
#[derive(Debug, Clone)]
pub enum ExtractorError {
    InvalidPath,
    InvalidEntityId,
    RouteNotFound(String),
    LinkNotFound,
    JsonError(String),
}

impl std::fmt::Display for ExtractorError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ExtractorError::InvalidPath => write!(f, "Invalid path format"),
            ExtractorError::InvalidEntityId => write!(f, "Invalid entity ID format"),
            ExtractorError::RouteNotFound(route) => write!(f, "Route not found: {}", route),
            ExtractorError::LinkNotFound => write!(f, "Link not found"),
            ExtractorError::JsonError(msg) => write!(f, "JSON error: {}", msg),
        }
    }
}

impl std::error::Error for ExtractorError {}

impl IntoResponse for ExtractorError {
    fn into_response(self) -> Response {
        let (status, message) = match self {
            ExtractorError::InvalidPath => (StatusCode::BAD_REQUEST, self.to_string()),
            ExtractorError::InvalidEntityId => (StatusCode::BAD_REQUEST, self.to_string()),
            ExtractorError::RouteNotFound(_) => (StatusCode::NOT_FOUND, self.to_string()),
            ExtractorError::LinkNotFound => (StatusCode::NOT_FOUND, self.to_string()),
            ExtractorError::JsonError(_) => (StatusCode::BAD_REQUEST, self.to_string()),
        };

        (status, Json(serde_json::json!({ "error": message }))).into_response()
    }
}

/// Extractor for link information from path
///
/// Automatically parses the path and resolves link definitions.
/// Supports both forward and reverse navigation.
#[derive(Debug, Clone)]
pub struct LinkExtractor {
    pub entity_id: Uuid,
    pub entity_type: String,
    pub link_definition: LinkDefinition,
    pub direction: LinkDirection,
}

impl LinkExtractor {
    /// Parse a link route path
    ///
    /// Expected format: `/{entity_type}/{entity_id}/{route_name}`
    /// Example: `/users/123.../cars-owned`
    pub fn from_path_and_registry(
        path_parts: (String, Uuid, String),
        registry: &LinkRouteRegistry,
        config: &LinksConfig,
    ) -> Result<Self, ExtractorError> {
        let (entity_type_plural, entity_id, route_name) = path_parts;

        // Convert plural to singular
        let entity_type = config
            .entities
            .iter()
            .find(|e| e.plural == entity_type_plural)
            .map(|e| e.singular.clone())
            .unwrap_or(entity_type_plural);

        // Resolve the route
        let (link_definition, direction) = registry
            .resolve_route(&entity_type, &route_name)
            .map_err(|_| ExtractorError::RouteNotFound(route_name.clone()))?;

        Ok(Self {
            entity_id,
            entity_type,
            link_definition,
            direction,
        })
    }
}

/// Extractor for direct link creation/deletion/update
///
/// Format: `/{source_type}/{source_id}/{route_name}/{target_id}`
/// Example: `/users/123.../cars-owned/456...`
///
/// This uses the route_name (e.g., "cars-owned") instead of link_type (e.g., "owner")
/// to provide more semantic and RESTful URLs.
#[derive(Debug, Clone)]
pub struct DirectLinkExtractor {
    pub source_id: Uuid,
    pub source_type: String,
    pub target_id: Uuid,
    pub target_type: String,
    pub link_definition: LinkDefinition,
    pub direction: LinkDirection,
}

impl DirectLinkExtractor {
    /// Parse a direct link path using route_name
    ///
    /// path_parts = (source_type_plural, source_id, route_name, target_id)
    ///
    /// The route_name is resolved to a link definition using the LinkRouteRegistry,
    /// which handles both forward and reverse navigation automatically.
    pub fn from_path(
        path_parts: (String, Uuid, String, Uuid),
        registry: &LinkRouteRegistry,
        config: &LinksConfig,
    ) -> Result<Self, ExtractorError> {
        let (source_type_plural, source_id, route_name, target_id) = path_parts;

        // Convert plural to singular
        let source_type = config
            .entities
            .iter()
            .find(|e| e.plural == source_type_plural)
            .map(|e| e.singular.clone())
            .unwrap_or(source_type_plural);

        // Resolve the route to get link definition and direction
        let (link_definition, direction) = registry
            .resolve_route(&source_type, &route_name)
            .map_err(|_| ExtractorError::RouteNotFound(route_name.clone()))?;

        // Determine target type based on direction
        let target_type = match direction {
            LinkDirection::Forward => link_definition.target_type.clone(),
            LinkDirection::Reverse => link_definition.source_type.clone(),
        };

        Ok(Self {
            source_id,
            source_type,
            target_id,
            target_type,
            link_definition,
            direction,
        })
    }
}

/// Segment d'une chaîne de liens imbriqués
#[derive(Debug, Clone, serde::Serialize)]
pub struct LinkPathSegment {
    /// Type d'entité (singulier)
    pub entity_type: String,
    /// ID de l'entité
    pub entity_id: Uuid,
    /// Nom de la route (si présent)
    pub route_name: Option<String>,
    /// Définition du lien (si présent)
    pub link_definition: Option<LinkDefinition>,
    /// Direction du lien (Forward ou Reverse)
    #[serde(skip_serializing)]
    pub link_direction: Option<LinkDirection>,
}

/// Extractor pour chemins imbriqués de profondeur illimitée
///
/// Parse dynamiquement des chemins comme:
/// - /users/123/invoices/456/orders
/// - /users/123/invoices/456/orders/789/payments/101
#[derive(Debug, Clone)]
pub struct RecursiveLinkExtractor {
    pub chain: Vec<LinkPathSegment>,
    /// True si le chemin se termine par une route (liste)
    /// False si le chemin se termine par un ID (item spécifique)
    pub is_list: bool,
}

impl RecursiveLinkExtractor {
    /// Parse un chemin complet dynamiquement
    pub fn from_segments(
        segments: Vec<String>,
        registry: &LinkRouteRegistry,
        config: &LinksConfig,
    ) -> Result<Self, ExtractorError> {
        if segments.len() < 2 {
            return Err(ExtractorError::InvalidPath);
        }

        let mut chain = Vec::new();
        let mut i = 0;
        let mut current_entity_type: Option<String> = None;

        // Pattern attendu: type/id/route/id/route/id...
        // Premier segment: toujours un type d'entité
        while i < segments.len() {
            // 1. Type d'entité (soit depuis URL pour le 1er, soit depuis link_def pour les suivants)
            let entity_type_singular = if let Some(ref entity_type) = current_entity_type {
                // Type connu depuis la résolution précédente
                entity_type.clone()
            } else {
                // Premier segment: lire le type depuis l'URL
                let entity_type_plural = &segments[i];
                let singular = config
                    .entities
                    .iter()
                    .find(|e| e.plural == *entity_type_plural)
                    .map(|e| e.singular.clone())
                    .ok_or(ExtractorError::InvalidPath)?;
                i += 1;
                singular
            };

            // Reset pour la prochaine itération
            current_entity_type = None;

            // 2. ID de l'entité (peut ne pas exister si fin du chemin)
            let entity_id = if i < segments.len() {
                segments[i]
                    .parse::<Uuid>()
                    .map_err(|_| ExtractorError::InvalidEntityId)?
            } else {
                // Pas d'ID = liste finale
                chain.push(LinkPathSegment {
                    entity_type: entity_type_singular,
                    entity_id: Uuid::nil(),
                    route_name: None,
                    link_definition: None,
                    link_direction: None,
                });
                break;
            };
            i += 1;

            // 3. Nom de route (peut ne pas exister si fin du chemin)
            let route_name = if i < segments.len() {
                Some(segments[i].clone())
            } else {
                None
            };

            if route_name.is_some() {
                i += 1;
            }

            // Résoudre la définition du lien si on a une route
            let (link_def, link_dir) = if let Some(route_name) = &route_name {
                let (link_def, direction) = registry
                    .resolve_route(&entity_type_singular, route_name)
                    .map_err(|_| ExtractorError::RouteNotFound(route_name.clone()))?;

                // Préparer le type pour la prochaine itération
                // Pour Forward: on va vers target_type
                // Pour Reverse: on va vers source_type (car on remonte la chaîne)
                current_entity_type = Some(match direction {
                    crate::links::registry::LinkDirection::Forward => link_def.target_type.clone(),
                    crate::links::registry::LinkDirection::Reverse => link_def.source_type.clone(),
                });

                (Some(link_def), Some(direction))
            } else {
                (None, None)
            };

            chain.push(LinkPathSegment {
                entity_type: entity_type_singular,
                entity_id,
                route_name,
                link_definition: link_def,
                link_direction: link_dir,
            });
        }

        // Si current_entity_type est défini, cela signifie que le chemin se termine par une route
        // et qu'on doit ajouter un segment final pour l'entité cible (liste)
        if let Some(final_entity_type) = current_entity_type {
            chain.push(LinkPathSegment {
                entity_type: final_entity_type,
                entity_id: Uuid::nil(), // Pas d'ID spécifique = liste
                route_name: None,
                link_definition: None,
                link_direction: None,
            });
        }

        // Déterminer si c'est une liste ou un item spécifique
        // Format: type/id/route/id/route → 5 segments → liste
        // Format: type/id/route/id/route/id → 6 segments → item
        // Si impair ≥ 5: liste, si pair ≥ 6: item spécifique
        let is_list = (segments.len() % 2 == 1) && (segments.len() >= 5);

        Ok(Self { chain, is_list })
    }

    /// Obtenir l'ID final et le type pour la requête finale
    pub fn final_target(&self) -> (Uuid, String) {
        let last = self.chain.last().unwrap();
        (last.entity_id, last.entity_type.clone())
    }

    /// Obtenir la définition du dernier lien
    pub fn final_link_def(&self) -> Option<&LinkDefinition> {
        // Le dernier segment n'a pas de link_def, le pénultième oui
        if self.chain.len() >= 2 {
            self.chain
                .get(self.chain.len() - 2)
                .and_then(|s| s.link_definition.as_ref())
        } else {
            None
        }
    }

    /// Obtenir l'avant-dernier segment (celui qui a le lien)
    pub fn penultimate_segment(&self) -> Option<&LinkPathSegment> {
        if self.chain.len() >= 2 {
            self.chain.get(self.chain.len() - 2)
        } else {
            None
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{EntityAuthConfig, EntityConfig, LinksConfig};
    use crate::core::LinkDefinition;
    use crate::links::registry::LinkRouteRegistry;
    use std::sync::Arc;
    use uuid::Uuid;

    /// Build a minimal LinksConfig + LinkRouteRegistry for testing.
    /// Entities: user (users), order (orders), invoice (invoices)
    /// Links: user->order (ownership), order->invoice (billing)
    fn test_config_and_registry() -> (Arc<LinksConfig>, LinkRouteRegistry) {
        let config = Arc::new(LinksConfig {
            entities: vec![
                EntityConfig {
                    singular: "user".to_string(),
                    plural: "users".to_string(),
                    auth: EntityAuthConfig::default(),
                },
                EntityConfig {
                    singular: "order".to_string(),
                    plural: "orders".to_string(),
                    auth: EntityAuthConfig::default(),
                },
                EntityConfig {
                    singular: "invoice".to_string(),
                    plural: "invoices".to_string(),
                    auth: EntityAuthConfig::default(),
                },
            ],
            links: vec![
                LinkDefinition {
                    link_type: "ownership".to_string(),
                    source_type: "user".to_string(),
                    target_type: "order".to_string(),
                    forward_route_name: "orders-owned".to_string(),
                    reverse_route_name: "owner".to_string(),
                    description: None,
                    required_fields: None,
                    auth: None,
                },
                LinkDefinition {
                    link_type: "billing".to_string(),
                    source_type: "order".to_string(),
                    target_type: "invoice".to_string(),
                    forward_route_name: "invoices".to_string(),
                    reverse_route_name: "order".to_string(),
                    description: None,
                    required_fields: None,
                    auth: None,
                },
            ],
            validation_rules: None,
            events: None,
            sinks: None,
        });
        let registry = LinkRouteRegistry::new(config.clone());
        (config, registry)
    }

    // === ExtractorError Display + IntoResponse ===

    #[test]
    fn test_extractor_error_display_invalid_path() {
        let err = ExtractorError::InvalidPath;
        assert_eq!(err.to_string(), "Invalid path format");
    }

    #[test]
    fn test_extractor_error_display_invalid_entity_id() {
        let err = ExtractorError::InvalidEntityId;
        assert_eq!(err.to_string(), "Invalid entity ID format");
    }

    #[test]
    fn test_extractor_error_display_route_not_found() {
        let err = ExtractorError::RouteNotFound("my-route".to_string());
        assert_eq!(err.to_string(), "Route not found: my-route");
    }

    #[test]
    fn test_extractor_error_display_link_not_found() {
        let err = ExtractorError::LinkNotFound;
        assert_eq!(err.to_string(), "Link not found");
    }

    #[test]
    fn test_extractor_error_display_json_error() {
        let err = ExtractorError::JsonError("bad json".to_string());
        assert_eq!(err.to_string(), "JSON error: bad json");
    }

    #[test]
    fn test_extractor_error_into_response_invalid_path_400() {
        let err = ExtractorError::InvalidPath;
        let response = err.into_response();
        assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    }

    #[test]
    fn test_extractor_error_into_response_invalid_entity_id_400() {
        let err = ExtractorError::InvalidEntityId;
        let response = err.into_response();
        assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    }

    #[test]
    fn test_extractor_error_into_response_route_not_found_404() {
        let err = ExtractorError::RouteNotFound("test".to_string());
        let response = err.into_response();
        assert_eq!(response.status(), StatusCode::NOT_FOUND);
    }

    #[test]
    fn test_extractor_error_into_response_link_not_found_404() {
        let err = ExtractorError::LinkNotFound;
        let response = err.into_response();
        assert_eq!(response.status(), StatusCode::NOT_FOUND);
    }

    #[test]
    fn test_extractor_error_into_response_json_error_400() {
        let err = ExtractorError::JsonError("oops".to_string());
        let response = err.into_response();
        assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    }

    // === LinkExtractor ===

    #[test]
    fn test_link_extractor_forward_route() {
        let (config, registry) = test_config_and_registry();
        let id = Uuid::new_v4();
        let result = LinkExtractor::from_path_and_registry(
            ("users".to_string(), id, "orders-owned".to_string()),
            &registry,
            &config,
        );
        assert!(result.is_ok());
        let ext = result.expect("should succeed");
        assert_eq!(ext.entity_type, "user");
        assert_eq!(ext.entity_id, id);
        assert_eq!(ext.link_definition.link_type, "ownership");
        assert!(matches!(ext.direction, LinkDirection::Forward));
    }

    #[test]
    fn test_link_extractor_reverse_route() {
        let (config, registry) = test_config_and_registry();
        let id = Uuid::new_v4();
        let result = LinkExtractor::from_path_and_registry(
            ("orders".to_string(), id, "owner".to_string()),
            &registry,
            &config,
        );
        assert!(result.is_ok());
        let ext = result.expect("should succeed");
        assert_eq!(ext.entity_type, "order");
        assert!(matches!(ext.direction, LinkDirection::Reverse));
    }

    #[test]
    fn test_link_extractor_route_not_found() {
        let (config, registry) = test_config_and_registry();
        let id = Uuid::new_v4();
        let result = LinkExtractor::from_path_and_registry(
            ("users".to_string(), id, "nonexistent".to_string()),
            &registry,
            &config,
        );
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            ExtractorError::RouteNotFound(_)
        ));
    }

    #[test]
    fn test_link_extractor_plural_to_singular_conversion() {
        let (config, registry) = test_config_and_registry();
        let id = Uuid::new_v4();
        let result = LinkExtractor::from_path_and_registry(
            ("users".to_string(), id, "orders-owned".to_string()),
            &registry,
            &config,
        );
        let ext = result.expect("should succeed");
        // "users" converted to "user"
        assert_eq!(ext.entity_type, "user");
    }

    #[test]
    fn test_link_extractor_unknown_plural_used_as_is() {
        let (config, registry) = test_config_and_registry();
        let id = Uuid::new_v4();
        // "widgets" not in config → used as-is as entity_type
        let result = LinkExtractor::from_path_and_registry(
            ("widgets".to_string(), id, "orders-owned".to_string()),
            &registry,
            &config,
        );
        // Route resolution will likely fail since "widgets" is not a known entity
        assert!(result.is_err());
    }

    // === DirectLinkExtractor ===

    #[test]
    fn test_direct_link_extractor_forward() {
        let (config, registry) = test_config_and_registry();
        let source_id = Uuid::new_v4();
        let target_id = Uuid::new_v4();
        let result = DirectLinkExtractor::from_path(
            (
                "users".to_string(),
                source_id,
                "orders-owned".to_string(),
                target_id,
            ),
            &registry,
            &config,
        );
        assert!(result.is_ok());
        let ext = result.expect("should succeed");
        assert_eq!(ext.source_type, "user");
        assert_eq!(ext.source_id, source_id);
        assert_eq!(ext.target_id, target_id);
        assert_eq!(ext.target_type, "order"); // Forward → target_type
        assert!(matches!(ext.direction, LinkDirection::Forward));
    }

    #[test]
    fn test_direct_link_extractor_reverse() {
        let (config, registry) = test_config_and_registry();
        let source_id = Uuid::new_v4();
        let target_id = Uuid::new_v4();
        let result = DirectLinkExtractor::from_path(
            (
                "orders".to_string(),
                source_id,
                "owner".to_string(),
                target_id,
            ),
            &registry,
            &config,
        );
        assert!(result.is_ok());
        let ext = result.expect("should succeed");
        assert_eq!(ext.source_type, "order");
        assert_eq!(ext.target_type, "user"); // Reverse → source_type
        assert!(matches!(ext.direction, LinkDirection::Reverse));
    }

    #[test]
    fn test_direct_link_extractor_route_not_found() {
        let (config, registry) = test_config_and_registry();
        let result = DirectLinkExtractor::from_path(
            (
                "users".to_string(),
                Uuid::new_v4(),
                "nope".to_string(),
                Uuid::new_v4(),
            ),
            &registry,
            &config,
        );
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            ExtractorError::RouteNotFound(_)
        ));
    }

    // === RecursiveLinkExtractor ===

    #[test]
    fn test_recursive_too_few_segments_error() {
        let (config, registry) = test_config_and_registry();
        let result =
            RecursiveLinkExtractor::from_segments(vec!["users".to_string()], &registry, &config);
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), ExtractorError::InvalidPath));
    }

    #[test]
    fn test_recursive_entity_type_and_id() {
        let (config, registry) = test_config_and_registry();
        let id = Uuid::new_v4();
        let result = RecursiveLinkExtractor::from_segments(
            vec!["users".to_string(), id.to_string()],
            &registry,
            &config,
        );
        assert!(result.is_ok());
        let ext = result.expect("should succeed");
        assert_eq!(ext.chain.len(), 1);
        assert_eq!(ext.chain[0].entity_type, "user");
        assert_eq!(ext.chain[0].entity_id, id);
    }

    #[test]
    fn test_recursive_invalid_uuid_error() {
        let (config, registry) = test_config_and_registry();
        let result = RecursiveLinkExtractor::from_segments(
            vec!["users".to_string(), "not-a-uuid".to_string()],
            &registry,
            &config,
        );
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            ExtractorError::InvalidEntityId
        ));
    }

    #[test]
    fn test_recursive_unknown_entity_type_error() {
        let (config, registry) = test_config_and_registry();
        let result = RecursiveLinkExtractor::from_segments(
            vec!["widgets".to_string(), Uuid::new_v4().to_string()],
            &registry,
            &config,
        );
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), ExtractorError::InvalidPath));
    }

    #[test]
    fn test_recursive_entity_id_route_forward() {
        let (config, registry) = test_config_and_registry();
        let user_id = Uuid::new_v4();
        let result = RecursiveLinkExtractor::from_segments(
            vec![
                "users".to_string(),
                user_id.to_string(),
                "orders-owned".to_string(),
            ],
            &registry,
            &config,
        );
        assert!(result.is_ok());
        let ext = result.expect("should succeed");
        // Chain: user(user_id, route=orders-owned) → order(nil, list)
        assert_eq!(ext.chain.len(), 2);
        assert_eq!(ext.chain[0].entity_type, "user");
        assert_eq!(ext.chain[0].entity_id, user_id);
        assert_eq!(ext.chain[0].route_name.as_deref(), Some("orders-owned"));
        assert_eq!(
            ext.chain[0]
                .link_definition
                .as_ref()
                .expect("should have link_def")
                .link_type,
            "ownership"
        );
        assert_eq!(ext.chain[1].entity_type, "order");
        assert!(ext.chain[1].entity_id.is_nil()); // list segment
    }

    #[test]
    fn test_recursive_multi_level_chain() {
        let (config, registry) = test_config_and_registry();
        let user_id = Uuid::new_v4();
        let order_id = Uuid::new_v4();
        let result = RecursiveLinkExtractor::from_segments(
            vec![
                "users".to_string(),
                user_id.to_string(),
                "orders-owned".to_string(),
                order_id.to_string(),
                "invoices".to_string(),
            ],
            &registry,
            &config,
        );
        assert!(result.is_ok());
        let ext = result.expect("should succeed");
        // Chain: user(user_id) → order(order_id) → invoice(nil, list)
        assert_eq!(ext.chain.len(), 3);
        assert_eq!(ext.chain[0].entity_type, "user");
        assert_eq!(ext.chain[0].entity_id, user_id);
        assert_eq!(ext.chain[1].entity_type, "order");
        assert_eq!(ext.chain[1].entity_id, order_id);
        assert_eq!(ext.chain[2].entity_type, "invoice");
        assert!(ext.is_list); // 5 segments → list
    }

    #[test]
    fn test_recursive_multi_level_specific_item() {
        let (config, registry) = test_config_and_registry();
        let user_id = Uuid::new_v4();
        let order_id = Uuid::new_v4();
        let invoice_id = Uuid::new_v4();
        let result = RecursiveLinkExtractor::from_segments(
            vec![
                "users".to_string(),
                user_id.to_string(),
                "orders-owned".to_string(),
                order_id.to_string(),
                "invoices".to_string(),
                invoice_id.to_string(),
            ],
            &registry,
            &config,
        );
        assert!(result.is_ok());
        let ext = result.expect("should succeed");
        assert_eq!(ext.chain.len(), 3);
        assert_eq!(ext.chain[2].entity_id, invoice_id);
        assert!(!ext.is_list); // 6 segments → specific item
    }

    #[test]
    fn test_recursive_route_not_found_mid_chain() {
        let (config, registry) = test_config_and_registry();
        let result = RecursiveLinkExtractor::from_segments(
            vec![
                "users".to_string(),
                Uuid::new_v4().to_string(),
                "nonexistent-route".to_string(),
            ],
            &registry,
            &config,
        );
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            ExtractorError::RouteNotFound(_)
        ));
    }

    #[test]
    fn test_recursive_reverse_direction_propagation() {
        let (config, registry) = test_config_and_registry();
        let order_id = Uuid::new_v4();
        // orders/{id}/owner → reverse → navigates to user
        let result = RecursiveLinkExtractor::from_segments(
            vec![
                "orders".to_string(),
                order_id.to_string(),
                "owner".to_string(),
            ],
            &registry,
            &config,
        );
        assert!(result.is_ok());
        let ext = result.expect("should succeed");
        assert_eq!(ext.chain.len(), 2);
        assert_eq!(ext.chain[0].entity_type, "order");
        assert!(matches!(
            ext.chain[0].link_direction,
            Some(LinkDirection::Reverse)
        ));
        // Reverse direction → target entity is source_type (user)
        assert_eq!(ext.chain[1].entity_type, "user");
    }

    // === final_target / final_link_def / penultimate_segment ===

    #[test]
    fn test_final_target_returns_last_segment() {
        let (config, registry) = test_config_and_registry();
        let user_id = Uuid::new_v4();
        let ext = RecursiveLinkExtractor::from_segments(
            vec![
                "users".to_string(),
                user_id.to_string(),
                "orders-owned".to_string(),
            ],
            &registry,
            &config,
        )
        .expect("should succeed");
        let (id, entity_type) = ext.final_target();
        assert_eq!(entity_type, "order");
        assert!(id.is_nil()); // list target
    }

    #[test]
    fn test_final_link_def_returns_penultimate_link() {
        let (config, registry) = test_config_and_registry();
        let user_id = Uuid::new_v4();
        let ext = RecursiveLinkExtractor::from_segments(
            vec![
                "users".to_string(),
                user_id.to_string(),
                "orders-owned".to_string(),
            ],
            &registry,
            &config,
        )
        .expect("should succeed");
        let link_def = ext.final_link_def();
        assert!(link_def.is_some());
        assert_eq!(link_def.expect("should have link").link_type, "ownership");
    }

    #[test]
    fn test_final_link_def_single_segment_returns_none() {
        let (config, registry) = test_config_and_registry();
        let ext = RecursiveLinkExtractor::from_segments(
            vec!["users".to_string(), Uuid::new_v4().to_string()],
            &registry,
            &config,
        )
        .expect("should succeed");
        assert!(ext.final_link_def().is_none());
    }

    #[test]
    fn test_penultimate_segment_returns_correct() {
        let (config, registry) = test_config_and_registry();
        let user_id = Uuid::new_v4();
        let ext = RecursiveLinkExtractor::from_segments(
            vec![
                "users".to_string(),
                user_id.to_string(),
                "orders-owned".to_string(),
            ],
            &registry,
            &config,
        )
        .expect("should succeed");
        let pen = ext.penultimate_segment();
        assert!(pen.is_some());
        assert_eq!(pen.expect("should exist").entity_type, "user");
        assert_eq!(pen.expect("should exist").entity_id, user_id);
    }

    #[test]
    fn test_penultimate_segment_single_segment_returns_none() {
        let (config, registry) = test_config_and_registry();
        let ext = RecursiveLinkExtractor::from_segments(
            vec!["users".to_string(), Uuid::new_v4().to_string()],
            &registry,
            &config,
        )
        .expect("should succeed");
        assert!(ext.penultimate_segment().is_none());
    }
}