msy 0.4.6

Modern musl rsync alternative - Fast, parallel file synchronization
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
/// Comprehensive SSH transport tests (requires fedora)
///
/// These tests verify ALL SSH transport operations, error scenarios, edge cases,
/// and large-scale operations to ensure production-ready SSH functionality.
///
/// **CRITICAL**: Run ALL these tests before considering v0.0.52 production-ready!
///
/// Run with: cargo test --test ssh_comprehensive_test -- --ignored
///
/// Prerequisites:
/// - fedora accessible via SSH (tailscale: nick@fedora)
/// - sy-remote installed on fedora: `cargo install --path . --bin sy-remote`
/// - SSH keys configured for passwordless login
/// - ~5GB free space on fedora:/tmp/
///
use std::fs;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, SystemTime};
use tempfile::TempDir;

const FEDORA_HOST: &str = "fedora";
const FEDORA_USER: &str = "nick";

/// Helper to create SSH config for fedora
fn create_fedora_config() -> msy::ssh::config::SshConfig {
	use msy::ssh::config::SshConfig;

	let mut config = SshConfig::new(FEDORA_HOST);
	config.user = FEDORA_USER.to_string();
	config.port = 22;
	config
}

/// Helper to create a remote path for testing
fn create_remote_test_path(test_name: &str) -> String {
	format!("/tmp/sy_ssh_test_{}_{}", test_name, std::process::id())
}

/// Helper to cleanup remote path via SSH command
fn cleanup_remote_path(path: &str) {
	let cleanup_cmd = format!("ssh {} 'rm -rf {}'", FEDORA_HOST, path);
	let _ = std::process::Command::new("sh").arg("-c").arg(&cleanup_cmd).output();
}

// =============================================================================
// SECTION 1: Basic Transport Operations
// =============================================================================

#[tokio::test]
#[serial_test::serial]
#[ignore]
async fn test_ssh_scan_directory() {
	use msy::transport::Transport;
	use msy::transport::ssh::SshTransport;

	let remote_path = create_remote_test_path("scan");

	// Create test directory structure on remote
	let setup_cmd = format!(
		"ssh {} 'mkdir -p {}/subdir && echo test1 > {}/file1.txt && echo test2 > {}/subdir/file2.txt && touch {}/empty.txt'",
		FEDORA_HOST, remote_path, remote_path, remote_path, remote_path
	);
	std::process::Command::new("sh").arg("-c").arg(&setup_cmd).output().expect("Failed to create remote test structure");

	let config = create_fedora_config();
	let transport = SshTransport::new(&config).await.expect("Failed to connect");

	// Test scan
	let entries = transport.scan(std::path::Path::new(&remote_path)).await.expect("Scan failed");

	// Verify we found all files and directories
	assert!(entries.len() >= 4, "Expected at least 4 entries, got {}", entries.len());

	let paths: Vec<String> = entries.iter().map(|e| e.path.to_string_lossy().to_string()).collect();
	assert!(paths.iter().any(|p| p.contains("file1.txt")), "Missing file1.txt");
	assert!(paths.iter().any(|p| p.contains("file2.txt")), "Missing file2.txt");
	assert!(paths.iter().any(|p| p.contains("empty.txt")), "Missing empty.txt");
	assert!(paths.iter().any(|p| p.contains("subdir")), "Missing subdir");

	cleanup_remote_path(&remote_path);
	println!("✅ SSH scan_directory: PASS");
}

