rch-common 1.0.26

Shared types and utilities for Remote Compilation Helper
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
//! Test worker configuration loading for true E2E tests.
//!
//! This module handles loading and validating worker configurations
//! specifically for end-to-end testing scenarios that require real
//! SSH-accessible worker machines.
//!
//! # Configuration File
//!
//! Tests use the configuration in `tests/true_e2e/workers_test.toml`.
//! Override with the `RCH_E2E_WORKERS_CONFIG` environment variable.
//!
//! # Example Configuration
//!
//! ```toml
//! [settings]
//! default_timeout_secs = 300
//! ssh_connection_timeout_secs = 10
//! remote_work_dir = "/tmp/rch_test"
//!
//! [[workers]]
//! id = "test-worker"
//! host = "192.168.1.100"
//! user = "ubuntu"
//! identity_file = "~/.ssh/id_rsa"
//! total_slots = 16
//! enabled = true
//! ```

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::net::{TcpStream, ToSocketAddrs};
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};

use super::logging::{LogLevel, LogSource, TestLogger};

/// Environment variable to override config file location.
pub const ENV_WORKERS_CONFIG: &str = "RCH_E2E_WORKERS_CONFIG";

/// Environment variable to skip worker availability checks.
pub const ENV_SKIP_WORKER_CHECK: &str = "RCH_E2E_SKIP_WORKER_CHECK";

/// Environment variable to skip all true E2E tests.
pub const ENV_SKIP_ALL_TESTS: &str = "RCH_E2E_SKIP";

/// Environment variable to override command timeout.
pub const ENV_TIMEOUT_SECS: &str = "RCH_E2E_TIMEOUT_SECS";

/// Default config file path relative to workspace root.
pub const DEFAULT_CONFIG_PATH: &str = "tests/true_e2e/workers_test.toml";

/// Error type for configuration operations.
#[derive(Debug, thiserror::Error)]
pub enum TestConfigError {
    #[error("Configuration file not found: {0}")]
    NotFound(PathBuf),

    #[error("Failed to read configuration file: {0}")]
    ReadError(#[from] std::io::Error),

    #[error("Failed to parse configuration: {0}")]
    ParseError(#[from] toml::de::Error),

    #[error("Invalid configuration: {0}")]
    ValidationError(String),

    #[error("No workers configured")]
    NoWorkersConfigured,

    #[error("Path expansion failed for: {0}")]
    PathExpansionFailed(String),
}

/// Result type for configuration operations.
pub type TestConfigResult<T> = Result<T, TestConfigError>;

/// Test-specific settings for E2E tests.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestSettings {
    /// Default timeout for test commands in seconds.
    #[serde(default = "default_timeout_secs")]
    pub default_timeout_secs: u64,

    /// SSH connection timeout in seconds.
    #[serde(default = "default_ssh_timeout")]
    pub ssh_connection_timeout_secs: u64,

    /// Remote working directory for test artifacts.
    #[serde(default = "default_remote_work_dir")]
    pub remote_work_dir: String,

    /// Rsync compression method.
    #[serde(default = "default_rsync_compression")]
    pub rsync_compression: String,

    /// Whether to clean up remote artifacts after tests.
    #[serde(default = "default_cleanup_after_test")]
    pub cleanup_after_test: bool,

    /// Minimum required Rust version on workers.
    #[serde(default)]
    pub min_rust_version: Option<String>,
}

impl Default for TestSettings {
    fn default() -> Self {
        Self {
            default_timeout_secs: default_timeout_secs(),
            ssh_connection_timeout_secs: default_ssh_timeout(),
            remote_work_dir: default_remote_work_dir(),
            rsync_compression: default_rsync_compression(),
            cleanup_after_test: default_cleanup_after_test(),
            min_rust_version: None,
        }
    }
}

fn default_timeout_secs() -> u64 {
    300
}

fn default_ssh_timeout() -> u64 {
    10
}

fn default_remote_work_dir() -> String {
    "/tmp/rch_test".to_string()
}

fn default_rsync_compression() -> String {
    "zstd".to_string()
}

fn default_cleanup_after_test() -> bool {
    true
}

/// Single worker entry in test configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestWorkerEntry {
    /// Unique identifier for this worker.
    pub id: String,

