clawspec-core 0.4.4

Core library for generating OpenAPI specifications from tests
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
use std::future::Future;
use std::net::TcpListener;
use std::time::Duration;

use crate::{ApiClient, ApiClientBuilder};

/// A trait for implementing test server abstractions for different web frameworks.
///
/// This trait provides a generic interface for launching and managing test servers
/// for various web frameworks (e.g., Axum, Warp, actix-web). It allows the TestClient
/// to work with any server implementation in a framework-agnostic way.
///
/// # Associated Types
///
/// - [`Error`](TestServer::Error): Error type that can occur during server operations
///
/// # Required Methods
///
/// - [`launch`](TestServer::launch): Starts the server with the provided TcpListener
///
/// # Optional Methods
///
/// - [`is_healthy`](TestServer::is_healthy): Checks if the server is ready to accept requests
/// - [`config`](TestServer::config): Provides configuration for the test framework
///
/// # Example
///
/// ```rust
/// use clawspec_core::test_client::{TestServer, TestServerConfig, HealthStatus};
/// use std::net::TcpListener;
/// use std::time::Duration;
///
/// #[derive(Debug)]
/// struct ServerError;
///
/// impl std::fmt::Display for ServerError {
///     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
///         write!(f, "Server error")
///     }
/// }
///
/// impl std::error::Error for ServerError {}
///
/// #[derive(Debug)]
/// struct MyTestServer;
///
/// impl TestServer for MyTestServer {
///     type Error = ServerError;
///
///     async fn launch(&self, listener: TcpListener) -> Result<(), Self::Error> {
///         // Convert to non-blocking for tokio compatibility
///         listener.set_nonblocking(true).map_err(|_| ServerError)?;
///         let tokio_listener = tokio::net::TcpListener::from_std(listener)
///             .map_err(|_| ServerError)?;
///         
///         // Start your web server here
///         // For example, with axum:
///         // axum::serve(tokio_listener, app).await.map_err(|_| ServerError)?;
///         Ok(())
///     }
///
///     async fn is_healthy(&self, client: &mut clawspec_core::ApiClient) -> Result<HealthStatus, Self::Error> {
///         // Check if server is ready by making a health check request
///         match client.get("/health").unwrap().await {
///             Ok(_) => Ok(HealthStatus::Healthy),
///             Err(_) => Ok(HealthStatus::Unhealthy),
///         }
///     }
///
///     fn config(&self) -> TestServerConfig {
///         TestServerConfig {
///             api_client: Some(
///                 clawspec_core::ApiClient::builder()
///                     .with_host("localhost")
///                     .with_base_path("/api").unwrap()
///             ),
///             min_backoff_delay: Duration::from_millis(10),
///             max_backoff_delay: Duration::from_secs(1),
///             backoff_jitter: true,
///             max_retry_attempts: 10,
///         }
///     }
/// }
/// ```
///
/// # Framework Integration
///
/// ## Framework Integration Example
///
/// ```rust,no_run
/// use clawspec_core::test_client::{TestServer, TestServerConfig};
/// use std::net::TcpListener;
///
/// #[derive(Debug)]
/// struct WebFrameworkTestServer {
///     // Your web framework's app/router would go here
///     // For example: app: axum::Router, or app: warp::Filter, etc.
/// }
///
/// impl WebFrameworkTestServer {
///     fn new(/* app: YourApp */) -> Self {
///         Self { /* app */ }
///     }
/// }
///
/// impl TestServer for WebFrameworkTestServer {
///     type Error = std::io::Error; // or your custom error type
///
///     async fn launch(&self, listener: TcpListener) -> Result<(), Self::Error> {
///         listener.set_nonblocking(true)?;
///         let tokio_listener = tokio::net::TcpListener::from_std(listener)?;
///         
///         // Start your web framework here:
///         // For Axum: axum::serve(tokio_listener, self.app.clone()).await?;
///         // For Warp: warp::serve(self.app.clone()).run_async(tokio_listener).await;
///         // For actix-web: HttpServer::new(|| self.app.clone()).listen(tokio_listener)?.run().await?;
///         Ok(())
///     }
/// }
/// ```
///
/// # Health Checking
///
/// The `is_healthy` method allows implementing custom health check logic:
///
/// - Return `Ok(HealthStatus::Healthy)` if the server is ready to accept requests
/// - Return `Ok(HealthStatus::Unhealthy)` if the server is not ready
/// - Return `Ok(HealthStatus::Uncheckable)` to use the default TCP connection test
/// - Return `Err(Self::Error)` if an error occurs during health checking
///
/// The TestClient will wait for the server to become healthy before returning success.
/// Health check status returned by the `is_healthy` method.
///
/// This enum provides more explicit control over health checking behavior
/// compared to the previous `Option<bool>` approach.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HealthStatus {
    /// Server is healthy and ready to accept requests.
    Healthy,
    /// Server is not healthy or not ready yet.
    Unhealthy,
    /// Use the default TCP connection test to determine health.
    /// This is equivalent to the previous `None` return value.
    Uncheckable,
}

