Files
Febe/tests/Febe.Tests.Windows/WebView2CollectionTests.cs
T
Alby96andClaude Opus 5.5 66a3fc7db6 Prove per Windows: la composizione di Febe.exe preparata in un posto solo
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>
2026-10-20 10:53:48 +02:00

103 lines
5.1 KiB
C#

using System.Net.Http;
using Febe.App;
using Febe.Core.Domain;
using Febe.Desktop.Browser;
using Febe.Tests.Windows.Support;
namespace Febe.Tests.Windows;
/// <summary>
/// One real collection session of the shared composition (<see cref="FebeApp"/>) running on WebView2, as Febe.exe
/// composes it, against the local test site, with files arriving in a temporary library. The Linux suite has the
/// equivalent with Chromium (ServerEndToEndTests). Never Instagram: the browser only sees 127.0.0.1.
/// </summary>
[TestClass]
public sealed class WebView2CollectionTests
{
public TestContext TestContext { get; set; } = null!;
[TestInitialize]
public void RequireWebView2() => WebView2Runtime.Require();
[TestMethod]
[Timeout(420_000, CooperativeCancellation = true)]
public async Task ARealSessionOfTheCompositionRunsOnWebView2AndTheFilesArriveInTheLibrary()
{
var ct = TestContext.CancellationToken;
await using var bed = await DesktopBed.StartAsync("raccolta", autoLogin: true, ct);
var app = bed.App;
Assert.IsEmpty(app.Problems, string.Join("; ", app.Problems.Select(p => p.Title)));
// The browser starts from a thread of the composition (the path that failed on Alberto's PC), the pipeline and
// the engine follow.
await app.Running.WaitAsync(TimeSpan.FromSeconds(120), ct);
Assert.IsInstanceOfType<WebView2Browser>(app.Browser);
Assert.IsTrue(bed.Lines.Any("Browser di Febe pronto: WebView2 "), bed.Lines.Text);
Assert.IsFalse(bed.Lines.Any("non parte"), bed.Lines.Text);
Assert.AreEqual(Febe.Web.Connection.ConnectionState.Connected, (await app.Connection!.GetStatusAsync(ct)).State, "the test site's automatic login");
// Accounts and «Controlla ora» through the interface, as from the main window.
var added = await bed.JsonAsync(HttpMethod.Post, "/api/accounts", """{"usernames":["marta.prova","luca.prova"],"start":"everything"}""", ct);
Assert.AreEqual(2, added.GetProperty("added").GetInt32());
var marta = await app.Database.Accounts.FindByUsernameAsync("marta.prova", ct) ?? throw new AssertFailedException("marta.prova manca");
var check = await bed.JsonAsync(HttpMethod.Post, $"/api/accounts/{marta.Id}/check-now", null, ct);
if (check.GetProperty("status").GetString() == "needsConfirmation")
{
check = await bed.JsonAsync(HttpMethod.Post, $"/api/accounts/{marta.Id}/check-now", $$"""{"token":"{{check.GetProperty("token").GetString()}}"}""", ct);
}
Assert.AreEqual("queued", check.GetProperty("status").GetString(), check.GetProperty("message").GetString());
// A session of the engine acts on the site through WebView2: every action has its Governor's permit.
await bed.WaitUntilAsync(
async () => (await app.Database.ActionLog.ListAsync(DateTimeOffset.UnixEpoch, bed.Clock.Now.AddDays(1), ct)).Any(a => a.AccountId == marta.Id),
TimeSpan.FromSeconds(120),
"un'azione del motore su marta.prova nel registro delle azioni",
ct);
// Files arrive in the library, complete, downloaded through the browser from the test site's CDN.
var library = app.Library ?? throw new AssertFailedException("nessuna libreria");
List<Media> stored = [];
await bed.WaitUntilAsync(
async () =>
{
stored = await StoredAsync(app, marta.Id, ct);
return stored.Count >= 3;
},
TimeSpan.FromSeconds(180),
"tre file di marta.prova nella libreria",
ct);
foreach (var media in stored)
{
var path = library.FullPath(marta, media.RelativePath!);
Assert.IsTrue(File.Exists(path), path);
Assert.AreEqual(media.Bytes, new FileInfo(path).Length, path);
StringAssert.StartsWith(path, bed.Folder.Path, "inside the temporary library");
}
var actions = await app.Database.ActionLog.ListAsync(DateTimeOffset.UnixEpoch, bed.Clock.Now.AddDays(1), ct);
Assert.IsTrue(actions.All(a => a.SessionId is not null), "every counted action belongs to a session");
TestContext.WriteLine($"File di marta.prova: {stored.Count}; azioni: {actions.Count}; CDN: {bed.Site.Control.CdnFilesServed} file serviti.");
TestContext.WriteLine(bed.Lines.Text);
Assert.IsFalse(bed.Lines.Any("non parte"), "the browser never failed to start");
Assert.IsFalse(bed.Lines.Any("firma="), "no signed address in the log");
}
private static async Task<List<Media>> StoredAsync(FebeApp app, long accountId, CancellationToken ct)
{
var list = new List<Media>();
foreach (var entry in await app.Database.Entries.ListByAccountAsync(accountId, null, ct))
{
foreach (var media in await app.Database.Media.ListByEntryAsync(entry.Id, ct))
{
if (media.State == MediaState.Downloaded && media.RelativePath is not null && media.Sha256 is not null && entry.Kind != EntryKind.ProfilePicture)
{
list.Add(media);
}
}
}
return list;
}
}