    /// SSH hostname or IP address.
    pub host: String,

    /// SSH username.
    #[serde(default = "default_user")]
    pub user: String,

    /// SSH port.
    #[serde(default = "default_port")]
    pub port: u16,

    /// Path to SSH private key.
    #[serde(default = "default_identity_file")]
    pub identity_file: String,

    /// Total CPU slots available on this worker.
    #[serde(default = "default_slots")]
    pub total_slots: u32,

    /// Priority for worker selection (higher = preferred).
    #[serde(default = "default_priority")]
    pub priority: u32,

    /// Optional tags for filtering.
    #[serde(default)]
    pub tags: Vec<String>,

    /// Whether this worker is enabled.
    #[serde(default = "default_enabled")]
    pub enabled: bool,
}

fn default_user() -> String {
    "ubuntu".to_string()
}

fn default_port() -> u16 {
    22
}

fn default_identity_file() -> String {
    "~/.ssh/id_rsa".to_string()
}

fn default_slots() -> u32 {
    8
}

fn default_priority() -> u32 {
    100
}

fn default_enabled() -> bool {
    true
}

impl TestWorkerEntry {
    /// Expand tilde in the identity file path.
    pub fn expanded_identity_file(&self) -> TestConfigResult<PathBuf> {
        expand_tilde_path(&self.identity_file)
    }

    /// Convert to the standard WorkerConfig type for daemon integration.
    pub fn to_worker_config(&self) -> crate::WorkerConfig {
        crate::WorkerConfig {
            id: crate::WorkerId::new(&self.id),
            host: self.host.clone(),
            user: self.user.clone(),
            identity_file: self.identity_file.clone(),
            total_slots: self.total_slots,
            priority: self.priority,
            tags: self.tags.clone(),
        }
    }
}

/// Complete test worker configuration.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TestWorkersConfig {
    /// Test-specific settings.
    #[serde(default)]
    pub settings: TestSettings,

    /// List of worker definitions.
    #[serde(default)]
    pub workers: Vec<TestWorkerEntry>,
}

impl TestWorkersConfig {
    /// Load configuration from the default path or environment override.
    pub fn load() -> TestConfigResult<Self> {
        let path = get_config_path();
        Self::load_from(&path)
    }

    /// Load configuration from a specific path.
    pub fn load_from(path: &Path) -> TestConfigResult<Self> {
        if !path.exists() {
            return Err(TestConfigError::NotFound(path.to_path_buf()));
        }

        let contents = std::fs::read_to_string(path)?;
        let config: TestWorkersConfig = toml::from_str(&contents)?;

        config.validate()?;
        Ok(config)
    }

    /// Try to load configuration, returning None if not found.
    pub fn try_load() -> Option<Self> {
        Self::load().ok()
    }

    /// Validate the configuration.
    pub fn validate(&self) -> TestConfigResult<()> {
        // Check for duplicate worker IDs
        let mut seen_ids: HashMap<&str, usize> = HashMap::new();
        for (i, worker) in self.workers.iter().enumerate() {
            if let Some(prev_idx) = seen_ids.insert(&worker.id, i) {
                return Err(TestConfigError::ValidationError(format!(
                    "Duplicate worker ID '{}' at indices {} and {}",
                    worker.id, prev_idx, i
                )));
            }

            // Validate individual worker entries
            if worker.host.is_empty() {
                return Err(TestConfigError::ValidationError(format!(
                    "Worker '{}' has empty hostname",
                    worker.id
                )));
            }

            if worker.user.is_empty() {
                return Err(TestConfigError::ValidationError(format!(
                    "Worker '{}' has empty username",
                    worker.id
                )));
            }
        }

        Ok(())
    }

