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;
///
/// 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.
///
[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(360_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 server.Running.WaitAsync(TimeSpan.FromSeconds(60), 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 bed.JsonAsync(HttpMethod.Post, $"/api/accounts/{marta.Id}/check-now");
if (check.GetProperty("status").GetString() == "needsConfirmation")
{
check = await bed.JsonAsync(HttpMethod.Post, $"/api/accounts/{marta.Id}/check-now", $$"""{"token":"{{check.GetProperty("token").GetString()}}"}""");
}
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 ServerTestBed.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));
// 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 ServerTestBed.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); if that session already
// ended, in the next one, after the minimum pause (20 minutes at 10×: two 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 ServerTestBed.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(200),
"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 ServerTestBed.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 ServerTestBed.WaitUntilAsync(() => Task.FromResult(!server.Browser.UserIsDriving), TimeSpan.FromSeconds(10), "la vista chiusa libera il browser");
}
[TestMethod]
[Timeout(120_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 server.Running.WaitAsync(TimeSpan.FromSeconds(60), 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);
Assert.IsTrue(server.Engine!.Services.Runtime.IsRunning || await WaitRunningAsync(server), "the engine loop runs again (and waits for a connection)");
// «Ho finito» without having logged in: Febe says it does not find the session.
await connection.BeginUserDrivingAsync(openLogin: false, ct);
Assert.IsTrue(server.Browser.UserIsDriving);
var finish = await connection.FinishAsync(ct);
Assert.IsFalse(finish.Connected);
StringAssert.Contains(finish.Message, "non trova la sessione");
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(120_000, CooperativeCancellation = true)]
public async Task TwoViewsOpenedAtTheSameMomentLeaveOneViewWithItsFramesAndOneLease()
{
var ct = TestContext.CancellationToken;
await using var bed = await ServerTestBed.StartAsync();
var server = bed.Server;
await server.Running.WaitAsync(TimeSpan.FromSeconds(60), ct);
var host = server.RemoteView!;
var gate = server.Browser!.UserDriving;
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.
await ServerTestBed.WaitUntilAsync(
async () => await Febe.Browser.Cdp.CdpPageExtensions.GetMainFrameUrlAsync(server.Browser.Cdp, ct) == "about:blank",
TimeSpan.FromSeconds(20),
"la pagina di riposo");
// Two tabs restored together, or the phone and the PC at once: the newest wins, the other is ended.
var views = await Task.WhenAll(Task.Run(() => host.OpenAsync(ct), ct), Task.Run(() => host.OpenAsync(ct), ct));
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.");
// 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.");
}
}
private static async Task FrameArrivesAsync(IRemoteViewConnection view, CancellationToken ct)
{
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(ct);
timeout.CancelAfter(TimeSpan.FromSeconds(15));
try
{
await foreach (var output in view.ReadAsync(timeout.Token))
{
if (output is RemoteViewFrame)
{
return true;
}
}
}
catch (OperationCanceledException) when (!ct.IsCancellationRequested)
{
}
return false;
}
private static async Task WaitRunningAsync(Febe.Server.FebeServer server)
{
for (var i = 0; i < 40 && !server.Engine!.Services.Runtime.IsRunning; i++)
{
await Task.Delay(100);
}
return server.Engine!.Services.Runtime.IsRunning;
}
private static async Task> 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 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);
}
}