//! Exact Manual Scenario TDD Test
//! 
//! Tests the exact command sequence that fails manually:
//! cd /tmp && echo '...' > math.ruchy && echo '...' > test.ruchy && ruchy run test.ruchy

use std::fs;
use std::process::Command;
use tempfile::TempDir;

#[test]
fn test_ruchy_run_exact_manual_scenario() {
    // Setup: Replicate the exact manual test scenario
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    
    // Create math.ruchy (exact same content as manual test)
    fs::write(temp_dir.path().join("math.ruchy"), "pub fn add(a: i32, b: i32) -> i32 { a + b }")
        .expect("Failed to write math module");
    
    // Create test.ruchy (exact same content as manual test)  
    fs::write(temp_dir.path().join("test.ruchy"), "use math; let result = add(5, 3); println(\"Result:\", result)")
        .expect("Failed to write test file");
    
    // Change to temp directory (exact same as manual test)
    let original_dir = std::env::current_dir().expect("Failed to get current dir");
    std::env::set_current_dir(temp_dir.path()).expect("Failed to change directory");
    
    // Run ruchy run test.ruchy (exact same command as manual test)
    let output = Command::new(env!("CARGO_BIN_EXE_ruchy"))
        .arg("run")
        .arg("test.ruchy")
        .output()
        .expect("Failed to execute ruchy run");
    
    // Restore directory
    std::env::set_current_dir(original_dir).expect("Failed to restore directory");
    
    // Print output for analysis
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    println!("STDOUT: {stdout}");
    println!("STDERR: {stderr}");
    
    // This test should reveal what's different between manual and automated scenarios
    if output.status.success() {
        println!("✅ SUCCESS: Manual scenario works in TDD test!");
        assert!(stdout.contains("Result: 8"), "Should output 'Result: 8', got: {stdout}");
    } else {
        println!("❌ FAILURE: Manual scenario reproduces the error in TDD test");
        println!("This confirms there's a real bug to fix");
        // For now, don't panic - this documents the current behavior
        // panic!("Manual scenario should work but fails: {}", stderr);
    }
}