pub trait TestServer {
    /// The error type that can be returned by server operations.
    ///
    /// This should implement [`std::error::Error`] to provide proper error handling
    /// and error chain support.
    type Error: std::error::Error + Send + Sync + 'static;

    /// Launch the server using the provided TcpListener.
    ///
    /// This method should start the web server and bind it to the given listener.
    /// The implementation should convert the std::net::TcpListener to a tokio::net::TcpListener
    /// for async compatibility.
    ///
    /// # Arguments
    ///
    /// * `listener` - A TcpListener bound to a random port for testing
    ///
    /// # Returns
    ///
    /// * `Ok(())` - Server launched successfully
    /// * `Err(Self::Error)` - Server failed to launch
    ///
    /// # Example
    ///
    /// ```rust
    /// # use clawspec_core::test_client::{TestServer, HealthStatus};
    /// # use std::net::TcpListener;
    /// # #[derive(Debug)] struct ServerError;
    /// # impl std::fmt::Display for ServerError {
    /// #     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    /// #         write!(f, "Server error")
    /// #     }
    /// # }
    /// # impl std::error::Error for ServerError {}
    /// # struct MyServer;
    /// impl TestServer for MyServer {
    ///     type Error = ServerError;
    ///
    ///     async fn launch(&self, listener: TcpListener) -> Result<(), Self::Error> {
    ///         listener.set_nonblocking(true).map_err(|_| ServerError)?;
    ///         let tokio_listener = tokio::net::TcpListener::from_std(listener)
    ///             .map_err(|_| ServerError)?;
    ///         
    ///         // Start your server here
    ///         loop {
    ///             if let Ok((stream, _)) = tokio_listener.accept().await {
    ///                 // Handle connection
    ///             }
    ///         }
    ///     }
    /// }
    /// ```
    fn launch(&self, listener: TcpListener)
    -> impl Future<Output = Result<(), Self::Error>> + Send;

    /// Check if the server is healthy and ready to accept requests.
    ///
    /// This method is called periodically during server startup to determine
    /// when the server is ready. The default implementation returns `HealthStatus::Uncheckable`,
    /// which triggers a TCP connection test.
    ///
    /// # Arguments
    ///
    /// * `client` - An ApiClient configured for this test server
    ///
    /// # Returns
    ///
    /// * `Ok(HealthStatus::Healthy)` - Server is healthy and ready
    /// * `Ok(HealthStatus::Unhealthy)` - Server is not healthy
    /// * `Ok(HealthStatus::Uncheckable)` - Use default TCP connection test
    /// * `Err(Self::Error)` - Error occurred during health check
    ///
    /// # Example
    ///
    /// ```rust
    /// # use clawspec_core::{test_client::{TestServer, HealthStatus}, ApiClient};
    /// # use std::net::TcpListener;
    /// # #[derive(Debug)] struct ServerError;
    /// # impl std::fmt::Display for ServerError {
    /// #     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    /// #         write!(f, "Server error")
    /// #     }
    /// # }
    /// # impl std::error::Error for ServerError {}
    /// # struct MyServer;
    /// impl TestServer for MyServer {
    ///     # type Error = ServerError;
    ///     # async fn launch(&self, _listener: TcpListener) -> Result<(), Self::Error> { Ok(()) }
    ///     async fn is_healthy(&self, client: &mut ApiClient) -> Result<HealthStatus, Self::Error> {
    ///         // Try to make a health check request
    ///         match client.get("/health").unwrap().await {
    ///             Ok(_) => Ok(HealthStatus::Healthy),
    ///             Err(_) => Ok(HealthStatus::Unhealthy),
    ///         }
    ///     }
    /// }
    /// ```
    fn is_healthy(
        &self,
        _client: &mut ApiClient,
    ) -> impl Future<Output = Result<HealthStatus, Self::Error>> + Send {
        std::future::ready(Ok(HealthStatus::Uncheckable))
    }

