vkteams-bot 0.11.5

High-performance VK Teams Bot API toolkit with CLI and MCP server support
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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
//! Network module
use crate::api::types::*;
use crate::config::CONFIG;
use crate::error::{BotError, Result};
use bytes::Bytes;
use rand::Rng;
use reqwest::{
    Body, Client, ClientBuilder, StatusCode, Url,
    multipart::{Form, Part},
};
use std::time::Duration;
use tokio::fs::File;
use tokio::signal;
use tokio::time::sleep;
use tokio_util::codec::{BytesCodec, FramedRead};
use tracing::{debug, error, trace, warn};
/// Connection pool for managing HTTP connections
#[derive(Debug, Clone)]
pub struct ConnectionPool {
    client: Client,
    retries: usize,
    max_backoff: Duration,
}

impl Default for ConnectionPool {
    fn default() -> Self {
        let cfg = &CONFIG.network;
        Self::new(
            Client::new(),
            cfg.retries,
            Duration::from_millis(cfg.max_backoff_ms),
        )
    }
}

impl ConnectionPool {
    /// Create a new connection pool with custom settings
    pub fn new(client: Client, retries: usize, max_backoff: Duration) -> Self {
        Self {
            client,
            retries,
            max_backoff,
        }
    }

    /// Create a connection pool with optimized settings for the VK Teams Bot API
    pub fn optimized() -> Self {
        let cfg = &CONFIG.network;
        let client = build_optimized_client().unwrap_or_else(|e| {
            warn!(
                "Failed to build optimized client. Use default instead: {}",
                e
            );
            Client::new()
        });
        let retries = cfg.retries;
        let max_backoff = Duration::from_millis(cfg.max_backoff_ms);

        Self {
            client,
            retries,
            max_backoff,
        }
    }

    /// Execute a request with exponential backoff retry strategy
    pub async fn execute_with_retry<F, Fut, T>(&self, operation: F) -> Result<T>
    where
        F: Fn() -> Fut + Send + Sync,
        Fut: std::future::Future<Output = Result<T>> + Send,
        T: Send,
    {
        let mut retries = 0;
        let mut backoff_ms = 100;

        loop {
            match operation().await {
                Ok(result) => return Ok(result),
                Err(e) => {
                    if let BotError::Network(ref req_err) = e {
                        if !should_retry(req_err) || retries >= self.retries {
                            return Err(e);
                        }

                        retries += 1;
                        let jitter = rand::random::<u64>() % 100;
                        let delay = Duration::from_millis(backoff_ms + jitter);

                        warn!(
                            "Request failed, retrying ({}/{}): {} after {:?}",
                            retries, self.retries, req_err, delay
                        );

                        sleep(delay).await;
                        backoff_ms =
                            std::cmp::min(backoff_ms * 2, self.max_backoff.as_millis() as u64);
                    } else {
                        return Err(e);
                    }
                }
            }
        }
    }

    /// Get text response from API with retry capability
    #[tracing::instrument(skip(self))]
    pub async fn get_text(&self, url: Url) -> Result<String> {
        debug!("Getting response from API at path {}...", url);

        let url_str = url.as_str().to_string();
        self.execute_with_retry(move || {
            let client = self.client.clone();
            let url_str = url_str.clone();

            async move {
                let response = client.get(&url_str).send().await?;
                trace!("Response status: {}", response.status());

                validate_response(&response.status())?;

                let text = response.text().await?;
                trace!("Response body length: {} bytes", text.len());
                Ok(text)
            }
        })
        .await
    }

    /// Get bytes response from API with retry capability
    #[tracing::instrument(skip(self))]
    pub async fn get_bytes(&self, url: Url) -> Result<Vec<u8>> {
        debug!("Getting binary response from API at path {}...", url);

        let url_str = url.as_str().to_string();
        self.execute_with_retry(move || {
            let client = self.client.clone();
            let url_str = url_str.clone();

            async move {
                let response = client.get(&url_str).send().await?;
                trace!("Response status: {}", response.status());

                validate_response(&response.status())?;

                let bytes = response.bytes().await?;
                trace!("Response body size: {} bytes", bytes.len());
                Ok(bytes.to_vec())
            }
        })
        .await
    }

