Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
90 lines
2.4 KiB
C#
90 lines
2.4 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Threading.Channels;
|
|
using Febe.Web.Connection;
|
|
|
|
namespace Febe.Tests.Server.Support;
|
|
|
|
/// <summary>A remote view without browser: the test writes what goes to the page and reads what comes back.</summary>
|
|
internal sealed class FakeRemoteViewHost : IRemoteViewHost
|
|
{
|
|
private readonly Lock _gate = new();
|
|
|
|
public List<FakeRemoteViewConnection> Opened { get; } = [];
|
|
|
|
public bool Unavailable { get; set; }
|
|
|
|
public Task<IRemoteViewConnection> 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<IRemoteViewConnection>(connection);
|
|
}
|
|
}
|
|
|
|
internal sealed class FakeRemoteViewConnection : IRemoteViewConnection
|
|
{
|
|
private readonly Channel<RemoteViewOutput> _output = Channel.CreateUnbounded<RemoteViewOutput>();
|
|
|
|
public ConcurrentQueue<object> Inputs { get; } = new();
|
|
|
|
public bool Disposed { get; private set; }
|
|
|
|
public RemoteViewEnd? Ended { get; private set; }
|
|
|
|
public void Write(RemoteViewOutput output) => _output.Writer.TryWrite(output);
|
|
|
|
public void End(RemoteViewEnd reason)
|
|
{
|
|
Ended = reason;
|
|
_output.Writer.TryComplete();
|
|
}
|
|
|
|
public async IAsyncEnumerable<RemoteViewOutput> ReadAsync([EnumeratorCancellation] CancellationToken ct)
|
|
{
|
|
await foreach (var output in _output.Reader.ReadAllAsync(ct))
|
|
{
|
|
yield return output;
|
|
}
|
|
}
|
|
|
|
public Task MouseAsync(RemoteViewMouse input, CancellationToken ct)
|
|
{
|
|
Inputs.Enqueue(input);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task KeyAsync(RemoteViewKey input, CancellationToken ct)
|
|
{
|
|
Inputs.Enqueue(input);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task NavigateAsync(RemoteViewNavigation action, CancellationToken ct)
|
|
{
|
|
Inputs.Enqueue(action);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public ValueTask DisposeAsync()
|
|
{
|
|
Disposed = true;
|
|
_output.Writer.TryComplete();
|
|
return ValueTask.CompletedTask;
|
|
}
|
|
}
|