fastapi-output 0.3.0

Agent-aware rich console output for fastapi_rust
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
//! Routing debug output component.
//!
//! Provides detailed visual display of the routing decision process,
//! showing which routes were considered, why matches succeeded or failed,
//! parameter extraction results, and middleware that will be applied.
//!
//! # Feature Gating
//!
//! This module is designed for debug output. In production, routing debug
//! should only be enabled when explicitly requested.
//!
//! ```rust,ignore
//! if config.debug_routing {
//!     let debug = RoutingDebug::new(OutputMode::Rich);
//!     println!("{}", debug.format(&routing_result));
//! }
//! ```

use crate::mode::OutputMode;
use crate::themes::FastApiTheme;
use std::time::Duration;

const ANSI_RESET: &str = "\x1b[0m";
const ANSI_BOLD: &str = "\x1b[1m";

/// Result of a route matching attempt.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MatchResult {
    /// Route matched successfully.
    Matched,
    /// Route did not match - path mismatch.
    PathMismatch,
    /// Route did not match - method mismatch.
    MethodMismatch,
    /// Route did not match - parameter type validation failed.
    ParamTypeMismatch {
        /// Name of the parameter that failed.
        param_name: String,
        /// Expected type.
        expected_type: String,
        /// Actual value that didn't match.
        actual_value: String,
    },
    /// Route did not match - guard/condition failed.
    GuardFailed {
        /// Name of the guard that failed.
        guard_name: String,
    },
}

impl MatchResult {
    /// Get a human-readable description of the result.
    #[must_use]
    pub fn description(&self) -> String {
        match self {
            Self::Matched => "Matched".to_string(),
            Self::PathMismatch => "Path did not match".to_string(),
            Self::MethodMismatch => "Method not allowed".to_string(),
            Self::ParamTypeMismatch {
                param_name,
                expected_type,
                actual_value,
            } => {
                format!("Parameter '{param_name}' expected {expected_type}, got '{actual_value}'")
            }
            Self::GuardFailed { guard_name } => format!("Guard '{guard_name}' failed"),
        }
    }

    /// Check if this is a successful match.
    #[must_use]
    pub fn is_match(&self) -> bool {
        matches!(self, Self::Matched)
    }
}

/// Information about a candidate route that was considered.
#[derive(Debug, Clone)]
pub struct CandidateRoute {
    /// Route pattern (e.g., "/users/{id}").
    pub pattern: String,
    /// Allowed HTTP methods for this route.
    pub methods: Vec<String>,
    /// Handler function name.
    pub handler: Option<String>,
    /// Match result for this candidate.
    pub result: MatchResult,
    /// Whether this route partially matched (path ok, method wrong).
    pub partial_match: bool,
}

impl CandidateRoute {
    /// Create a new candidate route.
    #[must_use]
    pub fn new(pattern: impl Into<String>, result: MatchResult) -> Self {
        Self {
            pattern: pattern.into(),
            methods: Vec::new(),
            handler: None,
            result,
            partial_match: false,
        }
    }

    /// Set the allowed methods.
    #[must_use]
    pub fn methods(mut self, methods: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.methods = methods.into_iter().map(Into::into).collect();
        self
    }

    /// Set the handler name.
    #[must_use]
    pub fn handler(mut self, handler: impl Into<String>) -> Self {
        self.handler = Some(handler.into());
        self
    }

    /// Mark as partial match.
    #[must_use]
    pub fn partial_match(mut self, partial: bool) -> Self {
        self.partial_match = partial;
        self
    }
}

/// Extracted path parameters.
#[derive(Debug, Clone)]
pub struct ExtractedParams {
    /// Parameter name to extracted value.
    pub params: Vec<(String, String)>,
}

impl ExtractedParams {
    /// Create new extracted params.
    #[must_use]
    pub fn new() -> Self {
        Self { params: Vec::new() }
    }

    /// Add a parameter.
    #[must_use]
    pub fn param(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.params.push((name.into(), value.into()));
        self
    }

    /// Check if empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.params.is_empty()
    }
}

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

/// Information about middleware that will be applied.
#[derive(Debug, Clone)]
pub struct MiddlewareInfo {
    /// Middleware name.
    pub name: String,
    /// Whether this middleware is route-specific.
    pub route_specific: bool,
    /// Order in the middleware stack.
    pub order: usize,
}

impl MiddlewareInfo {
    /// Create new middleware info.
    #[must_use]
    pub fn new(name: impl Into<String>, order: usize) -> Self {
        Self {
            name: name.into(),
            route_specific: false,
            order,
        }
    }

