sqlmodel-console 0.2.2

Beautiful terminal output for SQLModel Rust - automatically adapts to agents vs humans
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
//! Connection pool status display renderable.
//!
//! Provides a visual dashboard for connection pool status, showing utilization,
//! health, and queue information at a glance.
//!
//! # Example
//!
//! ```rust
//! use sqlmodel_console::renderables::PoolStatusDisplay;
//!
//! // Create display with pool stats: active=8, idle=2, max=20, pending=0, timeouts=3
//! let display = PoolStatusDisplay::new(8, 2, 20, 0, 3);
//!
//! // Rich mode: Styled panel with progress bar
//! // Plain mode: Simple text output for agents
//! println!("{}", display.render_plain());
//! ```

use crate::theme::Theme;
use std::time::Duration;

/// Pool health status based on utilization and queue depth.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PoolHealth {
    /// Pool is healthy: low utilization, no waiting requests
    Healthy,
    /// Pool is busy: high utilization (>80%) but no waiting requests
    Busy,
    /// Pool is degraded: some requests are waiting
    Degraded,
    /// Pool is exhausted: at capacity with significant waiting queue
    Exhausted,
}

impl PoolHealth {
    /// Get a human-readable status string.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Healthy => "HEALTHY",
            Self::Busy => "BUSY",
            Self::Degraded => "DEGRADED",
            Self::Exhausted => "EXHAUSTED",
        }
    }

    /// Get the ANSI color code for this health status.
    #[must_use]
    pub fn color_code(&self) -> &'static str {
        match self {
            Self::Healthy => "\x1b[32m",        // Green
            Self::Busy => "\x1b[33m",           // Yellow
            Self::Degraded => "\x1b[38;5;208m", // Orange (256-color)
            Self::Exhausted => "\x1b[31m",      // Red
        }
    }
}

impl std::fmt::Display for PoolHealth {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Statistics snapshot for pool status display.
///
/// This trait allows PoolStatusDisplay to work with any type that provides
/// pool statistics, including `sqlmodel_pool::PoolStats`.
pub trait PoolStatsProvider {
    /// Number of connections currently in use.
    fn active_connections(&self) -> usize;
    /// Number of idle connections available.
    fn idle_connections(&self) -> usize;
    /// Maximum number of connections allowed.
    fn max_connections(&self) -> usize;
    /// Minimum number of connections to maintain.
    fn min_connections(&self) -> usize;
    /// Number of requests waiting for a connection.
    fn pending_requests(&self) -> usize;
    /// Total connections ever created.
    fn connections_created(&self) -> u64;
    /// Total connections closed.
    fn connections_closed(&self) -> u64;
    /// Total successful acquires.
    fn total_acquires(&self) -> u64;
    /// Total acquire timeouts.
    fn total_timeouts(&self) -> u64;
}

/// Display options for pool status.
#[derive(Debug, Clone)]
pub struct PoolStatusDisplay {
    /// Active connections
    active: usize,
    /// Idle connections
    idle: usize,
    /// Maximum connections
    max: usize,
    /// Minimum connections
    min: usize,
    /// Pending requests
    pending: usize,
    /// Total connections created
    created: u64,
    /// Total connections closed
    closed: u64,
    /// Total acquires
    acquires: u64,
    /// Total timeouts
    timeouts: u64,
    /// Theme for styled output
    theme: Theme,
    /// Optional width constraint
    width: Option<usize>,
    /// Pool uptime
    uptime: Option<Duration>,
    /// Pool name/label
    name: Option<String>,
}

impl PoolStatusDisplay {
    /// Create a new pool status display from statistics.
    #[must_use]
    pub fn from_stats<S: PoolStatsProvider>(stats: &S) -> Self {
        Self {
            active: stats.active_connections(),
            idle: stats.idle_connections(),
            max: stats.max_connections(),
            min: stats.min_connections(),
            pending: stats.pending_requests(),
            created: stats.connections_created(),
            closed: stats.connections_closed(),
            acquires: stats.total_acquires(),
            timeouts: stats.total_timeouts(),
            theme: Theme::default(),
            width: None,
            uptime: None,
            name: None,
        }
    }

    /// Create a pool status display with explicit values.
    #[must_use]
    pub fn new(active: usize, idle: usize, max: usize, min: usize, pending: usize) -> Self {
        Self {
            active,
            idle,
            max,
            min,
            pending,
            created: 0,
            closed: 0,
            acquires: 0,
            timeouts: 0,
            theme: Theme::default(),
            width: None,
            uptime: None,
            name: None,
        }
    }

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

