vkteams-bot-cli 0.7.6

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
//! Diagnostic commands module
//!
//! This module contains all commands related to diagnostics, testing, and system information.

use crate::commands::{Command, OutputFormat};
use crate::config::Config;
use crate::constants::ui::emoji;
use crate::errors::prelude::{CliError, Result as CliResult};
use crate::file_utils;
use crate::output::{CliResponse, OutputFormatter};
use crate::utils::output::print_success_result;
use crate::utils::{validate_directory_path, validate_file_id};
use async_trait::async_trait;
use clap::{Subcommand, ValueHint};
use colored::Colorize;
use serde_json::json;
use tracing::{debug, info};
use vkteams_bot::prelude::*;

/// All diagnostic-related commands
#[derive(Subcommand, Debug, Clone)]
pub enum DiagnosticCommands {
    /// Get bot information and status
    GetSelf {
        /// Show detailed bot information
        #[arg(short, long)]
        detailed: bool,
    },
    /// Get events once or listen with optional flag
    GetEvents {
        #[arg(short, long, required = false, value_name = "LISTEN")]
        listen: Option<bool>,
    },
    /// Download file with given ID into specified path
    GetFile {
        #[arg(short = 'f', long, required = true, value_name = "FILE_ID")]
        file_id: String,
        #[arg(short = 'p', long, required = false, value_name = "FILE_PATH", value_hint = ValueHint::DirPath)]
        file_path: String,
    },
    /// Perform comprehensive health check
    HealthCheck,
    /// Test network connectivity to API endpoints
    NetworkTest,
    /// Show system and environment information
    SystemInfo,
    /// Test API rate limits
    RateLimitTest {
        /// Number of requests to send
        #[arg(short = 'n', long, default_value = "10")]
        requests: u32,
        /// Delay between requests in milliseconds
        #[arg(short = 'd', long, default_value = "100")]
        delay_ms: u64,
    },
}

#[async_trait]
impl Command for DiagnosticCommands {
    async fn execute(&self, bot: &Bot) -> CliResult<()> {
        match self {
            DiagnosticCommands::GetSelf { detailed } => execute_get_self(bot, *detailed).await,
            DiagnosticCommands::GetEvents { listen } => {
                execute_get_events(bot, listen.unwrap_or(false)).await
            }
            DiagnosticCommands::GetFile { file_id, file_path } => {
                execute_get_file(bot, file_id, file_path).await
            }
            DiagnosticCommands::HealthCheck => execute_health_check(bot).await,
            DiagnosticCommands::NetworkTest => execute_network_test(bot).await,
            DiagnosticCommands::SystemInfo => execute_system_info().await,
            DiagnosticCommands::RateLimitTest { requests, delay_ms } => {
                execute_rate_limit_test(bot, *requests, *delay_ms).await
            }
        }
    }

