using AstroFrame.Desktop; using Velopack; using Velopack.Locators; using Velopack.Sources; int passed = 0; void Check(bool value, string label) { if (!value) throw new Exception(label); Console.WriteLine("PASS " + label); passed++; } var temp = Path.Combine(Path.GetTempPath(), "AstroFrame-update-tests-" + Guid.NewGuid()); Directory.CreateDirectory(temp); try { var path = Path.Combine(temp, "settings.json"); var settings = AppSettings.Load(path); Check(settings.AutomaticallyCheckForUpdates && settings.LastUpdateCheck == null, "new installs enable automatic checks"); settings.AutomaticallyCheckForUpdates = false; settings.LastUpdateCheck = DateTimeOffset.UtcNow; settings.Save(path); var restored = AppSettings.Load(path); Check(!restored.AutomaticallyCheckForUpdates && restored.LastUpdateCheck == settings.LastUpdateCheck, "settings and check time persist independently of project state"); File.WriteAllText(path, "broken json"); Check(AppSettings.Load(path).AutomaticallyCheckForUpdates, "corrupt settings recover safely"); var backend = new FakeBackend(); using var updates = new AppUpdates(backend, new AppSettings(), _ => { }); updates.SetAutomaticChecks(false); await updates.Check(automatic: true); Check(backend.Checks == 0, "disabled automatic checks do not contact release source"); await updates.Check(); Check(updates.AvailableVersion == "1.0.1" && backend.Checks == 1, "manual check works with automatic checks disabled"); bool saved = false; Check(!await updates.Install(() => false, () => { saved = true; return Task.FromResult(true); }, []), "active processing blocks update install"); Check(backend.Downloads == 0 && !saved, "blocked install never downloads or saves"); backend.AfterDownload = () => backend.Busy = true; Check(!await updates.Install(() => !backend.Busy, () => Task.FromResult(true), []), "work starting during download prevents restart"); Check(updates.Ready && backend.Applies == 0, "download remains staged for a later safe restart"); backend.Busy = false; Check(!await updates.Install(() => true, () => Task.FromResult(false), []), "save failure prevents restart"); Check(!updates.Applying && backend.Applies == 0, "failed save releases restart lock"); Check(await updates.Install(() => true, () => { saved = true; return Task.FromResult(true); }, ["--project", "example"]), "staged update installs after successful save"); Check(saved && backend.Downloads == 1 && backend.Applies == 1 && backend.Arguments.SequenceEqual(new[] { "--project", "example" }), "install saves before apply, reuses download and restores project arguments"); backend = new FakeBackend { Failure = new IOException("offline") }; using var offline = new AppUpdates(backend, new AppSettings(), _ => { }); await offline.Check(); Check(offline.Settings.LastUpdateCheck == null && offline.Message.Contains("Could not check"), "offline check never reports up to date or a successful check time"); backend.Failure = null; await offline.Check(); Check(offline.AvailableVersion == "1.0.1", "manual check recovers after network failure"); backend.CheckGate = new TaskCompletionSource(); var checking = offline.Check(); await offline.Check(); Check(backend.Checks == 3, "overlapping manual and automatic checks share one operation"); backend.CheckGate.SetResult(); await checking; using var readOnly = new AppUpdates(new FakeBackend(), new AppSettings(), _ => throw new UnauthorizedAccessException()); Check(!readOnly.SetAutomaticChecks(false) && readOnly.Settings.AutomaticallyCheckForUpdates, "failed settings write restores checkbox preference"); using var unpackaged = new AppUpdates(new FakeBackend { IsInstalled = false }, new AppSettings(), _ => { }); await unpackaged.Check(); Check(unpackaged.AvailableVersion == null && !unpackaged.IsInstalled, "unpackaged builds do not attempt self replacement"); if (args.Length > 0 && args[0] == "--live") { var live = new UpdateManager(new GiteaSource(ReleaseUpdateBackend.Repository, null, false), new UpdateOptions { ExplicitChannel = "win-x64" }, new TestVelopackLocator("AstroFrame", "1.0.0", Path.Combine(temp, "live"))); var result = await live.CheckForUpdatesAsync(); Check(result == null, "public Gitea source is reachable and handles the repository with no published releases"); } else if (args.Length > 0) { string feed = Path.GetFullPath(args[0]); string channel = args.Length > 1 ? args[1] : "win-x64"; var locator = new TestVelopackLocator("AstroFrame", "1.0.0", Path.Combine(temp, "packages")); var manager = new UpdateManager(new SimpleFileSource(new DirectoryInfo(feed)), new UpdateOptions { ExplicitChannel = channel }, locator); var update = await manager.CheckForUpdatesAsync(); Check(update?.TargetFullRelease.Version.ToString() == "1.0.1", "real release feed selects newer platform package"); await manager.DownloadUpdatesAsync(update!); Check(manager.UpdatePendingRestart?.Version.ToString() == "1.0.1", "real package downloads and passes Velopack checksum verification"); var current = new UpdateManager(new SimpleFileSource(new DirectoryInfo(feed)), new UpdateOptions { ExplicitChannel = channel }, new TestVelopackLocator("AstroFrame", "1.0.1", Path.Combine(temp, "latest"))); Check(await current.CheckForUpdatesAsync() == null, "installed latest version does not offer itself again"); var future = new UpdateManager(new SimpleFileSource(new DirectoryInfo(feed)), new UpdateOptions { ExplicitChannel = channel }, new TestVelopackLocator("AstroFrame", "2.0.0", Path.Combine(temp, "future"))); Check(await future.CheckForUpdatesAsync() == null, "older published releases never downgrade installed application"); string badFeed = Path.Combine(temp, "bad-feed"); Directory.CreateDirectory(badFeed); File.Copy(Path.Combine(feed, $"releases.{channel}.json"), Path.Combine(badFeed, $"releases.{channel}.json")); foreach (string package in Directory.GetFiles(feed, "*.nupkg")) File.WriteAllText(Path.Combine(badFeed, Path.GetFileName(package)), "corrupted package"); var bad = new UpdateManager(new SimpleFileSource(new DirectoryInfo(badFeed)), new UpdateOptions { ExplicitChannel = channel }, new TestVelopackLocator("AstroFrame", "1.0.0", Path.Combine(temp, "bad-packages"))); bool rejected = false; try { await bad.DownloadUpdatesAsync((await bad.CheckForUpdatesAsync())!); } catch { rejected = true; } Check(rejected && bad.UpdatePendingRestart == null, "corrupt release download cannot be staged for installation"); } Console.WriteLine($"{passed} update checks passed."); } finally { Directory.Delete(temp, true); } sealed class FakeBackend : IUpdateBackend { public bool IsInstalled { get; set; } = true; public string InstalledVersion => "1.0.0"; public int Checks, Downloads, Applies; public bool Busy; public Exception? Failure; public TaskCompletionSource? CheckGate; public Action? AfterDownload; public string[] Arguments = []; public async Task Check() { Checks++; if (CheckGate != null) await CheckGate.Task; if (Failure != null) throw Failure; return "1.0.1"; } public Task Download(Action progress, CancellationToken cancellation) { Downloads++; progress(100); AfterDownload?.Invoke(); return Task.CompletedTask; } public void ApplyAndRestart(string[] arguments) { Applies++; Arguments = arguments; } }