    /// Set the display width.
    #[must_use]
    pub fn width(mut self, width: usize) -> Self {
        self.width = Some(width);
        self
    }

    /// Set the pool uptime.
    #[must_use]
    pub fn uptime(mut self, uptime: Duration) -> Self {
        self.uptime = Some(uptime);
        self
    }

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

    /// Set acquisition statistics.
    #[must_use]
    pub fn with_acquisition_stats(mut self, acquires: u64, timeouts: u64) -> Self {
        self.acquires = acquires;
        self.timeouts = timeouts;
        self
    }

    /// Set lifetime statistics.
    #[must_use]
    pub fn with_lifetime_stats(mut self, created: u64, closed: u64) -> Self {
        self.created = created;
        self.closed = closed;
        self
    }

    /// Get the total number of connections.
    #[must_use]
    pub fn total(&self) -> usize {
        self.active + self.idle
    }

    /// Calculate pool utilization as a percentage.
    #[must_use]
    pub fn utilization(&self) -> f64 {
        if self.max == 0 {
            0.0
        } else {
            (self.active as f64 / self.max as f64) * 100.0
        }
    }

    /// Determine pool health status.
    #[must_use]
    pub fn health(&self) -> PoolHealth {
        let utilization = self.utilization();

        if self.pending > 0 {
            if self.pending >= self.max || self.active >= self.max {
                PoolHealth::Exhausted
            } else {
                PoolHealth::Degraded
            }
        } else if utilization >= 80.0 {
            PoolHealth::Busy
        } else {
            PoolHealth::Healthy
        }
    }

    /// Format uptime duration as human-readable string.
    fn format_uptime(duration: Duration) -> String {
        let secs = duration.as_secs();
        if secs < 60 {
            format!("{}s", secs)
        } else if secs < 3600 {
            let mins = secs / 60;
            let secs = secs % 60;
            if secs == 0 {
                format!("{}m", mins)
            } else {
                format!("{}m {}s", mins, secs)
            }
        } else if secs < 86400 {
            let hours = secs / 3600;
            let mins = (secs % 3600) / 60;
            if mins == 0 {
                format!("{}h", hours)
            } else {
                format!("{}h {}m", hours, mins)
            }
        } else {
            let days = secs / 86400;
            let hours = (secs % 86400) / 3600;
            if hours == 0 {
                format!("{}d", days)
            } else {
                format!("{}d {}h", days, hours)
            }
        }
    }

    /// Render as plain text for agent consumption.
    #[must_use]
    pub fn render_plain(&self) -> String {
        let health = self.health();
        let utilization = self.utilization();
        let total = self.total();

        let mut lines = Vec::new();

        // Main status line
        let name_prefix = self
            .name
            .as_ref()
            .map(|n| format!("{}: ", n))
            .unwrap_or_default();

        lines.push(format!(
            "{}Pool: {}/{} active ({:.0}%), {} waiting, {}",
            name_prefix, self.active, self.max, utilization, self.pending, health
        ));

        // Detail line
        lines.push(format!(
            "  Active: {}, Idle: {}, Total: {}, Max: {}, Min: {}",
            self.active, self.idle, total, self.max, self.min
        ));

        // Statistics line (if available)
        if self.acquires > 0 || self.timeouts > 0 || self.created > 0 {
            let mut stats_parts = Vec::new();

            if self.acquires > 0 {
                stats_parts.push(format!("Acquires: {}", self.acquires));
            }
            if self.timeouts > 0 {
                stats_parts.push(format!("Timeouts: {}", self.timeouts));
            }
            if self.created > 0 {
                stats_parts.push(format!("Created: {}", self.created));
            }
            if self.closed > 0 {
                stats_parts.push(format!("Closed: {}", self.closed));
            }

            if !stats_parts.is_empty() {
                lines.push(format!("  {}", stats_parts.join(", ")));
            }
        }

        // Uptime line (if available)
        if let Some(uptime) = self.uptime {
            lines.push(format!("  Uptime: {}", Self::format_uptime(uptime)));
        }

        lines.join("\n")
    }