    /// Provide configuration for the test framework.
    ///
    /// This method allows customizing the ApiClient and health check behavior
    /// for the specific server implementation.
    ///
    /// # Returns
    ///
    /// A TestServerConfig with custom settings, or default if not overridden.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use clawspec_core::{test_client::{TestServer, TestServerConfig}, ApiClient};
    /// # use std::{net::TcpListener, time::Duration};
    /// # struct MyServer;
    /// impl TestServer for MyServer {
    ///     # type Error = std::io::Error;
    ///     # async fn launch(&self, _listener: TcpListener) -> Result<(), Self::Error> { Ok(()) }
    ///     fn config(&self) -> TestServerConfig {
    ///         TestServerConfig {
    ///             api_client: Some(
    ///                 ApiClient::builder()
    ///                     .with_host("localhost")
    ///                     .with_base_path("/api").unwrap()
    ///             ),
    ///             min_backoff_delay: Duration::from_millis(10),
    ///             max_backoff_delay: Duration::from_secs(1),
    ///             backoff_jitter: true,
    ///             max_retry_attempts: 10,
    ///         }
    ///     }
    /// }
    /// ```
    fn config(&self) -> TestServerConfig {
        TestServerConfig::default()
    }
}

/// Configuration for test server behavior and client setup.
///
/// This struct allows customizing how the TestClient interacts with the test server,
/// including the ApiClient configuration and exponential backoff timing for health checks.
///
/// # Fields
///
/// * `api_client` - Optional pre-configured ApiClient builder for custom client setup
/// * `min_backoff_delay` - Minimum delay for exponential backoff between health check retries
/// * `max_backoff_delay` - Maximum delay for exponential backoff between health check retries
/// * `backoff_jitter` - Whether to add jitter to exponential backoff delays
/// * `max_retry_attempts` - Maximum number of health check retry attempts
///
/// # Examples
///
/// ## Default Configuration
///
/// ```rust
/// use clawspec_core::test_client::TestServerConfig;
/// use std::time::Duration;
///
/// let config = TestServerConfig::default();
/// assert!(config.api_client.is_none());
/// assert_eq!(config.min_backoff_delay, Duration::from_millis(10));
/// assert_eq!(config.max_backoff_delay, Duration::from_secs(1));
/// assert_eq!(config.backoff_jitter, true);
/// assert_eq!(config.max_retry_attempts, 10);
/// ```
///
/// ## Custom Configuration
///
/// ```rust
/// use clawspec_core::{test_client::TestServerConfig, ApiClient};
/// use std::time::Duration;
///
/// let config = TestServerConfig {
///     api_client: Some(
///         ApiClient::builder()
///             .with_host("test-server.local")
///             .with_port(3000)
///             .with_base_path("/api/v1").unwrap()
///     ),
///     min_backoff_delay: Duration::from_millis(50),
///     max_backoff_delay: Duration::from_secs(5),
///     backoff_jitter: false,
///     max_retry_attempts: 3,
/// };
/// ```
///
/// ## Using with TestServer
///
/// ```rust
/// use clawspec_core::test_client::{TestServer, TestServerConfig};
/// use std::{net::TcpListener, time::Duration};
///
/// #[derive(Debug)]
/// struct MyTestServer;
///
/// impl TestServer for MyTestServer {
///     type Error = std::io::Error;
///
///     async fn launch(&self, listener: TcpListener) -> Result<(), Self::Error> {
///         // Server implementation
///         listener.set_nonblocking(true)?;
///         let _tokio_listener = tokio::net::TcpListener::from_std(listener)?;
///         Ok(())
///     }
///
///     fn config(&self) -> TestServerConfig {
///         TestServerConfig {
///             api_client: Some(
///                 clawspec_core::ApiClient::builder()
///                     .with_host("localhost")
///                     .with_base_path("/api").unwrap()
///             ),
///             min_backoff_delay: Duration::from_millis(25),
///             max_backoff_delay: Duration::from_secs(2),
///             backoff_jitter: true,
///             max_retry_attempts: 15,
///         }
///     }
/// }
/// ```
#[derive(Debug, Clone)]
pub struct TestServerConfig {
    /// Optional pre-configured ApiClient builder.
    ///
    /// If provided, this builder will be used as the base for creating the ApiClient.
    /// If None, a default builder will be used. The TestClient will automatically
    /// configure the port based on the bound server address.
    ///
    /// # Example
    ///
    /// ```rust
    /// use clawspec_core::{test_client::TestServerConfig, ApiClient};
    ///
    /// let config = TestServerConfig {
    ///     api_client: Some(
    ///         ApiClient::builder()
    ///             .with_host("api.example.com")
    ///             .with_base_path("/v1").unwrap()
    ///     ),
    ///     min_backoff_delay: std::time::Duration::from_millis(10),
    ///     max_backoff_delay: std::time::Duration::from_secs(1),
    ///     backoff_jitter: true,
    ///     max_retry_attempts: 10,
    /// };
    /// ```
    pub api_client: Option<ApiClientBuilder>,