    fn name(&self) -> &'static str {
        match self {
            DiagnosticCommands::GetSelf { .. } => "get-self",
            DiagnosticCommands::GetEvents { .. } => "get-events",
            DiagnosticCommands::GetFile { .. } => "get-file",
            DiagnosticCommands::HealthCheck => "health-check",
            DiagnosticCommands::NetworkTest => "network-test",
            DiagnosticCommands::SystemInfo => "system-info",
            DiagnosticCommands::RateLimitTest { .. } => "rate-limit-test",
        }
    }

    fn validate(&self) -> CliResult<()> {
        match self {
            DiagnosticCommands::GetFile { file_id, file_path } => {
                validate_file_id(file_id)?;
                if !file_path.is_empty() {
                    validate_directory_path(file_path)?;
                }
            }
            DiagnosticCommands::RateLimitTest {
                requests,
                delay_ms: _,
            } => {
                if *requests == 0 || *requests > 1000 {
                    return Err(CliError::InputError(
                        "Number of requests must be between 1 and 1000".to_string(),
                    ));
                }
            }
            _ => {} // Other commands don't need validation
        }
        Ok(())
    }

    /// New method for structured output support
    async fn execute_with_output(&self, bot: &Bot, output_format: &OutputFormat) -> CliResult<()> {
        let response = match self {
            DiagnosticCommands::GetSelf { detailed } => {
                execute_get_self_structured(bot, *detailed).await
            }
            DiagnosticCommands::GetEvents { listen } => {
                execute_get_events_structured(bot, listen.unwrap_or(false)).await
            }
            DiagnosticCommands::GetFile { file_id, file_path } => {
                execute_get_file_structured(bot, file_id, file_path).await
            }
            DiagnosticCommands::HealthCheck => execute_health_check_structured(bot).await,
            DiagnosticCommands::NetworkTest => execute_network_test_structured(bot).await,
            DiagnosticCommands::SystemInfo => execute_system_info_structured().await,
            DiagnosticCommands::RateLimitTest { requests, delay_ms } => {
                execute_rate_limit_test_structured(bot, *requests, *delay_ms).await
            }
        };

        OutputFormatter::print(&response, output_format)?;

        if !response.success {
            return Err(CliError::UnexpectedError("Command failed".to_string()));
        }

        Ok(())
    }
}

// Command execution functions

async fn execute_get_self(bot: &Bot, detailed: bool) -> CliResult<()> {
    debug!("Getting bot information");

    let request = RequestSelfGet::new(());
    let result = bot
        .send_api_request(request)
        .await
        .map_err(CliError::ApiError)?;

    if detailed {
        info!("Bot information retrieved successfully");
        print_success_result(&result, &OutputFormat::Pretty)?;
    } else {
        // Show simplified bot info
        println!("{} Bot is configured and accessible", emoji::CHECK);
        if let Ok(json_str) = serde_json::to_string_pretty(&result) {
            println!("{}", json_str.green());
        }
    }

    Ok(())
}

async fn execute_get_events(bot: &Bot, listen: bool) -> CliResult<()> {
    debug!("Getting events, listen mode: {}", listen);

    if listen {
        info!("Starting event listener (long polling)...");
        println!(
            "{} Starting event listener. Press Ctrl+C to stop.",
            emoji::ROCKET
        );

        match bot.event_listener(handle_event).await {
            Ok(()) => (),
            Err(e) => return Err(CliError::ApiError(e)),
        }
    } else {
        let result = bot
            .send_api_request(RequestEventsGet::new(bot.get_last_event_id()).with_poll_time(30))
            .await
            .map_err(CliError::ApiError)?;

        info!("Successfully retrieved events");
        print_success_result(&result, &OutputFormat::Pretty)?;
    }

    Ok(())
}

async fn execute_get_file(bot: &Bot, file_id: &str, file_path: &str) -> CliResult<()> {
    debug!("Downloading file {} to {}", file_id, file_path);

    let downloaded_path = file_utils::download_and_save_file(bot, file_id, file_path).await?;

    info!("Successfully downloaded file with ID: {}", file_id);
    println!(
        "{} File downloaded to: {}",
        emoji::CHECK,
        downloaded_path.display().to_string().green()
    );

    Ok(())
}