    /// Render with ANSI colors for terminal display.
    #[must_use]
    #[allow(clippy::cast_possible_truncation)]
    pub fn render_styled(&self) -> String {
        let health = self.health();
        let utilization = self.utilization();
        let width = self.width.unwrap_or(60).max(24);

        let mut lines = Vec::new();

        // Header with pool name
        let title = self.name.as_ref().map_or_else(
            || "Connection Pool Status".to_string(),
            |n| format!("Connection Pool: {}", n),
        );
        let title_display = self.truncate_plain_to_width(&title, width.saturating_sub(3));

        // Box drawing
        let top_border = format!("{}", "".repeat(width.saturating_sub(2)));
        let bottom_border = format!("{}", "".repeat(width.saturating_sub(2)));

        lines.push(top_border);
        lines.push(format!(
            "│ {:<inner_width$}│",
            title_display,
            inner_width = width.saturating_sub(3)
        ));
        lines.push(format!("{}", "".repeat(width.saturating_sub(2))));

        // Utilization bar
        let bar_width = width.saturating_sub(20);
        // Intentional truncation: utilization is 0-100%, bar_width is small
        let filled = ((utilization / 100.0) * bar_width as f64) as usize;
        let empty = bar_width.saturating_sub(filled);
        let bar = format!("{}{}", "".repeat(filled), "".repeat(empty));
        let status_width = width.saturating_sub(bar_width + 12);

        // Progress bar with percentage and status
        lines.push(format!(
            "│ [{}] {:.0}% {:<width$}│",
            bar,
            utilization,
            health.as_str(),
            width = status_width
        ));

        // Connection counts
        lines.push(
            format!(
                "│ Active: {:>4}  │  Idle: {:>4}  │  Max: {:>4}",
                self.active, self.idle, self.max
            )
            .chars()
            .take(width.saturating_sub(1))
            .collect::<String>()
                + "",
        );

        // Waiting requests (with color if any)
        if self.pending > 0 {
            lines.push(format!(
                "│ ⚠ Waiting requests: {:<width$}│",
                self.pending,
                width = width.saturating_sub(24)
            ));
        }

        // Statistics
        if self.acquires > 0 || self.timeouts > 0 {
            let timeout_rate = if self.acquires > 0 {
                (self.timeouts as f64 / self.acquires as f64) * 100.0
            } else {
                0.0
            };
            lines.push(format!(
                "│ Acquires: {} | Timeouts: {} ({:.1}%){:>width$}│",
                self.acquires,
                self.timeouts,
                timeout_rate,
                "",
                width = width.saturating_sub(40)
            ));
        }

        // Uptime
        if let Some(uptime) = self.uptime {
            lines.push(format!(
                "│ Uptime: {:<width$}│",
                Self::format_uptime(uptime),
                width = width.saturating_sub(12)
            ));
        }

        lines.push(bottom_border);

        lines.join("\n")
    }

    fn truncate_plain_to_width(&self, s: &str, max_visible: usize) -> String {
        if max_visible == 0 {
            return String::new();
        }

        let char_count = s.chars().count();
        if char_count <= max_visible {
            return s.to_string();
        }

        if max_visible <= 3 {
            return ".".repeat(max_visible);
        }

        let truncated: String = s.chars().take(max_visible - 3).collect();
        format!("{truncated}...")
    }
}

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

    /// Mock implementation of PoolStatsProvider for testing.
    #[derive(Debug, Clone)]
    struct MockPoolStats {
        active: usize,
        idle: usize,
        max: usize,
        min: usize,
        pending: usize,
        created: u64,
        closed: u64,
        acquires: u64,
        timeouts: u64,
    }

    impl MockPoolStats {
        fn healthy() -> Self {
            Self {
                active: 3,
                idle: 2,
                max: 20,
                min: 2,
                pending: 0,
                created: 100,
                closed: 95,
                acquires: 1000,
                timeouts: 5,
            }
        }

        fn busy() -> Self {
            Self {
                active: 17,
                idle: 1,
                max: 20,
                min: 2,
                pending: 0,
                created: 200,
                closed: 182,
                acquires: 5000,
                timeouts: 10,
            }
        }

        fn degraded() -> Self {
            // Degraded: some requests waiting but not at full capacity
            Self {
                active: 15,
                idle: 0,
                max: 20,
                min: 2,
                pending: 3,
                created: 300,
                closed: 280,
                acquires: 8000,
                timeouts: 50,
            }
        }

        fn exhausted() -> Self {
            Self {
                active: 20,
                idle: 0,
                max: 20,
                min: 2,
                pending: 25,
                created: 500,
                closed: 480,
                acquires: 10000,
                timeouts: 200,
            }
        }
    }