    /// Minimum delay for exponential backoff between health check retries.
    ///
    /// This is the initial delay used when the server is unhealthy and needs
    /// to be retried. The delay will increase exponentially up to `max_backoff_delay`.
    ///
    /// # Default
    ///
    /// 10 milliseconds
    ///
    /// # Example
    ///
    /// ```rust
    /// use clawspec_core::test_client::TestServerConfig;
    /// use std::time::Duration;
    ///
    /// let config = TestServerConfig {
    ///     min_backoff_delay: Duration::from_millis(50), // Start with 50ms
    ///     ..Default::default()
    /// };
    /// ```
    pub min_backoff_delay: Duration,

    /// Maximum delay for exponential backoff between health check retries.
    ///
    /// This is the upper bound for the exponential backoff delay. Once the
    /// delay reaches this value, it will not increase further.
    ///
    /// # Default
    ///
    /// 1 second
    ///
    /// # Example
    ///
    /// ```rust
    /// use clawspec_core::test_client::TestServerConfig;
    /// use std::time::Duration;
    ///
    /// let config = TestServerConfig {
    ///     max_backoff_delay: Duration::from_secs(5), // Max 5 seconds
    ///     ..Default::default()
    /// };
    /// ```
    pub max_backoff_delay: Duration,

    /// Whether to add jitter to the exponential backoff delays.
    ///
    /// Jitter adds randomization to retry delays to prevent the "thundering herd"
    /// problem when multiple clients retry simultaneously. This is generally
    /// recommended for production use.
    ///
    /// # Default
    ///
    /// `true` (jitter enabled)
    ///
    /// # Example
    ///
    /// ```rust
    /// use clawspec_core::test_client::TestServerConfig;
    ///
    /// let config = TestServerConfig {
    ///     backoff_jitter: false, // Disable jitter for predictable timing
    ///     ..Default::default()
    /// };
    /// ```
    pub backoff_jitter: bool,

    /// Maximum number of health check retry attempts.
    ///
    /// This limits the total number of health check attempts before giving up.
    /// The health check will stop retrying once this number of attempts is reached,
    /// preventing infinite loops when a server never becomes healthy.
    ///
    /// # Default
    ///
    /// 10 attempts
    ///
    /// # Example
    ///
    /// ```rust
    /// use clawspec_core::test_client::TestServerConfig;
    ///
    /// let config = TestServerConfig {
    ///     max_retry_attempts: 5, // Only try 5 times before giving up
    ///     ..Default::default()
    /// };
    /// ```
    pub max_retry_attempts: usize,
}

