Sito di prova, cartelle, thread dell'interfaccia, DesktopPlatform con DesktopShell, orologio veloce e richieste all'interfaccia con il gettone passano dalla prova della sessione di raccolta a DesktopBed, così la prova del collegamento che segue usa la stessa composizione senza copiarla. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
151 lines
5.8 KiB
C#
151 lines
5.8 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
/// The shared composition (<see cref="FebeApp"/>) as Febe.exe composes it — <see cref="DesktopPlatform"/>,
|
|
/// <c>DesktopShell</c>, 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.
|
|
/// </summary>
|
|
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; }
|
|
|
|
/// <summary>What Febe wrote in its log.</summary>
|
|
public LogLines Lines { get; }
|
|
|
|
public CompressedClock Clock { get; }
|
|
|
|
public DesktopPlatform Platform { get; }
|
|
|
|
public FebeApp App { get; }
|
|
|
|
/// <summary>The browser of Febe: WebView2 (Febe.exe never uses another one unless asked with <c>--browser edge</c>).</summary>
|
|
public WebView2Browser Browser => App.Browser as WebView2Browser ?? throw new AssertFailedException("Il browser della composizione non è WebView2.");
|
|
|
|
/// <param name="name">Prefix of the temporary folder.</param>
|
|
/// <param name="autoLogin"><c>--accesso-di-prova</c>: the composition opens the test site once at start-up.</param>
|
|
public static async Task<DesktopBed> 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<string> 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;
|
|
}
|
|
}
|
|
|
|
/// <summary>A request as the main window sends it; the answer must be 200 with JSON.</summary>
|
|
public async Task<JsonElement> 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();
|
|
}
|
|
|
|
/// <summary>Waits (real time) until <paramref name="condition"/> holds; fails with <paramref name="what"/> and the log otherwise.</summary>
|
|
public async Task WaitUntilAsync(Func<Task<bool>> 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);
|
|
}
|
|
}
|
|
|
|
/// <summary>Stops the composition (the browser of Febe closes on its UI thread), then the UI thread, folders, site.</summary>
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
_client.Dispose();
|
|
try
|
|
{
|
|
await App.DisposeAsync();
|
|
}
|
|
finally
|
|
{
|
|
await Ui.DisposeAsync();
|
|
Folder.Dispose();
|
|
await Site.DisposeAsync();
|
|
}
|
|
}
|
|
}
|