using System.Diagnostics; using System.Net; using System.Net.Sockets; using System.Text; using System.Text.Json; using RynthCore.App.Avalonia; namespace UsageStatsTests; /// /// Checks for UsageStats.cs (the launcher's opt-in usage statistics). /// dotnet run -c Release offline checks, in a temp folder against a local test server /// dotnet run -c Release -- live then ONE real report to https://aelrynth.com/rynth/ping, built from /// this PC's enabled plugins (engine.json, read only) and the live /// update feed's plugin names, with a throwaway install ID (printed) /// internal static class Program { private static int _checks, _failed; private static readonly HashSet AllowedKeys = new() { "schema", "install", "version", "windows", "crashes", "plugins", "otherPlugins" }; private static async Task Main(string[] args) { string root = Path.Combine(Path.GetTempPath(), "rc-usage-stats-tests-" + Guid.NewGuid().ToString("N")); Directory.CreateDirectory(root); try { Versions(); Payload(); Plugins(root); Crashes(root); OptInOut(root); await Sending(root); await FailuresAreSilent(root); } finally { try { Directory.Delete(root, true); } catch { } } Console.WriteLine($"{_checks - _failed}/{_checks} checks passed"); if (_failed > 0) return 1; if (args.Any(a => a.Equals("live", StringComparison.OrdinalIgnoreCase))) return await Live(); return 0; } private static void Versions() { Check(UsageStats.ShortVersion("2026.10.2.1+abcdef0123") == "2026.10.2.1", "release version keeps the number, drops the commit"); Check(UsageStats.ShortVersion("1.0.0+abc") == "dev", "unversioned build is dev"); Check(UsageStats.ShortVersion("2026.9.28.10 (4e4c4b7)") == "2026.9.28.10", "formatted version"); Check(UsageStats.ShortVersion(@"C:\Users\Tom\x.dll") == "unknown", "anything else is unknown (no free text)"); Check(UsageStats.ShortVersion(null) == "unknown", "null is unknown"); string w = UsageStats.WindowsMajor(); Check(w is "10" or "11" or "other", $"windows major is 10/11/other (got {w})"); } private static void Payload() { var plugins = new List { new("RynthAi", "2026.10.2.1+abc"), new("RynthAi", "2026.10.2.1+abc"), // duplicate copy: reported once new(@"C:\secret\Name", "1.0.0"), // not a safe name: dropped new("RynthNav", "1.0.0+dev"), }; string json = UsageStats.BuildPayload(Guid.NewGuid().ToString("D"), "2026.10.3.1+abc", plugins, 3, 2, "10"); using var doc = JsonDocument.Parse(json); var keys = doc.RootElement.EnumerateObject().Select(p => p.Name).ToList(); Check(keys.All(AllowedKeys.Contains) && keys.Count == AllowedKeys.Count, "payload has exactly the approved keys: " + string.Join(",", keys)); Check(doc.RootElement.GetProperty("plugins").GetArrayLength() == 2, "plugins deduplicated, unsafe names dropped"); Check(doc.RootElement.GetProperty("plugins")[0].EnumerateObject().Select(p => p.Name).SequenceEqual(new[] { "name", "version" }), "plugin entries are name+version only"); Check(!json.Contains('\\') && !json.Contains("C:"), "no file paths in the payload"); Check(Encoding.UTF8.GetByteCount(json) < 2048, $"payload is small ({Encoding.UTF8.GetByteCount(json)} bytes)"); string clamped = UsageStats.BuildPayload(Guid.NewGuid().ToString("D"), "dev", Array.Empty(), 5000, 99999, "11"); using var c = JsonDocument.Parse(clamped); Check(c.RootElement.GetProperty("crashes").GetInt32() == 1000 && c.RootElement.GetProperty("otherPlugins").GetInt32() == 100, "counts are clamped"); } private static void Plugins(string root) { string dir = Path.Combine(root, "plugins"); Directory.CreateDirectory(dir); string ai = Path.Combine(dir, "RynthCore.Plugin.RynthAi.dll"); string mine = Path.Combine(dir, "MyPrivatePlugin.dll"); File.Copy(typeof(Program).Assembly.Location, ai); File.Copy(typeof(Program).Assembly.Location, mine); var feed = new Dictionary(StringComparer.OrdinalIgnoreCase) { ["RynthCore.Plugin.RynthAi.dll"] = "RynthAi" }; var (known, other) = UsageStats.DescribePlugins(new[] { ai, mine, ai.ToUpperInvariant(), Path.Combine(dir, "Missing.dll") }, feed); Check(known.Count == 1 && known[0].Name == "RynthAi", "a feed plugin is reported by its feed name"); Check(other == 2, $"other plugins (incl. a missing file) only counted (got {other})"); } private static void Crashes(string root) { string dumps = Path.Combine(root, "dumps"); Directory.CreateDirectory(dumps); DateTime now = DateTime.UtcNow; void Make(string name, DateTime t) { string p = Path.Combine(dumps, name); File.WriteAllText(p, "x"); File.SetLastWriteTimeUtc(p, t); } Make("crash_1_old.dmp", now.AddDays(-2)); Make("crash_2_new.dmp", now.AddMinutes(-10)); Make("crash_3_new.dmp", now.AddMinutes(-5)); Make("hang_4_new.dmp", now.AddMinutes(-5)); Check(UsageStats.CountCrashDumps(dumps, now.AddDays(-1), now) == 2, "only crash_*.dmp newer than the last report count (hangs don't)"); Check(UsageStats.CountCrashDumps(Path.Combine(root, "nope"), now.AddDays(-1), now) == 0, "missing dump folder = 0"); // CoreCLR fatal-error dumps (the loader's clr__