    impl PoolStatsProvider for MockPoolStats {
        fn active_connections(&self) -> usize {
            self.active
        }
        fn idle_connections(&self) -> usize {
            self.idle
        }
        fn max_connections(&self) -> usize {
            self.max
        }
        fn min_connections(&self) -> usize {
            self.min
        }
        fn pending_requests(&self) -> usize {
            self.pending
        }
        fn connections_created(&self) -> u64 {
            self.created
        }
        fn connections_closed(&self) -> u64 {
            self.closed
        }
        fn total_acquires(&self) -> u64 {
            self.acquires
        }
        fn total_timeouts(&self) -> u64 {
            self.timeouts
        }
    }

    #[test]
    fn test_pool_health_healthy() {
        let stats = MockPoolStats::healthy();
        let display = PoolStatusDisplay::from_stats(&stats);
        assert_eq!(display.health(), PoolHealth::Healthy);
    }

    #[test]
    fn test_pool_health_busy() {
        let stats = MockPoolStats::busy();
        let display = PoolStatusDisplay::from_stats(&stats);
        assert_eq!(display.health(), PoolHealth::Busy);
    }

    #[test]
    fn test_pool_health_degraded() {
        let stats = MockPoolStats::degraded();
        let display = PoolStatusDisplay::from_stats(&stats);
        assert_eq!(display.health(), PoolHealth::Degraded);
    }

    #[test]
    fn test_pool_health_exhausted() {
        let stats = MockPoolStats::exhausted();
        let display = PoolStatusDisplay::from_stats(&stats);
        assert_eq!(display.health(), PoolHealth::Exhausted);
    }

    #[test]
    fn test_pool_health_as_str() {
        assert_eq!(PoolHealth::Healthy.as_str(), "HEALTHY");
        assert_eq!(PoolHealth::Busy.as_str(), "BUSY");
        assert_eq!(PoolHealth::Degraded.as_str(), "DEGRADED");
        assert_eq!(PoolHealth::Exhausted.as_str(), "EXHAUSTED");
    }

    #[test]
    fn test_pool_health_display() {
        assert_eq!(format!("{}", PoolHealth::Healthy), "HEALTHY");
        assert_eq!(format!("{}", PoolHealth::Exhausted), "EXHAUSTED");
    }

    #[test]
    fn test_utilization_calculation() {
        let display = PoolStatusDisplay::new(10, 5, 20, 2, 0);
        assert!((display.utilization() - 50.0).abs() < 0.01);
    }

    #[test]
    fn test_utilization_zero_max() {
        let display = PoolStatusDisplay::new(0, 0, 0, 0, 0);
        assert!(display.utilization().abs() < f64::EPSILON);
    }

    #[test]
    fn test_total_connections() {
        let display = PoolStatusDisplay::new(8, 4, 20, 2, 0);
        assert_eq!(display.total(), 12);
    }

    #[test]
    fn test_render_plain_healthy() {
        let stats = MockPoolStats::healthy();
        let display = PoolStatusDisplay::from_stats(&stats);
        let output = display.render_plain();

        assert!(output.contains("Pool:"));
        assert!(output.contains("HEALTHY"));
        assert!(output.contains("Active: 3"));
        assert!(output.contains("Idle: 2"));
    }

    #[test]
    fn test_render_plain_with_name() {
        let display = PoolStatusDisplay::new(5, 3, 20, 2, 0).name("PostgreSQL Main");
        let output = display.render_plain();

        assert!(output.contains("PostgreSQL Main:"));
    }

    #[test]
    fn test_render_plain_with_uptime() {
        let display = PoolStatusDisplay::new(5, 3, 20, 2, 0).uptime(Duration::from_secs(3725)); // 1h 2m 5s
        let output = display.render_plain();

        assert!(output.contains("Uptime:"));
        assert!(output.contains("1h 2m"));
    }

    #[test]
    fn test_render_plain_with_waiting() {
        let display = PoolStatusDisplay::new(20, 0, 20, 2, 5);
        let output = display.render_plain();

        assert!(output.contains("5 waiting"));
        assert!(output.contains("DEGRADED") || output.contains("EXHAUSTED"));
    }

    #[test]
    fn test_format_uptime_seconds() {
        assert_eq!(
            PoolStatusDisplay::format_uptime(Duration::from_secs(45)),
            "45s"
        );
    }