#[tokio::test]
#[serial_test::serial]
#[ignore]
async fn test_ssh_exists() {
	use msy::transport::Transport;
	use msy::transport::ssh::SshTransport;

	let remote_path = create_remote_test_path("exists");

	// Create test file
	let setup_cmd = format!("ssh {} 'echo test > {}'", FEDORA_HOST, remote_path);
	std::process::Command::new("sh").arg("-c").arg(&setup_cmd).output().expect("Failed to create remote file");

	let config = create_fedora_config();
	let transport = SshTransport::new(&config).await.expect("Failed to connect");

	// Test exists (file that exists)
	let exists = transport.exists(std::path::Path::new(&remote_path)).await.expect("exists() failed");
	assert!(exists, "File should exist");

	// Test exists (file that doesn't exist)
	let nonexistent = format!("{}_nonexistent", remote_path);
	let exists = transport.exists(std::path::Path::new(&nonexistent)).await.expect("exists() failed");
	assert!(!exists, "Nonexistent file should not exist");

	cleanup_remote_path(&remote_path);
	println!("✅ SSH exists: PASS");
}

#[tokio::test]
#[serial_test::serial]
#[ignore]
async fn test_ssh_create_dir_all() {
	use msy::transport::Transport;
	use msy::transport::ssh::SshTransport;

	let remote_path = create_remote_test_path("create_dir");
	let nested_path = format!("{}/a/b/c/d", remote_path);

	let config = create_fedora_config();
	let transport = SshTransport::new(&config).await.expect("Failed to connect");

	// Test create_dir_all (nested directories)
	transport.create_dir_all(std::path::Path::new(&nested_path)).await.expect("create_dir_all failed");

	// Verify directory exists
	let exists = transport.exists(std::path::Path::new(&nested_path)).await.expect("exists check failed");
	assert!(exists, "Nested directory should exist");

	cleanup_remote_path(&remote_path);
	println!("✅ SSH create_dir_all: PASS");
}

#[tokio::test]
#[serial_test::serial]
#[ignore]
async fn test_ssh_read_write_file() {
	use msy::transport::Transport;
	use msy::transport::ssh::SshTransport;

	let remote_path = create_remote_test_path("read_write");

	let config = create_fedora_config();
	let transport = SshTransport::new(&config).await.expect("Failed to connect");

	// Test write_file
	let test_data = b"Hello from SSH write_file test!\nLine 2\nLine 3";
	transport.write_file(std::path::Path::new(&remote_path), test_data, SystemTime::now()).await.expect("write_file failed");

	// Test read_file
	let read_data = transport.read_file(std::path::Path::new(&remote_path)).await.expect("read_file failed");

	assert_eq!(read_data, test_data, "Read data doesn't match written data");

	cleanup_remote_path(&remote_path);
	println!("✅ SSH read_write_file: PASS");
}

#[tokio::test]
#[serial_test::serial]
#[ignore]
#[serial_test::serial]
async fn test_ssh_remove() {
	use msy::transport::Transport;
	use msy::transport::ssh::SshTransport;

	let remote_path = create_remote_test_path("remove");

	let config = create_fedora_config();
	let transport = SshTransport::new(&config).await.expect("Failed to connect");

	// Create file
	transport.write_file(std::path::Path::new(&remote_path), b"test", SystemTime::now()).await.expect("write_file failed");

	// Verify exists
	let exists = transport.exists(std::path::Path::new(&remote_path)).await.expect("exists check failed");
	assert!(exists, "File should exist before removal");

	// Test remove
	transport.remove(std::path::Path::new(&remote_path), false).await.expect("remove failed");

	// Verify deleted
	let exists = transport.exists(std::path::Path::new(&remote_path)).await.expect("exists check failed");
	assert!(!exists, "File should not exist after removal");

	println!("✅ SSH remove: PASS");
}

#[tokio::test]
#[serial_test::serial]
#[ignore]
async fn test_ssh_get_mtime() {
	use msy::transport::Transport;
	use msy::transport::ssh::SshTransport;

	let remote_path = create_remote_test_path("mtime");

	let config = create_fedora_config();
	let transport = SshTransport::new(&config).await.expect("Failed to connect");

	// Create file with known mtime
	let known_time = SystemTime::now() - Duration::from_secs(3600); // 1 hour ago
	transport.write_file(std::path::Path::new(&remote_path), b"test", known_time).await.expect("write_file failed");

	// Get mtime
	let mtime = transport.get_mtime(std::path::Path::new(&remote_path)).await.expect("get_mtime failed");

	// Verify mtime is close to known_time (within 2 seconds for clock skew)
	let diff = mtime.duration_since(known_time).unwrap_or_else(|e| e.duration());
	assert!(diff < Duration::from_secs(2), "Mtime difference too large: {:?}", diff);

	cleanup_remote_path(&remote_path);
	println!("✅ SSH get_mtime: PASS");
}

