Files
Febe/tests/Febe.Tests/Server/ServerEndToEndTests.cs
T
Alby96andClaude Opus 5.5 4dc3a83a51
verifica / controlli (push) Successful in 22s
verifica / prove (push) Successful in 23m56s
verifica / installatore (push) Successful in 2m6s
verifica / immagine (push) In progress
Merge remote-tracking branch 'origin/main' into correzione/prova-da-capo-a-fondo
Revisione: la causa è l'orologio che somma le attese, e «Controlla ora» si chiede
di nuovo prima di aspettare la verifica, perché a sessione finita la pausa può
arrivare a 90 minuti.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 17:54:28 +01:00

440 lines
22 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Net;
using System.Net.WebSockets;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using Febe.Core.Domain;
using Febe.Core.Library;
using Febe.Core.Storage;
using Febe.SitoDiProva;
using Febe.Tests.Alerts.Support;
using Febe.Tests.Server.Support;
using Febe.Tests.Support;
using Febe.Web.Connection;
namespace Febe.Tests.Server;
/// <summary>
/// The real composition of the server (milestone M5) against the local test site, with Xvfb and a real Chromium: the
/// password, the connection through the login page, accounts added through the API, «Controlla ora», a real session of
/// the engine with the Governor's permits, files and thumbnails in the library, the stop on the verification page and
/// the remote view. Never Instagram: the browser only sees 127.0.0.1.
/// </summary>
[TestClass]
[TestCategory(SyntheticSite.Category)]
[DoNotParallelize]
public sealed partial class ServerEndToEndTests
{
public TestContext TestContext { get; set; } = null!;
[TestInitialize]
public void RequireBrowser()
{
if (ServerTestBed.UnavailableReason() is { } reason)
{
Assert.Inconclusive(reason);
}
}
[TestMethod]
[Timeout(900_000, CooperativeCancellation = true)]
public async Task TheRealServerCollectsFromTheTestSiteStopsAtTheVerificationAndShowsTheBrowser()
{
var ct = TestContext.CancellationToken;
var telegram = new FakeTelegramHandler();
var disk = new FakeFreeSpaceProbe();
await using var bed = await ServerTestBed.StartAsync(telegram: telegram, freeSpace: disk);
var server = bed.Server;
Assert.IsEmpty(server.Problems);
await bed.WaitRunningAsync(ct);
var connection = server.Connection!;
// The health check answers without a session; everything else wants the password.
using (var health = await bed.Client.GetAsync("/salute", ct))
{
Assert.AreEqual(HttpStatusCode.OK, health.StatusCode);
Assert.AreEqual("""{"stato":"ok"}""", await health.Content.ReadAsStringAsync(ct));
}
using (var anonymous = await bed.Client.GetAsync("/api/accounts", ct))
{
Assert.AreEqual(HttpStatusCode.Unauthorized, anonymous.StatusCode);
}
await bed.LoginAsync();
// A fresh profile: not connected, the engine waits.
Assert.AreEqual(ConnectionState.NotConnected, (await connection.GetStatusAsync(ct)).State);
// Accounts added through the API, as from the «Aggiungi account» dialog.
var added = await bed.JsonAsync(HttpMethod.Post, "/api/accounts", """{"usernames":["marta.prova","luca.prova"],"start":"everything"}""");
Assert.AreEqual(2, added.GetProperty("added").GetInt32());
var marta = await server.Database.Accounts.FindByUsernameAsync("marta.prova", ct) ?? throw new AssertFailedException("marta.prova manca");
// «Accedi a Instagram» opens the login page (here: the test site, whose first visit starts its session);
// meanwhile Alberto is driving and the engine is still.
await connection.BeginUserDrivingAsync(openLogin: true, ct);
var driving = await connection.GetStatusAsync(ct);
Assert.IsTrue(driving.UserIsDriving);
Assert.IsTrue(server.Browser!.UserIsDriving);
StringAssert.StartsWith(driving.CurrentUrl, bed.Site.SiteUrl, "the login page of the configured site, never Instagram in tests");
var finish = await connection.FinishAsync(ct);
Assert.IsTrue(finish.Connected, finish.Message);
Assert.IsFalse(server.Browser.UserIsDriving, "«Ho finito» ends the driving");
Assert.IsNotNull(await server.Database.Settings.GetInt64Async(Febe.App.BrowserConnectionService.FirstConnectionKey, ct), "first connection recorded");
Assert.AreEqual(ConnectionState.Connected, (await connection.GetStatusAsync(ct)).State);
// «Controlla ora».
var check = await CheckNowAsync(bed, marta.Id);
Assert.AreEqual("queued", check.GetProperty("status").GetString(), check.GetProperty("message").GetString());
// The engine runs a session: every action toward the site has its Governor's permit in action_log.
await bed.WaitUntilAsync(
async () => (await server.Database.ActionLog.ListAsync(DateTimeOffset.UnixEpoch, bed.Clock.Now.AddDays(1), ct)).Any(a => a.AccountId == marta.Id),
TimeSpan.FromSeconds(90),
"un'azione del motore su marta.prova nel registro delle azioni");
var actions = await server.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");
Assert.IsNotNull(await server.Database.Sessions.GetLastAsync(ct));
// The session is the one Alberto asked for, and starts from the account he chose: not a session of the engine's
// own that began at «Ho finito», before his request.
if (actions.First(a => a.AccountId is not null).AccountId != marta.Id)
{
Assert.Fail($"La prima azione della sessione non è su marta.prova, chiesta con «Controlla ora».\n{await bed.DescribeAsync()}");
}
// Files arrive in the library with the names of the scheme, and their thumbnails.
var library = bed.Library;
var thumbnails = bed.Thumbnails;
List<(Entry Entry, Media Media)> stored = [];
await bed.WaitUntilAsync(
async () =>
{
stored = await StoredAsync(server, marta.Id, ct);
return stored.Count >= 3;
},
TimeSpan.FromSeconds(90),
"tre file di marta.prova scaricati");
foreach (var (entry, 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);
Assert.MatchesRegex(NamePattern(), media.RelativePath!, "naming scheme {tipo}/{anno}/{data}_{ora}_{codice}_{n}");
if (media.Kind == MediaKind.Photo)
{
Assert.IsTrue(thumbnails.Exists(media.Sha256!), $"thumbnail of {media.RelativePath}");
}
}
Assert.IsTrue(File.Exists(Path.Combine(library.AccountFolder(marta), AccountJournal.FileName)), "_febe.jsonl");
var recipes = (await server.Database.Events.ListAsync(new EventQuery(), ct)).Where(e => e.Code.StartsWith("ricetta.", StringComparison.Ordinal)).ToList();
Assert.IsNotEmpty(recipes, "one event line per recipe run");
TestContext.WriteLine($"Ricette: {string.Join(", ", recipes.Select(r => r.Code).Distinct())}; file di marta.prova: {stored.Count}.");
// The site asks for a verification: at its next action toward the site the engine stops and the banner appears
// in /api/engine. Usually still in the first session (two accounts to read in full). Were that session over, or
// ending right now, the next one would come 20 to 90 minutes later (4 to 18 real ones at 5×): «Controlla ora» again
// brings it to the minimum pause, four real minutes.
// Alerts on Telegram (M8): token and chat set from Impostazioni ▸ Notifiche; the composition sends through the fake.
const string token = "4839574812:AAF-gettoneDelServerDiProva_123";
var events = """{"verification":true,"disconnection":true,"throttle":true,"backfillDone":true,"diskLow":true,"unknownFormat":true}""";
await bed.JsonAsync(HttpMethod.Put, "/api/settings/notifications", $$"""{"events":{{events}},"windows":false,"telegramChatId":"42","telegramToken":"{{token}}","removeTelegramToken":false}""");
var servedBefore = bed.Site.Control.CdnFilesServed;
bed.Site.Control.Set(StopMode.Challenge);
await CheckNowAsync(bed, marta.Id);
await bed.WaitUntilAsync(
async () =>
{
var engine = await bed.JsonAsync(HttpMethod.Get, "/api/engine");
var snapshot = engine.GetProperty("snapshot");
return snapshot.GetProperty("mode").GetString() == "StoppedForVerification" && snapshot.GetProperty("banner").ValueKind == JsonValueKind.Object;
},
TimeSpan.FromSeconds(360),
"la striscia di arresto per la verifica in /api/engine");
Assert.AreEqual(ConnectionState.VerificationRequired, (await connection.GetStatusAsync(ct)).State);
TestContext.WriteLine($"CDN: {servedBefore} file prima del fermo, {bed.Site.Control.CdnFilesServed} adesso.");
// The same stop reached Telegram, with the Italian text, and the «Avvisi» list of Attività.
await bed.WaitUntilAsync(
() => Task.FromResult(telegram.Requests.Any(r => r.Text.StartsWith("Febe · Instagram chiede una verifica\n", StringComparison.Ordinal))),
TimeSpan.FromSeconds(20),
"il messaggio Telegram della verifica");
var message = telegram.Requests.First(r => r.Text.StartsWith("Febe · Instagram chiede una verifica", StringComparison.Ordinal));
Assert.AreEqual($"/bot{token}/sendMessage", message.Uri.AbsolutePath);
Assert.AreEqual("42", message.ChatId);
StringAssert.Contains(message.Text, "premi «Riprendi»");
var alerts = await bed.JsonAsync(HttpMethod.Get, "/api/alerts");
Assert.IsTrue(alerts.GetProperty("items").EnumerateArray().Any(i => i.GetProperty("kind").GetString() == "verification"));
foreach (var file in Directory.EnumerateFiles(server.Data!.Logs))
{
var log = await ReadSharedAsync(file, ct);
Assert.DoesNotContain(token, log, file);
Assert.DoesNotContain("api.telegram.org", log, file);
}
// The remote view: meta first, then JPEG frames; Alberto is driving while it is open.
using (var socket = await bed.OpenRemoteViewAsync(ct))
{
var (metaType, meta) = await ReceiveAsync(socket, ct);
Assert.AreEqual(WebSocketMessageType.Text, metaType);
using var metaJson = JsonDocument.Parse(meta);
Assert.AreEqual("meta", metaJson.RootElement.GetProperty("type").GetString());
Assert.IsGreaterThan(0, metaJson.RootElement.GetProperty("width").GetInt32());
Assert.IsTrue(server.Browser.UserIsDriving, "the engine is still while the view is open");
byte[]? jpeg = null;
for (var i = 0; i < 20 && jpeg is null; i++)
{
// A gesture makes the page repaint, so a frame comes even on a still page.
await SendAsync(socket, """{"type":"mouse","action":"move","x":0.5,"y":0.5}""", ct);
var (type, data) = await ReceiveAsync(socket, ct);
if (type == WebSocketMessageType.Binary)
{
jpeg = data;
}
}
Assert.IsNotNull(jpeg, "a JPEG frame");
Assert.AreEqual(0xFF, jpeg[0]);
Assert.AreEqual(0xD8, jpeg[1]);
await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "fine", ct);
}
await bed.WaitUntilAsync(() => Task.FromResult(!server.Browser.UserIsDriving), TimeSpan.FromSeconds(10), "la vista chiusa libera il browser");
}
[TestMethod]
[Timeout(360_000, CooperativeCancellation = true)]
public async Task FinishWithoutSessionSaysSoAndDisconnectDeletesTheProfile()
{
var ct = TestContext.CancellationToken;
await using var bed = await ServerTestBed.StartAsync(autoLogin: true);
var server = bed.Server;
await bed.WaitRunningAsync(ct);
var connection = server.Connection!;
Assert.AreEqual(ConnectionState.Connected, (await connection.GetStatusAsync(ct)).State, "the test site's automatic login");
// «Scollega»: engine stopped, profile deleted, browser restarted empty.
var marker = Path.Combine(server.Browser!.ProfileDirectory, "segno-della-prova");
await File.WriteAllTextAsync(marker, "x", ct);
await connection.DisconnectAsync(ct);
Assert.IsFalse(File.Exists(marker), "the profile folder was deleted");
Assert.IsTrue(server.Browser.IsAvailable, "the browser runs again, with an empty profile");
Assert.AreEqual(ConnectionState.NotConnected, (await connection.GetStatusAsync(ct)).State);
Assert.IsFalse(server.Browser.UserIsDriving);
await bed.WaitUntilAsync(
() => Task.FromResult(server.Engine!.Services.Runtime.IsRunning),
TimeSpan.FromSeconds(60),
"il motore di nuovo in funzione, in attesa del collegamento");
// «Usa il browser» on the rest page, not connected (issue #52): never the white page, the login page. (The test
// site sets its session on any visit, so «Ho finito» without a login is proved with the fake browser of
// BrowserStartPageTests.)
await connection.BeginUserDrivingAsync(openLogin: false, ct);
Assert.IsTrue(server.Browser.UserIsDriving);
var opened = (await connection.GetStatusAsync(ct)).CurrentUrl;
Assert.IsNotNull(opened);
StringAssert.Contains(opened, "/benvenuto", "the login page of the test site");
await connection.CancelAsync(ct);
Assert.IsFalse(server.Browser.UserIsDriving);
// «Annulla» ends the driving and leaves the browser on the rest page.
await connection.BeginUserDrivingAsync(openLogin: true, ct);
await connection.CancelAsync(ct);
Assert.IsFalse(server.Browser.UserIsDriving);
Assert.AreEqual("about:blank", (await connection.GetStatusAsync(ct)).CurrentUrl);
// The debugging port listens on 127.0.0.1 only.
Assert.IsTrue(Febe.Browser.Hosting.LoopbackPortCheck.IsLoopbackOnly(bed.Options.DebugPort, out _));
}
[TestMethod]
[Timeout(420_000, CooperativeCancellation = true)]
public async Task TwoViewsOpenedAtTheSameMomentLeaveOneViewWithItsFramesAndOneLease()
{
var ct = TestContext.CancellationToken;
await using var bed = await ServerTestBed.StartAsync();
var server = bed.Server;
await bed.WaitRunningAsync(ct);
var host = server.RemoteView!;
var gate = server.Browser!.UserDriving;
var released = 0;
void OnChanged(object? sender, bool driving)
{
if (!driving)
{
Interlocked.Increment(ref released);
}
}
for (var round = 0; round < 5; round++)
{
// From the rest page: where the browser starts, and where the engine puts it once nobody is at the browser.
// Page.getFrameTree is answered by the page itself, which DevTools holds back while a navigation is pending:
// «about:blank» here means the engine's rest has committed.
await bed.WaitUntilAsync(
async () => await Febe.Browser.Cdp.CdpPageExtensions.GetMainFrameUrlAsync(server.Browser.Cdp, ct) == "about:blank",
TimeSpan.FromSeconds(60),
"la pagina di riposo");
// Two tabs restored together, or the phone and the PC at once: the newest wins, the other is ended — and the
// engine never sees nobody at the browser in between (it would put the page to rest under the newer view).
Volatile.Write(ref released, 0);
gate.Changed += OnChanged;
IRemoteViewConnection[] views;
try
{
views = await Task.WhenAll(Task.Run(() => host.OpenAsync(ct), ct), Task.Run(() => host.OpenAsync(ct), ct));
}
finally
{
gate.Changed -= OnChanged;
}
var open = views.Where(v => v.Ended is null).ToList();
Assert.HasCount(1, open, $"Round {round}: one view stays open.");
Assert.AreEqual(RemoteViewEnd.Replaced, views.Single(v => v.Ended is not null).Ended);
Assert.AreEqual(1, gate.Leases, $"Round {round}: only the open view holds the browser.");
Assert.AreEqual(0, Volatile.Read(ref released), $"Round {round}: the browser was never left without a driver while one view replaced the other.");
// The page of the replaced view closes its socket: the open view still gets its frames.
await views.Single(v => v.Ended is not null).DisposeAsync();
await Febe.Browser.Cdp.CdpPageExtensions.NavigateAsync(server.Browser.Cdp, $"{bed.Site.SiteUrl}/?giro={round}", ct);
Assert.IsTrue(await FrameArrivesAsync(open[0], ct), $"Round {round}: frames go on after the other view closed.");
await open[0].DisposeAsync();
Assert.AreEqual(0, gate.Leases, $"Round {round}: no view, no lease.");
}
}
[TestMethod]
[Timeout(480_000, CooperativeCancellation = true)]
public async Task AViewOpenedWhileThePageIsNavigatingOpensAndShowsItsFrames()
{
var ct = TestContext.CancellationToken;
await using var bed = await ServerTestBed.StartAsync();
var server = bed.Server;
await bed.WaitRunningAsync(ct);
var host = server.RemoteView!;
var browser = server.Browser!;
for (var round = 0; round < 6; round++)
{
// From the rest page, as in TwoViews…: the previous round ends on a site page, so «about:blank» here means the
// engine's rest after the last view has committed and nothing else navigates.
await bed.WaitUntilAsync(
async () => await Febe.Browser.Cdp.CdpPageExtensions.GetMainFrameUrlAsync(browser.Cdp, ct) == "about:blank",
TimeSpan.FromSeconds(60),
"la pagina di riposo");
// Even rounds: «Accedi a Instagram» on the Collegamento page navigates to the login page while the same page
// opens the view. Odd rounds: the rest page, as the engine puts it after a session or after the last view.
// A guard of these pairs, not of the retry: with the navigation already sent, the view's first look at the
// page (Page.getFrameTree) is answered only once it has committed, so the start comes after the commit (seen
// with Chrome 155). RemoteViewDuringNavigationTests makes the start meet the refusal on purpose.
var navigation = round % 2 == 0
? Febe.Browser.Cdp.CdpPageExtensions.NavigateAsync(browser.Cdp, $"{bed.Site.SiteUrl}/?vista={round}", ct)
: browser.RestAsync(ct);
var view = await host.OpenAsync(ct);
try
{
var navigated = await navigation;
Assert.IsTrue(navigated.Succeeded, $"Round {round}: {navigated.ErrorText}");
Assert.IsNull(view.Ended, $"Round {round}: the view is open.");
Assert.AreEqual(1, browser.UserDriving.Leases, $"Round {round}: only the view holds the browser.");
// A new page paints: its frames reach the view.
await Febe.Browser.Cdp.CdpPageExtensions.NavigateAsync(browser.Cdp, $"{bed.Site.SiteUrl}/?giro={round}", ct);
Assert.IsTrue(await FrameArrivesAsync(view, ct), $"Round {round}: frames of the page.");
}
finally
{
await view.DisposeAsync();
}
Assert.AreEqual(0, browser.UserDriving.Leases, $"Round {round}: no view, no lease.");
}
}
private static async Task<bool> FrameArrivesAsync(IRemoteViewConnection view, CancellationToken ct)
{
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(ct);
timeout.CancelAfter(TimeSpan.FromSeconds(60)); // only a hang guard: it returns at the first frame
try
{
await foreach (var output in view.ReadAsync(timeout.Token))
{
if (output is RemoteViewFrame)
{
return true;
}
}
}
catch (OperationCanceledException) when (!ct.IsCancellationRequested)
{
}
return false;
}
/// <summary>«Controlla ora», confirmed when Febe asks for it.</summary>
private static async Task<JsonElement> CheckNowAsync(ServerTestBed bed, long accountId)
{
var path = $"/api/accounts/{accountId}/check-now";
var check = await bed.JsonAsync(HttpMethod.Post, path);
return check.GetProperty("status").GetString() == "needsConfirmation"
? await bed.JsonAsync(HttpMethod.Post, path, $$"""{"token":"{{check.GetProperty("token").GetString()}}"}""")
: check;
}
private static async Task<List<(Entry Entry, Media Media)>> StoredAsync(Febe.Server.FebeServer server, long accountId, CancellationToken ct)
{
var list = new List<(Entry, Media)>();
foreach (var entry in await server.Database.Entries.ListByAccountAsync(accountId, null, ct))
{
foreach (var media in await server.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((entry, media));
}
}
}
return list;
}
private static Task SendAsync(ClientWebSocket socket, string json, CancellationToken ct) =>
socket.SendAsync(Encoding.UTF8.GetBytes(json), WebSocketMessageType.Text, endOfMessage: true, ct);
private static async Task<(WebSocketMessageType Type, byte[] Data)> ReceiveAsync(ClientWebSocket socket, CancellationToken ct)
{
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(ct);
timeout.CancelAfter(TimeSpan.FromSeconds(10));
using var buffer = new MemoryStream();
var chunk = new byte[64 * 1024];
WebSocketReceiveResult result;
do
{
result = await socket.ReceiveAsync(chunk, timeout.Token);
buffer.Write(chunk, 0, result.Count);
}
while (!result.EndOfMessage);
return (result.MessageType, buffer.ToArray());
}
[GeneratedRegex(@"^(post|storie|evidenza/[^/]+|taggati)/(\d{4}/)?\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}_[^_/]+_\d+\.(jpg|mp4)$")]
private static partial Regex NamePattern();
private static async Task<string> ReadSharedAsync(string path, CancellationToken ct)
{
await using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using var reader = new StreamReader(stream);
return await reader.ReadToEndAsync(ct);
}
}