    #[test]
    fn test_format_uptime_minutes() {
        assert_eq!(
            PoolStatusDisplay::format_uptime(Duration::from_secs(125)),
            "2m 5s"
        );
        assert_eq!(
            PoolStatusDisplay::format_uptime(Duration::from_secs(120)),
            "2m"
        );
    }

    #[test]
    fn test_format_uptime_hours() {
        assert_eq!(
            PoolStatusDisplay::format_uptime(Duration::from_secs(3725)),
            "1h 2m"
        );
        assert_eq!(
            PoolStatusDisplay::format_uptime(Duration::from_secs(3600)),
            "1h"
        );
    }

    #[test]
    fn test_format_uptime_days() {
        assert_eq!(
            PoolStatusDisplay::format_uptime(Duration::from_secs(90000)),
            "1d 1h"
        );
        assert_eq!(
            PoolStatusDisplay::format_uptime(Duration::from_secs(86400)),
            "1d"
        );
    }

    #[test]
    fn test_new_with_explicit_values() {
        let display = PoolStatusDisplay::new(10, 5, 30, 3, 2);

        assert_eq!(display.active, 10);
        assert_eq!(display.idle, 5);
        assert_eq!(display.max, 30);
        assert_eq!(display.min, 3);
        assert_eq!(display.pending, 2);
    }

    #[test]
    fn test_builder_pattern() {
        let display = PoolStatusDisplay::new(5, 3, 20, 2, 0)
            .theme(Theme::light())
            .width(80)
            .name("TestPool")
            .uptime(Duration::from_secs(60))
            .with_acquisition_stats(100, 5)
            .with_lifetime_stats(50, 45);

        assert_eq!(display.width, Some(80));
        assert_eq!(display.name, Some("TestPool".to_string()));
        assert!(display.uptime.is_some());
        assert_eq!(display.acquires, 100);
        assert_eq!(display.timeouts, 5);
        assert_eq!(display.created, 50);
        assert_eq!(display.closed, 45);
    }

    #[test]
    fn test_health_color_codes() {
        assert!(PoolHealth::Healthy.color_code().contains("32")); // Green
        assert!(PoolHealth::Busy.color_code().contains("33")); // Yellow
        assert!(PoolHealth::Exhausted.color_code().contains("31")); // Red
    }

    #[test]
    fn test_render_styled_contains_box_drawing() {
        let display = PoolStatusDisplay::new(5, 3, 20, 2, 0).width(60);
        let output = display.render_styled();

        assert!(output.contains(""));
        assert!(output.contains(""));
        assert!(output.contains(""));
        assert!(output.contains(""));
        assert!(output.contains(""));
    }

    #[test]
    fn test_render_styled_contains_progress_bar() {
        let display = PoolStatusDisplay::new(10, 5, 20, 2, 0).width(60);
        let output = display.render_styled();

        // Should contain bar characters
        assert!(output.contains("") || output.contains(""));
        assert!(output.contains("50%")); // 10/20 = 50%
    }

    #[test]
    fn test_render_styled_tiny_width_does_not_panic() {
        let display = PoolStatusDisplay::new(5, 3, 20, 2, 0).width(1);
        let output = display.render_styled();

        assert!(!output.is_empty());
        assert!(output.contains(""));
        assert!(output.contains(""));
    }

    #[test]
    fn test_render_styled_narrow_width_name_is_truncated() {
        let display = PoolStatusDisplay::new(5, 3, 20, 2, 0)
            .name("ExtremelyLongPoolNameForNarrowLayout")
            .width(24);
        let output = display.render_styled();

        assert!(output.contains("..."));
    }

    #[test]
    fn test_from_stats_captures_all_values() {
        let stats = MockPoolStats {
            active: 7,
            idle: 3,
            max: 25,
            min: 5,
            pending: 1,
            created: 150,
            closed: 140,
            acquires: 2000,
            timeouts: 15,
        };

        let display = PoolStatusDisplay::from_stats(&stats);

        assert_eq!(display.active, 7);
        assert_eq!(display.idle, 3);
        assert_eq!(display.max, 25);
        assert_eq!(display.min, 5);
        assert_eq!(display.pending, 1);
        assert_eq!(display.created, 150);
        assert_eq!(display.closed, 140);
        assert_eq!(display.acquires, 2000);
        assert_eq!(display.timeouts, 15);
    }
}