#[tokio::test]
#[serial_test::serial]
#[ignore]
#[serial_test::serial]
async fn test_ssh_file_info() {
	use msy::transport::Transport;
	use msy::transport::ssh::SshTransport;

	let remote_path = create_remote_test_path("file_info");

	let config = create_fedora_config();
	let transport = SshTransport::new(&config).await.expect("Failed to connect");

	// Create file
	let test_data = b"test file info content";
	transport.write_file(std::path::Path::new(&remote_path), test_data, SystemTime::now()).await.expect("write_file failed");

	// Get file info
	let info = transport.file_info(std::path::Path::new(&remote_path)).await.expect("file_info failed");

	assert_eq!(info.size, test_data.len() as u64, "File size mismatch");

	cleanup_remote_path(&remote_path);
	println!("✅ SSH file_info: PASS");
}

// =============================================================================
// SECTION 2: File Transfer Operations
// =============================================================================

#[tokio::test]
#[serial_test::serial]
#[ignore]
async fn test_ssh_copy_file_basic() {
	use msy::transport::Transport;
	use msy::transport::ssh::SshTransport;

	let remote_source = create_remote_test_path("copy_source");
	let remote_dest = create_remote_test_path("copy_dest");

	let config = create_fedora_config();
	let transport = SshTransport::new(&config).await.expect("Failed to connect");

	// Create source file
	let test_data = vec![0x42u8; 1024 * 1024]; // 1MB
	transport
		.write_file(std::path::Path::new(&remote_source), &test_data, SystemTime::now())
		.await
		.expect("write_file failed");

	// Copy file (using copy_file_streaming for remote→local, then local→remote)
	let local_temp = TempDir::new().unwrap();
	let local_file = local_temp.path().join("temp.dat");

	// Remote → Local
	let result = transport
		.copy_file_streaming(std::path::Path::new(&remote_source), &local_file, None)
		.await
		.expect("copy_file_streaming failed");

	assert_eq!(result.bytes_written, 1024 * 1024, "Bytes written mismatch");

	// Verify content
	let read_data = fs::read(&local_file).expect("Failed to read local file");
	assert_eq!(read_data, test_data, "Copied data doesn't match");

	cleanup_remote_path(&remote_source);
	cleanup_remote_path(&remote_dest);
	println!("✅ SSH copy_file_basic: PASS");
}

#[tokio::test]
#[serial_test::serial]
#[ignore]
async fn test_ssh_copy_file_with_progress() {
	use msy::transport::Transport;
	use msy::transport::ssh::SshTransport;

	let remote_source = create_remote_test_path("copy_progress");

	// Create 5MB file
	let create_cmd = format!("ssh {} 'dd if=/dev/zero of={} bs=1M count=5 2>/dev/null'", FEDORA_HOST, remote_source);
	std::process::Command::new("sh").arg("-c").arg(&create_cmd).output().expect("Failed to create remote file");

	let config = create_fedora_config();
	let transport = SshTransport::new(&config).await.expect("Failed to connect");

	let local_dest = TempDir::new().unwrap().path().join("downloaded.dat");

	// Track progress
	let updates = Arc::new(AtomicU64::new(0));
	let updates_clone = updates.clone();
	let last_bytes = Arc::new(AtomicU64::new(0));
	let last_bytes_clone = last_bytes.clone();

	let progress_callback = Arc::new(move |bytes: u64, total: u64| {
		updates_clone.fetch_add(1, Ordering::SeqCst);
		last_bytes_clone.store(bytes, Ordering::SeqCst);
		assert!(bytes <= total, "bytes should not exceed total");
	});

	// Download with progress
	let result = transport
		.copy_file_streaming(std::path::Path::new(&remote_source), &local_dest, Some(progress_callback))
		.await
		.expect("Download failed");

	assert_eq!(result.bytes_written, 5 * 1024 * 1024);

	let update_count = updates.load(Ordering::SeqCst);
	assert!(update_count >= 5, "Expected >= 5 updates, got {}", update_count);

	let final_bytes = last_bytes.load(Ordering::SeqCst);
	assert_eq!(final_bytes, 5 * 1024 * 1024);

	cleanup_remote_path(&remote_source);
	println!("✅ SSH copy_file_with_progress: PASS");
}