async fn execute_health_check(bot: &Bot) -> CliResult<()> {
    println!(
        "{} Performing comprehensive health check...",
        emoji::TEST_TUBE.bold().blue()
    );
    println!();

    let mut all_passed = true;

    // Test 1: Basic connectivity
    print!("{} Testing basic API connectivity... ", emoji::GEAR);
    match bot.send_api_request(RequestSelfGet::new(())).await {
        Ok(_) => println!("{}", "PASS".green()),
        Err(e) => {
            println!("{} - {}", "FAIL".red(), e);
            all_passed = false;
        }
    }

    // Test 2: Configuration check
    print!("{} Checking configuration... ", emoji::GEAR);
    match Config::from_file() {
        Ok(config) => {
            if config.api.token.is_some() && config.api.url.is_some() {
                println!("{}", "PASS".green());
            } else {
                println!("{} - Missing required configuration", "FAIL".red());
                all_passed = false;
            }
        }
        Err(_) => {
            println!("{} - Configuration file not found", "FAIL".red());
            all_passed = false;
        }
    }

    // Test 3: Network latency
    print!("{} Testing network latency... ", emoji::GEAR);
    let start = std::time::Instant::now();
    match bot.send_api_request(RequestSelfGet::new(())).await {
        Ok(_) => {
            let latency = start.elapsed();
            if latency.as_millis() < 1000 {
                println!("{} - {}ms", "PASS".green(), latency.as_millis());
            } else {
                println!(
                    "{} - High latency: {}ms",
                    "WARN".yellow(),
                    latency.as_millis()
                );
            }
        }
        Err(e) => {
            println!("{} - {}", "FAIL".red(), e);
            all_passed = false;
        }
    }

    println!();
    if all_passed {
        println!("{} All health checks passed!", emoji::CHECK.bold().green());
    } else {
        println!(
            "{} Some health checks failed. Check configuration and network connectivity.",
            emoji::WARNING.bold().yellow()
        );
    }

    Ok(())
}

async fn execute_network_test(bot: &Bot) -> CliResult<()> {
    println!(
        "{} Testing network connectivity...",
        emoji::GEAR.bold().blue()
    );
    println!();

    // Test multiple endpoints with timing
    let endpoints = vec![("Bot Info", RequestSelfGet::new(()))];

    for (name, request) in endpoints {
        print!("Testing {name}: ");
        let start = std::time::Instant::now();

        match bot.send_api_request(request).await {
            Ok(_) => {
                let duration = start.elapsed();
                println!("{} ({}ms)", "OK".green(), duration.as_millis());
            }
            Err(e) => {
                println!("{} - {}", "FAILED".red(), e);
            }
        }
    }

    println!();
    println!("{} Network test completed", emoji::CHECK);

    Ok(())
}

async fn execute_system_info() -> CliResult<()> {
    println!("{} System Information", emoji::INFO.bold().blue());
    println!();

    // Runtime information
    println!("{}", "Runtime:".bold().green());
    println!("  OS: {}", std::env::consts::OS);
    println!("  Architecture: {}", std::env::consts::ARCH);
    println!("  Family: {}", std::env::consts::FAMILY);

    // Current directory
    if let Ok(current_dir) = std::env::current_dir() {
        println!("  Current directory: {}", current_dir.display());
    }

    // Environment variables
    println!("\n{}", "Environment:".bold().green());
    let env_vars = [
        "VKTEAMS_BOT_API_TOKEN",
        "VKTEAMS_BOT_API_URL",
        "VKTEAMS_PROXY",
        "VKTEAMS_LOG_LEVEL",
    ];

    for var in &env_vars {
        match std::env::var(var) {
            Ok(value) => {
                if var.contains("TOKEN") {
                    println!("  {}: {}***", var, &value[..8.min(value.len())]);
                } else {
                    println!("  {var}: {value}");
                }
            }
            Err(_) => println!("  {}: {}", var, "Not set".dimmed()),
        }
    }

    // Configuration file status
    println!("\n{}", "Configuration:".bold().green());
    match Config::from_file() {
        Ok(_) => println!("  Configuration file: {}", "Found".green()),
        Err(_) => println!("  Configuration file: {}", "Not found".red()),
    }

    Ok(())
}