    /// Get only enabled workers.
    pub fn enabled_workers(&self) -> Vec<&TestWorkerEntry> {
        self.workers.iter().filter(|w| w.enabled).collect()
    }

    /// Check if any workers are configured.
    pub fn has_workers(&self) -> bool {
        !self.workers.is_empty()
    }

    /// Check if any workers are enabled.
    pub fn has_enabled_workers(&self) -> bool {
        self.workers.iter().any(|w| w.enabled)
    }

    /// Get the effective timeout, considering environment override.
    pub fn effective_timeout_secs(&self) -> u64 {
        std::env::var(ENV_TIMEOUT_SECS)
            .ok()
            .and_then(|v| v.parse().ok())
            .unwrap_or(self.settings.default_timeout_secs)
    }

    /// Convert all enabled workers to standard WorkerConfig for daemon integration.
    pub fn to_worker_configs(&self) -> Vec<crate::WorkerConfig> {
        self.enabled_workers()
            .iter()
            .map(|w| w.to_worker_config())
            .collect()
    }
}

/// Get the configuration file path, considering environment override.
pub fn get_config_path() -> PathBuf {
    if let Ok(override_path) = std::env::var(ENV_WORKERS_CONFIG) {
        return PathBuf::from(override_path);
    }

    // Try to find the config relative to CARGO_MANIFEST_DIR
    if let Ok(manifest_dir) = std::env::var("CARGO_MANIFEST_DIR") {
        // Go up one level from the crate directory to workspace root
        let path = PathBuf::from(manifest_dir)
            .parent()
            .map(|p| p.join(DEFAULT_CONFIG_PATH))
            .unwrap_or_else(|| PathBuf::from(DEFAULT_CONFIG_PATH));
        if path.exists() {
            return path;
        }
    }

    // Fall back to relative path from current directory
    PathBuf::from(DEFAULT_CONFIG_PATH)
}

/// Expand tilde (~) in a path to the user's home directory.
pub fn expand_tilde_path(path: &str) -> TestConfigResult<PathBuf> {
    if let Some(rest) = path.strip_prefix("~/") {
        let home = std::env::var("HOME")
            .or_else(|_| std::env::var("USERPROFILE"))
            .map_err(|_| TestConfigError::PathExpansionFailed(path.to_string()))?;
        Ok(PathBuf::from(home).join(rest))
    } else if path == "~" {
        let home = std::env::var("HOME")
            .or_else(|_| std::env::var("USERPROFILE"))
            .map_err(|_| TestConfigError::PathExpansionFailed(path.to_string()))?;
        Ok(PathBuf::from(home))
    } else {
        Ok(PathBuf::from(path))
    }
}

/// Check if worker availability checks should be skipped.
pub fn should_skip_worker_check() -> bool {
    std::env::var(ENV_SKIP_WORKER_CHECK)
        .map(|v| v == "1" || v.to_lowercase() == "true")
        .unwrap_or(false)
}

/// Check if all true E2E tests should be skipped.
pub fn should_skip_all_tests() -> bool {
    std::env::var(ENV_SKIP_ALL_TESTS)
        .map(|v| v == "1" || v.to_lowercase() == "true")
        .unwrap_or(false)
}

/// Check if mock SSH mode is enabled (for CI testing without real workers).
pub fn is_mock_ssh_mode() -> bool {
    std::env::var("RCH_MOCK_SSH")
        .map(|v| v == "1" || v.to_lowercase() == "true")
        .unwrap_or(false)
}

fn is_ci() -> bool {
    std::env::var("CI")
        .map(|v| v == "1" || v.to_lowercase() == "true")
        .unwrap_or(false)
}

