using System.Runtime.CompilerServices;
using System.Text.Json;
using Febe.Core.Abstractions;
using Febe.Core.Domain;
using Febe.Core.Engine;
using Febe.Tests.Support;
using Febe.Tests.Web.Part2Support;
using Febe.Tests.Web.Support;
using Febe.Web.Connection;
using Febe.Web.Engine;
using Microsoft.Extensions.DependencyInjection;
namespace Febe.Tests.Web;
///
/// The link to Instagram in the sidebar (/api/engine, «connection»): it comes from the session cookie, as the
/// engine's mode or the connection says, never from whether the account «Tu» is already known (H10: its name may come
/// much later, and meanwhile the sidebar says «Collegato» with «Nome non ancora noto»).
///
[TestClass]
public sealed class EngineConnectionStateTests
{
[TestMethod]
public async Task TheSidebarSaysConnectedFromTheSessionCookieEvenBeforeTuIsKnown()
{
var connection = new RecordingConnection { Status = new ConnectionStatus(ConnectionState.Connected, null, null, false, null) };
await using var server = await Part2.StartAsync(services: s =>
{
s.AddSingleton(sp => new FixedModeMonitor(sp.GetRequiredService()));
s.AddSingleton(connection);
});
var monitor = (FixedModeMonitor)server.App.Services.GetRequiredService();
async Task LinkAsync(EngineMode mode)
{
monitor.Mode = mode;
return (await server.GetJsonAsync("/api/engine")).GetProperty("connection");
}
// Collecting (or about to), with the cookie and without «Tu»: connected, no name yet.
foreach (var mode in new[] { EngineMode.Starting, EngineMode.InSession, EngineMode.Resting, EngineMode.OutsideWindow, EngineMode.Paused, EngineMode.Throttled, EngineMode.UserDriving })
{
var link = await LinkAsync(mode);
Assert.AreEqual("connected", link.Str("state"), mode.ToString());
Assert.AreEqual(JsonValueKind.Null, link.GetProperty("username").ValueKind, mode.ToString());
}
Assert.AreEqual("notConnected", (await LinkAsync(EngineMode.NotConnected)).Str("state"));
Assert.AreEqual("disconnected", (await LinkAsync(EngineMode.Disconnected)).Str("state"));
Assert.AreEqual("verification", (await LinkAsync(EngineMode.StoppedForVerification)).Str("state"));
// The engine loop does not run: the connection itself is asked.
Assert.AreEqual("connected", (await LinkAsync(EngineMode.Off)).Str("state"));
connection.Status = new ConnectionStatus(ConnectionState.NotConnected, null, null, false, null);
Assert.AreEqual("notConnected", (await LinkAsync(EngineMode.Off)).Str("state"));
// Once «Tu» is known, its name comes along.
await server.Db.Accounts.AddAsync(Make.Account("tuo.profilo", mine: true));
var named = await LinkAsync(EngineMode.Resting);
Assert.AreEqual("connected", named.Str("state"));
Assert.AreEqual("tuo.profilo", named.Str("username"));
Assert.AreEqual(JsonValueKind.Null, (await LinkAsync(EngineMode.NotConnected)).GetProperty("username").ValueKind, "Not connected: no name.");
}
[TestMethod]
public async Task WithoutEngineAndBrowserTheSidebarSaysNotConnectedEvenIfTuIsKnown()
{
await using var server = await Part2.StartAsync();
await server.Db.Accounts.AddAsync(Make.Account("tuo.profilo", mine: true));
var engine = await server.GetJsonAsync("/api/engine");
Assert.AreEqual("Off", engine.GetProperty("snapshot").Str("mode"));
Assert.AreEqual("notConnected", engine.GetProperty("connection").Str("state"), "No engine has looked at the cookie and the browser is not there.");
}
/// An engine that reports the mode the test sets.
private sealed class FixedModeMonitor(IClock clock) : IEngineMonitor
{
public EngineMode Mode { get; set; } = EngineMode.Resting;
public Task GetSnapshotAsync(CancellationToken ct = default) =>
Task.FromResult(SnapshotParts.Build(clock, Mode, "Prova", [], "Prova", CautionProfile.Prudent));
public async IAsyncEnumerable SubscribeAsync([EnumeratorCancellation] CancellationToken ct = default)
{
await Task.Delay(Timeout.Infinite, ct);
yield break;
}
}
}