    /// Post file to API with retry capability using retryable form
    #[tracing::instrument(skip(self, retryable_form))]
    pub async fn post_file_retryable(
        &self,
        url: Url,
        retryable_form: &RetryableMultipartForm,
    ) -> Result<String> {
        debug!(
            "Sending file to API at path {} (size: {} bytes)...",
            url,
            retryable_form.size()
        );

        let mut attempts = 0;
        let max_attempts = self.retries + 1;

        loop {
            attempts += 1;

            // Create fresh form for each attempt
            let form = retryable_form.to_form();

            trace!("Attempt {} of {}", attempts, max_attempts);

            let response = self.client.post(url.as_str()).multipart(form).send().await;

            match response {
                Ok(response) => {
                    trace!("Response status: {}", response.status());

                    // Validate the response
                    if let Err(e) = validate_response(&response.status()) {
                        if attempts >= max_attempts || !should_retry_status(&response.status()) {
                            return Err(e);
                        }

                        let backoff = calculate_backoff_duration(attempts, self.max_backoff);
                        warn!(
                            "HTTP error {}, retrying in {:?} (attempt {} of {})",
                            response.status(),
                            backoff,
                            attempts,
                            max_attempts
                        );
                        sleep(backoff).await;
                        continue;
                    }

                    // Get the response text
                    let text = response.text().await?;
                    trace!("Response body length: {} bytes", text.len());
                    debug!("File uploaded successfully after {} attempt(s)", attempts);
                    return Ok(text);
                }
                Err(err) => {
                    if attempts >= max_attempts || !should_retry(&err) {
                        error!("File upload failed after {} attempt(s): {}", attempts, err);
                        return Err(BotError::Network(err));
                    }

                    let backoff = calculate_backoff_duration(attempts, self.max_backoff);
                    warn!(
                        "File upload failed, retrying in {:?} (attempt {} of {}): {}",
                        backoff, attempts, max_attempts, err
                    );
                    sleep(backoff).await;
                }
            }
        }
    }

    /// Post file to API with retry capability (legacy method for backward compatibility)
    #[tracing::instrument(skip(self, form))]
    pub async fn post_file(&self, url: Url, form: Form) -> Result<String> {
        debug!(
            "Sending file to API at path {} (legacy method, no retry)...",
            url
        );

        let response = self.client.post(url.as_str()).multipart(form).send().await;

        match response {
            Ok(response) => {
                trace!("Response status: {}", response.status());
                validate_response(&response.status())?;
                let text = response.text().await?;
                trace!("Response body length: {} bytes", text.len());
                Ok(text)
            }
            Err(err) => {
                warn!("File upload failed (no retry available): {}", err);
                Err(BotError::Network(err))
            }
        }
    }
}

/// Validate HTTP response status
fn validate_response(status: &StatusCode) -> Result<()> {
    if status.is_success() {
        Ok(())
    } else if status.is_server_error() {
        warn!("Server error: {}", status);
        Err(BotError::System(format!("Server error: HTTP {status}")))
    } else if status.is_client_error() {
        error!("Client error: {}", status);
        Err(BotError::Validation(format!("HTTP error: {status}")))
    } else {
        warn!("Unexpected status code: {}", status);
        Err(BotError::System(format!(
            "Unexpected HTTP status code: {status}"
        )))
    }
}

/// Determine if the request should be retried based on the error
fn should_retry(err: &reqwest::Error) -> bool {
    err.is_timeout()
        || err.is_connect()
        || err.is_request()
        || (err.status().is_some_and(|s| s.is_server_error()))
}

/// Check if HTTP status code should trigger a retry
pub fn should_retry_status(status: &StatusCode) -> bool {
    match status.as_u16() {
        // Retry on server errors (5xx)
        500..=599 => true,
        // Retry on rate limiting
        429 => true,
        // Retry on specific client errors that might be transient
        408 | 409 | 423 | 424 => true,
        // Don't retry on other client errors (4xx) or success (2xx/3xx)
        _ => false,
    }
}

