using System; using System.IO; using System.Threading.Tasks; using CtrEditor.HydraulicSimulator.Python; using CtrEditor.HydraulicSimulator.TSNet; using CtrEditor.ObjetosSim; namespace CtrEditor.HydraulicSimulator.Tests { /// /// Pruebas de integración para TSNet /// public static class TSNetIntegrationTest { /// /// Ejecuta todas las pruebas de integración TSNet /// public static async Task RunAllTestsAsync() { Console.WriteLine("=== TSNet Integration Tests ==="); bool allPassed = true; // Test 1: Inicialización de Python allPassed &= await TestPythonInitialization(); // Test 2: Verificación de TSNet allPassed &= await TestTSNetAvailability(); // Test 3: Ejecución de script básico allPassed &= await TestBasicScriptExecution(); // Test 4: Creación de TSNetSimulationManager allPassed &= await TestTSNetSimulationManager(); Console.WriteLine($"\n=== Tests Result: {(allPassed ? "PASSED" : "FAILED")} ==="); return allPassed; } /// /// Test 1: Inicialización de Python /// private static async Task TestPythonInitialization() { try { Console.WriteLine("\n1. Testing Python Initialization..."); // Verificar que los archivos existen var pythonDll = PythonInterop.PythonDll; Console.WriteLine($" Python DLL path: {pythonDll}"); if (!File.Exists(pythonDll)) { Console.WriteLine($" ✗ Python DLL not found: {pythonDll}"); return false; } Console.WriteLine(" ✓ Python DLL found"); // Inicializar Python var initialized = PythonInterop.Initialize(); Console.WriteLine($" Python initialized: {initialized}"); if (initialized) { Console.WriteLine(" ✓ Python initialization successful"); return true; } else { Console.WriteLine(" ✗ Python initialization failed"); return false; } } catch (Exception ex) { Console.WriteLine($" ✗ Exception in Python initialization: {ex.Message}"); return false; } } /// /// Test 2: Verificación de TSNet /// private static async Task TestTSNetAvailability() { try { Console.WriteLine("\n2. Testing TSNet Availability..."); var available = PythonInterop.IsTSNetAvailable(); if (available) { Console.WriteLine(" ✓ TSNet is available"); return true; } else { Console.WriteLine(" ✗ TSNet is not available"); return false; } } catch (Exception ex) { Console.WriteLine($" ✗ Exception checking TSNet: {ex.Message}"); return false; } } /// /// Test 3: Ejecución de script básico /// private static async Task TestBasicScriptExecution() { try { Console.WriteLine("\n3. Testing Basic Script Execution..."); var scriptPath = Path.Combine( @"d:\Proyectos\VisualStudio\CtrEditor\HydraulicSimulator\TSNet", "test_tsnet_integration.py" ); Console.WriteLine($" Script path: {scriptPath}"); if (!File.Exists(scriptPath)) { Console.WriteLine($" ✗ Test script not found: {scriptPath}"); return false; } var result = await PythonInterop.ExecuteScriptAsync(scriptPath); Console.WriteLine($" Script execution result:"); Console.WriteLine($" Success: {result.Success}"); Console.WriteLine($" Exit code: {result.ExitCode}"); if (!string.IsNullOrEmpty(result.Output)) { Console.WriteLine($" Output: {result.Output.Substring(0, Math.Min(200, result.Output.Length))}..."); } if (!string.IsNullOrEmpty(result.Error)) { Console.WriteLine($" Error: {result.Error}"); } if (result.Success) { Console.WriteLine(" ✓ Script execution successful"); return true; } else { Console.WriteLine(" ✗ Script execution failed"); return false; } } catch (Exception ex) { Console.WriteLine($" ✗ Exception in script execution: {ex.Message}"); return false; } } /// /// Test 4: Creación de TSNetSimulationManager /// private static async Task TestTSNetSimulationManager() { try { Console.WriteLine("\n4. Testing TSNetSimulationManager..."); using var manager = new TSNetSimulationManager(); Console.WriteLine($" Working directory: {manager.WorkingDirectory}"); Console.WriteLine($" Configuration duration: {manager.Configuration.Duration}s"); Console.WriteLine($" Configuration time step: {manager.Configuration.TimeStep}s"); // Verificar que el directorio de trabajo se crea if (Directory.Exists(manager.WorkingDirectory)) { Console.WriteLine(" ✓ Working directory created"); } else { Console.WriteLine(" ✗ Working directory not created"); return false; } Console.WriteLine(" ✓ TSNetSimulationManager created successfully"); return true; } catch (Exception ex) { Console.WriteLine($" ✗ Exception creating TSNetSimulationManager: {ex.Message}"); return false; } } /// /// Ejecuta un test específico de generación INP /// public static async Task TestINPGeneration() { try { Console.WriteLine("\n=== Testing INP Generation ==="); using var manager = new TSNetSimulationManager(); // Simular un tanque simple var mockTank = CreateMockTank(); manager.RegisterHydraulicObject(mockTank); // Generar archivo INP var inpFile = await manager.GenerateINPFileAsync(); if (File.Exists(inpFile)) { Console.WriteLine($"✓ INP file generated: {inpFile}"); // Leer y mostrar parte del contenido var content = await File.ReadAllTextAsync(inpFile); var preview = content.Substring(0, Math.Min(500, content.Length)); Console.WriteLine($"INP Preview:\n{preview}..."); return true; } else { Console.WriteLine("✗ INP file not generated"); return false; } } catch (Exception ex) { Console.WriteLine($"✗ Exception in INP generation test: {ex.Message}"); return false; } } /// /// Crea un tanque mock para pruebas /// private static osHydTank CreateMockTank() { return new osHydTank { Nombre = "TankTest", CrossSectionalArea = 1.0, MaxLevelM = 2.0, MinLevelM = 0.0, CurrentLevelM = 1.0, TankPressure = 1.013, IsFixedPressure = false }; } } }