using System.Collections.Concurrent;
using System.Runtime.CompilerServices;
using System.Threading.Channels;
using Febe.Web.Connection;
namespace Febe.Tests.Server.Support;
/// A remote view without browser: the test writes what goes to the page and reads what comes back.
internal sealed class FakeRemoteViewHost : IRemoteViewHost
{
private readonly Lock _gate = new();
public List Opened { get; } = [];
public bool Unavailable { get; set; }
public Task OpenAsync(CancellationToken ct)
{
if (Unavailable)
{
throw new RemoteViewUnavailableException("Il browser di prova non c'รจ.");
}
var connection = new FakeRemoteViewConnection();
lock (_gate)
{
if (Opened.LastOrDefault() is { Disposed: false } previous)
{
previous.End(RemoteViewEnd.Replaced);
}
Opened.Add(connection);
}
return Task.FromResult(connection);
}
}
internal sealed class FakeRemoteViewConnection : IRemoteViewConnection
{
private readonly Channel _output = Channel.CreateUnbounded();
public ConcurrentQueue