/// Calculate exponential backoff duration with jitter
pub fn calculate_backoff_duration(attempt: usize, max_backoff: Duration) -> Duration {
    let base_duration = Duration::from_millis(100); // 100ms base
    let exponential_backoff = base_duration * 2_u32.pow((attempt - 1) as u32);

    // Cap at max_backoff
    let capped_backoff = std::cmp::min(exponential_backoff, max_backoff);

    // Add jitter (random ±25%)
    let jitter_range = capped_backoff.as_millis() / 4; // 25% of the duration
    let mut rng = rand::rng();
    let jitter = rng.random_range(0..=(jitter_range as u64 * 2));
    let jitter_offset = jitter as i64 - jitter_range as i64;

    let final_duration = (capped_backoff.as_millis() as i64 + jitter_offset).max(0) as u64;
    Duration::from_millis(final_duration)
}

/// Build a client with optimized settings for the API
fn build_optimized_client() -> Result<Client> {
    let cfg = &CONFIG.network;
    let builder = ClientBuilder::new()
        .timeout(Duration::from_secs(cfg.request_timeout_secs))
        .connect_timeout(Duration::from_secs(cfg.connect_timeout_secs))
        .pool_idle_timeout(Duration::from_secs(cfg.pool_idle_timeout_secs))
        .tcp_nodelay(true)
        .pool_max_idle_per_host(cfg.max_idle_connections)
        .use_rustls_tls();

    builder.build().map_err(BotError::Network)
}
/// Get bytes response from API
/// Send request with [`Client`] `get` method and get body with [`reqwest::Response`] `bytes` method
/// - `url` - file URL
///
/// ## Errors
/// - `BotError::Network` - network error when sending request or receiving response
///
/// @deprecated Use ConnectionPool::get_bytes instead
#[tracing::instrument(skip(client))]
pub async fn get_bytes_response(client: Client, url: Url) -> Result<Vec<u8>> {
    debug!("Getting binary response from API at path {}...", url);
    let response = client.get(url.as_str()).send().await?;
    trace!("Response status: {}", response.status());
    let bytes = response.bytes().await?;
    Ok(bytes.to_vec())
}
/// Upload file stream to API in multipart form
/// - `file` - file name
///
/// ## Errors
/// - `BotError::Validation` - file not specified, invalid path, or filename validation failed
/// - `BotError::Io` - error working with file
#[tracing::instrument(skip(file))]
/// Create retryable multipart form from MultipartName
/// Recommended for file operations that need retry capability
pub async fn file_to_retryable_multipart(file: &MultipartName) -> Result<RetryableMultipartForm> {
    match file {
        MultipartName::FilePath(path) | MultipartName::ImagePath(path) => {
            RetryableMultipartForm::from_file_path(path.clone()).await
        }
        MultipartName::FileContent { filename, content }
        | MultipartName::ImageContent { filename, content } => {
            // Validate filename
            validate_filename(filename)?;

            // Validate content
            if content.is_empty() {
                return Err(BotError::Validation(
                    "File content cannot be empty".to_string(),
                ));
            }

            Ok(RetryableMultipartForm::from_content(
                filename.clone(),
                filename.clone(),
                content.clone(),
            ))
        }
        _ => Err(BotError::Validation("File not specified".to_string())),
    }
}

/// Create multipart form from MultipartName (legacy method)
/// Note: This method doesn't support retries due to Form's lack of Clone trait
pub async fn file_to_multipart(file: &MultipartName) -> Result<Form> {
    //Get name of the form part
    match file {
        MultipartName::FilePath(name) | MultipartName::ImagePath(name) => {
            // Validate file path
            validate_file_path(name)?;

            let file_stream = make_stream(name).await?;
            let part = Part::stream(file_stream).file_name(name.to_string());
            Ok(Form::new().part(name.to_string(), part))
        }
        MultipartName::FileContent { filename, content }
        | MultipartName::ImageContent { filename, content } => {
            // Validate filename
            validate_filename(filename)?;

            // Validate content
            if content.is_empty() {
                return Err(BotError::Validation(
                    "File content cannot be empty".to_string(),
                ));
            }

            let part = Part::bytes(content.clone()).file_name(filename.clone());
            Ok(Form::new().part(filename.to_string(), part))
        }
        _ => Err(BotError::Validation("File not specified".to_string())),
    }
}
/// Create stream from file
/// - `path` - file path
///
/// ## Errors
/// - `BotError::Io` - error opening file
#[tracing::instrument(skip(path))]
async fn make_stream(path: &String) -> Result<Body> {
    //Open file and check if it exists
    let file = File::open(path).await?;
    //Create stream from file
    let file_stream = Body::wrap_stream(FramedRead::new(file, BytesCodec::new()));
    Ok(file_stream)
}
/// Graceful shutdown signal
///
/// ## Errors
/// - `BotError::System` - error setting up signal handlers
pub async fn shutdown_signal() {
    let ctrl_c = async {
        signal::ctrl_c()
            .await
            .map_err(|e| BotError::System(format!("Failed to set up Ctrl+C handler: {e}")))
            .unwrap_or_else(|e| panic!("{}", e));
    };

    #[cfg(unix)]
    let terminate = async {
        signal::unix::signal(signal::unix::SignalKind::terminate())
            .map_err(|e| BotError::System(format!("Failed to set up signal handler: {e}")))
            .unwrap_or_else(|e| panic!("{}", e))
            .recv()
            .await;
    };

    #[cfg(not(unix))]
    let terminate = std::future::pending::<()>();

    tokio::select! {
        _ = ctrl_c => {},
        _ = terminate => {},
    }
}