#[tokio::test]
#[serial_test::serial]
#[ignore]
async fn test_ssh_copy_empty_file() {
	use msy::transport::Transport;
	use msy::transport::ssh::SshTransport;

	let remote_source = create_remote_test_path("empty_source");

	let config = create_fedora_config();
	let transport = SshTransport::new(&config).await.expect("Failed to connect");

	// Create empty file
	transport.write_file(std::path::Path::new(&remote_source), b"", SystemTime::now()).await.expect("write_file failed");

	let local_dest = TempDir::new().unwrap().path().join("empty.dat");

	// Copy empty file
	let result = transport.copy_file_streaming(std::path::Path::new(&remote_source), &local_dest, None).await.expect("Copy failed");

	assert_eq!(result.bytes_written, 0, "Empty file should have 0 bytes");
	assert!(local_dest.exists(), "Empty file should exist locally");

	cleanup_remote_path(&remote_source);
	println!("✅ SSH copy_empty_file: PASS");
}

// =============================================================================
// SECTION 3: Symlink Operations
// =============================================================================

#[tokio::test]
#[serial_test::serial]
#[ignore]
async fn test_ssh_create_symlink() {
	use msy::transport::Transport;
	use msy::transport::ssh::SshTransport;

	let remote_base = create_remote_test_path("symlink");
	let remote_target = format!("{}/target.txt", remote_base);
	let remote_link = format!("{}/link.txt", remote_base);

	let config = create_fedora_config();
	let transport = SshTransport::new(&config).await.expect("Failed to connect");

	// Create base directory
	transport.create_dir_all(std::path::Path::new(&remote_base)).await.expect("create_dir_all failed");

	// Create target file
	transport
		.write_file(std::path::Path::new(&remote_target), b"target content", SystemTime::now())
		.await
		.expect("write_file failed");

	// Create symlink
	transport
		.create_symlink(std::path::Path::new("target.txt"), std::path::Path::new(&remote_link))
		.await
		.expect("create_symlink failed");

	// Verify symlink exists by scanning directory
	let entries = transport.scan(std::path::Path::new(&remote_base)).await.expect("scan failed");

	let link_entry = entries.iter().find(|e| e.path.ends_with("link.txt")).expect("Link should exist");
	assert!(link_entry.is_symlink, "Should be a symlink");
	// Note: symlink_target would need verification via additional API if needed

	cleanup_remote_path(&remote_base);
	println!("✅ SSH create_symlink: PASS");
}

// =============================================================================
// SECTION 4: Error Scenarios
// =============================================================================

#[tokio::test]
#[serial_test::serial]
#[ignore]
async fn test_ssh_read_nonexistent_file() {
	use msy::transport::Transport;
	use msy::transport::ssh::SshTransport;

	let remote_path = create_remote_test_path("nonexistent");

	let config = create_fedora_config();
	let transport = SshTransport::new(&config).await.expect("Failed to connect");

	// Try to read nonexistent file
	let result = transport.read_file(std::path::Path::new(&remote_path)).await;

	assert!(result.is_err(), "Reading nonexistent file should fail");

	println!("✅ SSH read_nonexistent_file: PASS");
}