    /// Mark as route-specific.
    #[must_use]
    pub fn route_specific(mut self, specific: bool) -> Self {
        self.route_specific = specific;
        self
    }
}

/// Complete routing debug information.
#[derive(Debug, Clone)]
pub struct RoutingDebugInfo {
    /// The request path being routed.
    pub request_path: String,
    /// The request method.
    pub request_method: String,
    /// All candidate routes that were considered.
    pub candidates: Vec<CandidateRoute>,
    /// The matched route (if any).
    pub matched_route: Option<String>,
    /// Extracted path parameters.
    pub extracted_params: ExtractedParams,
    /// Middleware that will be applied.
    pub middleware: Vec<MiddlewareInfo>,
    /// Time taken to route (in microseconds).
    pub routing_time: Option<Duration>,
    /// Whether any routes partially matched (405 scenario).
    pub has_partial_matches: bool,
}

impl RoutingDebugInfo {
    /// Create new routing debug info.
    #[must_use]
    pub fn new(path: impl Into<String>, method: impl Into<String>) -> Self {
        Self {
            request_path: path.into(),
            request_method: method.into(),
            candidates: Vec::new(),
            matched_route: None,
            extracted_params: ExtractedParams::new(),
            middleware: Vec::new(),
            routing_time: None,
            has_partial_matches: false,
        }
    }

    /// Add a candidate route.
    #[must_use]
    pub fn candidate(mut self, candidate: CandidateRoute) -> Self {
        if candidate.partial_match {
            self.has_partial_matches = true;
        }
        if candidate.result.is_match() {
            self.matched_route = Some(candidate.pattern.clone());
        }
        self.candidates.push(candidate);
        self
    }

    /// Set extracted parameters.
    #[must_use]
    pub fn params(mut self, params: ExtractedParams) -> Self {
        self.extracted_params = params;
        self
    }

    /// Add middleware info.
    #[must_use]
    pub fn middleware(mut self, mw: MiddlewareInfo) -> Self {
        self.middleware.push(mw);
        self
    }

    /// Set routing time.
    #[must_use]
    pub fn routing_time(mut self, duration: Duration) -> Self {
        self.routing_time = Some(duration);
        self
    }

    /// Check if routing was successful.
    #[must_use]
    pub fn is_matched(&self) -> bool {
        self.matched_route.is_some()
    }
}

/// Routing debug output formatter.
#[derive(Debug, Clone)]
pub struct RoutingDebug {
    mode: OutputMode,
    theme: FastApiTheme,
    /// Show all candidates or just relevant ones.
    pub show_all_candidates: bool,
    /// Show middleware stack.
    pub show_middleware: bool,
}

impl RoutingDebug {
    /// Create a new routing debug formatter.
    #[must_use]
    pub fn new(mode: OutputMode) -> Self {
        Self {
            mode,
            theme: FastApiTheme::default(),
            show_all_candidates: true,
            show_middleware: true,
        }
    }

    /// Set the theme.
    #[must_use]
    pub fn theme(mut self, theme: FastApiTheme) -> Self {
        self.theme = theme;
        self
    }

    /// Format routing debug information.
    #[must_use]
    pub fn format(&self, info: &RoutingDebugInfo) -> String {
        match self.mode {
            OutputMode::Plain => self.format_plain(info),
            OutputMode::Minimal => self.format_minimal(info),
            OutputMode::Rich => self.format_rich(info),
        }
    }

    fn format_plain(&self, info: &RoutingDebugInfo) -> String {
        let mut lines = Vec::new();

        // Header
        lines.push("=== Routing Debug ===".to_string());
        lines.push(format!(
            "Request: {} {}",
            info.request_method, info.request_path
        ));

        if let Some(duration) = info.routing_time {
            lines.push(format!("Routing time: {}", format_duration(duration)));
        }

        // Result summary
        lines.push(String::new());
        if let Some(matched) = &info.matched_route {
            lines.push(format!("Result: MATCHED -> {matched}"));
        } else if info.has_partial_matches {
            lines.push("Result: 405 Method Not Allowed".to_string());
        } else {
            lines.push("Result: 404 Not Found".to_string());
        }

        // Candidates
        if self.show_all_candidates && !info.candidates.is_empty() {
            lines.push(String::new());
            lines.push("Candidates considered:".to_string());
            for candidate in &info.candidates {
                let status = if candidate.result.is_match() {
                    "[MATCH]"
                } else if candidate.partial_match {
                    "[PARTIAL]"
                } else {
                    "[SKIP]"
                };
                let methods = candidate.methods.join(", ");
                lines.push(format!("  {status} {} [{methods}]", candidate.pattern));
                if !candidate.result.is_match() {
                    lines.push(format!(
                        "        Reason: {}",
                        candidate.result.description()
                    ));
                }
            }
        }

        // Extracted parameters
        if !info.extracted_params.is_empty() {
            lines.push(String::new());
            lines.push("Extracted parameters:".to_string());
            for (name, value) in &info.extracted_params.params {
                lines.push(format!("  {name}: {value}"));
            }
        }

        // Middleware
        if self.show_middleware && !info.middleware.is_empty() {
            lines.push(String::new());
            lines.push("Middleware stack:".to_string());
            for mw in &info.middleware {
                let scope = if mw.route_specific {
                    "(route)"
                } else {
                    "(global)"
                };
                lines.push(format!("  {}. {} {scope}", mw.order, mw.name));
            }
        }

        lines.push("=====================".to_string());
        lines.join("\n")
    }