// Include tests
#[cfg(test)]
mod tests {
    use super::*;
    use reqwest::StatusCode;
    use std::time::Duration;

    #[tokio::test]
    async fn test_connection_pool_new_and_default() {
        let client = reqwest::Client::new();
        let pool = ConnectionPool::new(client.clone(), 2, Duration::from_millis(100));
        assert_eq!(pool.retries, 2);
        assert_eq!(pool.max_backoff, Duration::from_millis(100));
        let _default = ConnectionPool::default();
    }

    #[tokio::test]
    async fn test_validate_response_success() {
        assert!(validate_response(&StatusCode::OK).is_ok());
    }

    #[tokio::test]
    async fn test_validate_response_client_error() {
        let err = validate_response(&StatusCode::BAD_REQUEST).unwrap_err();
        match err {
            BotError::Validation(msg) => assert!(msg.contains("HTTP error")),
            _ => panic!("Expected Validation error"),
        }
    }

    #[tokio::test]
    async fn test_validate_response_server_error() {
        let err = validate_response(&StatusCode::INTERNAL_SERVER_ERROR).unwrap_err();
        match err {
            BotError::System(msg) => assert!(msg.contains("Server error")),
            _ => panic!("Expected System error"),
        }
    }

    #[tokio::test]
    async fn test_validate_response_unexpected_status() {
        let status = StatusCode::SWITCHING_PROTOCOLS;
        let err = validate_response(&status).unwrap_err();
        match err {
            BotError::System(msg) => assert!(msg.contains("Unexpected HTTP status code")),
            _ => panic!("Expected System error"),
        }
    }

    #[tokio::test]
    async fn test_should_retry_timeout() {
        let err = reqwest::ClientBuilder::new()
            .timeout(Duration::from_millis(1))
            .build()
            .unwrap()
            .get("http://httpbin.org/delay/10")
            .send()
            .await
            .unwrap_err();

        // Should retry on timeout
        assert!(should_retry(&err));
    }

    #[tokio::test]
    async fn test_should_retry_server_error() {
        // Create a mock server error
        let client = reqwest::Client::new();
        let response = client.get("http://httpbin.org/status/500").send().await;

        if let Err(err) = response {
            assert!(should_retry(&err));
        }
    }

    #[tokio::test]
    async fn test_build_optimized_client() {
        let result = build_optimized_client();
        assert!(
            result.is_ok(),
            "Failed to build optimized client: {:?}",
            result.err()
        );

        let client = result.unwrap();
        // Verify client was created successfully
        assert!(client.get("https://example.com").build().is_ok());
    }

    #[tokio::test]
    async fn test_connection_pool_optimized() {
        let pool = ConnectionPool::optimized();
        assert!(pool.retries > 0);
        assert!(pool.max_backoff > Duration::from_millis(0));
    }