#[tokio::test]
#[serial_test::serial]
#[ignore]
async fn test_ssh_remove_nonexistent_file() {
	use msy::transport::Transport;
	use msy::transport::ssh::SshTransport;

	let remote_path = create_remote_test_path("nonexistent_remove");

	let config = create_fedora_config();
	let transport = SshTransport::new(&config).await.expect("Failed to connect");

	// Try to remove nonexistent file (should not error, just succeed silently)
	let result = transport.remove(std::path::Path::new(&remote_path), false).await;

	// Some implementations error, some succeed - either is acceptable
	// Just verify it doesn't panic
	println!("Remove nonexistent result: {:?}", result);

	println!("✅ SSH remove_nonexistent_file: PASS");
}

#[tokio::test]
#[serial_test::serial]
#[ignore]
async fn test_ssh_write_to_readonly_parent() {
	use msy::transport::Transport;
	use msy::transport::ssh::SshTransport;

	let remote_base = create_remote_test_path("readonly");
	let remote_file = format!("{}/file.txt", remote_base);

	// Create directory and make it readonly
	let setup_cmd = format!("ssh {} 'mkdir -p {} && chmod 444 {}'", FEDORA_HOST, remote_base, remote_base);
	std::process::Command::new("sh").arg("-c").arg(&setup_cmd).output().expect("Failed to setup readonly directory");

	let config = create_fedora_config();
	let transport = SshTransport::new(&config).await.expect("Failed to connect");

	// Try to write file to readonly directory
	let result = transport.write_file(std::path::Path::new(&remote_file), b"test", SystemTime::now()).await;

	assert!(result.is_err(), "Writing to readonly directory should fail");

	// Cleanup (restore permissions first)
	let cleanup_cmd = format!("ssh {} 'chmod 755 {} && rm -rf {}'", FEDORA_HOST, remote_base, remote_base);
	let _ = std::process::Command::new("sh").arg("-c").arg(&cleanup_cmd).output();

	println!("✅ SSH write_to_readonly_parent: PASS");
}

// =============================================================================
// SECTION 5: Edge Cases
// =============================================================================

#[tokio::test]
#[serial_test::serial]
#[ignore]
async fn test_ssh_special_characters_in_filename() {
	use msy::transport::Transport;
	use msy::transport::ssh::SshTransport;

	let remote_base = create_remote_test_path("special_chars");
	let remote_file = format!("{}/file with spaces & special chars!.txt", remote_base);

	let config = create_fedora_config();
	let transport = SshTransport::new(&config).await.expect("Failed to connect");

	// Create directory
	transport.create_dir_all(std::path::Path::new(&remote_base)).await.expect("create_dir_all failed");

	// Write file with special characters in name
	let result = transport.write_file(std::path::Path::new(&remote_file), b"special content", SystemTime::now()).await;

	// Some SSH implementations may have issues with special chars
	if result.is_ok() {
		// Verify we can read it back
		let read_data = transport.read_file(std::path::Path::new(&remote_file)).await.expect("read_file failed");

		assert_eq!(read_data, b"special content");
		println!("✅ SSH special_characters_in_filename: PASS");
	} else {
		println!("⚠️  SSH special_characters_in_filename: SKIP (not supported)");
	}

	cleanup_remote_path(&remote_base);
}

#[tokio::test]
#[serial_test::serial]
#[ignore]
async fn test_ssh_deep_directory_hierarchy() {
	use msy::transport::Transport;
	use msy::transport::ssh::SshTransport;

	let remote_base = create_remote_test_path("deep");
	// Create 50-level deep hierarchy
	let mut deep_path = remote_base.clone();
	for i in 0..50 {
		deep_path.push_str(&format!("/level{}", i));
	}
	deep_path.push_str("/file.txt");

	let config = create_fedora_config();
	let transport = SshTransport::new(&config).await.expect("Failed to connect");

	// Create deep hierarchy
	let parent = std::path::Path::new(&deep_path).parent().unwrap();
	transport.create_dir_all(parent).await.expect("create_dir_all failed for deep hierarchy");

	// Write file at deep level
	transport
		.write_file(std::path::Path::new(&deep_path), b"deep file", SystemTime::now())
		.await
		.expect("write_file failed at deep level");

	// Verify we can read it
	let read_data = transport.read_file(std::path::Path::new(&deep_path)).await.expect("read_file failed at deep level");

	assert_eq!(read_data, b"deep file");

	cleanup_remote_path(&remote_base);
	println!("✅ SSH deep_directory_hierarchy: PASS");
}

