using System.Diagnostics; using System.IO; using System.Net; using System.Net.Sockets; using System.Reflection; using System.Security.Cryptography; using System.Windows; using System.Windows.Controls; using System.Windows.Interop; using System.Windows.Media.Imaging; using XFEToolBox.Core.Tools; namespace QuickTools.Validation; internal static partial class Program { private static readonly List verifiedPackages = []; private static async Task ValidateActualPackage(string package, ToolPackageManifest manifest, Type service) { string hash = Convert.ToHexString(SHA256.HashData(await File.ReadAllBytesAsync(package))); var launch = (Task)service.GetMethod("BuildPackageAndRunAsync")!.Invoke(null, [package, manifest.Id, manifest.Version, hash, false, CancellationToken.None, null])!; await launch.WaitAsync(TimeSpan.FromMinutes(4)); object result = launch.GetType().GetProperty("Result")!.GetValue(launch)!; Ensure((bool)result.GetType().GetProperty("Success")!.GetValue(result)!, (string)result.GetType().GetProperty("Message")!.GetValue(result)!); int pid = (int)result.GetType().GetProperty("ProcessId")!.GetValue(result)!; using var process = Process.GetProcessById(pid); try { var timer = Stopwatch.StartNew(); do { await Task.Delay(100); process.Refresh(); } while (!process.HasExited && process.MainWindowHandle == 0 && timer.Elapsed < TimeSpan.FromSeconds(15)); Ensure(!process.HasExited && process.MainWindowHandle != 0, "Exact package did not open a window"); Ensure(process.MainWindowTitle == manifest.Name, "Unexpected runtime window: " + process.MainWindowTitle); string executable = process.MainModule!.FileName; Ensure(Path.GetFileName(executable).StartsWith("XFEToolRuntime_", StringComparison.Ordinal), "Unexpected test process"); // Load the actual production-compiled package assembly, not the host's // component library. Exercise its real entry and button handlers with // isolated fixtures while the independently launched tool remains live. var assembly = Assembly.LoadFrom(Path.ChangeExtension(executable, ".dll")); await ValidateCompiledEntry(assembly, manifest); process.Refresh(); Ensure(!process.HasExited, "Independent package exited during validation"); verifiedPackages.Add(new { id = manifest.Id, version = manifest.Version, sha256 = hash, opened = true, functional = true, evidence = "Production .xfetool launch + exact compiled entry/button handlers; isolated fixtures", pid }); } finally { // This handle refers only to the process launched above, never a user's existing tool. if (!process.HasExited) { process.CloseMainWindow(); try { await process.WaitForExitAsync().WaitAsync(TimeSpan.FromSeconds(10)); } catch (TimeoutException) { process.Kill(); await process.WaitForExitAsync(); } } } } private static async Task ValidateCompiledEntry(Assembly assembly, ToolPackageManifest manifest) { const string ns = "XFEToolBox.Tools.QuickSuite."; string fixtures = Path.Combine(root, "actual-package-fixtures", manifest.Id); Directory.CreateDirectory(fixtures); assembly.GetType(ns + "QuickToolPaths", true)!.GetField("ValidationDataRoot", BindingFlags.NonPublic | BindingFlags.Static)!.SetValue(null, fixtures); var entry = (FrameworkElement)Activator.CreateInstance(assembly.GetType(manifest.Entry.ViewClass, true)!)!; var window = FixtureWindow(new Border { Child = new ContentControl { Content = entry } }); window.Show(); await Pump(window); var page = (FrameworkElement)Descendants(entry).Single(x => x.GetType().FullName == ns + "QuickToolPage"); Ensure(page.GetType().Assembly == assembly, "Package fell back to the old host page"); TextBox Box(string name) => Descendants(page).OfType().Single(b => b.Name == name); async Task ClickActual(string title) { var button = Descendants(page).OfType