using System.Diagnostics; using System.Text.Json; namespace UCVL.VPNPlus; internal static class ClientShutdownSelfTest { internal static int Run() { var flow = RunFlowAsync().GetAwaiter().GetResult(); var ui = flow == 0 ? ClientShutdownUiSelfTest.Run() : -1; var handoff = flow == 0 && ui == 0 ? ClientShutdownChildSelfTest.Run() : -1; var passed = flow == 0 && ui == 0 && handoff == 0; var checks = new Dictionary(); foreach (var (prefix, file) in new[] { ("flow", "client-exit-self-test.json"), ("ui", "client-exit-ui-self-test.json"), ("handoff", "client-exit-child-handoff-self-test.json") }) { var path = Path.Combine(AppContext.BaseDirectory, file); if (!File.Exists(path)) { checks[prefix + ".report_exists"] = false; continue; } using var document = JsonDocument.Parse(File.ReadAllText(path)); foreach (var assertion in document.RootElement.GetProperty("Checks").EnumerateObject()) checks[prefix + "." + assertion.Name] = assertion.Value.GetBoolean(); } passed &= checks.Count > 0 && checks.Values.All(value => value); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, "client-shutdown-self-test.json"), JsonSerializer.Serialize(new { passed, checks, flow_exit_code = flow, ui_exit_code = ui, child_handoff_exit_code = handoff, network_modified = false, installed_client_modified = false }, new JsonSerializerOptions { WriteIndented = true })); return passed ? 0 : 85; } private static async Task RunFlowAsync() { var checks = new Dictionary(); try { var current = false; var confirms = 0; var downloads = 0; var launches = 0; var accepted = await ClientUpdateFlow.RunAsync(() => current, () => { confirms++; return true; }, () => { downloads++; return Task.FromResult("fixture.exe"); }, _ => launches++); checks["closed_before_offer_has_no_side_effect"] = !accepted && confirms == 0 && downloads == 0 && launches == 0; current = true; confirms = downloads = launches = 0; accepted = await ClientUpdateFlow.RunAsync(() => current, () => { confirms++; current = false; return true; }, () => { downloads++; return Task.FromResult("fixture.exe"); }, _ => launches++); checks["shutdown_during_confirmation_never_opens_progress_or_installer"] = !accepted && confirms == 1 && downloads == 0 && launches == 0; current = true; confirms = downloads = launches = 0; var downloaded = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var pending = ClientUpdateFlow.RunAsync(() => current, () => { confirms++; return true; }, () => { downloads++; return downloaded.Task; }, _ => launches++); current = false; downloaded.SetResult("fixture.exe"); accepted = await pending; checks["shutdown_during_download_never_launches_installer"] = !accepted && confirms == 1 && downloads == 1 && launches == 0; current = true; confirms = downloads = launches = 0; accepted = await ClientUpdateFlow.RunAsync(() => current, () => false, () => { downloads++; return Task.FromResult("fixture.exe"); }, _ => launches++); checks["declined_offer_retains_client"] = !accepted && downloads == 0 && launches == 0; accepted = await ClientUpdateFlow.RunAsync(() => current, () => true, () => Task.FromResult("verified-fixture.exe"), path => { if (path == "verified-fixture.exe") launches++; }); checks["active_accepted_update_launches_exact_download_once"] = accepted && launches == 1; } catch (Exception error) { checks["unexpected_error"] = false; Console.Error.WriteLine(error); } File.WriteAllText(Path.Combine(AppContext.BaseDirectory, "client-exit-self-test.json"), JsonSerializer.Serialize(new { Passed = checks.Values.All(x => x), Checks = checks }, new JsonSerializerOptions { WriteIndented = true })); return checks.Values.All(x => x) ? 0 : 85; } }