async fn execute_rate_limit_test(bot: &Bot, requests: u32, delay_ms: u64) -> CliResult<()> {
    println!(
        "{} Testing rate limits with {} requests ({}ms delay)...",
        emoji::ROCKET.bold().blue(),
        requests,
        delay_ms
    );
    println!();

    let mut successful = 0;
    let mut failed = 0;
    let start_time = std::time::Instant::now();

    for i in 1..=requests {
        let request_start = std::time::Instant::now();

        match bot.send_api_request(RequestSelfGet::new(())).await {
            Ok(_) => {
                successful += 1;
                let duration = request_start.elapsed();
                println!(
                    "Request {}/{}: {} ({}ms)",
                    i,
                    requests,
                    "OK".green(),
                    duration.as_millis()
                );
            }
            Err(e) => {
                failed += 1;
                println!("Request {}/{}: {} - {}", i, requests, "FAILED".red(), e);
            }
        }

        if i < requests {
            tokio::time::sleep(tokio::time::Duration::from_millis(delay_ms)).await;
        }
    }

    let total_time = start_time.elapsed();

    println!();
    println!("{}", "Rate Limit Test Results:".bold().green());
    println!("  Total requests: {requests}");
    println!("  Successful: {}", successful.to_string().green());
    println!("  Failed: {}", failed.to_string().red());
    println!(
        "  Success rate: {:.1}%",
        (successful as f64 / requests as f64) * 100.0
    );
    println!("  Total time: {:.2}s", total_time.as_secs_f64());
    println!(
        "  Average rate: {:.1} req/s",
        requests as f64 / total_time.as_secs_f64()
    );

    Ok(())
}

// Event handler for long polling
async fn handle_event<T>(
    bot: Bot,
    result: T,
) -> std::result::Result<(), vkteams_bot::error::BotError>
where
    T: serde::Serialize + std::fmt::Debug,
{
    debug!("Last event id: {:?}", bot.get_last_event_id());

    if let Ok(json_str) = serde_json::to_string_pretty(&result) {
        println!("{}", json_str.green());
    } else {
        println!("Event: {result:?}");
    }

    Ok(())
}

// Validation functions are now imported from utils/validation module

// Structured output versions

async fn execute_get_self_structured(bot: &Bot, detailed: bool) -> CliResponse<serde_json::Value> {
    debug!("Getting bot information (structured)");

    let request = RequestSelfGet::new(());
    match bot.send_api_request(request).await {
        Ok(result) => {
            info!("Bot information retrieved successfully");
            let data = if detailed {
                serde_json::to_value(&result).unwrap_or(json!({}))
            } else {
                json!({
                    "bot_id": result.user_id,
                    "nickname": result.nick,
                    "first_name": result.first_name,
                    "about": result.about,
                    "photo": result.photo
                })
            };
            CliResponse::success("get-self", data)
        }
        Err(e) => CliResponse::error("get-self", format!("Failed to get bot info: {e}")),
    }
}

async fn execute_get_events_structured(bot: &Bot, listen: bool) -> CliResponse<serde_json::Value> {
    debug!("Getting events, listen mode: {}", listen);

    if listen {
        // For listen mode, we can't return structured data easily
        // Return a message indicating the mode
        CliResponse::success(
            "get-events",
            json!({
                "mode": "listen",
                "message": "Event listener started. Press Ctrl+C to stop.",
                "note": "Use regular execute mode for event listening"
            }),
        )
    } else {
        match bot
            .send_api_request(RequestEventsGet::new(bot.get_last_event_id()).with_poll_time(30))
            .await
        {
            Ok(result) => {
                info!("Successfully retrieved events");
                let data = serde_json::to_value(&result).unwrap_or(json!({}));
                CliResponse::success("get-events", data)
            }
            Err(e) => CliResponse::error("get-events", format!("Failed to get events: {e}")),
        }
    }
}

async fn execute_get_file_structured(
    bot: &Bot,
    file_id: &str,
    file_path: &str,
) -> CliResponse<serde_json::Value> {
    debug!("Downloading file {} to {}", file_id, file_path);

    match file_utils::download_and_save_file(bot, file_id, file_path).await {
        Ok(downloaded_path) => {
            info!("Successfully downloaded file with ID: {}", file_id);
            let data = json!({
                "file_id": file_id,
                "download_path": downloaded_path.display().to_string(),
                "status": "downloaded"
            });
            CliResponse::success("get-file", data)
        }
        Err(e) => CliResponse::error("get-file", format!("Failed to download file: {e}")),
    }
}