    fn format_minimal(&self, info: &RoutingDebugInfo) -> String {
        let muted = self.theme.muted.to_ansi_fg();
        let accent = self.theme.accent.to_ansi_fg();
        let success = self.theme.success.to_ansi_fg();
        let error = self.theme.error.to_ansi_fg();
        let warning = self.theme.warning.to_ansi_fg();

        let mut lines = Vec::new();

        // Header
        lines.push(format!("{muted}=== Routing Debug ==={ANSI_RESET}"));
        let method_color = self.method_color(&info.request_method).to_ansi_fg();
        lines.push(format!(
            "{method_color}{}{ANSI_RESET} {accent}{}{ANSI_RESET}",
            info.request_method, info.request_path
        ));

        // Result
        if let Some(matched) = &info.matched_route {
            lines.push(format!("{success}✓ Matched:{ANSI_RESET} {matched}"));
        } else if info.has_partial_matches {
            lines.push(format!("{warning}⚠ 405 Method Not Allowed{ANSI_RESET}"));
        } else {
            lines.push(format!("{error}✗ 404 Not Found{ANSI_RESET}"));
        }

        // Timing
        if let Some(duration) = info.routing_time {
            lines.push(format!(
                "{muted}Routed in {}{ANSI_RESET}",
                format_duration(duration)
            ));
        }

        // Extracted parameters
        if !info.extracted_params.is_empty() {
            lines.push(format!("{muted}Parameters:{ANSI_RESET}"));
            for (name, value) in &info.extracted_params.params {
                lines.push(format!("  {accent}{name}{ANSI_RESET}: {value}"));
            }
        }

        lines.push(format!("{muted}=================={ANSI_RESET}"));
        lines.join("\n")
    }