#[tokio::test]
#[serial_test::serial]
#[ignore]
async fn test_ssh_binary_data_integrity() {
	use msy::transport::Transport;
	use msy::transport::ssh::SshTransport;

	let remote_path = create_remote_test_path("binary");

	let config = create_fedora_config();
	let transport = SshTransport::new(&config).await.expect("Failed to connect");

	// Create binary data with all byte values
	let mut binary_data = Vec::new();
	for i in 0..=255u8 {
		binary_data.extend_from_slice(&[i; 256]); // Each byte value repeated 256 times
	}

	// Write binary data
	transport
		.write_file(std::path::Path::new(&remote_path), &binary_data, SystemTime::now())
		.await
		.expect("write_file failed");

	// Read back
	let read_data = transport.read_file(std::path::Path::new(&remote_path)).await.expect("read_file failed");

	assert_eq!(read_data.len(), binary_data.len(), "Binary data length mismatch");
	assert_eq!(read_data, binary_data, "Binary data integrity check failed");

	cleanup_remote_path(&remote_path);
	println!("✅ SSH binary_data_integrity: PASS");
}

// =============================================================================
// SECTION 6: Large Scale Operations
// =============================================================================

#[tokio::test]
#[serial_test::serial]
#[ignore]
async fn test_ssh_large_file_100mb() {
	use msy::transport::Transport;
	use msy::transport::ssh::SshTransport;

	let remote_source = create_remote_test_path("large_100mb");

	// Create 100MB file
	let create_cmd = format!("ssh {} 'dd if=/dev/zero of={} bs=1M count=100 2>/dev/null'", FEDORA_HOST, remote_source);

	println!("Creating 100MB test file on fedora...");
	std::process::Command::new("sh").arg("-c").arg(&create_cmd).output().expect("Failed to create 100MB file");

	let config = create_fedora_config();
	let transport = SshTransport::new(&config).await.expect("Failed to connect");

	let local_dest = TempDir::new().unwrap().path().join("large_100mb.dat");

	println!("Downloading 100MB file...");
	let start = std::time::Instant::now();

	let result = transport.copy_file_streaming(std::path::Path::new(&remote_source), &local_dest, None).await.expect("Download failed");

	let duration = start.elapsed();
	let speed_mbps = (100.0 / duration.as_secs_f64()).round();

	assert_eq!(result.bytes_written, 100 * 1024 * 1024);
	assert!(local_dest.exists());

	cleanup_remote_path(&remote_source);
	println!("✅ SSH large_file_100mb: PASS ({:.2}s @ {} MB/s)", duration.as_secs_f64(), speed_mbps);
}

#[tokio::test]
#[serial_test::serial]
#[ignore]
async fn test_ssh_many_small_files() {
	use msy::transport::Transport;
	use msy::transport::ssh::SshTransport;

	let remote_base = create_remote_test_path("many_files");

	let config = create_fedora_config();
	let transport = SshTransport::new(&config).await.expect("Failed to connect");

	// Create directory
	transport.create_dir_all(std::path::Path::new(&remote_base)).await.expect("create_dir_all failed");

	println!("Creating 100 small files...");
	let start = std::time::Instant::now();

	// Create 100 small files
	for i in 0..100 {
		let file_path = format!("{}/file_{:03}.txt", remote_base, i);
		let content = format!("File number {}", i);
		transport
			.write_file(std::path::Path::new(&file_path), content.as_bytes(), SystemTime::now())
			.await
			.expect("write_file failed");
	}

	let duration = start.elapsed();

	// Verify by scanning
	let entries = transport.scan(std::path::Path::new(&remote_base)).await.expect("scan failed");

	assert!(entries.len() >= 100, "Expected at least 100 files, got {}", entries.len());

	cleanup_remote_path(&remote_base);
	println!("✅ SSH many_small_files: PASS (100 files in {:.2}s)", duration.as_secs_f64());
}