    #[tokio::test]
    async fn test_connection_pool_execute_with_retry_success() {
        let pool = ConnectionPool::new(reqwest::Client::new(), 2, Duration::from_millis(100));

        let counter = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let counter_clone = counter.clone();

        let result = pool
            .execute_with_retry(|| {
                let counter = counter_clone.clone();
                async move {
                    counter.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                    Ok::<String, BotError>("success".to_string())
                }
            })
            .await;

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "success");
        assert_eq!(counter.load(std::sync::atomic::Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn test_connection_pool_execute_with_retry_failure() {
        let pool = ConnectionPool::new(reqwest::Client::new(), 0, Duration::from_millis(10));

        let result = pool
            .execute_with_retry(|| async {
                Err::<String, BotError>(BotError::Network(
                    reqwest::ClientBuilder::new()
                        .build()
                        .unwrap()
                        .get("http://invalid-url-that-does-not-exist.invalid")
                        .send()
                        .await
                        .unwrap_err(),
                ))
            })
            .await;

        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_connection_pool_execute_with_retry_non_retryable_error() {
        let pool = ConnectionPool::new(reqwest::Client::new(), 2, Duration::from_millis(10));

        let result = pool
            .execute_with_retry(|| async {
                Err::<String, BotError>(BotError::Validation("Non-retryable error".to_string()))
            })
            .await;

        assert!(result.is_err());
        match result.unwrap_err() {
            BotError::Validation(msg) => assert_eq!(msg, "Non-retryable error"),
            _ => panic!("Expected Validation error"),
        }
    }

    #[tokio::test]
    async fn test_file_to_multipart_filepath() {
        use std::io::Write;
        use tempfile::NamedTempFile;

        // Create a temporary file
        let mut temp_file = NamedTempFile::new().unwrap();
        write!(temp_file, "test content").unwrap();
        let temp_path = temp_file.path().to_string_lossy().to_string();

        let multipart = MultipartName::FilePath(temp_path);
        let result = file_to_multipart(&multipart).await;

        assert!(
            result.is_ok(),
            "Failed to create multipart: {:?}",
            result.err()
        );
    }

    #[tokio::test]
    async fn test_file_to_multipart_file_content() {
        let multipart = MultipartName::FileContent {
            filename: "test.txt".to_string(),
            content: b"test content".to_vec(),
        };

        let result = file_to_multipart(&multipart).await;
        assert!(
            result.is_ok(),
            "Failed to create multipart from content: {:?}",
            result.err()
        );
    }

    #[tokio::test]
    async fn test_file_to_multipart_image_content() {
        let multipart = MultipartName::ImageContent {
            filename: "test.jpg".to_string(),
            content: b"fake image content".to_vec(),
        };

        let result = file_to_multipart(&multipart).await;
        assert!(
            result.is_ok(),
            "Failed to create multipart from image content: {:?}",
            result.err()
        );
    }

    #[tokio::test]
    async fn test_file_to_multipart_invalid() {
        let multipart = MultipartName::FilePath("/non/existent/file.txt".to_string());
        let result = file_to_multipart(&multipart).await;

        assert!(result.is_err());
        match result.unwrap_err() {
            BotError::Validation(msg) => assert!(msg.contains("File does not exist")),
            _ => panic!("Expected Validation error"),
        }
    }

    #[tokio::test]
    async fn test_file_to_multipart_path_traversal() {
        let multipart = MultipartName::FilePath("../../../etc/passwd".to_string());
        let result = file_to_multipart(&multipart).await;

        assert!(result.is_err());
        match result.unwrap_err() {
            BotError::Validation(msg) => assert!(msg.contains("parent directory references")),
            _ => panic!("Expected Validation error"),
        }
    }

    #[tokio::test]
    async fn test_file_to_multipart_empty_path() {
        let multipart = MultipartName::FilePath("".to_string());
        let result = file_to_multipart(&multipart).await;

        assert!(result.is_err());
        match result.unwrap_err() {
            BotError::Validation(msg) => assert_eq!(msg, "File path cannot be empty"),
            _ => panic!("Expected Validation error"),
        }
    }

    #[tokio::test]
    async fn test_file_to_multipart_invalid_filename() {
        let multipart = MultipartName::FileContent {
            filename: "file<name>.txt".to_string(), // Contains forbidden character
            content: b"test content".to_vec(),
        };

        let result = file_to_multipart(&multipart).await;
        assert!(result.is_err());
        match result.unwrap_err() {
            BotError::Validation(msg) => assert!(msg.contains("forbidden character")),
            _ => panic!("Expected Validation error"),
        }
    }

    #[tokio::test]
    async fn test_file_to_multipart_empty_content() {
        let multipart = MultipartName::FileContent {
            filename: "empty.txt".to_string(),
            content: Vec::new(), // Empty content
        };

        let result = file_to_multipart(&multipart).await;
        assert!(result.is_err());
        match result.unwrap_err() {
            BotError::Validation(msg) => assert_eq!(msg, "File content cannot be empty"),
            _ => panic!("Expected Validation error"),
        }
    }

    #[tokio::test]
    async fn test_validate_filename_reserved_names() {
        let reserved_names = ["CON", "PRN", "AUX", "NUL", "COM1", "LPT1"];

        for name in reserved_names.iter() {
            let result = validate_filename(name);
            assert!(result.is_err());
            match result.unwrap_err() {
                BotError::Validation(msg) => assert!(msg.contains("reserved name")),
                _ => panic!("Expected Validation error for {name}"),
            }
        }
    }

    #[tokio::test]
    async fn test_validate_filename_valid() {
        let valid_names = ["document.txt", "image.jpg", "data.json", "archive.zip"];

        for name in valid_names.iter() {
            let result = validate_filename(name);
            assert!(result.is_ok(), "Filename {name} should be valid");
        }
    }

    #[tokio::test]
    async fn test_make_stream_valid_file() {
        use std::io::Write;
        use tempfile::NamedTempFile;

        // Create a temporary file
        let mut temp_file = NamedTempFile::new().unwrap();
        write!(temp_file, "test stream content").unwrap();
        let temp_path = temp_file.path().to_string_lossy().to_string();

        let result = make_stream(&temp_path).await;
        assert!(
            result.is_ok(),
            "Failed to create stream: {:?}",
            result.err()
        );
    }

    #[tokio::test]
    async fn test_make_stream_invalid_file() {
        let invalid_path = "/path/that/does/not/exist/file.txt".to_string();
        let result = make_stream(&invalid_path).await;

        assert!(result.is_err());
        match result.unwrap_err() {
            BotError::Io(_) => {} // Expected IO error
            _ => panic!("Expected IO error"),
        }
    }

    #[tokio::test]
    async fn test_validate_response_all_success_codes() {
        let success_codes = [
            StatusCode::OK,
            StatusCode::CREATED,
            StatusCode::ACCEPTED,
            StatusCode::NO_CONTENT,
        ];

        for code in success_codes.iter() {
            assert!(
                validate_response(code).is_ok(),
                "Status code {code:?} should be valid"
            );
        }
    }

    #[tokio::test]
    async fn test_validate_response_all_client_error_codes() {
        let client_error_codes = [
            StatusCode::BAD_REQUEST,
            StatusCode::UNAUTHORIZED,
            StatusCode::FORBIDDEN,
            StatusCode::NOT_FOUND,
            StatusCode::METHOD_NOT_ALLOWED,
        ];

        for code in client_error_codes.iter() {
            let result = validate_response(code);
            assert!(result.is_err(), "Status code {code:?} should be error");
            match result.unwrap_err() {
                BotError::Validation(_) => {} // Expected
                _ => panic!("Expected Validation error for code {code:?}"),
            }
        }
    }

    #[tokio::test]
    async fn test_validate_response_all_server_error_codes() {
        let server_error_codes = [
            StatusCode::INTERNAL_SERVER_ERROR,
            StatusCode::NOT_IMPLEMENTED,
            StatusCode::BAD_GATEWAY,
            StatusCode::SERVICE_UNAVAILABLE,
            StatusCode::GATEWAY_TIMEOUT,
        ];

        for code in server_error_codes.iter() {
            let result = validate_response(code);
            assert!(result.is_err(), "Status code {code:?} should be error");
            match result.unwrap_err() {
                BotError::System(_) => {} // Expected
                _ => panic!("Expected System error for code {code:?}"),
            }
        }
    }

    #[tokio::test]
    async fn test_connection_pool_clone() {
        let pool1 = ConnectionPool::new(reqwest::Client::new(), 3, Duration::from_millis(200));
        let pool2 = pool1.clone();

        assert_eq!(pool1.retries, pool2.retries);
        assert_eq!(pool1.max_backoff, pool2.max_backoff);
    }

    #[test]
    fn test_connection_pool_debug() {
        let pool = ConnectionPool::new(reqwest::Client::new(), 2, Duration::from_millis(100));
        let debug_str = format!("{pool:?}");
        assert!(debug_str.contains("ConnectionPool"));
    }

    #[tokio::test]
    async fn test_deprecated_get_bytes_response() {
        // Test the deprecated function still works
        let client = reqwest::Client::new();
        let url = reqwest::Url::parse("https://httpbin.org/bytes/10").unwrap();

        let result = get_bytes_response(client, url).await;
        // This might fail in CI/testing environments, so we just check it doesn't panic
        if let Ok(bytes) = result {
            assert!(!bytes.is_empty());
        } // Network errors are acceptable in tests
    }

    #[tokio::test]
    async fn test_shutdown_signal_setup() {
        // Test that shutdown_signal can be set up without panicking
        // We can't easily test the actual signal handling in unit tests

        let signal_task = tokio::spawn(async {
            tokio::time::timeout(Duration::from_millis(100), shutdown_signal()).await
        });

        // Should timeout since no signal is sent
        let result = signal_task.await.unwrap();
        assert!(result.is_err()); // Should timeout
    }
}
/// Validate file path for security and correctness
///
/// ## Errors
/// - `BotError::Validation` - invalid file path
fn validate_file_path(path: &str) -> Result<()> {
    use std::path::Path;

    // Check if path is empty
    if path.is_empty() {
        return Err(BotError::Validation(
            "File path cannot be empty".to_string(),
        ));
    }

    // Check for null bytes (security vulnerability)
    if path.contains('\0') {
        return Err(BotError::Validation(
            "File path contains null bytes".to_string(),
        ));
    }

    // Normalize path and check for directory traversal attempts
    let path_obj = Path::new(path);

    // Check for dangerous path components
    for component in path_obj.components() {
        match component {
            std::path::Component::ParentDir => {
                return Err(BotError::Validation(
                    "File path contains parent directory references (..)".to_string(),
                ));
            }
            std::path::Component::CurDir => {
                return Err(BotError::Validation(
                    "File path contains current directory references (.)".to_string(),
                ));
            }
            _ => {}
        }
    }

    // Check if path is absolute or relative
    if path_obj.is_absolute() {
        // For absolute paths, ensure they exist and are readable
        if !path_obj.exists() {
            return Err(BotError::Validation(format!("File does not exist: {path}")));
        }

        if !path_obj.is_file() {
            return Err(BotError::Validation(format!("Path is not a file: {path}")));
        }
    }

    // Additional checks for maximum path length (varies by OS)
    #[cfg(target_os = "windows")]
    const MAX_PATH_LEN: usize = 260;
    #[cfg(not(target_os = "windows"))]
    const MAX_PATH_LEN: usize = 4096;

    if path.len() > MAX_PATH_LEN {
        return Err(BotError::Validation(format!(
            "File path too long: {} characters (max: {})",
            path.len(),
            MAX_PATH_LEN
        )));
    }

    Ok(())
}

/// Validate filename for security and correctness
///
/// ## Errors
/// - `BotError::Validation` - invalid filename
fn validate_filename(filename: &str) -> Result<()> {
    // Check if filename is empty
    if filename.is_empty() {
        return Err(BotError::Validation("Filename cannot be empty".to_string()));
    }

    // Check for null bytes
    if filename.contains('\0') {
        return Err(BotError::Validation(
            "Filename contains null bytes".to_string(),
        ));
    }

    // Check for dangerous characters
    const FORBIDDEN_CHARS: &[char] = &['/', '\\', ':', '*', '?', '"', '<', '>', '|'];
    for &forbidden_char in FORBIDDEN_CHARS {
        if filename.contains(forbidden_char) {
            return Err(BotError::Validation(format!(
                "Filename contains forbidden character: '{forbidden_char}'"
            )));
        }
    }

    // Check for reserved names on Windows
    const RESERVED_NAMES: &[&str] = &[
        "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8",
        "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
    ];

    let filename_upper = filename.to_uppercase();
    let name_without_ext = filename_upper.split('.').next().unwrap_or("");

    if RESERVED_NAMES.contains(&name_without_ext) {
        return Err(BotError::Validation(format!(
            "Filename uses reserved name: {filename}"
        )));
    }

    // Check filename length
    const MAX_FILENAME_LEN: usize = 255;
    if filename.len() > MAX_FILENAME_LEN {
        return Err(BotError::Validation(format!(
            "Filename too long: {} characters (max: {})",
            filename.len(),
            MAX_FILENAME_LEN
        )));
    }

    // Check for filenames starting or ending with dots or spaces
    if filename.starts_with('.') && filename != "." && filename != ".." {
        // Hidden files are generally OK, but we might want to warn
    }

    if filename.ends_with(' ') || filename.ends_with('.') {
        return Err(BotError::Validation(
            "Filename cannot end with space or dot".to_string(),
        ));
    }

    Ok(())
}
/// Retryable multipart form that can be recreated for retry attempts
#[derive(Debug, Clone)]
pub struct RetryableMultipartForm {
    file_data: Bytes,
    pub filename: String,
    field_name: String,
}

impl RetryableMultipartForm {
    /// Create new retryable form from file content
    pub fn from_content(filename: String, field_name: String, content: Vec<u8>) -> Self {
        Self {
            file_data: Bytes::from(content),
            filename,
            field_name,
        }
    }

    /// Create new retryable form from file path (loads content into memory)
    pub async fn from_file_path(path: String) -> Result<Self> {
        // Validate file path first
        validate_file_path_async(&path).await?;

        // Read file content into memory for retry capability
        let content = tokio::fs::read(&path).await.map_err(BotError::Io)?;

        let filename = std::path::Path::new(&path)
            .file_name()
            .and_then(|name| name.to_str())
            .unwrap_or(&path)
            .to_string();

        Ok(Self::from_content(filename.clone(), filename, content))
    }

    /// Convert to reqwest Form for sending
    pub fn to_form(&self) -> Form {
        let part = Part::bytes(self.file_data.clone().to_vec()).file_name(self.filename.clone());
        Form::new().part(self.field_name.clone(), part)
    }

    /// Get file size for logging/validation
    pub fn size(&self) -> usize {
        self.file_data.len()
    }
}

/// Validate file path asynchronously for security and correctness
///
/// ## Errors
/// - `BotError::Validation` - invalid file path
pub async fn validate_file_path_async(path: &str) -> Result<()> {
    // Check if path is empty
    if path.is_empty() {
        return Err(BotError::Validation(
            "File path cannot be empty".to_string(),
        ));
    }

    // Check for null bytes (security vulnerability)
    if path.contains('\0') {
        return Err(BotError::Validation(
            "File path contains null bytes".to_string(),
        ));
    }

    // Normalize path and check for directory traversal attempts
    let path_obj = std::path::Path::new(path);

    // Check for dangerous path components
    for component in path_obj.components() {
        match component {
            std::path::Component::ParentDir => {
                return Err(BotError::Validation(
                    "File path contains parent directory references (..)".to_string(),
                ));
            }
            std::path::Component::CurDir => {
                return Err(BotError::Validation(
                    "File path contains current directory references (.)".to_string(),
                ));
            }
            _ => {}
        }
    }

    // Check if path is absolute or relative - use async operations
    if path_obj.is_absolute() {
        // For absolute paths, ensure they exist and are readable using async operations
        let metadata = tokio::fs::metadata(path)
            .await
            .map_err(|e| BotError::Validation(format!("File does not exist: {path} ({e})")))?;

        if !metadata.is_file() {
            return Err(BotError::Validation(format!("Path is not a file: {path}")));
        }

        // Check if file is readable by attempting to get canonicalized path
        let _canonical = tokio::fs::canonicalize(path)
            .await
            .map_err(|e| BotError::Validation(format!("Cannot access file: {path} ({e})")))?;
    }

    // Additional checks for maximum path length (varies by OS)
    #[cfg(target_os = "windows")]
    const MAX_PATH_LEN: usize = 260;
    #[cfg(not(target_os = "windows"))]
    const MAX_PATH_LEN: usize = 4096;

    if path.len() > MAX_PATH_LEN {
        return Err(BotError::Validation(format!(
            "File path too long: {} characters (max: {})",
            path.len(),
            MAX_PATH_LEN
        )));
    }

    Ok(())
}