using System.Net; using System.Net.Http; using System.Text; using System.Text.Json; using Febe.App; using Febe.Core.Abstractions; using Febe.Desktop; using Febe.Desktop.Browser; using Febe.Desktop.Core; using Febe.SitoDiProva; using Febe.Web.Native; namespace Febe.Tests.Windows.Support; /// /// The shared composition () as Febe.exe composes it — , /// DesktopShell, the window «Browser di Febe» on a WPF UI thread — against its own test site, in temporary /// folders, on a clock ten times faster and in a zone where it is noon. The interface is asked like the main window asks /// it: with the token of this start and the Origin of the interface. /// internal sealed class DesktopBed : IAsyncDisposable { private readonly HttpClient _client; private DesktopBed(TestSite site, TestFolder folder, UiThread ui, LogLines lines, CompressedClock clock, DesktopPlatform platform, FebeApp app) { Site = site; Folder = folder; Ui = ui; Lines = lines; Clock = clock; Platform = platform; App = app; _client = new HttpClient(new HttpClientHandler { UseCookies = false, AllowAutoRedirect = false, UseProxy = false }) { BaseAddress = app.Address, Timeout = TimeSpan.FromSeconds(30), }; } public TestSite Site { get; } public TestFolder Folder { get; } public UiThread Ui { get; } /// What Febe wrote in its log. public LogLines Lines { get; } public CompressedClock Clock { get; } public DesktopPlatform Platform { get; } public FebeApp App { get; } /// The browser of Febe: WebView2 (Febe.exe never uses another one unless asked with --browser edge). public WebView2Browser Browser => App.Browser as WebView2Browser ?? throw new AssertFailedException("Il browser della composizione non è WebView2."); /// Prefix of the temporary folder. /// --accesso-di-prova: the composition opens the test site once at start-up. public static async Task StartAsync(string name, bool autoLogin, CancellationToken ct) { var site = await TestSite.StartAsync(new TestSiteOptions(), ct); var folder = new TestFolder(name); var ui = await UiThread.StartAsync(); try { var lines = new LogLines(); var data = folder.Combine("dati"); List arguments = [ "--dati", data, "--libreria", folder.Combine("libreria"), "--sito-di-prova", site.SiteUrl, "--cdn-di-prova", site.CdnUrl, "--prima-sessione", "1", ]; if (autoLogin) { arguments.Add("--accesso-di-prova"); } if (!DesktopCommandLine.TryParse([.. arguments], out var options, out var problem)) { throw new ArgumentException(problem); } var zone = TestZones.Noon(); var clock = new CompressedClock(10, zone); var shell = new DesktopShell(ui.Dispatcher, null, BrowserShell.Instance); var platform = new DesktopPlatform(options, shell, new MemoryLocations(), dataFolderFixed: true); var appOptions = options.AppOptions(data, new DesktopFolders(folder.Combine("AppData"), folder.Combine("Immagini"))) with { Zone = zone, ZoneId = zone.Id }; var hooks = new FebeAppHooks { Clock = clock, Random = new SeededRandom(20261020), Console = lines, ShutdownTimeout = TimeSpan.FromSeconds(1) }; var app = await FebeApp.StartAsync(appOptions, platform, hooks, ct); return new DesktopBed(site, folder, ui, lines, clock, platform, app); } catch { await ui.DisposeAsync(); folder.Dispose(); await site.DisposeAsync(); throw; } } /// A request as the main window sends it; the answer must be 200 with JSON. public async Task JsonAsync(HttpMethod method, string path, string? json, CancellationToken ct) { using var request = new HttpRequestMessage(method, path); request.Headers.TryAddWithoutValidation(Febe.Web.Security.AccessToken.HeaderName, Platform.AccessToken); request.Headers.TryAddWithoutValidation("Origin", $"{App.Address.Scheme}://{App.Address.Authority}"); if (json is not null) { request.Content = new StringContent(json, Encoding.UTF8, "application/json"); } using var response = await _client.SendAsync(request, ct); var text = await response.Content.ReadAsStringAsync(ct); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, $"{path}: {text}"); return JsonDocument.Parse(text).RootElement.Clone(); } /// Waits (real time) until holds; fails with and the log otherwise. public async Task WaitUntilAsync(Func> condition, TimeSpan timeout, string what, CancellationToken ct) { var deadline = DateTime.UtcNow + timeout; while (!await condition()) { if (DateTime.UtcNow > deadline) { Assert.Fail($"Tempo scaduto: {what}.\n{Lines.Text}"); } await Task.Delay(250, ct); } } /// Stops the composition (the browser of Febe closes on its UI thread), then the UI thread, folders, site. public async ValueTask DisposeAsync() { _client.Dispose(); try { await App.DisposeAsync(); } finally { await Ui.DisposeAsync(); Folder.Dispose(); await Site.DisposeAsync(); } } }