fn log_skip_event(
    logger: &TestLogger,
    level: LogLevel,
    message: &str,
    context: Vec<(String, String)>,
) {
    let mut ctx = Vec::with_capacity(context.len() + 1);
    ctx.push(("phase".to_string(), "skip_check".to_string()));
    ctx.extend(context);
    logger.log_with_context(
        level,
        LogSource::Custom("skip_check".to_string()),
        message,
        ctx,
    );
}

fn ping_worker(host: &str, port: u16, timeout: Duration) -> Result<bool, std::io::Error> {
    let addrs = (host, port).to_socket_addrs()?;
    for addr in addrs {
        if TcpStream::connect_timeout(&addr, timeout).is_ok() {
            return Ok(true);
        }
    }
    Ok(false)
}

fn effective_ping_timeout(settings: &TestSettings) -> Duration {
    let requested = settings.ssh_connection_timeout_secs.max(1);
    let cap = if is_ci() { 3 } else { 8 };
    Duration::from_secs(requested.min(cap))
}

/// Load worker config and verify at least one reachable worker.
/// Returns None when tests should be skipped, with structured logs explaining why.
pub fn require_workers(logger: &TestLogger) -> Option<TestWorkersConfig> {
    let config_path = get_config_path();
    log_skip_event(
        logger,
        LogLevel::Info,
        "Checking worker availability",
        vec![
            ("config_path".to_string(), config_path.display().to_string()),
            ("ci".to_string(), is_ci().to_string()),
        ],
    );

    if should_skip_all_tests() {
        log_skip_event(
            logger,
            LogLevel::Info,
            "E2E tests disabled via env",
            vec![
                ("reason".to_string(), "env_skip_all".to_string()),
                ("env".to_string(), ENV_SKIP_ALL_TESTS.to_string()),
                ("skip".to_string(), "true".to_string()),
            ],
        );
        return None;
    }

    let config = match TestWorkersConfig::load_from(&config_path) {
        Ok(config) => config,
        Err(TestConfigError::NotFound(path)) => {
            log_skip_event(
                logger,
                LogLevel::Info,
                "Workers unavailable, skipping test",
                vec![
                    ("reason".to_string(), "config_not_found".to_string()),
                    ("path".to_string(), path.display().to_string()),
                    ("skip".to_string(), "true".to_string()),
                ],
            );
            return None;
        }
        Err(e) => {
            log_skip_event(
                logger,
                LogLevel::Warn,
                "Workers unavailable, skipping test",
                vec![
                    ("reason".to_string(), "config_load_failed".to_string()),
                    ("error".to_string(), e.to_string()),
                    ("skip".to_string(), "true".to_string()),
                ],
            );
            return None;
        }
    };

    if !config.has_enabled_workers() {
        log_skip_event(
            logger,
            LogLevel::Info,
            "Workers unavailable, skipping test",
            vec![
                ("reason".to_string(), "no_enabled_workers".to_string()),
                ("configured".to_string(), config.workers.len().to_string()),
                ("skip".to_string(), "true".to_string()),
            ],
        );
        return None;
    }

    if should_skip_worker_check() {
        log_skip_event(
            logger,
            LogLevel::Info,
            "Skipping worker reachability check via env",
            vec![
                ("reason".to_string(), "env_skip_check".to_string()),
                ("env".to_string(), ENV_SKIP_WORKER_CHECK.to_string()),
                ("skip".to_string(), "false".to_string()),
            ],
        );
        return Some(config);
    }

    let timeout = effective_ping_timeout(&config.settings);
    let mut reachable = 0usize;

    for worker in config.enabled_workers() {
        let start = Instant::now();
        let outcome = ping_worker(&worker.host, worker.port, timeout);
        let elapsed_ms = start.elapsed().as_millis().to_string();

        match outcome {
            Ok(true) => {
                reachable += 1;
                logger.log_with_context(
                    LogLevel::Info,
                    LogSource::Custom("skip_check".to_string()),
                    "Worker reachable",
                    vec![
                        ("phase".to_string(), "skip_check".to_string()),
                        ("worker_id".to_string(), worker.id.clone()),
                        ("host".to_string(), worker.host.clone()),
                        ("port".to_string(), worker.port.to_string()),
                        ("reachable".to_string(), "true".to_string()),
                        ("duration_ms".to_string(), elapsed_ms),
                    ],
                );
            }
            Ok(false) => {
                logger.log_with_context(
                    LogLevel::Warn,
                    LogSource::Custom("skip_check".to_string()),
                    "Worker unreachable",
                    vec![
                        ("phase".to_string(), "skip_check".to_string()),
                        ("worker_id".to_string(), worker.id.clone()),
                        ("host".to_string(), worker.host.clone()),
                        ("port".to_string(), worker.port.to_string()),
                        ("reachable".to_string(), "false".to_string()),
                        ("duration_ms".to_string(), elapsed_ms),
                    ],
                );
            }
            Err(e) => {
                logger.log_with_context(
                    LogLevel::Warn,
                    LogSource::Custom("skip_check".to_string()),
                    "Worker reachability check failed",
                    vec![
                        ("phase".to_string(), "skip_check".to_string()),
                        ("worker_id".to_string(), worker.id.clone()),
                        ("host".to_string(), worker.host.clone()),
                        ("port".to_string(), worker.port.to_string()),
                        ("error".to_string(), e.to_string()),
                        ("duration_ms".to_string(), elapsed_ms),
                    ],
                );
            }
        }
    }

    if reachable == 0 {
        log_skip_event(
            logger,
            LogLevel::Info,
            "Workers unavailable, skipping test",
            vec![
                ("reason".to_string(), "no_reachable_workers".to_string()),
                (
                    "configured".to_string(),
                    config.enabled_workers().len().to_string(),
                ),
                ("reachable".to_string(), reachable.to_string()),
                ("skip".to_string(), "true".to_string()),
            ],
        );
        return None;
    }

    log_skip_event(
        logger,
        LogLevel::Info,
        "Worker availability check complete",
        vec![
            (
                "configured".to_string(),
                config.enabled_workers().len().to_string(),
            ),
            ("reachable".to_string(), reachable.to_string()),
            ("skip".to_string(), "false".to_string()),
        ],
    );

    Some(config)
}