// =============================================================================
// SECTION 7: Connection Pool
// =============================================================================

#[tokio::test]
#[serial_test::serial]
#[ignore]
async fn test_ssh_connection_pool_concurrent_transfers() {
	use msy::transport::Transport;
	use msy::transport::ssh::SshTransport;

	let remote_base = create_remote_test_path("pool_test");

	// Create 3 test files
	for i in 1..=3 {
		let file_path = format!("{}/file{}.dat", remote_base, i);
		let create_cmd = format!("ssh {} 'mkdir -p {} && dd if=/dev/zero of={} bs=1M count=5 2>/dev/null'", FEDORA_HOST, remote_base, file_path);
		std::process::Command::new("sh").arg("-c").arg(&create_cmd).output().expect("Failed to create remote file");
	}

	let config = create_fedora_config();
	// Create pool with 3 connections
	let transport = Arc::new(SshTransport::with_pool_size(&config, 3).await.expect("Failed to create pool"));

	let dest_dir = TempDir::new().unwrap();

	// Download 3 files concurrently
	let mut handles = vec![];

	for i in 1..=3 {
		let t = transport.clone();
		let remote_file = format!("{}/file{}.dat", remote_base, i);
		let local_dest = dest_dir.path().join(format!("file{}.dat", i));

		let handle = tokio::spawn(async move { t.copy_file_streaming(std::path::Path::new(&remote_file), &local_dest, None).await.unwrap() });

		handles.push(handle);
	}

	// Wait for all transfers
	for handle in handles {
		let result = handle.await.unwrap();
		assert_eq!(result.bytes_written, 5 * 1024 * 1024);
	}

	cleanup_remote_path(&remote_base);
	println!("✅ SSH connection_pool_concurrent_transfers: PASS");
}

// =============================================================================
// Test Summary
// =============================================================================

#[test]
#[ignore]
fn print_ssh_test_summary() {
	println!("\n========================================");
	println!("SSH Comprehensive Test Suite Summary");
	println!("========================================\n");
	println!("SECTION 1: Basic Transport Operations");
	println!("  - scan_directory");
	println!("  - exists");
	println!("  - create_dir_all");
	println!("  - read_write_file");
	println!("  - remove");
	println!("  - get_mtime");
	println!("  - file_info");
	println!("\nSECTION 2: File Transfer Operations");
	println!("  - copy_file_basic");
	println!("  - copy_file_with_progress");
	println!("  - copy_empty_file");
	println!("\nSECTION 3: Symlink Operations");
	println!("  - create_symlink");
	println!("\nSECTION 4: Error Scenarios");
	println!("  - read_nonexistent_file");
	println!("  - remove_nonexistent_file");
	println!("  - write_to_readonly_parent");
	println!("\nSECTION 5: Edge Cases");
	println!("  - special_characters_in_filename");
	println!("  - deep_directory_hierarchy");
	println!("  - binary_data_integrity");
	println!("\nSECTION 6: Large Scale Operations");
	println!("  - large_file_100mb");
	println!("  - many_small_files");
	println!("\nSECTION 7: Connection Pool");
	println!("  - connection_pool_concurrent_transfers");
	println!("\nTotal: 23 comprehensive SSH tests");
	println!("\nRun with:");
	println!("  cargo test --test ssh_comprehensive_test -- --ignored");
	println!("========================================\n");
}