Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
266 lines
13 KiB
C#
266 lines
13 KiB
C#
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.Server.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]
|
||
[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;
|
||
await using var bed = await ServerTestBed.StartAsync();
|
||
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).
|
||
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 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 _));
|
||
}
|
||
|
||
private static async Task<bool> 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<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();
|
||
}
|