using System.Net; using System.Text; using System.Text.Json; using Febe.App; using Febe.Desktop.Core; using Febe.Tests.Support; namespace Febe.Tests.Desktop.Support; /// /// The Windows composition on Linux: the shared with the real /// (token of this start, 127.0.0.1 on a port chosen at start-up, start-up checks, hosting of S2, native services) and /// a fake of the Windows-only part (). Data and library in a temporary folder; the /// interface is asked like the main window asks it, with the token and the Origin of the interface. /// internal sealed class DesktopTestBed : IAsyncDisposable { private readonly bool _ownsTemp; private DesktopTestBed(TempFolder temp, bool ownsTemp, FakeDesktopShell shell, MemoryLocationStore locations, DesktopPlatform platform, FebeApp app) { Temp = temp; _ownsTemp = ownsTemp; Shell = shell; Locations = locations; Platform = platform; App = app; Client = new HttpClient(new HttpClientHandler { UseCookies = false, AllowAutoRedirect = false }) { BaseAddress = app.Address, Timeout = TimeSpan.FromSeconds(30) }; } public TempFolder Temp { get; } public FakeDesktopShell Shell { get; } public MemoryLocationStore Locations { get; } public DesktopPlatform Platform { get; } public FebeApp App { get; } public HttpClient Client { get; } public DesktopFolders Folders => new(Temp.Combine("AppData", "Local"), Temp.Combine("Immagini")); /// The fake window «Browser di Febe» (null with a configuration problem or with Edge). public FakeBrowser? Browser => App.Browser as FakeBrowser; public string Origin => $"{App.Address.Scheme}://{App.Address.Authority}"; /// The command line of Febe.exe. /// The fake Windows-only part. /// The folder of «AppData» and «Immagini»; null: a new one, deleted with the bed. /// The registry values of the Data folder. /// Options the command line cannot give (the debugging port of Edge). public static async Task StartAsync(string[]? args = null, FakeDesktopShell? shell = null, TempFolder? temp = null, MemoryLocationStore? locations = null, Func? adjust = null) { var ownsTemp = temp is null; temp ??= new TempFolder("desktop"); shell ??= new FakeDesktopShell(); locations ??= new MemoryLocationStore(); if (!DesktopCommandLine.TryParse(args ?? [], out var options, out var problem)) { throw new ArgumentException(problem); } options = adjust?.Invoke(options) ?? options; var folders = new DesktopFolders(temp.Combine("AppData", "Local"), temp.Combine("Immagini")); var platform = new DesktopPlatform(options, shell, locations, dataFolderFixed: options.DataFolder is not null); var app = await FebeApp.StartAsync(options.AppOptions(options.DataFolder ?? folders.DefaultDataFolder, folders), platform, new FebeAppHooks { ShutdownTimeout = TimeSpan.FromSeconds(1) }); return new DesktopTestBed(temp, ownsTemp, shell, locations, platform, app); } /// A request as the main window sends it: the token (here as the header the cookie stands for) and the Origin. public async Task SendAsync(HttpMethod method, string path, string? json = null, bool token = true) { using var request = new HttpRequestMessage(method, path); if (token) { request.Headers.TryAddWithoutValidation(Febe.Web.Security.AccessToken.HeaderName, Platform.AccessToken); } request.Headers.TryAddWithoutValidation("Origin", Origin); if (json is not null) { request.Content = new StringContent(json, Encoding.UTF8, "application/json"); } return await Client.SendAsync(request); } public async Task JsonAsync(HttpMethod method, string path, string? json = null) { using var response = await SendAsync(method, path, json); var text = await response.Content.ReadAsStringAsync(); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, $"{path}: {text}"); return JsonDocument.Parse(text).RootElement.Clone(); } public static async Task WaitUntilAsync(Func condition, TimeSpan timeout, string what) { var deadline = DateTime.UtcNow + timeout; while (!condition()) { if (DateTime.UtcNow > deadline) { Assert.Fail($"Tempo scaduto: {what}."); } await Task.Delay(50); } } /// Stops the composition; the folders stay for a second start (dispose at the end). public async ValueTask StopAsync() { Client.Dispose(); await App.DisposeAsync(); } /// Stops; deletes the temporary folder only when the bed created it (a shared one is the caller's). public async ValueTask DisposeAsync() { await StopAsync(); if (_ownsTemp) { Temp.Dispose(); } } }