impl Default for TestServerConfig {
    fn default() -> Self {
        Self {
            api_client: None,
            min_backoff_delay: Duration::from_millis(10),
            max_backoff_delay: Duration::from_secs(1),
            backoff_jitter: true,
            max_retry_attempts: 10,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ApiClient;
    use std::net::TcpListener;
    use std::time::Duration;
    use tokio::net::TcpListener as TokioTcpListener;

    /// Mock server implementation for testing
    #[derive(Debug)]
    struct MockServer {
        config: TestServerConfig,
        should_be_healthy: bool,
    }

    impl MockServer {
        fn new() -> Self {
            Self {
                config: TestServerConfig::default(),
                should_be_healthy: true,
            }
        }

        fn with_health_status(mut self, healthy: bool) -> Self {
            self.should_be_healthy = healthy;
            self
        }
    }

    impl TestServer for MockServer {
        type Error = std::io::Error;

        async fn launch(&self, listener: TcpListener) -> Result<(), Self::Error> {
            // Convert to non-blocking for tokio compatibility
            listener.set_nonblocking(true)?;
            let tokio_listener = TokioTcpListener::from_std(listener)?;

            // Simple echo server for testing
            loop {
                if let Ok((mut stream, _)) = tokio_listener.accept().await {
                    tokio::spawn(async move {
                        // Simple HTTP response
                        let response = "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n";
                        if let Err(e) =
                            tokio::io::AsyncWriteExt::write_all(&mut stream, response.as_bytes())
                                .await
                        {
                            eprintln!("Failed to write response: {e}");
                        }
                    });
                }
            }
        }

        async fn is_healthy(&self, _client: &mut ApiClient) -> Result<HealthStatus, Self::Error> {
            Ok(if self.should_be_healthy {
                HealthStatus::Healthy
            } else {
                HealthStatus::Unhealthy
            })
        }

        fn config(&self) -> TestServerConfig {
            self.config.clone()
        }
    }

    #[test]
    fn test_test_server_config_default() {
        let config = TestServerConfig::default();

        assert!(config.api_client.is_none());
        assert_eq!(config.min_backoff_delay, Duration::from_millis(10));
        assert_eq!(config.max_backoff_delay, Duration::from_secs(1));
        assert!(config.backoff_jitter);
        assert_eq!(config.max_retry_attempts, 10);
    }

    #[test]
    fn test_test_server_config_custom() {
        let min_delay = Duration::from_millis(50);
        let max_delay = Duration::from_secs(5);
        let client_builder = ApiClient::builder()
            .with_host("test.example.com")
            .with_port(8080);

        let config = TestServerConfig {
            api_client: Some(client_builder),
            min_backoff_delay: min_delay,
            max_backoff_delay: max_delay,
            backoff_jitter: false,
            max_retry_attempts: 5,
        };

        assert!(config.api_client.is_some());
        assert_eq!(config.min_backoff_delay, min_delay);
        assert_eq!(config.max_backoff_delay, max_delay);
        assert!(!config.backoff_jitter);
        assert_eq!(config.max_retry_attempts, 5);
    }

    #[tokio::test]
    async fn test_mock_server_health_check_healthy() {
        let server = MockServer::new().with_health_status(true);
        let mut client = ApiClient::builder().build().expect("valid client");

        let result = server.is_healthy(&mut client).await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), HealthStatus::Healthy);
    }

