Registro di casa 192.168.30.23:3000 (HTTP) nel modello Unraid, in compose e nel rilascio con buildx; profilo seccomp fuori da /dati. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
131 lines
5.3 KiB
C#
131 lines
5.3 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
/// The Windows composition on Linux: the shared <see cref="FebeApp"/> with the real <see cref="DesktopPlatform"/>
|
|
/// (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 (<see cref="FakeDesktopShell"/>). 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.
|
|
/// </summary>
|
|
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"));
|
|
|
|
/// <summary>The fake window «Browser di Febe» (null with a configuration problem or with Edge).</summary>
|
|
public FakeBrowser? Browser => App.Browser as FakeBrowser;
|
|
|
|
public string Origin => $"{App.Address.Scheme}://{App.Address.Authority}";
|
|
|
|
/// <param name="args">The command line of Febe.exe.</param>
|
|
/// <param name="shell">The fake Windows-only part.</param>
|
|
/// <param name="temp">The folder of «AppData» and «Immagini»; null: a new one, deleted with the bed.</param>
|
|
/// <param name="locations">The registry values of the Data folder.</param>
|
|
/// <param name="adjust">Options the command line cannot give (the debugging port of Edge).</param>
|
|
public static async Task<DesktopTestBed> StartAsync(string[]? args = null, FakeDesktopShell? shell = null, TempFolder? temp = null, MemoryLocationStore? locations = null, Func<DesktopOptions, DesktopOptions>? 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);
|
|
}
|
|
|
|
/// <summary>A request as the main window sends it: the token (here as the header the cookie stands for) and the Origin.</summary>
|
|
public async Task<HttpResponseMessage> 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<JsonElement> 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<bool> 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);
|
|
}
|
|
}
|
|
|
|
/// <summary>Stops the composition; the folders stay for a second start (dispose <see cref="Temp"/> at the end).</summary>
|
|
public async ValueTask StopAsync()
|
|
{
|
|
Client.Dispose();
|
|
await App.DisposeAsync();
|
|
}
|
|
|
|
/// <summary>Stops; deletes the temporary folder only when the bed created it (a shared one is the caller's).</summary>
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
await StopAsync();
|
|
if (_ownsTemp)
|
|
{
|
|
Temp.Dispose();
|
|
}
|
|
}
|
|
}
|