    #[allow(clippy::too_many_lines)]
    fn format_rich(&self, info: &RoutingDebugInfo) -> String {
        let muted = self.theme.muted.to_ansi_fg();
        let accent = self.theme.accent.to_ansi_fg();
        let success = self.theme.success.to_ansi_fg();
        let error = self.theme.error.to_ansi_fg();
        let warning = self.theme.warning.to_ansi_fg();
        let border = self.theme.border.to_ansi_fg();
        let header_style = self.theme.header.to_ansi_fg();

        let mut lines = Vec::new();

        // Top border
        lines.push(format!(
            "{border}┌─────────────────────────────────────────────┐{ANSI_RESET}"
        ));
        lines.push(format!(
            "{border}{ANSI_RESET} {header_style}{ANSI_BOLD}Routing Debug{ANSI_RESET}                                {border}{ANSI_RESET}"
        ));
        lines.push(format!(
            "{border}├─────────────────────────────────────────────┤{ANSI_RESET}"
        ));

        // Request line
        let method_bg = self.method_color(&info.request_method).to_ansi_bg();
        lines.push(format!(
            "{border}{ANSI_RESET} {method_bg}{ANSI_BOLD} {} {ANSI_RESET} {accent}{}{ANSI_RESET}",
            info.request_method, info.request_path
        ));

        // Result row
        lines.push(format!(
            "{border}├─────────────────────────────────────────────┤{ANSI_RESET}"
        ));
        if let Some(matched) = &info.matched_route {
            lines.push(format!(
                "{border}{ANSI_RESET} {success}✓ Matched{ANSI_RESET}{matched}"
            ));
        } else if info.has_partial_matches {
            lines.push(format!(
                "{border}{ANSI_RESET} {warning}⚠ 405 Method Not Allowed{ANSI_RESET}"
            ));
            // Show allowed methods
            let allowed: Vec<_> = info
                .candidates
                .iter()
                .filter(|c| c.partial_match)
                .flat_map(|c| c.methods.iter())
                .collect();
            if !allowed.is_empty() {
                lines.push(format!(
                    "{border}{ANSI_RESET}   {muted}Allowed:{ANSI_RESET} {}",
                    allowed.into_iter().cloned().collect::<Vec<_>>().join(", ")
                ));
            }
        } else {
            lines.push(format!(
                "{border}{ANSI_RESET} {error}✗ 404 Not Found{ANSI_RESET}"
            ));
        }

        // Timing
        if let Some(duration) = info.routing_time {
            lines.push(format!(
                "{border}{ANSI_RESET} {muted}Routed in {}{ANSI_RESET}",
                format_duration(duration)
            ));
        }

        // Candidates section
        if self.show_all_candidates && !info.candidates.is_empty() {
            lines.push(format!(
                "{border}├─────────────────────────────────────────────┤{ANSI_RESET}"
            ));
            lines.push(format!(
                "{border}{ANSI_RESET} {header_style}Candidates{ANSI_RESET} {muted}({}){ANSI_RESET}",
                info.candidates.len()
            ));

            for candidate in &info.candidates {
                let (icon, color) = if candidate.result.is_match() {
                    ("", &success)
                } else if candidate.partial_match {
                    ("", &warning)
                } else {
                    ("", &muted)
                };
                let methods = candidate.methods.join("|");
                lines.push(format!(
                    "{border}{ANSI_RESET}   {color}{icon}{ANSI_RESET} {}{} {muted}[{methods}]{ANSI_RESET}",
                    candidate.pattern,
                    if let Some(h) = &candidate.handler {
                        format!(" {muted}{h}{ANSI_RESET}")
                    } else {
                        String::new()
                    }
                ));
                if !candidate.result.is_match()
                    && !matches!(candidate.result, MatchResult::PathMismatch)
                {
                    lines.push(format!(
                        "{border}{ANSI_RESET}     {muted}{}{ANSI_RESET}",
                        candidate.result.description()
                    ));
                }
            }
        }

        // Extracted parameters
        if !info.extracted_params.is_empty() {
            lines.push(format!(
                "{border}├─────────────────────────────────────────────┤{ANSI_RESET}"
            ));
            lines.push(format!(
                "{border}{ANSI_RESET} {header_style}Extracted Parameters{ANSI_RESET}"
            ));
            for (name, value) in &info.extracted_params.params {
                lines.push(format!(
                    "{border}{ANSI_RESET}   {accent}{name}{ANSI_RESET}: {value}"
                ));
            }
        }

        // Middleware stack
        if self.show_middleware && !info.middleware.is_empty() {
            lines.push(format!(
                "{border}├─────────────────────────────────────────────┤{ANSI_RESET}"
            ));
            lines.push(format!(
                "{border}{ANSI_RESET} {header_style}Middleware Stack{ANSI_RESET}"
            ));
            for mw in &info.middleware {
                let scope = if mw.route_specific {
                    format!("{accent}(route){ANSI_RESET}")
                } else {
                    format!("{muted}(global){ANSI_RESET}")
                };
                lines.push(format!(
                    "{border}{ANSI_RESET}   {muted}{}{ANSI_RESET} {} {scope}",
                    mw.order, mw.name
                ));
            }
        }

        // Bottom border
        lines.push(format!(
            "{border}└─────────────────────────────────────────────┘{ANSI_RESET}"
        ));

        lines.join("\n")
    }

    fn method_color(&self, method: &str) -> crate::themes::Color {
        match method.to_uppercase().as_str() {
            "GET" => self.theme.http_get,
            "POST" => self.theme.http_post,
            "PUT" => self.theme.http_put,
            "DELETE" => self.theme.http_delete,
            "PATCH" => self.theme.http_patch,
            "OPTIONS" => self.theme.http_options,
            "HEAD" => self.theme.http_head,
            _ => self.theme.muted,
        }
    }
}