    #[tokio::test]
    async fn test_mock_server_health_check_unhealthy() {
        let server = MockServer::new().with_health_status(false);
        let mut client = ApiClient::builder().build().expect("valid client");

        let result = server.is_healthy(&mut client).await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), HealthStatus::Unhealthy);
    }

    #[test]
    fn test_default_backoff_configuration() {
        let config = TestServerConfig::default();

        // Verify the default backoff configuration values
        assert_eq!(config.min_backoff_delay, Duration::from_millis(10));
        assert_eq!(config.max_backoff_delay, Duration::from_secs(1));
        assert!(config.backoff_jitter);
        assert_eq!(config.max_retry_attempts, 10);
    }

    #[test]
    fn test_test_server_trait_bounds() {
        // Test that MockServer implements the required traits
        fn assert_test_server<T: TestServer + Send + Sync + 'static>(_: T) {}

        let server = MockServer::new();
        assert_test_server(server);
    }

    #[test]
    fn test_health_status_healthy_variant() {
        let status = HealthStatus::Healthy;
        assert_eq!(status, HealthStatus::Healthy);
    }

    #[test]
    fn test_health_status_unhealthy_variant() {
        let status = HealthStatus::Unhealthy;
        assert_eq!(status, HealthStatus::Unhealthy);
    }

    #[test]
    fn test_health_status_uncheckable_variant() {
        let status = HealthStatus::Uncheckable;
        assert_eq!(status, HealthStatus::Uncheckable);
    }

    #[test]
    fn test_health_status_debug() {
        let healthy = HealthStatus::Healthy;
        let unhealthy = HealthStatus::Unhealthy;
        let uncheckable = HealthStatus::Uncheckable;

        assert!(format!("{healthy:?}").contains("Healthy"));
        assert!(format!("{unhealthy:?}").contains("Unhealthy"));
        assert!(format!("{uncheckable:?}").contains("Uncheckable"));
    }

    #[test]
    fn test_health_status_clone() {
        let original = HealthStatus::Healthy;
        let cloned = original;

        assert_eq!(original, cloned);
    }

    #[test]
    fn test_health_status_copy() {
        let status = HealthStatus::Unhealthy;
        let copied = status;

        assert_eq!(status, copied);
    }

    #[test]
    fn test_health_status_equality() {
        assert_eq!(HealthStatus::Healthy, HealthStatus::Healthy);
        assert_eq!(HealthStatus::Unhealthy, HealthStatus::Unhealthy);
        assert_eq!(HealthStatus::Uncheckable, HealthStatus::Uncheckable);

        assert_ne!(HealthStatus::Healthy, HealthStatus::Unhealthy);
        assert_ne!(HealthStatus::Healthy, HealthStatus::Uncheckable);
        assert_ne!(HealthStatus::Unhealthy, HealthStatus::Uncheckable);
    }

    /// A minimal test server that uses the default is_healthy implementation
    #[derive(Debug)]
    struct MinimalServer;

    impl TestServer for MinimalServer {
        type Error = std::io::Error;

        async fn launch(&self, _listener: TcpListener) -> Result<(), Self::Error> {
            Ok(())
        }
        // Uses default is_healthy() which returns Uncheckable
        // Uses default config()
    }

    #[tokio::test]
    async fn test_default_is_healthy_returns_uncheckable() {
        let server = MinimalServer;
        let mut client = ApiClient::builder().build().expect("valid client");

        let result = server.is_healthy(&mut client).await;
        assert!(result.is_ok());
        assert_eq!(result.expect("should be Ok"), HealthStatus::Uncheckable);
    }

    #[test]
    fn test_default_config_returns_defaults() {
        let server = MinimalServer;
        let config = server.config();

        assert!(config.api_client.is_none());
        assert_eq!(config.min_backoff_delay, Duration::from_millis(10));
        assert_eq!(config.max_backoff_delay, Duration::from_secs(1));
        assert!(config.backoff_jitter);
        assert_eq!(config.max_retry_attempts, 10);
    }

    #[test]
    fn test_test_server_config_debug() {
        let config = TestServerConfig::default();
        let debug_str = format!("{config:?}");

        assert!(debug_str.contains("TestServerConfig"));
        assert!(debug_str.contains("min_backoff_delay"));
        assert!(debug_str.contains("max_backoff_delay"));
    }

    #[test]
    fn test_test_server_config_clone() {
        let original = TestServerConfig {
            api_client: None,
            min_backoff_delay: Duration::from_millis(100),
            max_backoff_delay: Duration::from_secs(10),
            backoff_jitter: false,
            max_retry_attempts: 3,
        };
        let cloned = original.clone();

        assert!(cloned.api_client.is_none());
        assert_eq!(cloned.min_backoff_delay, Duration::from_millis(100));
        assert_eq!(cloned.max_backoff_delay, Duration::from_secs(10));
        assert!(!cloned.backoff_jitter);
        assert_eq!(cloned.max_retry_attempts, 3);
    }

    #[test]
    fn test_test_server_config_with_api_client_clone() {
        let original = TestServerConfig {
            api_client: Some(ApiClient::builder().with_host("test.local").with_port(3000)),
            min_backoff_delay: Duration::from_millis(50),
            max_backoff_delay: Duration::from_secs(2),
            backoff_jitter: true,
            max_retry_attempts: 5,
        };
        let cloned = original.clone();

        assert!(cloned.api_client.is_some());
        assert_eq!(cloned.min_backoff_delay, Duration::from_millis(50));
        assert_eq!(cloned.max_backoff_delay, Duration::from_secs(2));
        assert!(cloned.backoff_jitter);
        assert_eq!(cloned.max_retry_attempts, 5);
    }
}