using System.Diagnostics; namespace Tests; [SetUpFixture] public class TestSetup { private const string ServerUrl = "http://localhost:5256"; private Process? _serverProcess; [OneTimeSetUp] public async Task BeforeAllTests() { Console.WriteLine("[DEBUG_LOG] Starting Server..."); // Find solution root var currentDir = AppContext.BaseDirectory; while (currentDir != null && !File.Exists(Path.Combine(currentDir, "Chrono.sln"))) currentDir = Path.GetDirectoryName(currentDir); if (currentDir == null) throw new Exception("Could not find Chrono.sln to determine project path."); var projectPath = Path.Combine(currentDir, "Server", "Server.csproj"); var startInfo = new ProcessStartInfo { FileName = "dotnet", Arguments = $"run --project \"{projectPath}\" --no-build", RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = true, WorkingDirectory = currentDir }; _serverProcess = Process.Start(startInfo); _serverProcess.OutputDataReceived += (s, e) => { if (e.Data != null) Console.WriteLine($"[SERVER OUT] {e.Data}"); }; _serverProcess.ErrorDataReceived += (s, e) => { if (e.Data != null) Console.WriteLine($"[SERVER ERR] {e.Data}"); }; _serverProcess.BeginOutputReadLine(); _serverProcess.BeginErrorReadLine(); if (_serverProcess == null) throw new Exception("Failed to start server process."); // Wait for the server to be ready using var client = new HttpClient(); var sw = Stopwatch.StartNew(); var timeout = TimeSpan.FromSeconds(30); var isReady = false; while (sw.Elapsed < timeout) try { var response = await client.GetAsync(ServerUrl); if (response.IsSuccessStatusCode) { isReady = true; break; } } catch { // Wait and retry await Task.Delay(1000); } if (!isReady) { _serverProcess.Kill(true); throw new Exception($"Server did not start within {timeout.TotalSeconds} seconds."); } Console.WriteLine("[DEBUG_LOG] Server is ready."); } [OneTimeTearDown] public void AfterAllTests() { if (_serverProcess != null && !_serverProcess.HasExited) { Console.WriteLine("[DEBUG_LOG] Stopping Server..."); _serverProcess.Kill(true); _serverProcess.Dispose(); } } }