async fn execute_health_check_structured(bot: &Bot) -> CliResponse<serde_json::Value> {
    debug!("Performing health check");

    let mut tests = Vec::new();
    let mut all_passed = true;

    // Test 1: Basic connectivity
    let start = std::time::Instant::now();
    let connectivity_result = match bot.send_api_request(RequestSelfGet::new(())).await {
        Ok(_) => {
            json!({
                "name": "API Connectivity",
                "status": "pass",
                "latency_ms": start.elapsed().as_millis()
            })
        }
        Err(e) => {
            all_passed = false;
            json!({
                "name": "API Connectivity",
                "status": "fail",
                "error": e.to_string()
            })
        }
    };
    tests.push(connectivity_result);

    // Test 2: Configuration check
    let config_result = match Config::from_file() {
        Ok(config) => {
            if config.api.token.is_some() && config.api.url.is_some() {
                json!({
                    "name": "Configuration",
                    "status": "pass",
                    "details": "All required fields present"
                })
            } else {
                all_passed = false;
                json!({
                    "name": "Configuration",
                    "status": "fail",
                    "error": "Missing required configuration"
                })
            }
        }
        Err(_) => {
            all_passed = false;
            json!({
                "name": "Configuration",
                "status": "fail",
                "error": "Configuration file not found"
            })
        }
    };
    tests.push(config_result);

    // Test 3: Network latency
    let latency_start = std::time::Instant::now();
    let latency_result = match bot.send_api_request(RequestSelfGet::new(())).await {
        Ok(_) => {
            let latency = latency_start.elapsed();
            let status = if latency.as_millis() < 1000 {
                "pass"
            } else {
                "warn"
            };
            json!({
                "name": "Network Latency",
                "status": status,
                "latency_ms": latency.as_millis()
            })
        }
        Err(e) => {
            all_passed = false;
            json!({
                "name": "Network Latency",
                "status": "fail",
                "error": e.to_string()
            })
        }
    };
    tests.push(latency_result);

    let data = json!({
        "overall_status": if all_passed { "healthy" } else { "unhealthy" },
        "tests": tests,
        "timestamp": chrono::Utc::now().to_rfc3339()
    });

    CliResponse::success("health-check", data)
}

async fn execute_network_test_structured(bot: &Bot) -> CliResponse<serde_json::Value> {
    debug!("Testing network connectivity");

    let mut results = Vec::new();

    // Test multiple endpoints
    let endpoints = vec![("Bot Info", RequestSelfGet::new(()))];

    for (name, request) in endpoints {
        let start = std::time::Instant::now();

        let result = match bot.send_api_request(request).await {
            Ok(_) => {
                json!({
                    "endpoint": name,
                    "status": "ok",
                    "latency_ms": start.elapsed().as_millis()
                })
            }
            Err(e) => {
                json!({
                    "endpoint": name,
                    "status": "failed",
                    "error": e.to_string()
                })
            }
        };
        results.push(result);
    }

    let data = json!({
        "test_results": results,
        "timestamp": chrono::Utc::now().to_rfc3339()
    });

    CliResponse::success("network-test", data)
}