/// Format a duration in human-readable form.
fn format_duration(duration: Duration) -> String {
    let micros = duration.as_micros();
    if micros < 1000 {
        format!("{micros}µs")
    } else if micros < 1_000_000 {
        let whole = micros / 1000;
        let frac = (micros % 1000) / 10;
        format!("{whole}.{frac:02}ms")
    } else {
        let whole = micros / 1_000_000;
        let frac = (micros % 1_000_000) / 10_000;
        format!("{whole}.{frac:02}s")
    }
}

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

    fn sample_successful_routing() -> RoutingDebugInfo {
        RoutingDebugInfo::new("/api/users/42", "GET")
            .candidate(
                CandidateRoute::new("/api/health", MatchResult::PathMismatch)
                    .methods(["GET"])
                    .handler("health_check"),
            )
            .candidate(
                CandidateRoute::new("/api/users", MatchResult::PathMismatch)
                    .methods(["GET", "POST"])
                    .handler("list_users"),
            )
            .candidate(
                CandidateRoute::new("/api/users/{id}", MatchResult::Matched)
                    .methods(["GET", "PUT", "DELETE"])
                    .handler("get_user"),
            )
            .params(ExtractedParams::new().param("id", "42"))
            .middleware(MiddlewareInfo::new("RequestLogger", 1))
            .middleware(MiddlewareInfo::new("Auth", 2))
            .middleware(MiddlewareInfo::new("RateLimit", 3).route_specific(true))
            .routing_time(Duration::from_micros(45))
    }

    fn sample_404_routing() -> RoutingDebugInfo {
        RoutingDebugInfo::new("/api/nonexistent", "GET")
            .candidate(
                CandidateRoute::new("/api/users", MatchResult::PathMismatch)
                    .methods(["GET"])
                    .handler("list_users"),
            )
            .candidate(
                CandidateRoute::new("/api/items", MatchResult::PathMismatch)
                    .methods(["GET"])
                    .handler("list_items"),
            )
            .routing_time(Duration::from_micros(12))
    }

    fn sample_405_routing() -> RoutingDebugInfo {
        RoutingDebugInfo::new("/api/users", "DELETE")
            .candidate(
                CandidateRoute::new("/api/users", MatchResult::MethodMismatch)
                    .methods(["GET", "POST"])
                    .handler("list_users")
                    .partial_match(true),
            )
            .routing_time(Duration::from_micros(8))
    }

    #[test]
    fn test_match_result_description() {
        assert_eq!(MatchResult::Matched.description(), "Matched");
        assert_eq!(
            MatchResult::PathMismatch.description(),
            "Path did not match"
        );
        assert_eq!(
            MatchResult::ParamTypeMismatch {
                param_name: "id".to_string(),
                expected_type: "int".to_string(),
                actual_value: "abc".to_string(),
            }
            .description(),
            "Parameter 'id' expected int, got 'abc'"
        );
    }

    #[test]
    fn test_routing_debug_plain_success() {
        let debug = RoutingDebug::new(OutputMode::Plain);
        let output = debug.format(&sample_successful_routing());

        assert!(output.contains("Routing Debug"));
        assert!(output.contains("GET /api/users/42"));
        assert!(output.contains("MATCHED"));
        assert!(output.contains("/api/users/{id}"));
        assert!(output.contains("id: 42"));
        assert!(output.contains("RequestLogger"));
        assert!(!output.contains("\x1b["));
    }

    #[test]
    fn test_routing_debug_plain_404() {
        let debug = RoutingDebug::new(OutputMode::Plain);
        let output = debug.format(&sample_404_routing());

        assert!(output.contains("404 Not Found"));
        assert!(!output.contains("MATCHED"));
    }

    #[test]
    fn test_routing_debug_plain_405() {
        let debug = RoutingDebug::new(OutputMode::Plain);
        let output = debug.format(&sample_405_routing());

        assert!(output.contains("405 Method Not Allowed"));
        assert!(output.contains("[PARTIAL]"));
    }

    #[test]
    fn test_routing_debug_rich_has_ansi() {
        let debug = RoutingDebug::new(OutputMode::Rich);
        let output = debug.format(&sample_successful_routing());

        assert!(output.contains("\x1b["));
        assert!(output.contains("✓ Matched"));
    }

    #[test]
    fn test_extracted_params_builder() {
        let params = ExtractedParams::new()
            .param("id", "42")
            .param("name", "alice");

        assert_eq!(params.params.len(), 2);
        assert!(!params.is_empty());
    }

    #[test]
    fn test_candidate_route_builder() {
        let candidate = CandidateRoute::new("/api/users/{id}", MatchResult::Matched)
            .methods(["GET", "PUT"])
            .handler("get_user")
            .partial_match(false);

        assert_eq!(candidate.pattern, "/api/users/{id}");
        assert_eq!(candidate.methods, vec!["GET", "PUT"]);
        assert!(candidate.result.is_match());
    }

    #[test]
    fn test_middleware_info_builder() {
        let mw = MiddlewareInfo::new("Auth", 1).route_specific(true);

        assert_eq!(mw.name, "Auth");
        assert_eq!(mw.order, 1);
        assert!(mw.route_specific);
    }
}