#[cfg(test)]
#[allow(unsafe_code)] // Tests need to set/remove env vars, which is unsafe in Rust 2024
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::TempDir;

    #[test]
    fn test_config_parses_valid_toml() {
        let config_str = r#"
[settings]
default_timeout_secs = 300

[[workers]]
id = "test"
host = "test.example.com"
user = "builder"
"#;
        let config: TestWorkersConfig = toml::from_str(config_str).unwrap();
        assert_eq!(config.workers.len(), 1);
        assert_eq!(config.workers[0].id, "test");
        assert_eq!(config.workers[0].host, "test.example.com");
    }

    #[test]
    fn test_config_rejects_missing_hostname() {
        let config_str = r#"
[[workers]]
id = "test"
user = "builder"
"#;
        let result: Result<TestWorkersConfig, _> = toml::from_str(config_str);
        assert!(result.is_err());
    }

    #[test]
    fn test_config_rejects_missing_id() {
        let config_str = r#"
[[workers]]
host = "test.example.com"
user = "builder"
"#;
        let result: Result<TestWorkersConfig, _> = toml::from_str(config_str);
        assert!(result.is_err());
    }

    #[test]
    fn test_config_default_port() {
        let config_str = r#"
[[workers]]
id = "test"
host = "test.example.com"
user = "builder"
"#;
        let config: TestWorkersConfig = toml::from_str(config_str).unwrap();
        assert_eq!(config.workers[0].port, 22);
    }

    #[test]
    fn test_config_custom_port() {
        let config_str = r#"
[[workers]]
id = "test"
host = "test.example.com"
user = "builder"
port = 2222
"#;
        let config: TestWorkersConfig = toml::from_str(config_str).unwrap();
        assert_eq!(config.workers[0].port, 2222);
    }

    #[test]
    fn test_config_expands_home_in_identity_file() {
        let config_str = r#"
[[workers]]
id = "test"
host = "test.example.com"
user = "builder"
identity_file = "~/.ssh/id_ed25519"
"#;
        let config: TestWorkersConfig = toml::from_str(config_str).unwrap();
        let expanded = config.workers[0].expanded_identity_file().unwrap();
        assert!(!expanded.to_string_lossy().contains("~"));
    }

    #[test]
    fn test_config_from_env_override() {
        let temp_dir = TempDir::new().unwrap();
        let custom_path = temp_dir.path().join("custom_workers.toml");

        // SAFETY: Test isolation - env var is set and removed in same test
        unsafe {
            std::env::set_var(ENV_WORKERS_CONFIG, custom_path.to_string_lossy().as_ref());
        }
        let path = get_config_path();
        assert_eq!(path, custom_path);
        unsafe {
            std::env::remove_var(ENV_WORKERS_CONFIG);
        }
    }

    #[test]
    fn test_config_missing_file_returns_error() {
        let result = TestWorkersConfig::load_from(Path::new("/nonexistent/workers_test.toml"));
        assert!(result.is_err());
        match result {
            Err(TestConfigError::NotFound(_)) => {}
            _ => panic!("Expected NotFound error"),
        }
    }

    #[test]
    fn test_config_empty_workers_is_valid() {
        let config_str = r#"
[settings]
default_timeout_secs = 300
"#;
        let config: TestWorkersConfig = toml::from_str(config_str).unwrap();
        assert!(config.workers.is_empty());
        assert!(!config.has_workers());
    }

    #[test]
    fn test_config_duplicate_worker_ids_rejected() {
        let config_str = r#"
[[workers]]
id = "duplicate"
host = "host1.example.com"

[[workers]]
id = "duplicate"
host = "host2.example.com"
"#;
        let config: TestWorkersConfig = toml::from_str(config_str).unwrap();
        let result = config.validate();
        assert!(result.is_err());
        match result {
            Err(TestConfigError::ValidationError(msg)) => {
                assert!(msg.contains("Duplicate worker ID"));
            }
            _ => panic!("Expected ValidationError"),
        }
    }

    #[test]
    fn test_config_enabled_workers_filter() {
        let config_str = r#"
[[workers]]
id = "enabled1"
host = "host1.example.com"
enabled = true

[[workers]]
id = "disabled"
host = "host2.example.com"
enabled = false

[[workers]]
id = "enabled2"
host = "host3.example.com"
enabled = true
"#;
        let config: TestWorkersConfig = toml::from_str(config_str).unwrap();
        let enabled = config.enabled_workers();
        assert_eq!(enabled.len(), 2);
        assert_eq!(enabled[0].id, "enabled1");
        assert_eq!(enabled[1].id, "enabled2");
    }

    #[test]
    fn test_config_default_settings() {
        let config_str = r#"
[[workers]]
id = "test"
host = "test.example.com"
"#;
        let config: TestWorkersConfig = toml::from_str(config_str).unwrap();
        assert_eq!(config.settings.default_timeout_secs, 300);
        assert_eq!(config.settings.ssh_connection_timeout_secs, 10);
        assert_eq!(config.settings.remote_work_dir, "/tmp/rch_test");
        assert_eq!(config.settings.rsync_compression, "zstd");
        assert!(config.settings.cleanup_after_test);
    }

    #[test]
    fn test_config_custom_settings() {
        let config_str = r#"
[settings]
default_timeout_secs = 600
ssh_connection_timeout_secs = 30
remote_work_dir = "/data/rch_test"
rsync_compression = "lz4"
cleanup_after_test = false
min_rust_version = "1.85.0"

[[workers]]
id = "test"
host = "test.example.com"
"#;
        let config: TestWorkersConfig = toml::from_str(config_str).unwrap();
        assert_eq!(config.settings.default_timeout_secs, 600);
        assert_eq!(config.settings.ssh_connection_timeout_secs, 30);
        assert_eq!(config.settings.remote_work_dir, "/data/rch_test");
        assert_eq!(config.settings.rsync_compression, "lz4");
        assert!(!config.settings.cleanup_after_test);
        assert_eq!(config.settings.min_rust_version, Some("1.85.0".to_string()));
    }

    #[test]
    fn test_effective_timeout_from_env() {
        let config_str = r#"
[settings]
default_timeout_secs = 300

[[workers]]
id = "test"
host = "test.example.com"
"#;
        let config: TestWorkersConfig = toml::from_str(config_str).unwrap();

        // Without env var, use config value
        assert_eq!(config.effective_timeout_secs(), 300);

        // With env var, use override
        // SAFETY: Test isolation
        unsafe {
            std::env::set_var(ENV_TIMEOUT_SECS, "600");
        }
        assert_eq!(config.effective_timeout_secs(), 600);
        unsafe {
            std::env::remove_var(ENV_TIMEOUT_SECS);
        }
    }

    #[test]
    fn test_config_loads_from_file() {
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("workers_test.toml");

        let config_content = r#"
[settings]
default_timeout_secs = 120

[[workers]]
id = "file-test"
host = "192.168.1.100"
user = "testuser"
total_slots = 16
"#;
        let mut file = std::fs::File::create(&config_path).unwrap();
        file.write_all(config_content.as_bytes()).unwrap();

        let config = TestWorkersConfig::load_from(&config_path).unwrap();
        assert_eq!(config.settings.default_timeout_secs, 120);
        assert_eq!(config.workers.len(), 1);
        assert_eq!(config.workers[0].id, "file-test");
        assert_eq!(config.workers[0].total_slots, 16);
    }

    #[test]
    fn test_should_skip_worker_check() {
        // Default is false
        unsafe {
            std::env::remove_var(ENV_SKIP_WORKER_CHECK);
        }
        assert!(!should_skip_worker_check());

        // Set to "1"
        unsafe {
            std::env::set_var(ENV_SKIP_WORKER_CHECK, "1");
        }
        assert!(should_skip_worker_check());

        // Set to "true"
        unsafe {
            std::env::set_var(ENV_SKIP_WORKER_CHECK, "true");
        }
        assert!(should_skip_worker_check());

        // Set to "0" (false)
        unsafe {
            std::env::set_var(ENV_SKIP_WORKER_CHECK, "0");
        }
        assert!(!should_skip_worker_check());

        // Clean up
        unsafe {
            std::env::remove_var(ENV_SKIP_WORKER_CHECK);
        }
    }

    #[test]
    fn test_expand_path_tilde() {
        let expanded = expand_tilde_path("~/.ssh/id_rsa").unwrap();
        assert!(!expanded.to_string_lossy().starts_with("~"));
        assert!(expanded.to_string_lossy().ends_with(".ssh/id_rsa"));
    }

    #[test]
    fn test_expand_path_absolute() {
        let expanded = expand_tilde_path("/etc/ssh/ssh_host_key").unwrap();
        assert_eq!(expanded, PathBuf::from("/etc/ssh/ssh_host_key"));
    }

    #[test]
    fn test_expand_path_relative() {
        let expanded = expand_tilde_path("./keys/id_rsa").unwrap();
        assert_eq!(expanded, PathBuf::from("./keys/id_rsa"));
    }

    #[test]
    fn test_worker_tags() {
        let config_str = r#"
[[workers]]
id = "tagged"
host = "test.example.com"
tags = ["rust", "fast", "production"]
"#;
        let config: TestWorkersConfig = toml::from_str(config_str).unwrap();
        assert_eq!(config.workers[0].tags.len(), 3);
        assert!(config.workers[0].tags.contains(&"rust".to_string()));
        assert!(config.workers[0].tags.contains(&"fast".to_string()));
        assert!(config.workers[0].tags.contains(&"production".to_string()));
    }

    #[test]
    fn test_config_all_default_values() {
        let config_str = r#"
[[workers]]
id = "minimal"
host = "minimal.example.com"
"#;
        let config: TestWorkersConfig = toml::from_str(config_str).unwrap();
        let worker = &config.workers[0];

        assert_eq!(worker.user, "ubuntu");
        assert_eq!(worker.port, 22);
        assert_eq!(worker.identity_file, "~/.ssh/id_rsa");
        assert_eq!(worker.total_slots, 8);
        assert_eq!(worker.priority, 100);
        assert!(worker.tags.is_empty());
        assert!(worker.enabled);
    }

    #[test]
    fn test_validation_empty_hostname_rejected() {
        let config = TestWorkersConfig {
            settings: TestSettings::default(),
            workers: vec![TestWorkerEntry {
                id: "test".to_string(),
                host: String::new(), // Empty!
                user: "ubuntu".to_string(),
                port: 22,
                identity_file: "~/.ssh/id_rsa".to_string(),
                total_slots: 8,
                priority: 100,
                tags: vec![],
                enabled: true,
            }],
        };
        let result = config.validate();
        assert!(result.is_err());
        match result {
            Err(TestConfigError::ValidationError(msg)) => {
                assert!(msg.contains("empty hostname"));
            }
            _ => panic!("Expected ValidationError"),
        }
    }

    #[test]
    fn test_validation_empty_username_rejected() {
        let config = TestWorkersConfig {
            settings: TestSettings::default(),
            workers: vec![TestWorkerEntry {
                id: "test".to_string(),
                host: "test.example.com".to_string(),
                user: String::new(), // Empty!
                port: 22,
                identity_file: "~/.ssh/id_rsa".to_string(),
                total_slots: 8,
                priority: 100,
                tags: vec![],
                enabled: true,
            }],
        };
        let result = config.validate();
        assert!(result.is_err());
        match result {
            Err(TestConfigError::ValidationError(msg)) => {
                assert!(msg.contains("empty username"));
            }
            _ => panic!("Expected ValidationError"),
        }
    }

    #[test]
    fn test_to_worker_config_conversion() {
        let config_str = r#"
[[workers]]
id = "convert-test"
host = "192.168.1.50"
user = "admin"
identity_file = "~/.ssh/admin_key"
total_slots = 32
priority = 150
tags = ["fast", "ssd"]
"#;
        let config: TestWorkersConfig = toml::from_str(config_str).unwrap();
        let worker_config = config.workers[0].to_worker_config();

        assert_eq!(worker_config.id.as_str(), "convert-test");
        assert_eq!(worker_config.host, "192.168.1.50");
        assert_eq!(worker_config.user, "admin");
        assert_eq!(worker_config.identity_file, "~/.ssh/admin_key");
        assert_eq!(worker_config.total_slots, 32);
        assert_eq!(worker_config.priority, 150);
        assert_eq!(worker_config.tags, vec!["fast", "ssd"]);
    }

    #[test]
    fn test_to_worker_configs_batch() {
        let config_str = r#"
[[workers]]
id = "worker1"
host = "host1.example.com"
enabled = true

[[workers]]
id = "worker2"
host = "host2.example.com"
enabled = false

[[workers]]
id = "worker3"
host = "host3.example.com"
enabled = true
"#;
        let config: TestWorkersConfig = toml::from_str(config_str).unwrap();
        let worker_configs = config.to_worker_configs();

        // Only enabled workers should be converted
        assert_eq!(worker_configs.len(), 2);
        assert_eq!(worker_configs[0].id.as_str(), "worker1");
        assert_eq!(worker_configs[1].id.as_str(), "worker3");
    }
}