async fn execute_system_info_structured() -> CliResponse<serde_json::Value> {
    debug!("Gathering system information");

    let mut env_vars = serde_json::Map::new();
    let vars = [
        "VKTEAMS_BOT_API_TOKEN",
        "VKTEAMS_BOT_API_URL",
        "VKTEAMS_PROXY",
        "VKTEAMS_LOG_LEVEL",
    ];

    for var in &vars {
        match std::env::var(var) {
            Ok(value) => {
                if var.contains("TOKEN") {
                    env_vars.insert(
                        var.to_string(),
                        json!(format!("{}***", &value[..8.min(value.len())])),
                    );
                } else {
                    env_vars.insert(var.to_string(), json!(value));
                }
            }
            Err(_) => {
                env_vars.insert(var.to_string(), json!("Not set"));
            }
        }
    }

    let config_status = match Config::from_file() {
        Ok(_) => "found",
        Err(_) => "not_found",
    };

    let data = json!({
        "runtime": {
            "os": std::env::consts::OS,
            "architecture": std::env::consts::ARCH,
            "family": std::env::consts::FAMILY,
            "current_directory": std::env::current_dir().ok().map(|p| p.display().to_string())
        },
        "environment": env_vars,
        "configuration": {
            "status": config_status
        }
    });

    CliResponse::success("system-info", data)
}

async fn execute_rate_limit_test_structured(
    bot: &Bot,
    requests: u32,
    delay_ms: u64,
) -> CliResponse<serde_json::Value> {
    debug!("Testing rate limits with {} requests", requests);

    let mut successful = 0;
    let mut failed = 0;
    let mut request_results = Vec::new();
    let start_time = std::time::Instant::now();

    for i in 1..=requests {
        let request_start = std::time::Instant::now();

        let result = match bot.send_api_request(RequestSelfGet::new(())).await {
            Ok(_) => {
                successful += 1;
                json!({
                    "request_number": i,
                    "status": "success",
                    "latency_ms": request_start.elapsed().as_millis()
                })
            }
            Err(e) => {
                failed += 1;
                json!({
                    "request_number": i,
                    "status": "failed",
                    "error": e.to_string()
                })
            }
        };
        request_results.push(result);

        if i < requests {
            tokio::time::sleep(tokio::time::Duration::from_millis(delay_ms)).await;
        }
    }

    let total_time = start_time.elapsed();
    let success_rate = (successful as f64 / requests as f64) * 100.0;
    let average_rate = requests as f64 / total_time.as_secs_f64();

    let data = json!({
        "summary": {
            "total_requests": requests,
            "successful": successful,
            "failed": failed,
            "success_rate_percent": success_rate,
            "total_time_seconds": total_time.as_secs_f64(),
            "average_rate_per_second": average_rate,
            "delay_between_requests_ms": delay_ms
        },
        "request_details": request_results
    });

    CliResponse::success("rate-limit-test", data)
}

// Utility functions

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

    fn dummy_bot() -> Bot {
        Bot::with_params(&APIVersionUrl::V1, "dummy_token", "https://dummy.api.com").unwrap()
    }

    #[test]
    fn test_validate_get_file_empty_file_id() {
        let cmd = DiagnosticCommands::GetFile {
            file_id: "".to_string(),
            file_path: "/tmp".to_string(),
        };
        let res = cmd.validate();
        assert!(res.is_err());
    }

    #[test]
    fn test_validate_get_file_invalid_path() {
        let cmd = DiagnosticCommands::GetFile {
            file_id: "fileid".to_string(),
            file_path: "".to_string(),
        };
        let res = cmd.validate();
        assert!(res.is_ok()); // пустой путь допустим
    }

    #[test]
    fn test_validate_rate_limit_zero() {
        let cmd = DiagnosticCommands::RateLimitTest {
            requests: 0,
            delay_ms: 100,
        };
        let res = cmd.validate();
        assert!(res.is_err());
    }

    #[test]
    fn test_validate_rate_limit_too_many() {
        let cmd = DiagnosticCommands::RateLimitTest {
            requests: 1001,
            delay_ms: 100,
        };
        let res = cmd.validate();
        assert!(res.is_err());
    }

    #[test]
    fn test_validate_rate_limit_valid() {
        let cmd = DiagnosticCommands::RateLimitTest {
            requests: 10,
            delay_ms: 100,
        };
        let res = cmd.validate();
        assert!(res.is_ok());
    }

    #[test]
    fn test_execute_get_self_api_error() {
        let cmd = DiagnosticCommands::GetSelf { detailed: true };
        let bot = dummy_bot();
        let rt = Runtime::new().unwrap();
        let res = rt.block_on(cmd.execute(&bot));
        assert!(res.is_err());
    }

    #[test]
    fn test_execute_get_events_api_error() {
        let cmd = DiagnosticCommands::GetEvents {
            listen: Some(false),
        };
        let bot = dummy_bot();
        let rt = Runtime::new().unwrap();
        let res = rt.block_on(cmd.execute(&bot));
        assert!(res.is_err());
    }

    #[test]
    fn test_execute_get_file_api_error() {
        let cmd = DiagnosticCommands::GetFile {
            file_id: "fileid".to_string(),
            file_path: "/tmp".to_string(),
        };
        let bot = dummy_bot();
        let rt = Runtime::new().unwrap();
        let res = rt.block_on(cmd.execute(&bot));
        assert!(res.is_err());
    }

    #[test]
    fn test_execute_health_check_api_error() {
        let cmd = DiagnosticCommands::HealthCheck;
        let bot = dummy_bot();
        let rt = Runtime::new().unwrap();
        let res = rt.block_on(cmd.execute(&bot));
        assert!(res.is_ok());
    }

    #[test]
    fn test_execute_network_test_api_error() {
        let cmd = DiagnosticCommands::NetworkTest;
        let bot = dummy_bot();
        let rt = Runtime::new().unwrap();
        let res = rt.block_on(cmd.execute(&bot));
        assert!(res.is_ok());
    }

    #[test]
    fn test_execute_system_info() {
        let cmd = DiagnosticCommands::SystemInfo;
        let bot = dummy_bot();
        let rt = Runtime::new().unwrap();
        // SystemInfo не требует bot, но для совместимости передаём
        let res = rt.block_on(cmd.execute(&bot));
        assert!(res.is_ok());
    }

    #[test]
    fn test_execute_rate_limit_test_api_error() {
        let cmd = DiagnosticCommands::RateLimitTest {
            requests: 2,
            delay_ms: 10,
        };
        let bot = dummy_bot();
        let rt = Runtime::new().unwrap();
        let res = rt.block_on(cmd.execute(&bot));
        assert!(res.is_ok());
    }

    #[test]
    fn test_diagnostic_commands_variants() {
        let get_self = DiagnosticCommands::GetSelf { detailed: true };
        assert_eq!(get_self.name(), "get-self");
        if let DiagnosticCommands::GetSelf { detailed } = get_self {
            assert!(detailed);
        }

        let get_events = DiagnosticCommands::GetEvents { listen: Some(true) };
        assert_eq!(get_events.name(), "get-events");
        if let DiagnosticCommands::GetEvents { listen } = get_events {
            assert_eq!(listen, Some(true));
        }

        let get_file = DiagnosticCommands::GetFile {
            file_id: "file123".to_string(),
            file_path: "/tmp".to_string(),
        };
        assert_eq!(get_file.name(), "get-file");
        if let DiagnosticCommands::GetFile { file_id, file_path } = get_file {
            assert_eq!(file_id, "file123");
            assert_eq!(file_path, "/tmp");
        }

        let health = DiagnosticCommands::HealthCheck;
        assert_eq!(health.name(), "health-check");

        let net = DiagnosticCommands::NetworkTest;
        assert_eq!(net.name(), "network-test");

        let sys = DiagnosticCommands::SystemInfo;
        assert_eq!(sys.name(), "system-info");

        let rate = DiagnosticCommands::RateLimitTest {
            requests: 10,
            delay_ms: 100,
        };
        assert_eq!(rate.name(), "rate-limit-test");
        if let DiagnosticCommands::RateLimitTest { requests, delay_ms } = rate {
            assert_eq!(requests, 10);
            assert_eq!(delay_ms, 100);
        }
    }
}